Version 1
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeAccessLanguageAwareTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\user\Entity\User;
9 use Drupal\field\Entity\FieldStorageConfig;
10
11 /**
12  * Tests node_access and db_select() with node_access tag functionality with
13  * multiple languages with node_access_test_language which is language-aware.
14  *
15  * @group node
16  */
17 class NodeAccessLanguageAwareTest extends NodeTestBase {
18
19   /**
20    * Enable language and a language-aware node access module.
21    *
22    * @var array
23    */
24   public static $modules = ['language', 'node_access_test_language'];
25
26   /**
27    * A set of nodes to use in testing.
28    *
29    * @var \Drupal\node\NodeInterface[]
30    */
31   protected $nodes = [];
32
33   /**
34    * A user with permission to bypass access content.
35    *
36    * @var \Drupal\user\UserInterface
37    */
38   protected $adminUser;
39
40   /**
41    * A normal authenticated user.
42    *
43    * @var \Drupal\user\UserInterface
44    */
45   protected $webUser;
46
47   protected function setUp() {
48     parent::setUp();
49
50     // Create the 'private' field, which allows the node to be marked as private
51     // (restricted access) in a given translation.
52     $field_storage = FieldStorageConfig::create([
53       'field_name' => 'field_private',
54       'entity_type' => 'node',
55       'type' => 'boolean',
56       'cardinality' => 1,
57     ]);
58     $field_storage->save();
59
60     FieldConfig::create([
61       'field_storage' => $field_storage,
62       'bundle' => 'page',
63       'widget' => [
64         'type' => 'options_buttons',
65       ],
66       'settings' => [
67         'on_label' => 'Private',
68         'off_label' => 'Not private',
69       ],
70     ])->save();
71
72     // After enabling a node access module, the access table has to be rebuild.
73     node_access_rebuild();
74
75     // Create a normal authenticated user.
76     $this->webUser = $this->drupalCreateUser(['access content']);
77
78     // Load the user 1 user for later use as an admin user with permission to
79     // see everything.
80     $this->adminUser = User::load(1);
81
82     // Add Hungarian and Catalan.
83     ConfigurableLanguage::createFromLangcode('hu')->save();
84     ConfigurableLanguage::createFromLangcode('ca')->save();
85
86     // The node_access_test_language module allows individual translations of a
87     // node to be marked private (not viewable by normal users).
88
89     // Create six nodes:
90     // 1. Four Hungarian nodes with Catalan translations
91     //   - One with neither language marked as private.
92     //   - One with only the Hungarian translation private.
93     //   - One with only the Catalan translation private.
94     //   - One with both the Hungarian and Catalan translations private.
95     // 2. Two nodes with no language specified.
96     //   - One public.
97     //   - One private.
98     $this->nodes['both_public'] = $node = $this->drupalCreateNode([
99       'body' => [[]],
100       'langcode' => 'hu',
101       'field_private' => [['value' => 0]],
102     ]);
103     $translation = $node->addTranslation('ca');
104     $translation->title->value = $this->randomString();
105     $translation->field_private->value = 0;
106     $node->save();
107
108     $this->nodes['ca_private'] = $node = $this->drupalCreateNode([
109       'body' => [[]],
110       'langcode' => 'hu',
111       'field_private' => [['value' => 0]],
112     ]);
113     $translation = $node->addTranslation('ca');
114     $translation->title->value = $this->randomString();
115     $translation->field_private->value = 1;
116     $node->save();
117
118     $this->nodes['hu_private'] = $node = $this->drupalCreateNode([
119       'body' => [[]],
120       'langcode' => 'hu',
121       'field_private' => [['value' => 1]],
122     ]);
123     $translation = $node->addTranslation('ca');
124     $translation->title->value = $this->randomString();
125     $translation->field_private->value = 0;
126     $node->save();
127
128     $this->nodes['both_private'] = $node = $this->drupalCreateNode([
129       'body' => [[]],
130       'langcode' => 'hu',
131       'field_private' => [['value' => 1]],
132     ]);
133     $translation = $node->addTranslation('ca');
134     $translation->title->value = $this->randomString();
135     $translation->field_private->value = 1;
136     $node->save();
137
138     $this->nodes['no_language_public'] = $this->drupalCreateNode([
139       'field_private' => [['value' => 0]],
140       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
141     ]);
142     $this->nodes['no_language_private'] = $this->drupalCreateNode([
143       'field_private' => [['value' => 1]],
144       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
145     ]);
146   }
147
148   /**
149    * Tests node access and node access queries with multiple node languages.
150    */
151   public function testNodeAccessLanguageAware() {
152     // The node_access_test_language module only grants view access.
153     $expected_node_access = ['view' => TRUE, 'update' => FALSE, 'delete' => FALSE];
154     $expected_node_access_no_access = ['view' => FALSE, 'update' => FALSE, 'delete' => FALSE];
155
156     // When both Hungarian and Catalan are marked as public, access to the
157     // Hungarian translation should be granted with the default entity object or
158     // when the Hungarian translation is specified explicitly.
159     $this->assertNodeAccess($expected_node_access, $this->nodes['both_public'], $this->webUser);
160     $this->assertNodeAccess($expected_node_access, $this->nodes['both_public']->getTranslation('hu'), $this->webUser);
161     // Access to the Catalan translation should also be granted.
162     $this->assertNodeAccess($expected_node_access, $this->nodes['both_public']->getTranslation('ca'), $this->webUser);
163
164     // When Hungarian is marked as private, access to the Hungarian translation
165     // should be denied with the default entity object or when the Hungarian
166     // translation is specified explicitly.
167     $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['hu_private'], $this->webUser);
168     $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['hu_private']->getTranslation('hu'), $this->webUser);
169     // Access to the Catalan translation should be granted.
170     $this->assertNodeAccess($expected_node_access, $this->nodes['hu_private']->getTranslation('ca'), $this->webUser);
171
172     // When Catalan is marked as private, access to the Hungarian translation
173     // should be granted with the default entity object or when the Hungarian
174     // translation is specified explicitly.
175     $this->assertNodeAccess($expected_node_access, $this->nodes['ca_private'], $this->webUser);
176     $this->assertNodeAccess($expected_node_access, $this->nodes['ca_private']->getTranslation('hu'), $this->webUser);
177     // Access to the Catalan translation should be granted.
178     $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['ca_private']->getTranslation('ca'), $this->webUser);
179
180     // When both translations are marked as private, access should be denied
181     // regardless of the entity object specified.
182     $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private'], $this->webUser);
183     $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private']->getTranslation('hu'), $this->webUser);
184     $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['both_private']->getTranslation('ca'), $this->webUser);
185
186     // When no language is specified for a private node, access to every node
187     // translation is denied.
188     $this->assertNodeAccess($expected_node_access_no_access, $this->nodes['no_language_private'], $this->webUser);
189
190     // When no language is specified for a public node, access should be
191     // granted.
192     $this->assertNodeAccess($expected_node_access, $this->nodes['no_language_public'], $this->webUser);
193
194     // Query the node table with the node access tag in several languages.
195
196     // Query with no language specified. The fallback (hu) will be used.
197     $select = db_select('node', 'n')
198       ->fields('n', ['nid'])
199       ->addMetaData('account', $this->webUser)
200       ->addTag('node_access');
201     $nids = $select->execute()->fetchAllAssoc('nid');
202
203     // Three nodes should be returned:
204     // - Node with both translations public.
205     // - Node with only the Catalan translation marked as private.
206     // - No language node marked as public.
207     $this->assertEqual(count($nids), 3, 'db_select() returns 3 nodes when no langcode is specified.');
208     $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.');
209     $this->assertTrue(array_key_exists($this->nodes['ca_private']->id(), $nids), 'The node with only the Catalan translation private is returned.');
210     $this->assertTrue(array_key_exists($this->nodes['no_language_public']->id(), $nids), 'The node with no language is returned.');
211
212     // Query with Hungarian (hu) specified.
213     $select = db_select('node', 'n')
214       ->fields('n', ['nid'])
215       ->addMetaData('account', $this->webUser)
216       ->addMetaData('langcode', 'hu')
217       ->addTag('node_access');
218     $nids = $select->execute()->fetchAllAssoc('nid');
219
220     // Two nodes should be returned: the node with both translations public, and
221     // the node with only the Catalan translation marked as private.
222     $this->assertEqual(count($nids), 2, 'db_select() returns 2 nodes when the hu langcode is specified.');
223     $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.');
224     $this->assertTrue(array_key_exists($this->nodes['ca_private']->id(), $nids), 'The node with only the Catalan translation private is returned.');
225
226     // Query with Catalan (ca) specified.
227     $select = db_select('node', 'n')
228       ->fields('n', ['nid'])
229       ->addMetaData('account', $this->webUser)
230       ->addMetaData('langcode', 'ca')
231       ->addTag('node_access');
232     $nids = $select->execute()->fetchAllAssoc('nid');
233
234     // Two nodes should be returned: the node with both translations public, and
235     // the node with only the Hungarian translation marked as private.
236     $this->assertEqual(count($nids), 2, 'db_select() returns 2 nodes when the hu langcode is specified.');
237     $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.');
238     $this->assertTrue(array_key_exists($this->nodes['hu_private']->id(), $nids), 'The node with only the Hungarian translation private is returned.');
239
240     // Query with German (de) specified.
241     $select = db_select('node', 'n')
242       ->fields('n', ['nid'])
243       ->addMetaData('account', $this->webUser)
244       ->addMetaData('langcode', 'de')
245       ->addTag('node_access');
246     $nids = $select->execute()->fetchAllAssoc('nid');
247
248     // There are no nodes with German translations, so no results are returned.
249     $this->assertTrue(empty($nids), 'db_select() returns an empty result when the de langcode is specified.');
250
251     // Query the nodes table as admin user (full access) with the node access
252     // tag and no specific langcode.
253     $select = db_select('node', 'n')
254       ->fields('n', ['nid'])
255       ->addMetaData('account', $this->adminUser)
256       ->addTag('node_access');
257     $nids = $select->execute()->fetchAllAssoc('nid');
258
259     // All nodes are returned.
260     $this->assertEqual(count($nids), 6, 'db_select() returns all nodes.');
261
262     // Query the nodes table as admin user (full access) with the node access
263     // tag and langcode de.
264     $select = db_select('node', 'n')
265       ->fields('n', ['nid'])
266       ->addMetaData('account', $this->adminUser)
267       ->addMetaData('langcode', 'de')
268       ->addTag('node_access');
269     $nids = $select->execute()->fetchAllAssoc('nid');
270
271     // Even though there is no German translation, all nodes are returned
272     // because node access filtering does not occur when the user is user 1.
273     $this->assertEqual(count($nids), 6, 'db_select() returns all nodes.');
274   }
275
276 }