NodeAccessLanguageFallbackTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\language\Entity\ConfigurableLanguage;
  4. /**
  5. * Tests that the node_access system stores the proper fallback marker.
  6. *
  7. * @group node
  8. */
  9. class NodeAccessLanguageFallbackTest extends NodeTestBase {
  10. /**
  11. * Enable language and a non-language-aware node access module.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['language', 'node_access_test', 'content_translation'];
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected function setUp() {
  20. parent::setUp();
  21. // After enabling a node access module, the {node_access} table has to be
  22. // rebuilt.
  23. node_access_rebuild();
  24. // Add Hungarian, Catalan, and Afrikaans.
  25. ConfigurableLanguage::createFromLangcode('hu')->save();
  26. ConfigurableLanguage::createFromLangcode('ca')->save();
  27. ConfigurableLanguage::createFromLangcode('af')->save();
  28. // Enable content translation for the current entity type.
  29. \Drupal::service('content_translation.manager')->setEnabled('node', 'page', TRUE);
  30. }
  31. /**
  32. * Tests node access fallback handling with multiple node languages.
  33. */
  34. public function testNodeAccessLanguageFallback() {
  35. // The node_access_test module allows nodes to be marked private. We need to
  36. // ensure that system honors the fallback system of node access properly.
  37. // Note that node_access_test_language is language-sensitive and does not
  38. // apply to the fallback test.
  39. // Create one node in Hungarian and marked as private.
  40. $node = $this->drupalCreateNode([
  41. 'body' => [[]],
  42. 'langcode' => 'hu',
  43. 'private' => [['value' => 1]],
  44. 'status' => 1,
  45. ]);
  46. // There should be one entry in node_access, with fallback set to hu.
  47. $this->checkRecords(1, 'hu');
  48. // Create a translation user.
  49. $admin = $this->drupalCreateUser([
  50. 'bypass node access',
  51. 'administer nodes',
  52. 'translate any entity',
  53. 'administer content translation',
  54. ]);
  55. $this->drupalLogin($admin);
  56. $this->drupalGet('node/' . $node->id() . '/translations');
  57. $this->assertSession()->statusCodeEquals(200);
  58. // Create a Catalan translation through the UI.
  59. $url_options = ['language' => \Drupal::languageManager()->getLanguage('ca')];
  60. $this->drupalGet('node/' . $node->id() . '/translations/add/hu/ca', $url_options);
  61. $this->assertSession()->statusCodeEquals(200);
  62. // Save the form.
  63. $this->getSession()->getPage()->pressButton('Save (this translation)');
  64. $this->assertSession()->statusCodeEquals(200);
  65. // Check the node access table.
  66. $this->checkRecords(2, 'hu');
  67. // Programmatically create a translation. This process lets us check that
  68. // both forms and code behave in the same way.
  69. $storage = \Drupal::entityTypeManager()->getStorage('node');
  70. // Reload the node.
  71. $node = $storage->load(1);
  72. // Create an Afrikaans translation.
  73. $translation = $node->addTranslation('af');
  74. $translation->title->value = $this->randomString();
  75. $translation->status = 1;
  76. $node->save();
  77. // Check the node access table.
  78. $this->checkRecords(3, 'hu');
  79. // For completeness, edit the Catalan version again.
  80. $this->drupalGet('node/' . $node->id() . '/edit', $url_options);
  81. $this->assertSession()->statusCodeEquals(200);
  82. // Save the form.
  83. $this->getSession()->getPage()->pressButton('Save (this translation)');
  84. $this->assertSession()->statusCodeEquals(200);
  85. // Check the node access table.
  86. $this->checkRecords(3, 'hu');
  87. }
  88. /**
  89. * Queries the node_access table and checks for proper storage.
  90. *
  91. * @param int $count
  92. * The number of rows expected by the query (equal to the translation
  93. * count).
  94. * @param $langcode
  95. * The expected language code set as the fallback property.
  96. */
  97. public function checkRecords($count, $langcode = 'hu') {
  98. $select = \Drupal::database()
  99. ->select('node_access', 'na')
  100. ->fields('na', ['nid', 'fallback', 'langcode', 'grant_view'])
  101. ->condition('na.realm', 'node_access_test', '=')
  102. ->condition('na.gid', 8888, '=');
  103. $records = $select->execute()->fetchAll();
  104. // Check that the expected record count is returned.
  105. $this->assertEquals(count($records), $count);
  106. // The fallback value is 'hu' and should be set to 1. For other languages,
  107. // it should be set to 0. Casting to boolean lets us run that comparison.
  108. foreach ($records as $record) {
  109. $this->assertEquals((bool) $record->fallback, $record->langcode === $langcode);
  110. }
  111. }
  112. }