NodeQueryAlterTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. /**
  4. * Tests that node access queries are properly altered by the node module.
  5. *
  6. * @group node
  7. */
  8. class NodeQueryAlterTest extends NodeTestBase {
  9. /**
  10. * Modules to enable.
  11. *
  12. * @var array
  13. */
  14. public static $modules = ['node_access_test'];
  15. /**
  16. * User with permission to view content.
  17. */
  18. protected $accessUser;
  19. /**
  20. * User without permission to view content.
  21. */
  22. protected $noAccessUser;
  23. protected function setUp() {
  24. parent::setUp();
  25. node_access_rebuild();
  26. // Create some content.
  27. $this->drupalCreateNode();
  28. $this->drupalCreateNode();
  29. $this->drupalCreateNode();
  30. $this->drupalCreateNode();
  31. // Create user with simple node access permission. The 'node test view'
  32. // permission is implemented and granted by the node_access_test module.
  33. $this->accessUser = $this->drupalCreateUser(['access content overview', 'access content', 'node test view']);
  34. $this->noAccessUser = $this->drupalCreateUser(['access content overview', 'access content']);
  35. $this->noAccessUser2 = $this->drupalCreateUser(['access content overview', 'access content']);
  36. }
  37. /**
  38. * Tests 'node_access' query alter, for user with access.
  39. *
  40. * Verifies that a non-standard table alias can be used, and that a user with
  41. * node access can view the nodes.
  42. */
  43. public function testNodeQueryAlterLowLevelWithAccess() {
  44. // User with access should be able to view 4 nodes.
  45. try {
  46. $query = db_select('node', 'mytab')
  47. ->fields('mytab');
  48. $query->addTag('node_access');
  49. $query->addMetaData('op', 'view');
  50. $query->addMetaData('account', $this->accessUser);
  51. $result = $query->execute()->fetchAll();
  52. $this->assertEqual(count($result), 4, 'User with access can see correct nodes');
  53. }
  54. catch (\Exception $e) {
  55. $this->fail(t('Altered query is malformed'));
  56. }
  57. }
  58. /**
  59. * Tests 'node_access' query alter with revision-enabled nodes.
  60. */
  61. public function testNodeQueryAlterWithRevisions() {
  62. // Execute a query that only deals with the 'node_revision' table.
  63. try {
  64. $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
  65. $result = $query
  66. ->allRevisions()
  67. ->execute();
  68. $this->assertEqual(count($result), 4, 'User with access can see correct nodes');
  69. }
  70. catch (\Exception $e) {
  71. $this->fail('Altered query is malformed');
  72. }
  73. }
  74. /**
  75. * Tests 'node_access' query alter, for user without access.
  76. *
  77. * Verifies that a non-standard table alias can be used, and that a user
  78. * without node access cannot view the nodes.
  79. */
  80. public function testNodeQueryAlterLowLevelNoAccess() {
  81. // User without access should be able to view 0 nodes.
  82. try {
  83. $query = db_select('node', 'mytab')
  84. ->fields('mytab');
  85. $query->addTag('node_access');
  86. $query->addMetaData('op', 'view');
  87. $query->addMetaData('account', $this->noAccessUser);
  88. $result = $query->execute()->fetchAll();
  89. $this->assertEqual(count($result), 0, 'User with no access cannot see nodes');
  90. }
  91. catch (\Exception $e) {
  92. $this->fail(t('Altered query is malformed'));
  93. }
  94. }
  95. /**
  96. * Tests 'node_access' query alter, for edit access.
  97. *
  98. * Verifies that a non-standard table alias can be used, and that a user with
  99. * view-only node access cannot edit the nodes.
  100. */
  101. public function testNodeQueryAlterLowLevelEditAccess() {
  102. // User with view-only access should not be able to edit nodes.
  103. try {
  104. $query = db_select('node', 'mytab')
  105. ->fields('mytab');
  106. $query->addTag('node_access');
  107. $query->addMetaData('op', 'update');
  108. $query->addMetaData('account', $this->accessUser);
  109. $result = $query->execute()->fetchAll();
  110. $this->assertEqual(count($result), 0, 'User with view-only access cannot edit nodes');
  111. }
  112. catch (\Exception $e) {
  113. $this->fail($e->getMessage());
  114. $this->fail((string) $query);
  115. $this->fail(t('Altered query is malformed'));
  116. }
  117. }
  118. /**
  119. * Tests 'node_access' query alter override.
  120. *
  121. * Verifies that node_access_view_all_nodes() is called from
  122. * node_query_node_access_alter(). We do this by checking that a user who
  123. * normally would not have view privileges is able to view the nodes when we
  124. * add a record to {node_access} paired with a corresponding privilege in
  125. * hook_node_grants().
  126. */
  127. public function testNodeQueryAlterOverride() {
  128. $record = [
  129. 'nid' => 0,
  130. 'gid' => 0,
  131. 'realm' => 'node_access_all',
  132. 'grant_view' => 1,
  133. 'grant_update' => 0,
  134. 'grant_delete' => 0,
  135. ];
  136. db_insert('node_access')->fields($record)->execute();
  137. // Test that the noAccessUser still doesn't have the 'view'
  138. // privilege after adding the node_access record.
  139. drupal_static_reset('node_access_view_all_nodes');
  140. try {
  141. $query = db_select('node', 'mytab')
  142. ->fields('mytab');
  143. $query->addTag('node_access');
  144. $query->addMetaData('op', 'view');
  145. $query->addMetaData('account', $this->noAccessUser);
  146. $result = $query->execute()->fetchAll();
  147. $this->assertEqual(count($result), 0, 'User view privileges are not overridden');
  148. }
  149. catch (\Exception $e) {
  150. $this->fail(t('Altered query is malformed'));
  151. }
  152. // Have node_test_node_grants return a node_access_all privilege,
  153. // to grant the noAccessUser 'view' access. To verify that
  154. // node_access_view_all_nodes is properly checking the specified
  155. // $account instead of the current user, we will log in as
  156. // noAccessUser2.
  157. $this->drupalLogin($this->noAccessUser2);
  158. \Drupal::state()->set('node_access_test.no_access_uid', $this->noAccessUser->id());
  159. drupal_static_reset('node_access_view_all_nodes');
  160. try {
  161. $query = db_select('node', 'mytab')
  162. ->fields('mytab');
  163. $query->addTag('node_access');
  164. $query->addMetaData('op', 'view');
  165. $query->addMetaData('account', $this->noAccessUser);
  166. $result = $query->execute()->fetchAll();
  167. $this->assertEqual(count($result), 4, 'User view privileges are overridden');
  168. }
  169. catch (\Exception $e) {
  170. $this->fail(t('Altered query is malformed'));
  171. }
  172. \Drupal::state()->delete('node_access_test.no_access_uid');
  173. }
  174. }