node_access_example.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Node Access example module.
  5. */
  6. /**
  7. * Functional tests for the Node Access Example module.
  8. *
  9. * @ingroup node_access_example
  10. */
  11. class NodeAccessExampleTestCase extends DrupalWebTestCase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Node Access Example functionality',
  18. 'description' => 'Checks behavior of Node Access Example.',
  19. 'group' => 'Examples',
  20. );
  21. }
  22. /**
  23. * Enable modules and create user with specific permissions.
  24. */
  25. public function setUp() {
  26. parent::setUp('node_access_example', 'search');
  27. node_access_rebuild();
  28. }
  29. /**
  30. * Test the "private" node access.
  31. *
  32. * - Create 3 users with "access content" and "create article" permissions.
  33. * - Each user creates one private and one not private article.
  34. * - Run cron to update search index.
  35. * - Test that each user can view the other user's non-private article.
  36. * - Test that each user cannot view the other user's private article.
  37. * - Test that each user finds only appropriate (non-private + own private)
  38. * in search results.
  39. * - Logout.
  40. * - Test that anonymous user can't view, edit or delete private content which
  41. * has author.
  42. * - Test that anonymous user can't view, edit or delete private content with
  43. * anonymous author.
  44. * - Create another user with 'view any private content'.
  45. * - Test that user 4 can view all content created above.
  46. * - Test that user 4 can search for all content created above.
  47. * - Test that user 4 cannot edit private content above.
  48. * - Create another user with 'edit any private content'
  49. * - Test that user 5 can edit private content.
  50. * - Test that user 5 can delete private content.
  51. * - Test listings of nodes with 'node_access' tag on database search.
  52. */
  53. public function testNodeAccessBasic() {
  54. $num_simple_users = 3;
  55. $simple_users = array();
  56. // Nodes keyed by uid and nid: $nodes[$uid][$nid] = $is_private;.
  57. $nodes_by_user = array();
  58. // Titles keyed by nid.
  59. $titles = array();
  60. // Array of nids marked private.
  61. $private_nodes = array();
  62. for ($i = 0; $i < $num_simple_users; $i++) {
  63. $simple_users[$i] = $this->drupalCreateUser(
  64. array(
  65. 'access content',
  66. 'create article content',
  67. 'search content',
  68. )
  69. );
  70. }
  71. foreach ($simple_users as $web_user) {
  72. $this->drupalLogin($web_user);
  73. foreach (array(0 => 'Public', 1 => 'Private') as $is_private => $type) {
  74. $edit = array(
  75. 'title' => t('@private_public Article created by @user', array('@private_public' => $type, '@user' => $web_user->name)),
  76. );
  77. if ($is_private) {
  78. $edit['private'] = TRUE;
  79. $edit['body[und][0][value]'] = 'private node';
  80. }
  81. else {
  82. $edit['body[und][0][value]'] = 'public node';
  83. }
  84. $this->drupalPost('node/add/article', $edit, t('Save'));
  85. debug(t('Created article with private=@private', array('@private' => $is_private)));
  86. $this->assertText(t('Article @title has been created', array('@title' => $edit['title'])));
  87. $nid = db_query('SELECT nid FROM {node} WHERE title = :title', array(':title' => $edit['title']))->fetchField();
  88. $this->assertText(t('New node @nid was created and private=@private', array('@nid' => $nid, '@private' => $is_private)));
  89. $private_status = db_query('SELECT private FROM {node_access_example} where nid = :nid', array(':nid' => $nid))->fetchField();
  90. $this->assertTrue($is_private == $private_status, 'Node was properly set to private or not private in node_access_example table.');
  91. if ($is_private) {
  92. $private_nodes[] = $nid;
  93. }
  94. $titles[$nid] = $edit['title'];
  95. $nodes_by_user[$web_user->uid][$nid] = $is_private;
  96. }
  97. }
  98. debug($nodes_by_user);
  99. // Build the search index.
  100. $this->cronRun();
  101. foreach ($simple_users as $web_user) {
  102. $this->drupalLogin($web_user);
  103. // Check to see that we find the number of search results expected.
  104. $this->checkSearchResults('Private node', 1);
  105. // Check own nodes to see that all are readable.
  106. foreach (array_keys($nodes_by_user) as $uid) {
  107. // All of this user's nodes should be readable to same.
  108. if ($uid == $web_user->uid) {
  109. foreach ($nodes_by_user[$uid] as $nid => $is_private) {
  110. $this->drupalGet('node/' . $nid);
  111. $this->assertResponse(200);
  112. $this->assertTitle($titles[$nid] . ' | Drupal', 'Correct title for node found');
  113. }
  114. }
  115. else {
  116. // Otherwise, for other users, private nodes should get a 403,
  117. // but we should be able to read non-private nodes.
  118. foreach ($nodes_by_user[$uid] as $nid => $is_private) {
  119. $this->drupalGet('node/' . $nid);
  120. $this->assertResponse(
  121. $is_private ? 403 : 200,
  122. format_string('Node @nid by user @uid should get a @response for this user (@web_user_uid)',
  123. array(
  124. '@nid' => $nid,
  125. '@uid' => $uid,
  126. '@response' => $is_private ? 403 : 200,
  127. '@web_user_uid' => $web_user->uid,
  128. )
  129. )
  130. );
  131. if (!$is_private) {
  132. $this->assertTitle($titles[$nid] . ' | Drupal', 'Correct title for node was found');
  133. }
  134. }
  135. }
  136. }
  137. // Check to see that the correct nodes are shown on examples/node_access.
  138. $this->drupalGet('examples/node_access');
  139. $accessible = $this->xpath("//tr[contains(@class,'accessible')]");
  140. $this->assertEqual(count($accessible), 1, 'One private item accessible');
  141. foreach ($accessible as $row) {
  142. $this->assertEqual($row->td[2], $web_user->uid, 'Accessible row owned by this user');
  143. }
  144. }
  145. // Test cases for anonymous user.
  146. $this->drupalLogout();
  147. // Test that private nodes with authors are not accessible.
  148. foreach ($private_nodes as $nid) {
  149. if (($node = node_load($nid)) === FALSE) {
  150. continue;
  151. }
  152. $this->checkNodeAccess($nid, FALSE, FALSE, FALSE);
  153. }
  154. // Test that private nodes that don't have author are not accessible.
  155. foreach ($private_nodes as $nid) {
  156. if (($node = node_load($nid)) === FALSE) {
  157. continue;
  158. }
  159. $original_uid = $node->uid;
  160. // Change node author to anonymous.
  161. $node->uid = 0;
  162. node_save($node);
  163. $node = node_load($nid);
  164. $this->assertEqual($node->uid, 0);
  165. $this->checkNodeAccess($nid, FALSE, FALSE, FALSE);
  166. // Change node to original author.
  167. $node->uid = $original_uid;
  168. node_save($node);
  169. }
  170. // Now test that a user with 'access any private content' can view content.
  171. $access_user = $this->drupalCreateUser(
  172. array(
  173. 'access content',
  174. 'create article content',
  175. 'access any private content',
  176. 'search content',
  177. )
  178. );
  179. $this->drupalLogin($access_user);
  180. // Check to see that we find the number of search results expected.
  181. $this->checkSearchResults('Private node', 3);
  182. foreach ($nodes_by_user as $uid => $private_status) {
  183. foreach ($private_status as $nid => $is_private) {
  184. $this->drupalGet('node/' . $nid);
  185. $this->assertResponse(200);
  186. }
  187. }
  188. // Check to see that the correct nodes are shown on examples/node_access.
  189. // This user should be able to see all 3 of them.
  190. $this->drupalGet('examples/node_access');
  191. $accessible = $this->xpath("//tr[contains(@class,'accessible')]");
  192. $this->assertEqual(count($accessible), 3);
  193. // Test that a user named 'foobar' can edit any private node due to
  194. // node_access_example_node_access(). Note that this user will not be
  195. // able to search for private nodes, and will not have available nodes
  196. // shown on examples/node_access, because node_access() is not called
  197. // for node listings, only for actual access to a node.
  198. $edit_user = $this->drupalCreateUser(
  199. array(
  200. 'access comments',
  201. 'access content',
  202. 'post comments',
  203. 'skip comment approval',
  204. 'search content',
  205. )
  206. );
  207. // Update the name of the user to 'foobar'.
  208. db_update('users')
  209. ->fields(array(
  210. 'name' => 'foobar',
  211. ))
  212. ->condition('uid', $edit_user->uid)
  213. ->execute();
  214. $edit_user->name = 'foobar';
  215. $this->drupalLogin($edit_user);
  216. // Try to edit each of the private nodes.
  217. foreach ($private_nodes as $nid) {
  218. $body = $this->randomName();
  219. $edit = array('body[und][0][value]' => $body);
  220. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  221. $this->assertText(t('has been updated'), 'Node was updated by "foobar" user');
  222. }
  223. // Test that a privileged user can edit and delete private content.
  224. // This test should go last, as the nodes get deleted.
  225. $edit_user = $this->drupalCreateUser(
  226. array(
  227. 'access content',
  228. 'access any private content',
  229. 'edit any private content',
  230. )
  231. );
  232. $this->drupalLogin($edit_user);
  233. foreach ($private_nodes as $nid) {
  234. $body = $this->randomName();
  235. $edit = array('body[und][0][value]' => $body);
  236. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  237. $this->assertText(t('has been updated'));
  238. $this->drupalPost('node/' . $nid . '/edit', array(), t('Delete'));
  239. $this->drupalPost(NULL, array(), t('Delete'));
  240. $this->assertText(t('has been deleted'));
  241. }
  242. }
  243. /**
  244. * Helper function.
  245. *
  246. * On the search page, search for a string and assert the expected number
  247. * of results.
  248. *
  249. * @param string $search_query
  250. * String to search for
  251. * @param int $expected_result_count
  252. * Expected result count
  253. */
  254. protected function checkSearchResults($search_query, $expected_result_count) {
  255. $this->drupalPost('search/node', array('keys' => $search_query), t('Search'));
  256. $search_results = $this->xpath("//ol[contains(@class, 'search-results')]/li");
  257. $this->assertEqual(count($search_results), $expected_result_count, 'Found the expected number of search results');
  258. }
  259. /**
  260. * Helper function.
  261. *
  262. * Test if a node with the id $nid has expected access grants.
  263. *
  264. * @param int $nid
  265. * Node that will be checked.
  266. *
  267. * @return bool
  268. * Checker ran successfully
  269. */
  270. protected function checkNodeAccess($nid, $grant_view, $grant_update, $grant_delete) {
  271. // Test if node can be viewed.
  272. if (!$this->checkResponse($grant_view, 'node/' . $nid)) {
  273. return FALSE;
  274. }
  275. // Test if private node can be edited.
  276. if (!$this->checkResponse($grant_update, 'node/' . $nid . '/edit')) {
  277. return FALSE;
  278. }
  279. // Test if private node can be deleted.
  280. if (!$this->checkResponse($grant_delete, 'node/' . $nid . '/delete')) {
  281. return FALSE;
  282. }
  283. return TRUE;
  284. }
  285. /**
  286. * Helper function.
  287. *
  288. * Test if there is access to an $url
  289. *
  290. * @param bool $grant
  291. * Access to the $url
  292. *
  293. * @param string $url
  294. * url to make the get call.
  295. *
  296. * @return bool
  297. * Get response
  298. */
  299. protected function checkResponse($grant, $url) {
  300. $this->drupalGet($url);
  301. if ($grant) {
  302. $response = $this->assertResponse(200);
  303. }
  304. else {
  305. $response = $this->assertResponse(403);
  306. }
  307. return $response;
  308. }
  309. }