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