3 namespace Drupal\Tests\node\Functional;
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;
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.
17 class NodeAccessLanguageAwareTest extends NodeTestBase {
20 * Enable language and a language-aware node access module.
24 public static $modules = ['language', 'node_access_test_language'];
27 * A set of nodes to use in testing.
29 * @var \Drupal\node\NodeInterface[]
31 protected $nodes = [];
34 * A user with permission to bypass access content.
36 * @var \Drupal\user\UserInterface
41 * A normal authenticated user.
43 * @var \Drupal\user\UserInterface
47 protected function setUp() {
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',
58 $field_storage->save();
61 'field_storage' => $field_storage,
64 'type' => 'options_buttons',
67 'on_label' => 'Private',
68 'off_label' => 'Not private',
72 // After enabling a node access module, the access table has to be rebuild.
73 node_access_rebuild();
75 // Create a normal authenticated user.
76 $this->webUser = $this->drupalCreateUser(['access content']);
78 // Load the user 1 user for later use as an admin user with permission to
80 $this->adminUser = User::load(1);
82 // Add Hungarian and Catalan.
83 ConfigurableLanguage::createFromLangcode('hu')->save();
84 ConfigurableLanguage::createFromLangcode('ca')->save();
86 // The node_access_test_language module allows individual translations of a
87 // node to be marked private (not viewable by normal users).
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.
98 $this->nodes['both_public'] = $node = $this->drupalCreateNode([
101 'field_private' => [['value' => 0]],
103 $translation = $node->addTranslation('ca');
104 $translation->title->value = $this->randomString();
105 $translation->field_private->value = 0;
108 $this->nodes['ca_private'] = $node = $this->drupalCreateNode([
111 'field_private' => [['value' => 0]],
113 $translation = $node->addTranslation('ca');
114 $translation->title->value = $this->randomString();
115 $translation->field_private->value = 1;
118 $this->nodes['hu_private'] = $node = $this->drupalCreateNode([
121 'field_private' => [['value' => 1]],
123 $translation = $node->addTranslation('ca');
124 $translation->title->value = $this->randomString();
125 $translation->field_private->value = 0;
128 $this->nodes['both_private'] = $node = $this->drupalCreateNode([
131 'field_private' => [['value' => 1]],
133 $translation = $node->addTranslation('ca');
134 $translation->title->value = $this->randomString();
135 $translation->field_private->value = 1;
138 $this->nodes['no_language_public'] = $this->drupalCreateNode([
139 'field_private' => [['value' => 0]],
140 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
142 $this->nodes['no_language_private'] = $this->drupalCreateNode([
143 'field_private' => [['value' => 1]],
144 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
149 * Tests node access and node access queries with multiple node languages.
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];
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);
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);
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);
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);
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);
190 // When no language is specified for a public node, access should be
192 $this->assertNodeAccess($expected_node_access, $this->nodes['no_language_public'], $this->webUser);
194 // Query the node table with the node access tag in several languages.
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');
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.');
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');
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.');
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');
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.');
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');
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.');
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');
259 // All nodes are returned.
260 $this->assertEqual(count($nids), 6, 'db_select() returns all nodes.');
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');
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.');