node_access_example.test

  1. examples
    1. 7 node_access_example/node_access_example.test
    2. 8 node_access_example/node_access_example.test

Tests for Node Access example module.

Classes

NameDescription
NodeAccessExampleTestCase@file Tests for Node Access example module.

File

node_access_example/node_access_example.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Node Access example module.
  5. */
  6. class NodeAccessExampleTestCase extends DrupalWebTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Node Access Example functionality',
  10. 'description' => 'Checks behavior of Node Access Example.',
  11. 'group' => 'Examples',
  12. );
  13. }
  14. /**
  15. * Enable modules and create user with specific permissions.
  16. */
  17. public function setUp() {
  18. parent::setUp('node_access_example', 'search');
  19. node_access_rebuild();
  20. }
  21. /**
  22. * Test the "private" node access.
  23. *
  24. * - Create 3 users with "access content" and "create article" permissions.
  25. * - Each user creates one private and one not private article.
  26. * - Run cron to update search index.
  27. * - Test that each user can view the other user's non-private article.
  28. * - Test that each user cannot view the other user's private article.
  29. * - Test that each user finds only appropriate (non-private + own private)
  30. * in search results.
  31. * - Create another user with 'view any private content'.
  32. * - Test that user 4 can view all content created above.
  33. * - Test that user 4 can search for all content created above.
  34. * - Test that user 4 cannot edit private content above.
  35. * - Create another user with 'edit any private content'
  36. * - Test that user 5 can edit private content.
  37. * - Test that user 5 can delete private content.
  38. * - Test listings of nodes with 'node_access' tag on database search.
  39. */
  40. function testNodeAccessBasic() {
  41. $num_simple_users = 3;
  42. $simple_users = array();
  43. // nodes keyed by uid and nid: $nodes[$uid][$nid] = $is_private;
  44. $nodes_by_user = array();
  45. $titles = array(); // Titles keyed by nid
  46. $private_nodes = array(); // Array of nids marked private.
  47. for ($i = 0; $i < $num_simple_users; $i++) {
  48. $simple_users[$i] = $this->drupalCreateUser(array('access content', 'create article content', 'search content'));
  49. }
  50. foreach ($simple_users as $web_user) {
  51. $this->drupalLogin($web_user);
  52. foreach (array(0 => 'Public', 1 => 'Private') as $is_private => $type) {
  53. $edit = array(
  54. 'title' => t('@private_public Article created by @user', array('@private_public' => $type, '@user' => $web_user->name)),
  55. );
  56. if ($is_private) {
  57. $edit['private'] = TRUE;
  58. $edit['body[und][0][value]'] = 'private node';
  59. }
  60. else {
  61. $edit['body[und][0][value]'] = 'public node';
  62. }
  63. $this->drupalPost('node/add/article', $edit, t('Save'));
  64. debug(t('Created article with private=@private', array('@private' => $is_private)));
  65. $this->assertText(t('Article @title has been created', array('@title' => $edit['title'])));
  66. $nid = db_query('SELECT nid FROM {node} WHERE title = :title', array(':title' => $edit['title']))->fetchField();
  67. $this->assertText(t('New node @nid was created and private=@private', array('@nid' => $nid, '@private' => $is_private)));
  68. $private_status = db_query('SELECT private FROM {node_access_example} where nid = :nid', array(':nid' => $nid))->fetchField();
  69. $this->assertTrue($is_private == $private_status, t('Node was properly set to private or not private in node_access_example table.'));
  70. if ($is_private) {
  71. $private_nodes[] = $nid;
  72. }
  73. $titles[$nid] = $edit['title'];
  74. $nodes_by_user[$web_user->uid][$nid] = $is_private;
  75. }
  76. }
  77. debug($nodes_by_user);
  78. $this->cronRun(); // Build the search index.
  79. foreach ($simple_users as $web_user) {
  80. $this->drupalLogin($web_user);
  81. // Check to see that we find the number of search results expected.
  82. $this->checkSearchResults('Private node', 1);
  83. // Check own nodes to see that all are readable.
  84. foreach (array_keys($nodes_by_user) as $uid) {
  85. // All of this user's nodes should be readable to same.
  86. if ($uid == $web_user->uid) {
  87. foreach ($nodes_by_user[$uid] as $nid => $is_private) {
  88. $this->drupalGet('node/' . $nid);
  89. $this->assertResponse(200);
  90. $this->assertTitle($titles[$nid] . ' | Drupal', t('Correct title for node found'));
  91. }
  92. }
  93. else {
  94. // Otherwise, for other users, private nodes should get a 403,
  95. // but we should be able to read non-private nodes.
  96. foreach ($nodes_by_user[$uid] as $nid => $is_private) {
  97. $this->drupalGet('node/' . $nid);
  98. $this->assertResponse($is_private ? 403 : 200, t('Node @nid by user @uid should get a @response for this user (@web_user_uid)', array('@nid' => $nid, '@uid' => $uid, '@response' => $is_private ? 403 : 200, '@web_user_uid' => $web_user->uid)));
  99. if (!$is_private) {
  100. $this->assertTitle($titles[$nid] . ' | Drupal', t('Correct title for node was found'));
  101. }
  102. }
  103. }
  104. }
  105. // Check to see that the correct nodes are shown on examples/node_access.
  106. $this->drupalGet('examples/node_access');
  107. $accessible = $this->xpath("//tr[contains(@class,'accessible')]");
  108. $this->assertEqual(count($accessible), 1, t('One private item accessible'));
  109. foreach ($accessible as $row) {
  110. $this->assertEqual($row->td[2], $web_user->uid, t('Accessible row owned by this user'));
  111. }
  112. }
  113. // Now test that a user with 'access any private content' can view content.
  114. $access_user = $this->drupalCreateUser(array('access content', 'create article content', 'access any private content', 'search content'));
  115. $this->drupalLogin($access_user);
  116. // Check to see that we find the number of search results expected.
  117. $this->checkSearchResults('Private node', 3);
  118. foreach ($nodes_by_user as $uid => $private_status) {
  119. foreach ($private_status as $nid => $is_private) {
  120. $this->drupalGet('node/' . $nid);
  121. $this->assertResponse(200);
  122. }
  123. }
  124. // Check to see that the correct nodes are shown on examples/node_access.
  125. // This user should be able to see all 3 of them.
  126. $this->drupalGet('examples/node_access');
  127. $accessible = $this->xpath("//tr[contains(@class,'accessible')]");
  128. $this->assertEqual(count($accessible), 3);
  129. // Test that a user named 'foobar' can edit any private node due to
  130. // node_access_example_node_access(). Note that this user will not be
  131. // able to search for private nodes, and will not have available nodes
  132. // shown on examples/node_access, because node_access() is not called
  133. // for node listings, only for actual access to a node.
  134. $edit_user = $this->drupalCreateUser(array('access comments', 'access content', 'post comments', 'skip comment approval', 'search content'));
  135. // Update the name of the user to 'foobar'.
  136. $num_updated = db_update('users')
  137. ->fields(array(
  138. 'name' => 'foobar',
  139. ))
  140. ->condition('uid', $edit_user->uid)
  141. ->execute();
  142. $edit_user->name = 'foobar';
  143. $this->drupalLogin($edit_user);
  144. // Try to edit each of the private nodes.
  145. foreach ($private_nodes as $nid) {
  146. $body = $this->randomName();
  147. $edit = array('body[und][0][value]' => $body);
  148. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  149. $this->assertText(t('has been updated'), t('Node was updated by "foobar" user'));
  150. }
  151. // Test that a privileged user can edit and delete private content.
  152. // This test should go last, as the nodes get deleted.
  153. $edit_user = $this->drupalCreateUser(array('access content', 'access any private content', 'edit any private content'));
  154. $this->drupalLogin($edit_user);
  155. foreach ($private_nodes as $nid) {
  156. $body = $this->randomName();
  157. $edit = array('body[und][0][value]' => $body);
  158. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  159. $this->assertText(t('has been updated'));
  160. $this->drupalPost('node/' . $nid . '/edit', array(), t('Delete'));
  161. $this->drupalPost(NULL, array(), t('Delete'));
  162. $this->assertText(t('has been deleted'));
  163. }
  164. }
  165. /**
  166. * On the search page, search for a string and assert the expected number
  167. * of results.
  168. * @param $search_query
  169. * String to search for
  170. * @param $expected_result_count
  171. * Expected result count
  172. */
  173. function checkSearchResults($search_query, $expected_result_count) {
  174. $this->drupalPost('search/node', array('keys' => $search_query), t('Search'));
  175. $search_results = $this->xpath("//ol[contains(@class, 'search-results')]/li");
  176. $this->assertEqual(count($search_results), $expected_result_count, t('Found the expected number of search results'));
  177. }
  178. }
Login or register to post comments