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