privatemsg_roles.test

  1. 7-1 privatemsg_roles/privatemsg_roles.test
  2. 7-2 privatemsg_roles/privatemsg_roles.test
  3. 6-2 privatemsg_roles/privatemsg_roles.test

This file contains tests for the privatemsg roles module

File

privatemsg_roles/privatemsg_roles.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file contains tests for the privatemsg roles module
  5. */
  6. /**
  7. * Test cases for the privatemsg_roles module.
  8. */
  9. class PrivatemsgRolesTestCase extends PrivatemsgBaseTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Privatemsg Roles functionality',
  13. 'description' => 'Tests sending messages to roles',
  14. 'group' => 'Privatemsg',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('privatemsg', 'privatemsg_roles', 'privatemsg_filter', 'pm_block_user');
  19. }
  20. function testSendMessagetoRoleAPI() {
  21. $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to all roles'));
  22. $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  23. $user2 = $this->drupalCreateUser();
  24. // Add role of user 1 to user 2;
  25. $edit = array('roles' => $user2->roles + $user1->roles);
  26. user_save($user2, $edit);
  27. $recipient = user_role_load($user1->roles[4]);
  28. $recipient->recipient = $recipient->rid;
  29. $recipient->type = 'role';
  30. privatemsg_new_thread(array($recipient), $subject = $this->randomName(10), $body = $this->randomName(50), array('author' => $admin));
  31. $this->drupalLogin($user2);
  32. $this->drupalGet('messages');
  33. $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  34. $this->clickLink($subject);
  35. $this->assertText($body, t('Thread starter body displayed.'));
  36. }
  37. function testSendMessagetoRoleCron() {
  38. $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  39. variable_set('privatemsg_recipient_small_threshold', 20);
  40. variable_set('privatemsg_cron_recipient_per_run', 20);
  41. // Create 26 users (more than are allowed to be process directly).
  42. $users = array();
  43. for ($i = 0; $i < 25; $i++) {
  44. $users[$i] = $this->drupalCreateUser();
  45. // Add role of user 1 to user 2;
  46. $edit = array('roles' => $users[$i]->roles + $user1->roles);
  47. user_save($users[$i], $edit);
  48. }
  49. // Update permissions.
  50. $this->checkPermissions(array(), TRUE);
  51. $recipient = user_role_load($user1->roles[3]);
  52. $recipient->recipient = $recipient->rid;
  53. $recipient->type = 'role';
  54. // Create user with specific role permission.
  55. $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to role ' . $recipient->name));
  56. privatemsg_new_thread(array($recipient), $subject = $this->randomName(10), $body = $this->randomName(50), array('author' => $admin));
  57. // Run cron.
  58. privatemsg_cron();
  59. // Test a few recipients to see if they received the message.
  60. foreach (array(0, 5, 18) as $uid) {
  61. $this->drupalLogin($users[$uid]);
  62. $this->drupalGet('messages');
  63. $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  64. $this->clickLink($subject);
  65. $this->assertText($body, t('Thread starter body displayed.'));
  66. }
  67. // Make sure that user 20 has not yet received the message.
  68. $this->drupalLogin($users[20]);
  69. $this->drupalGet('messages');
  70. $this->assertNoText($subject, t('Message is not yet displayed for this user'));
  71. // Run cron again.
  72. privatemsg_cron();
  73. // Test that the remaining recipients do now see the message too.
  74. foreach (array(20, 24) as $uid) {
  75. $this->drupalLogin($users[$uid]);
  76. $this->drupalGet('messages');
  77. $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  78. $this->clickLink($subject);
  79. $this->assertText($body, t('Thread starter body displayed.'));
  80. }
  81. }
  82. function testSendMessagetoRoleBatch() {
  83. $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to all roles'));
  84. $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  85. variable_set('privatemsg_recipient_small_threshold', 20);
  86. // Create 25 users (more than are allowed to be process directly).
  87. $users = array();
  88. for ($i = 0; $i < 25; $i++) {
  89. $users[$i] = $this->drupalCreateUser();
  90. // Add role of user 1 to user 2;
  91. $edit = array('roles' => $users[$i]->roles + $user1->roles);
  92. user_save($users[$i], $edit);
  93. }
  94. $this->drupalLogin($admin);
  95. // Send a message to the role shared by all users.
  96. $edit = array(
  97. 'recipient' => user_role_load($user1->roles[4])->name . '[role]',
  98. 'subject' => $this->randomName(10),
  99. 'body[value]' => $this->randomName(50),
  100. );
  101. $this->drupalPost('messages/new', $edit, t('Send message'));
  102. $this->assertText(t('A message has been sent to @role (role).', array('@role' => user_role_load($user1->roles[4])->name)));
  103. // Test a few recipients to see if they all received the message.
  104. foreach ($users as $user) {
  105. $this->drupalLogin($user);
  106. $this->drupalGet('messages');
  107. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  108. $this->clickLink($edit['subject']);
  109. $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
  110. }
  111. }
  112. function testPermission() {
  113. $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  114. // Update permissions.
  115. $this->checkPermissions(array(), TRUE);
  116. // Give permission to write only a specific role.
  117. $role_name = user_role_load($user1->roles[3])->name;
  118. $user2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to role ' . $role_name));
  119. $this->drupalLogin($user2);
  120. // Send a message to the role of user 1 and 2.
  121. $edit = array(
  122. 'recipient' => $user2->roles[4] . '[role]',
  123. 'subject' => $this->randomName(10),
  124. 'body[value]' => $this->randomName(50),
  125. );
  126. $this->drupalPost('messages/new', $edit, t('Send message'));
  127. $this->assertText(t('You must include at least one valid recipient.'));
  128. }
  129. function testSendMessageToRole() {
  130. $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  131. $user2 = $this->drupalCreateUser(array('view roles recipients'));
  132. // Add role of user 1 to user 2;
  133. $edit = array('roles' => $user2->roles + $user1->roles);
  134. user_save($user2, $edit);
  135. // Update permissions.
  136. $this->checkPermissions(array(), TRUE);
  137. $role_name = user_role_load($user1->roles[3])->name;
  138. $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'view roles recipients', 'write privatemsg to role ' . $role_name));
  139. $this->drupalLogin($admin);
  140. // Verify autocomplete feature.
  141. $role_name = user_role_load($user1->roles[3])->name;
  142. $json = $this->drupalGet('messages/autocomplete/' . drupal_substr($role_name, 0, 2));
  143. $autocomplete = (array)json_decode($json);
  144. $this->assertEqual($autocomplete[$role_name . ', '], $role_name);
  145. // Access the form through a url that pre-fills the recipient field.
  146. $this->drupalGet('messages/new/role_3');
  147. // Send a message to the role of user 1 and 2.
  148. $edit = array(
  149. 'subject' => $this->randomName(10),
  150. 'body[value]' => $this->randomName(50),
  151. );
  152. $this->drupalPost(NULL, $edit, t('Send message'));
  153. $this->assertText(t('A message has been sent to @role (role).', array('@role' => $role_name)));
  154. // Log in as user1 and check that the message is listed, is marked as new
  155. // and can be marked as read.
  156. $this->drupalLogin($user1);
  157. $this->drupalGet('messages');
  158. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  159. $this->clickLink($edit['subject']);
  160. $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
  161. // Make sure that the user does not see the role.
  162. $this->assertNoText(t('@role (role)', array('@role' => $role_name)));
  163. // Reply to the message, only admin should see this.
  164. $reply1 = array(
  165. 'body[value]' => $this->randomName(50),
  166. );
  167. $this->drupalPost(NULL, $reply1, t('Send message'));
  168. $this->drupalGet('messages');
  169. $this->assertNoRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as read'));
  170. // Login as admin again and check if he sees the response.
  171. $this->drupalLogin($admin);
  172. $this->drupalGet('messages');
  173. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  174. $this->clickLink($edit['subject']);
  175. $this->assertText($reply1['body[value]'], t('Reply1 is displayed'));
  176. // Reply to the message, all recipients should see this.
  177. $reply2 = array(
  178. 'body[value]' => $this->randomName(50),
  179. );
  180. $this->drupalPost(NULL, $reply2, t('Send message'));
  181. // Log in as user2 and check that he only sees the messages from admin.
  182. $this->drupalLogin($user2);
  183. $this->drupalGet('messages');
  184. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  185. $this->clickLink($edit['subject']);
  186. $this->assertText(t('@role (role)', array('@role' => $role_name)));
  187. $this->assertText($edit['body[value]'], t('Thread starter body is displayed'));
  188. $this->assertNoText($reply1['body[value]'], t('Reply1 is not displayed'));
  189. $this->assertText($reply2['body[value]'], t('Reply2 is displayed'));
  190. }
  191. function testSendMessageToRoleBlocked() {
  192. $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to all roles'));
  193. $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  194. $user2 = $this->drupalCreateUser();
  195. // Add role of user 1 to user 2;
  196. $edit = array('roles' => $user2->roles + $user1->roles);
  197. user_save($user2, $edit);
  198. // Block admin as user 2.
  199. $this->drupalLogin($user2);
  200. $this->drupalPost('messages/blocked', array('name' => $admin->name), t('Block user'));
  201. $this->drupalLogin($admin);
  202. // Send a message to the role of user 1 and 2.
  203. $edit = array(
  204. 'recipient' => user_role_load($user1->roles[4])->name . '[role]',
  205. 'subject' => $this->randomName(10),
  206. 'body[value]' => $this->randomName(50),
  207. );
  208. $this->drupalPost('messages/new', $edit, t('Send message'));
  209. $this->assertText(t('A message has been sent to @role (role).', array('@role' => user_role_load($user1->roles[4])->name)));
  210. // Log in as user1 and check that the message is listed, is marked as new
  211. // and can be marked as read.
  212. $this->drupalLogin($user1);
  213. $this->drupalGet('messages');
  214. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  215. $this->clickLink($edit['subject']);
  216. $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
  217. // Make sure that the user doesn't see the role recipient.
  218. $this->assertNoText(t('@role (role)', array('@role' => user_role_load($user1->roles[4])->name)));
  219. // Log in as user2 and make sure that he didn't received the messages
  220. // as he blocks admin.
  221. $this->drupalLogin($user2);
  222. $this->drupalGet('messages');
  223. $this->assertNoText($edit['subject'], t('Message is not displayed'));
  224. }
  225. function testNewUser() {
  226. $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to all roles'));
  227. $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  228. // Send a message to the role of user 1.
  229. $this->drupalLogin($admin);
  230. $edit = array(
  231. 'recipient' => user_role_load($user1->roles[4])->name . '[role]',
  232. 'subject' => $this->randomName(10),
  233. 'body[value]' => $this->randomName(50),
  234. );
  235. $this->drupalPost('messages/new', $edit, t('Send message'));
  236. $this->assertText(t('A message has been sent to @role (role).', array('@role' => user_role_load($user1->roles[4])->name)));
  237. // Add user 2 with the same role now. The user should not see the message.
  238. $user2 = $this->drupalCreateUser();
  239. // Add role of user 1 to user 2;
  240. $edit_roles = array('roles' => $user2->roles + $user1->roles);
  241. user_save($user2, $edit_roles);
  242. $this->drupalLogin($user2);
  243. $this->drupalGet('messages');
  244. $this->assertNoText($edit['subject'], t('Newly added user does not see the old message sent to his role'));
  245. }
  246. /**
  247. * Verify that deleting a user does not leave stale data behind.
  248. */
  249. function testDeleteUser() {
  250. $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to all roles'));
  251. $user = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  252. // Send a message to the role of user 1.
  253. $role_name = user_role_load(4)->name;
  254. $this->drupalLogin($admin);
  255. $edit = array(
  256. 'recipient' => $role_name . '[role]',
  257. 'subject' => $this->randomName(10),
  258. 'body[value]' => $this->randomName(50),
  259. );
  260. $this->drupalPost('messages/new', $edit, t('Send message'));
  261. $this->assertText(t('A message has been sent to @role (role).', array('@role' => $role_name)));
  262. $this->drupalLogin($user);
  263. $this->drupalGet('messages');
  264. $this->assertText($edit['subject'], t('Message sent to role is visible.'));
  265. // Delete user and make sure that no recipient entries of him are left in
  266. // table.
  267. user_delete($user->uid);
  268. $this->assertFalse(db_query_range("SELECT 1 FROM {pm_index} WHERE recipient = :recipient AND type IN ('user', 'hidden')", 0, 1, array(':recipient' => $user->uid))->fetchField());
  269. }
  270. }

Classes

Namesort descending Description
PrivatemsgRolesTestCase Test cases for the privatemsg_roles module.