NodeAccessLanguageAwareCombinationTest.php 15 KB

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