PageRenderTime 20ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/node/src/Tests/NodeAccessBaseTableTest.php

http://github.com/drupal/drupal
PHP | 226 lines | 120 code | 25 blank | 81 comment | 12 complexity | d7b9aeeef943c25375b2e7b3a5bb1a7f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\node\Tests;
  3. use Drupal\node\Entity\NodeType;
  4. /**
  5. * Tests behavior of the node access subsystem if the base table is not node.
  6. *
  7. * @group node
  8. */
  9. class NodeAccessBaseTableTest extends NodeTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = array('node_access_test', 'views');
  16. /**
  17. * The installation profile to use with this test.
  18. *
  19. * This test class requires the "tags" taxonomy field.
  20. *
  21. * @var string
  22. */
  23. protected $profile = 'standard';
  24. /**
  25. * Nodes by user.
  26. *
  27. * @var array
  28. */
  29. protected $nodesByUser;
  30. /**
  31. * A public tid.
  32. *
  33. * @var \Drupal\Core\Database\StatementInterface
  34. */
  35. protected $publicTid;
  36. /**
  37. * A private tid.
  38. *
  39. * @var \Drupal\Core\Database\StatementInterface
  40. */
  41. protected $privateTid;
  42. /**
  43. * A web user.
  44. */
  45. protected $webUser;
  46. /**
  47. * The nids visible.
  48. *
  49. * @var array
  50. */
  51. protected $nidsVisible;
  52. protected function setUp() {
  53. parent::setUp();
  54. node_access_test_add_field(NodeType::load('article'));
  55. node_access_rebuild();
  56. \Drupal::state()->set('node_access_test.private', TRUE);
  57. }
  58. /**
  59. * Tests the "private" node access functionality.
  60. *
  61. * - Create 2 users with "access content" and "create article" permissions.
  62. * - Each user creates one private and one not private article.
  63. *
  64. * - Test that each user can view the other user's non-private article.
  65. * - Test that each user cannot view the other user's private article.
  66. * - Test that each user finds only appropriate (non-private + own private)
  67. * in taxonomy listing.
  68. * - Create another user with 'view any private content'.
  69. * - Test that user 4 can view all content created above.
  70. * - Test that user 4 can view all content on taxonomy listing.
  71. */
  72. function testNodeAccessBasic() {
  73. $num_simple_users = 2;
  74. $simple_users = array();
  75. // Nodes keyed by uid and nid: $nodes[$uid][$nid] = $is_private;
  76. $this->nodesByUser = array();
  77. // Titles keyed by nid.
  78. $titles = [];
  79. // Array of nids marked private.
  80. $private_nodes = [];
  81. for ($i = 0; $i < $num_simple_users; $i++) {
  82. $simple_users[$i] = $this->drupalCreateUser(array('access content', 'create article content'));
  83. }
  84. foreach ($simple_users as $this->webUser) {
  85. $this->drupalLogin($this->webUser);
  86. foreach (array(0 => 'Public', 1 => 'Private') as $is_private => $type) {
  87. $edit = array(
  88. 'title[0][value]' => t('@private_public Article created by @user', array('@private_public' => $type, '@user' => $this->webUser->getUsername())),
  89. );
  90. if ($is_private) {
  91. $edit['private[0][value]'] = TRUE;
  92. $edit['body[0][value]'] = 'private node';
  93. $edit['field_tags[target_id]'] = 'private';
  94. }
  95. else {
  96. $edit['body[0][value]'] = 'public node';
  97. $edit['field_tags[target_id]'] = 'public';
  98. }
  99. $this->drupalPostForm('node/add/article', $edit, t('Save'));
  100. $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
  101. $this->assertEqual($is_private, (int)$node->private->value, 'The private status of the node was properly set in the node_access_test table.');
  102. if ($is_private) {
  103. $private_nodes[] = $node->id();
  104. }
  105. $titles[$node->id()] = $edit['title[0][value]'];
  106. $this->nodesByUser[$this->webUser->id()][$node->id()] = $is_private;
  107. }
  108. }
  109. $this->publicTid = db_query('SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1', array(':name' => 'public'))->fetchField();
  110. $this->privateTid = db_query('SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1', array(':name' => 'private'))->fetchField();
  111. $this->assertTrue($this->publicTid, 'Public tid was found');
  112. $this->assertTrue($this->privateTid, 'Private tid was found');
  113. foreach ($simple_users as $this->webUser) {
  114. $this->drupalLogin($this->webUser);
  115. // Check own nodes to see that all are readable.
  116. foreach ($this->nodesByUser as $uid => $data) {
  117. foreach ($data as $nid => $is_private) {
  118. $this->drupalGet('node/' . $nid);
  119. if ($is_private) {
  120. $should_be_visible = $uid == $this->webUser->id();
  121. }
  122. else {
  123. $should_be_visible = TRUE;
  124. }
  125. $this->assertResponse($should_be_visible ? 200 : 403, strtr('A %private node by user %uid is %visible for user %current_uid.', array(
  126. '%private' => $is_private ? 'private' : 'public',
  127. '%uid' => $uid,
  128. '%visible' => $should_be_visible ? 'visible' : 'not visible',
  129. '%current_uid' => $this->webUser->id(),
  130. )));
  131. }
  132. }
  133. // Check to see that the correct nodes are shown on taxonomy/private
  134. // and taxonomy/public.
  135. $this->assertTaxonomyPage(FALSE);
  136. }
  137. // Now test that a user with 'node test view' permissions can view content.
  138. $access_user = $this->drupalCreateUser(array('access content', 'create article content', 'node test view', 'search content'));
  139. $this->drupalLogin($access_user);
  140. foreach ($this->nodesByUser as $private_status) {
  141. foreach ($private_status as $nid => $is_private) {
  142. $this->drupalGet('node/' . $nid);
  143. $this->assertResponse(200);
  144. }
  145. }
  146. // This user should be able to see all of the nodes on the relevant
  147. // taxonomy pages.
  148. $this->assertTaxonomyPage(TRUE);
  149. // Rebuild the node access permissions, repeat the test. This is done to
  150. // ensure that node access is rebuilt correctly even if the current user
  151. // does not have the bypass node access permission.
  152. node_access_rebuild();
  153. foreach ($this->nodesByUser as $private_status) {
  154. foreach ($private_status as $nid => $is_private) {
  155. $this->drupalGet('node/' . $nid);
  156. $this->assertResponse(200);
  157. }
  158. }
  159. // This user should be able to see all of the nodes on the relevant
  160. // taxonomy pages.
  161. $this->assertTaxonomyPage(TRUE);
  162. }
  163. /**
  164. * Checks taxonomy/term listings to ensure only accessible nodes are listed.
  165. *
  166. * @param $is_admin
  167. * A boolean indicating whether the current user is an administrator. If
  168. * TRUE, all nodes should be listed. If FALSE, only public nodes and the
  169. * user's own private nodes should be listed.
  170. */
  171. protected function assertTaxonomyPage($is_admin) {
  172. foreach (array($this->publicTid, $this->privateTid) as $tid_is_private => $tid) {
  173. $this->drupalGet("taxonomy/term/$tid");
  174. $this->nidsVisible = [];
  175. foreach ($this->xpath("//a[text()='Read more']") as $link) {
  176. // See also testTranslationRendering() in NodeTranslationUITest.
  177. $this->assertTrue(preg_match('|node/(\d+)$|', (string) $link['href'], $matches), 'Read more points to a node');
  178. $this->nidsVisible[$matches[1]] = TRUE;
  179. }
  180. foreach ($this->nodesByUser as $uid => $data) {
  181. foreach ($data as $nid => $is_private) {
  182. // Private nodes should be visible on the private term page,
  183. // public nodes should be visible on the public term page.
  184. $should_be_visible = $tid_is_private == $is_private;
  185. // Non-administrators can only see their own nodes on the private
  186. // term page.
  187. if (!$is_admin && $tid_is_private) {
  188. $should_be_visible = $should_be_visible && $uid == $this->webUser->id();
  189. }
  190. $this->assertIdentical(isset($this->nidsVisible[$nid]), $should_be_visible, strtr('A %private node by user %uid is %visible for user %current_uid on the %tid_is_private page.', array(
  191. '%private' => $is_private ? 'private' : 'public',
  192. '%uid' => $uid,
  193. '%visible' => isset($this->nidsVisible[$nid]) ? 'visible' : 'not visible',
  194. '%current_uid' => $this->webUser->id(),
  195. '%tid_is_private' => $tid_is_private ? 'private' : 'public',
  196. )));
  197. }
  198. }
  199. }
  200. }
  201. }