role_delegation.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Role Delegation module.
  5. */
  6. /**
  7. * Base class for Role Delegation tests.
  8. */
  9. class RoleDelegationTestCase extends DrupalWebTestCase {
  10. protected $rid_high, $rid_low, $user_high, $user_low;
  11. /**
  12. * Assign or remove one role to/from one user.
  13. *
  14. * The logged in user must have the "administer users"
  15. * permission in order for this function to succeed.
  16. *
  17. * @param $rid
  18. * The role id of the role to assign or remove.
  19. * @param $user
  20. * The user object of the user to assign/remove the role to.
  21. * @param $assign
  22. * TRUE (the default) to assign the role, or
  23. * FALSE to remove it.
  24. *
  25. * @return
  26. * TRUE or FALSE depending on whether the role was
  27. * successfully assigned or removed.
  28. */
  29. protected function assignRoleToUser($rid, $user, $assign = TRUE) {
  30. $this->drupalGet("user/{$user->uid}/edit");
  31. if (count($this->xpath("//input[@name='roles[$rid]']"))) {
  32. $name = "roles[$rid]";
  33. }
  34. elseif (count($this->xpath("//input[@name='roles_change[$rid]']"))) {
  35. $name = "roles_change[$rid]";
  36. }
  37. else {
  38. return FALSE;
  39. }
  40. $this->drupalPost(NULL, array($name => $assign), t('Save'));
  41. $elements = $this->xpath("//input[@name='$name']");
  42. return isset($elements[0]) && ($assign XOR empty($elements[0]['checked']));
  43. }
  44. /**
  45. * Assign or remove one permission to/from one role, and assert
  46. * that the result succeeded.
  47. *
  48. * @param $permission
  49. * The name of the permission to assign or remove.
  50. * @param $rid
  51. * The role id of the role to assign/remove the permission to/from.
  52. * @param $assign
  53. * TRUE (the default) to assign the permission, or
  54. * FALSE to remove it.
  55. *
  56. * @return
  57. * TRUE or FALSE depending on whether the permission was
  58. * successfully assigned or removed.
  59. */
  60. protected function assignPermissionToRole($permission, $rid, $assign = TRUE) {
  61. $name = "{$rid}[{$permission}]";
  62. $this->drupalPost("admin/people/permissions/$rid", array($name => $assign), t('Save permissions'));
  63. $elements = $this->xpath("//input[@name='$name']");
  64. $this->assertTrue(
  65. isset($elements[0]) && ($assign XOR empty($elements[0]['checked'])),
  66. ($assign ? 'Assign' : 'Remove') . ' permission "' . $permission . '" ' . ($assign ? 'to' : 'from') . " role $rid."
  67. );
  68. }
  69. public function setUp() {
  70. // Enable modules
  71. parent::setUp('role_delegation');
  72. // Create roles
  73. $this->rid_high = $this->drupalCreateRole(array(), 'high');
  74. $this->rid_low = $this->drupalCreateRole(array(), 'low' );
  75. // Create users
  76. $this->user_high = $this->drupalCreateUser(array('administer users'));
  77. $this->user_low = $this->drupalCreateUser(array('administer users'));
  78. // Create privileged user and log in
  79. $this->drupalLogin($this->drupalCreateUser(array('administer users', 'administer permissions')));
  80. // Assign permissions to roles
  81. $this->assignPermissionToRole('assign low role', $this->rid_high); // 'high' can assign 'low'
  82. // Assign roles to users
  83. $this->assertTrue(
  84. $this->assignRoleToUser($this->rid_high, $this->user_high),
  85. 'Assign high role to high user'
  86. );
  87. }
  88. }
  89. /**
  90. * Functional tests for permissions.
  91. */
  92. class RoleDelegationPermissionsTestCase extends RoleDelegationTestCase {
  93. public static function getInfo() {
  94. return array(
  95. 'name' => t('Permissions'),
  96. 'description' => t('Check that role assignment permissions are enforced.'),
  97. 'group' => t('Role Delegation'),
  98. );
  99. }
  100. /**
  101. * Check that high role can assign low role.
  102. */
  103. public function testHighLow() {
  104. $this->drupalLogin($this->user_high);
  105. $this->assertTrue(
  106. $this->assignRoleToUser($this->rid_low, $this->user_low), // could be any user
  107. t('!role1 role can assign !role2 role.', array('!role1' => 'High', '!role2' => 'low')),
  108. t('Role Delegation')
  109. );
  110. }
  111. /**
  112. * Check that high role can't assign high role.
  113. */
  114. public function testHighHigh() {
  115. $this->drupalLogin($this->user_high);
  116. // Just check that no option is presented to the user.
  117. $this->assertFalse(
  118. $this->assignRoleToUser($this->rid_high, $this->user_high), // could be any user
  119. t("!role1 role can't assign !role2 role.", array('!role1' => 'High', '!role2' => 'high')),
  120. t('Role Delegation')
  121. );
  122. }
  123. /**
  124. * Check that roles can't be assigned by forgery.
  125. */
  126. public function testRoleForgery() {
  127. $this->drupalLogin($this->user_high);
  128. // Have the nefarious high user forge an option to assign the high role...
  129. $this->drupalGet("user/{$this->user_low->uid}/edit");
  130. $name = "roles_change[{$this->rid_low}]";
  131. $input = $this->xpath("//input[@name='$name']");
  132. $dome = dom_import_simplexml($input[0]);
  133. $dome->setAttribute('value', $this->rid_high);
  134. // ... then submit the form, and check that he didn't get the role.
  135. $this->drupalPost(NULL, array($name => TRUE), t('Save'));
  136. $this->assertRaw(
  137. t('An illegal choice has been detected. Please contact the site administrator.'),
  138. t('Role assignment forgery is blocked.') . ' (#1)',
  139. t('Role Delegation')
  140. );
  141. $this->assertFieldByName(
  142. $name,
  143. $this->rid_low,
  144. t('Role assignment forgery is blocked.') . ' (#2)',
  145. t('Role Delegation')
  146. );
  147. }
  148. }
  149. /**
  150. * Functional tests for operations.
  151. */
  152. class RoleDelegationOperationsTestCase extends RoleDelegationTestCase {
  153. public static function getInfo() {
  154. return array(
  155. 'name' => t('Operations'),
  156. 'description' => t('Check that role assignment bulk operations are available and work as intended.'),
  157. 'group' => t('Role Delegation'),
  158. );
  159. }
  160. /**
  161. * Check that the right combination of Add and Remove role
  162. * operations is present in the user bulk update form.
  163. */
  164. public function testOperationsExist() {
  165. $this->drupalLogin($this->user_high);
  166. $this->drupalGet('admin/people');
  167. $this->assertFieldByXPath(
  168. '//select[@name="operation"]//option',
  169. "role_delegation_add_role-{$this->rid_low}",
  170. t("!user user can use Add !role role operation.", array('!user' => 'High', '!role' => 'low')),
  171. t('Role Delegation')
  172. );
  173. $this->assertFieldByXPath(
  174. '//select[@name="operation"]//option',
  175. "role_delegation_remove_role-{$this->rid_low}",
  176. t("!user user can use Remove !role role operation.", array('!user' => 'High', '!role' => 'low')),
  177. t('Role Delegation')
  178. );
  179. $this->assertNoFieldByXPath(
  180. '//select[@name="operation"]//option',
  181. "role_delegation_add_role-{$this->rid_high}",
  182. t("!user user can't use Add !role role operation.", array('!user' => 'High', '!role' => 'high')),
  183. t('Role Delegation')
  184. );
  185. $this->assertNoFieldByXPath(
  186. '//select[@name="operation"]//option',
  187. "role_delegation_remove_role-{$this->rid_high}",
  188. t("!user user can't use Remove !role role operation.", array('!user' => 'High', '!role' => 'high')),
  189. t('Role Delegation')
  190. );
  191. }
  192. /**
  193. * Check that Add and Remove role operations work as intended.
  194. */
  195. public function testOperationsWork() {
  196. $uids_to_test = array($this->user_high->uid, $this->user_low->uid);
  197. $edit = array();
  198. foreach ($uids_to_test as $uid) {
  199. $edit["accounts[$uid]"] = TRUE;
  200. }
  201. $this->drupalLogin($this->user_high);
  202. $this->drupalGet('admin/people');
  203. // Add low role
  204. $edit['operation'] = "role_delegation_add_role-{$this->rid_low}";
  205. $this->drupalPost(NULL, $edit, t('Update'));
  206. foreach ($uids_to_test as $uid) {
  207. $this->assertFieldByXPath(
  208. "//tbody/tr[$uid]/td[4]//li",
  209. 'low',
  210. t('!user user assigned !role role to user !uid.', array('!user' => 'High', '!role' => 'low', '!uid' => $uid)),
  211. t('Role Delegation')
  212. );
  213. }
  214. // Remove low role
  215. $edit['operation'] = "role_delegation_remove_role-{$this->rid_low}";
  216. $this->drupalPost(NULL, $edit, t('Update'));
  217. foreach ($uids_to_test as $uid) {
  218. $this->assertNoFieldByXPath(
  219. "//tbody/tr[$uid]/td[4]//li",
  220. 'low',
  221. t('!user user removed !role role from user !uid.', array('!user' => 'High', '!role' => 'low', '!uid' => $uid)),
  222. t('Role Delegation')
  223. );
  224. }
  225. }
  226. /**
  227. * Check that operations can't be forged.
  228. */
  229. public function testOperationsForgery() {
  230. $this->drupalLogin($this->user_high);
  231. $this->drupalGet('admin/people');
  232. // Forge an operation to add the high role...
  233. $option = $this->xpath("//select[@name='operation']//option[@value='role_delegation_add_role-{$this->rid_low}']");
  234. if (count($option)==0) {
  235. return;
  236. }
  237. $dome = dom_import_simplexml($option[0]);
  238. $dome->setAttribute('value', "role_delegation_add_role-{$this->rid_high}");
  239. // ... then submit the form, and check that it wasn't granted.
  240. $edit = array(
  241. "accounts[{$this->user_low->uid}]" => TRUE,
  242. "operation" => "role_delegation_add_role-{$this->rid_high}",
  243. );
  244. $this->drupalPost(NULL, $edit, t('Update'));
  245. $this->assertRaw(
  246. t('An illegal choice has been detected. Please contact the site administrator.'),
  247. t('Role assignment forgery is blocked.') . ' (#1)',
  248. t('Role Delegation')
  249. );
  250. $this->assertNoFieldByXPath(
  251. "//tbody/tr[{$this->user_high->uid}]/td[4]//li",
  252. 'high',
  253. t('Role assignment forgery is blocked.') . ' (#2)',
  254. t('Role Delegation')
  255. );
  256. }
  257. }
  258. /**
  259. * Functional tests for editing roles.
  260. */
  261. class RoleDelegationRoleEditingTestCase extends RoleDelegationTestCase {
  262. public static function getInfo() {
  263. return array(
  264. 'name' => t('Role editing'),
  265. 'description' => t('Check that role assignment permissions are updated correctly when roles are renamed or deleted.'),
  266. 'group' => t('Role Delegation'),
  267. );
  268. }
  269. /**
  270. * Rename a role, and check that users that had permission to assign
  271. * the old role now have permission to assign the new one.
  272. */
  273. public function testRenameRole() {
  274. $this->drupalPost("admin/people/permissions/roles/edit/{$this->rid_low}", array('name' => 'new low'), t('Save role'));
  275. $this->drupalGet('admin/people/permissions');
  276. $this->assertFieldChecked(
  277. "edit-{$this->rid_high}-assign-new-low-role",
  278. t('Permissions are updated when role is renamed.'),
  279. t('Role Delegation')
  280. );
  281. }
  282. /**
  283. * Delete a role, then create a new one with the same name.
  284. * Check that no users have permission to assign the new role.
  285. */
  286. public function testDeleteRole() {
  287. $this->drupalPost("admin/people/permissions/roles/delete/{$this->rid_low}", NULL, t('Delete'));
  288. $this->drupalPost('admin/people/permissions/roles', array('name' => 'low'), t('Add role'));
  289. $this->drupalGet('admin/people/permissions');
  290. $this->assertNoFieldChecked(
  291. "edit-{$this->rid_high}-assign-low-role",
  292. t('Permissions are updated when role is deleted.'),
  293. t('Role Delegation')
  294. );
  295. }
  296. }