content_access.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * @file
  4. * Automatd SimpleTest Case for content access module
  5. */
  6. require_once(drupal_get_path('module', 'content_access') .'/tests/content_access_test_help.php');
  7. class ContentAccessModuleTestCase extends ContentAccessTestCase {
  8. /**
  9. * Implementation of get_info() for information
  10. */
  11. public static function getInfo() {
  12. return array(
  13. 'name' => t('Content Access Module Tests'),
  14. 'description' => t('Various tests to check permission settings on nodes.'),
  15. 'group' => t('Content Access'),
  16. );
  17. }
  18. function setUp($module = '') {
  19. parent::setUp();
  20. // Create test nodes
  21. $this->node1 = $this->drupalCreateNode(array('type' => $this->content_type->type));
  22. $this->node2 = $this->drupalCreateNode(array('type' => $this->content_type->type));
  23. }
  24. /**
  25. * Test for viewing nodes
  26. */
  27. function testViewAccess() {
  28. // Restrict access to the content type (access is only allowed for the author)
  29. $access_permissions = array(
  30. 'view[1]' => FALSE,
  31. 'view[2]' => FALSE,
  32. );
  33. $this->changeAccessContentType($access_permissions);
  34. // Logout admin and try to access the node anonymously
  35. $this->drupalLogout();
  36. $this->drupalGet('node/'. $this->node1->nid);
  37. $this->assertText(t('Access denied'), 'node is not viewable');
  38. // Login test user, view node, access must be denied
  39. $this->drupalLogin($this->test_user);
  40. $this->drupalGet('node/'. $this->node1->nid);
  41. $this->assertText(t('Access denied'), 'node is not viewable');
  42. // Login admin and grant access for viewing to the test user
  43. $this->drupalLogin($this->admin_user);
  44. $this->changeAccessContentTypeKeyword('view');
  45. // Logout admin and try to access the node anonymously
  46. // access must be denied again
  47. $this->drupalLogout();
  48. $this->drupalGet('node/'. $this->node1->nid);
  49. $this->assertText(t('Access denied'), 'node is not viewable');
  50. // Login test user, view node, access must be granted
  51. $this->drupalLogin($this->test_user);
  52. $this->drupalGet('node/'. $this->node1->nid);
  53. $this->assertNoText(t('Access denied'), 'node is viewable');
  54. // Login admin and enable per node access
  55. $this->drupalLogin($this->admin_user);
  56. $this->changeAccessPerNode();
  57. // Restrict access on node2 for the test user role
  58. $this->changeAccessNodeKeyword($this->node2, 'view', FALSE);
  59. // Logout admin and try to access both nodes anonymously
  60. $this->drupalLogout();
  61. $this->drupalGet('node/'. $this->node1->nid);
  62. $this->assertText(t('Access denied'), 'node1 is not viewable');
  63. $this->drupalGet('node/'. $this->node2->nid);
  64. $this->assertText(t('Access denied'), 'node2 is not viewable');
  65. // Login test user, view node1, access must be granted
  66. $this->drupalLogin($this->test_user);
  67. $this->drupalGet('node/'. $this->node1->nid);
  68. $this->assertNoText(t('Access denied'), 'node1 is viewable');
  69. // View node2, access must be denied
  70. $this->drupalGet('node/'. $this->node2->nid);
  71. $this->assertText(t('Access denied'), 'node2 is not viewable');
  72. // Login admin, swap permissions between content type and node2
  73. $this->drupalLogin($this->admin_user);
  74. // Restrict access to content type
  75. $this->changeAccessContentTypeKeyword('view', FALSE);
  76. // Grant access to node2
  77. $this->changeAccessNodeKeyword($this->node2, 'view');
  78. // Logout admin and try to access both nodes anonymously
  79. $this->drupalLogout();
  80. $this->drupalGet('node/'. $this->node1->nid);
  81. $this->assertText(t('Access denied'), 'node1 is not viewable');
  82. $this->drupalGet('node/'. $this->node2->nid);
  83. $this->assertText(t('Access denied'), 'node2 is not viewable');
  84. // Login test user, view node1, access must be denied
  85. $this->drupalLogin($this->test_user);
  86. $this->drupalGet('node/'. $this->node1->nid);
  87. $this->assertText(t('Access denied'), 'node1 is not viewable');
  88. // View node2, access must be granted
  89. $this->drupalGet('node/'. $this->node2->nid);
  90. $this->assertNoText(t('Access denied'), 'node2 is viewable');
  91. }
  92. /**
  93. * Test for editing nodes
  94. */
  95. function testEditAccess() {
  96. // Logout admin and try to edit the node anonymously
  97. $this->drupalLogout();
  98. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  99. $this->assertText(t('Access denied'), 'edit access denied for anonymous');
  100. // Login test user, edit node, access must be denied
  101. $this->drupalLogin($this->test_user);
  102. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  103. $this->assertText(t('Access denied'), 'edit access denied for test user');
  104. // Login admin and grant access for editing to the test user
  105. $this->drupalLogin($this->admin_user);
  106. $this->changeAccessContentTypeKeyword('update');
  107. // Logout admin and try to edit the node anonymously
  108. // access must be denied again
  109. $this->drupalLogout();
  110. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  111. $this->assertText(t('Access denied'), 'edit access denied for anonymous');
  112. // Login test user, edit node, access must be granted
  113. $this->drupalLogin($this->test_user);
  114. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  115. $this->assertNoText(t('Access denied'), 'node1 is editable');
  116. // Login admin and enable per node access
  117. $this->drupalLogin($this->admin_user);
  118. $this->changeAccessPerNode();
  119. // Restrict access for this content type for the test user
  120. $this->changeAccessContentTypeKeyword('update', FALSE);
  121. // Allow acces for node1 only
  122. $this->changeAccessNodeKeyword($this->node1, 'update');
  123. // Logout admin and try to edit both nodes anonymously
  124. $this->drupalLogout();
  125. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  126. $this->assertText(t('Access denied'), 'node1 is not editable');
  127. $this->drupalGet('node/'. $this->node2->nid .'/edit');
  128. $this->assertText(t('Access denied'), 'node2 is not editable');
  129. // Login test user, edit node1, access must be granted
  130. $this->drupalLogin($this->test_user);
  131. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  132. $this->assertNoText(t('Access denied'), 'node1 is editable');
  133. // Edit node2, access must be denied
  134. $this->drupalGet('node/'. $this->node2->nid .'/edit');
  135. $this->assertText(t('Access denied'), 'node2 is not editable');
  136. // Login admin, swap permissions between node1 and node2
  137. $this->drupalLogin($this->admin_user);
  138. // Grant edit access to node2
  139. $this->changeAccessNodeKeyword($this->node2, 'update');
  140. // Restrict edit acces to node1
  141. $this->changeAccessNodeKeyword($this->node1, 'update', FALSE);
  142. // Logout admin and try to edit both nodes anonymously
  143. $this->drupalLogout();
  144. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  145. $this->assertText(t('Access denied'), 'node1 is not editable');
  146. $this->drupalGet('node/'. $this->node2->nid .'/edit');
  147. $this->assertText(t('Access denied'), 'node2 is not editable');
  148. // Login test user, edit node1, access must be denied
  149. $this->drupalLogin($this->test_user);
  150. $this->drupalGet('node/'. $this->node1->nid .'/edit');
  151. $this->assertText(t('Access denied'), 'node1 is not editable');
  152. // Edit node2, access must be granted
  153. $this->drupalGet('node/'. $this->node2->nid .'/edit');
  154. $this->assertNoText(t('Access denied'), 'node2 is editable');
  155. }
  156. /**
  157. * Test for deleting nodes
  158. */
  159. function testDeleteAccess() {
  160. // Logout admin and try to delete the node anonymously
  161. $this->drupalLogout();
  162. $this->drupalGet('node/'. $this->node1->nid .'/delete');
  163. $this->assertText(t('Access denied'), 'delete access denied for anonymous');
  164. // Login test user, delete node, access must be denied
  165. $this->drupalLogin($this->test_user);
  166. $this->drupalGet('node/'. $this->node1->nid .'/delete');
  167. $this->assertText(t('Access denied'), 'delete access denied for test user');
  168. // Login admin and grant access for deleting to the test user
  169. $this->drupalLogin($this->admin_user);
  170. $this->changeAccessContentTypeKeyword('delete');
  171. // Logout admin and try to edit the node anonymously
  172. // access must be denied again
  173. $this->drupalLogout();
  174. $this->drupalGet('node/'. $this->node1->nid .'/delete');
  175. $this->assertText(t('Access denied'), 'delete access denied for anonymous');
  176. // Login test user, delete node, access must be granted
  177. $this->drupalLogin($this->test_user);
  178. $this->drupalPost('node/'. $this->node1->nid .'/delete', array(), 'Delete');
  179. $this->assertRaw(t('%node has been deleted', array('%node' => $this->node1->title)), 'Test node was deleted successfully by test user');
  180. // Login admin and recreate test node1
  181. $this->drupalLogin($this->admin_user);
  182. $this->node1 = $this->drupalCreateNode(array('type' => $this->content_type->type));
  183. // Enable per node access
  184. $this->changeAccessPerNode();
  185. // Restrict access for this content type for the test user
  186. $this->changeAccessContentTypeKeyword('delete', FALSE);
  187. // Allow acces for node1 only
  188. $this->changeAccessNodeKeyword($this->node1, 'delete');
  189. // Logout admin and try to delete both nodes anonymously
  190. $this->drupalLogout();
  191. $this->drupalGet('node/'. $this->node1->nid .'/delete');
  192. $this->assertText(t('Access denied'), 'node1 is not deletable');
  193. $this->drupalGet('node/'. $this->node2->nid .'/delete');
  194. $this->assertText(t('Access denied'), 'node2 is not deletable');
  195. // Login test user, delete node1, access must be granted
  196. $this->drupalLogin($this->test_user);
  197. $this->drupalGet('node/'. $this->node1->nid .'/delete');
  198. $this->assertNoText(t('Access denied'), 'node1 is deletable');
  199. // Delete node2, access must be denied
  200. $this->drupalGet('node/'. $this->node2->nid .'/delete');
  201. $this->assertText(t('Access denied'), 'node2 is not deletable');
  202. // Login admin, swap permissions between node1 and node2
  203. $this->drupalLogin($this->admin_user);
  204. // Grant delete access to node2
  205. $this->changeAccessNodeKeyword($this->node2, 'delete');
  206. // Restrict delete acces to node1
  207. $this->changeAccessNodeKeyword($this->node1, 'delete', FALSE);
  208. // Logout admin and try to delete both nodes anonymously
  209. $this->drupalLogout();
  210. $this->drupalGet('node/'. $this->node1->nid .'/delete');
  211. $this->assertText(t('Access denied'), 'node1 is not deletable');
  212. $this->drupalGet('node/'. $this->node2->nid .'/delete');
  213. $this->assertText(t('Access denied'), 'node2 is not deletable');
  214. // Login test user, delete node1, access must be denied
  215. $this->drupalLogin($this->test_user);
  216. $this->drupalGet('node/'. $this->node1->nid .'/delete');
  217. $this->assertText(t('Access denied'), 'node1 is not deletable');
  218. // Delete node2, access must be granted
  219. $this->drupalGet('node/'. $this->node2->nid .'/delete');
  220. $this->assertNoText(t('Access denied'), 'node2 is deletable');
  221. }
  222. /**
  223. * Test own view access
  224. */
  225. function testOwnViewAccess() {
  226. // Setup 2 test users
  227. $test_user1 = $this->test_user;
  228. $test_user2 = $this->drupalCreateUser();
  229. // Change ownership of test nodes to test users
  230. $this->node1->uid = $test_user1->uid;
  231. node_save($this->node1);
  232. $this->node2->uid = $test_user2->uid;
  233. node_save($this->node2);
  234. // Remove all view permissions for this content type
  235. $access_permissions = array(
  236. 'view[1]' => FALSE,
  237. 'view[2]' => FALSE,
  238. 'view_own[1]' => FALSE,
  239. 'view_own[2]' => FALSE,
  240. );
  241. $this->changeAccessContentType($access_permissions);
  242. // Allow view own content for test user 1 and 2 roles
  243. $this->changeAccessContentTypeKeyword('view_own', TRUE, $test_user1);
  244. $this->changeAccessContentTypeKeyword('view_own', TRUE, $test_user2);
  245. // Logout admin and try to access both nodes anonymously
  246. $this->drupalLogout();
  247. $this->drupalGet('node/'. $this->node1->nid);
  248. $this->assertText(t('Access denied'), 'node1 is not viewable');
  249. $this->drupalGet('node/'. $this->node2->nid);
  250. $this->assertText(t('Access denied'), 'node2 is not viewable');
  251. // Login test user 1, view node1, access must be granted
  252. $this->drupalLogin($test_user1);
  253. $this->drupalGet('node/'. $this->node1->nid);
  254. $this->assertNoText(t('Access denied'), 'node1 is viewable');
  255. // View node2, access must be denied
  256. $this->drupalGet('node/'. $this->node2->nid);
  257. $this->assertText(t('Access denied'), 'node2 is not viewable');
  258. // Login test user 2, view node1, access must be denied
  259. $this->drupalLogin($test_user2);
  260. $this->drupalGet('node/'. $this->node1->nid);
  261. $this->assertText(t('Access denied'), 'node1 is not viewable');
  262. // View node2, access must be granted
  263. $this->drupalGet('node/'. $this->node2->nid);
  264. $this->assertNoText(t('Access denied'), 'node2 is viewable');
  265. }
  266. }