account_sentinel.test 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * @file
  4. * Contains automated tests for Account Sentinel.
  5. */
  6. /**
  7. * Provides test cases for Account Sentinel.
  8. */
  9. class AccountSentinelTest extends DrupalWebTestCase {
  10. /**
  11. * Returns the test's metadata.
  12. *
  13. * @return array
  14. * Information about the test.
  15. */
  16. public static function getInfo() {
  17. return array(
  18. 'name' => 'Account Sentinel tests',
  19. 'description' => 'Test the Account Sentinel module.',
  20. 'group' => 'Account Sentinel',
  21. );
  22. }
  23. protected $admin;
  24. protected $subject;
  25. protected $role;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function setUp() {
  30. parent::setUp('account_sentinel');
  31. // Create an administrator and log in.
  32. $this->admin = $this->drupalCreateUser(array(
  33. 'administer permissions',
  34. 'administer users',
  35. 'access account sentinel logs',
  36. 'administer account sentinel',
  37. ));
  38. $this->drupalLogin($this->admin);
  39. // Create subject user.
  40. $this->subject = $this->drupalCreateUser();
  41. // Create role to monitor.
  42. $this->role = $this->drupalCreateRole(array());
  43. // Grant role to subject user.
  44. user_save($this->subject, array(
  45. 'name' => 'name',
  46. 'pass' => 'pass',
  47. 'mail' => 'mail@example.com',
  48. 'roles' => array($this->role => TRUE),
  49. ));
  50. // Configure the module.
  51. $edit = array(
  52. 'account_sentinel_cron_method' => 'custom',
  53. );
  54. foreach (user_roles(TRUE) as $rid => $role) {
  55. $edit['account_sentinel_monitored_roles[' . $rid . ']'] = ($rid == $this->role);
  56. }
  57. $this->drupalPost('admin/config/system/account-sentinel', $edit, t('Save configuration'));
  58. }
  59. /**
  60. * User modification detection for changes made in Drupal.
  61. */
  62. public function testModifyInDrupal() {
  63. // Change name, pass and email of user, also block it and remove its roles.
  64. user_save($this->subject, array(
  65. 'name' => 'name_',
  66. 'pass' => 'pass_',
  67. 'mail' => 'mail_@example.com',
  68. 'status' => 0,
  69. 'roles' => array(),
  70. ));
  71. // Update the $subject variable too.
  72. $this->subject = user_load($this->subject->uid);
  73. // Check the log for the changes made.
  74. $this->drupalGet('admin/reports/account-sentinel');
  75. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_NAME, array(
  76. 'old' => 'name',
  77. 'new' => 'name_',
  78. )));
  79. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_PASS, array()));
  80. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_MAIL, array(
  81. 'old' => 'mail@example.com',
  82. 'new' => 'mail_@example.com',
  83. )));
  84. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_ROLE_REMOVE, array(
  85. 'rid' => $this->role,
  86. )));
  87. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_USER_BLOCK, array(
  88. 'rid' => $this->role,
  89. )));
  90. // Unblock the user and grant the monitored role.
  91. user_save($this->subject, array(
  92. 'status' => 1,
  93. 'roles' => array($this->role => TRUE),
  94. ));
  95. // Check the log for the changes made.
  96. $this->drupalGet('admin/reports/account-sentinel');
  97. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_USER_UNBLOCK, array(
  98. 'rid' => $this->role,
  99. )));
  100. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_ROLE_ADD, array(
  101. 'rid' => $this->role,
  102. )));
  103. // Delete the user.
  104. user_delete($this->subject->uid);
  105. // Check the log for the changes made.
  106. $this->drupalGet('admin/reports/account-sentinel');
  107. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_USER_DELETE, array(
  108. 'uid' => $this->subject->uid,
  109. 'name' => $this->subject->name,
  110. 'mail' => $this->subject->mail,
  111. )));
  112. // Readd the user.
  113. $this->subject->is_new = TRUE;
  114. user_save($this->subject);
  115. // Check the log for the changes made.
  116. $this->drupalGet('admin/reports/account-sentinel');
  117. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_USER_ADD, array(
  118. 'uid' => $this->subject->uid,
  119. 'name' => $this->subject->name,
  120. 'mail' => $this->subject->mail,
  121. )));
  122. }
  123. /**
  124. * Runs the database audit by accessing the custom cron URL.
  125. */
  126. protected function runAudit() {
  127. $this->drupalGet('system/account-sentinel-cron', array(
  128. 'query' => array('key' => account_sentinel_get_cron_key()),
  129. ));
  130. }
  131. /**
  132. * User modification detection for changes made via database modification.
  133. */
  134. public function testModifyInDatabase() {
  135. // Change name, pass and mail of user, also block it.
  136. $update = db_update('users');
  137. $update->fields(array(
  138. 'name' => 'name_',
  139. 'pass' => 'pass_',
  140. 'mail' => 'mail_@example.com',
  141. 'status' => 0,
  142. ));
  143. $update->condition('uid', $this->subject->uid);
  144. $update->execute();
  145. // Update the $subject variable too.
  146. $this->subject = user_load($this->subject->uid);
  147. // Run the audit and check the log for the changes made.
  148. $this->runAudit();
  149. $this->drupalGet('admin/reports/account-sentinel');
  150. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_NAME, array(
  151. 'old' => 'name',
  152. 'new' => 'name_',
  153. )));
  154. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_PASS, array()));
  155. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_MAIL, array(
  156. 'old' => 'mail@example.com',
  157. 'new' => 'mail_@example.com',
  158. )));
  159. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_USER_BLOCK, array(
  160. 'rid' => $this->role,
  161. )));
  162. // Remove the monitored role from the user.
  163. $remove_role = db_delete('users_roles');
  164. $remove_role->condition('uid', $this->subject->uid)
  165. ->condition('rid', $this->role);
  166. $remove_role->execute();
  167. // Run the audit and check the log for the changes made.
  168. $this->runAudit();
  169. $this->drupalGet('admin/reports/account-sentinel');
  170. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_ROLE_REMOVE, array(
  171. 'rid' => $this->role,
  172. )));
  173. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_SNAPSHOT_MISSING, array()));
  174. // Grant the role to the user.
  175. $add_role = db_insert('users_roles');
  176. $add_role->fields(array(
  177. 'uid' => $this->subject->uid,
  178. 'rid' => $this->role,
  179. ));
  180. $add_role->execute();
  181. // Run the audit and check the log for the changes made.
  182. $this->runAudit();
  183. $this->drupalGet('admin/reports/account-sentinel');
  184. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_ROLE_ADD, array(
  185. 'rid' => $this->role,
  186. )));
  187. // Unblock the user.
  188. $update = db_update('account_sentinel_users');
  189. $update->fields(array(
  190. 'status' => 1,
  191. ));
  192. $update->condition('uid', $this->subject->uid);
  193. $update->execute();
  194. // Run the audit and check the log for the changes made.
  195. $this->runAudit();
  196. $this->drupalGet('admin/reports/account-sentinel');
  197. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_SNAPSHOT_INVALID, array()));
  198. // Delete the user.
  199. $remove_user = db_delete('users');
  200. $remove_user->condition('uid', $this->subject->uid);
  201. $remove_user->execute();
  202. // Run the audit and check the log for the changes made.
  203. $this->runAudit();
  204. $this->drupalGet('admin/reports/account-sentinel');
  205. $this->assertRaw(account_sentinel_get_event_message(ACCOUNT_SENTINEL_EVENT_TYPE_USER_DELETE, array(
  206. 'uid' => $this->subject->uid,
  207. 'name' => $this->subject->name,
  208. 'mail' => $this->subject->mail,
  209. )));
  210. }
  211. }