privatemsg_groups.test

This file contains tests for the privatemsg groups module

File

privatemsg_groups/privatemsg_groups.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file contains tests for the privatemsg groups module
  5. */
  6. /**
  7. * Test cases for the privatemsg_groups module.
  8. */
  9. class PrivatemsgGroupsTestCase extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Privatemsg Groups functionality',
  13. 'description' => 'Tests sending messages to organic groups',
  14. 'group' => 'Privatemsg',
  15. 'dependencies' => array('og'),
  16. );
  17. }
  18. function setUp() {
  19. parent::setUp('privatemsg', 'privatemsg_groups', 'privatemsg_filter', 'pm_block_user', 'og');
  20. // Add OG group field to a the node's "page" bundle.
  21. og_create_field(OG_GROUP_FIELD, 'node', 'page');
  22. // Create group managers for these tests.
  23. $this->group1_manager = $this->drupalCreateUser(array('administer group', 'access content', 'create page content', 'edit own page content',
  24. 'read privatemsg', 'write privatemsg', 'write privatemsg to all organic groups', 'view organic groups recipients'));
  25. $this->group2_manager = $this->drupalCreateUser(array('administer group', 'access content', 'create page content', 'edit own page content',
  26. 'read privatemsg', 'write privatemsg', 'write privatemsg to own organic groups', 'view organic groups recipients'));
  27. // Create group members for these tests
  28. $this->user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  29. // Create group nodes.
  30. $settings = array(
  31. 'type' => 'page',
  32. OG_GROUP_FIELD . '[und][0][value]' => 1,
  33. 'uid' => $this->group1_manager->uid
  34. );
  35. $this->group1 = $this->drupalCreateNode($settings);
  36. $settings['uid'] = $this->group2_manager->uid;
  37. $this->group2 = $this->drupalCreateNode($settings);
  38. // Add user as group member.
  39. og_group('node', $this->group1->nid, array('entity' => $this->user1->uid));
  40. drupal_static_reset('privatemsg_recipient_get_types');
  41. }
  42. /**
  43. * Test sending message to a group using privatemsg_new_thread API method.
  44. */
  45. function testSendMessagetoGroupAPI() {
  46. $recipient = clone $this->group1;
  47. $recipient->recipient = $this->group1->nid;
  48. $recipient->type = _privatemsg_groups_get_group_recipient_type('node');
  49. privatemsg_new_thread(array($recipient), $subject = $this->randomName(10), $body = $this->randomName(50), array('author' => $this->group1_manager));
  50. $this->drupalLogin($this->user1);
  51. $this->drupalGet('messages');
  52. $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  53. $this->clickLink($subject);
  54. $this->assertText($body, t('Thread starter body displayed.'));
  55. }
  56. /**
  57. * Test batch sending using API and cron.
  58. */
  59. function testSendMessagetoGroupCron() {
  60. variable_set('privatemsg_recipient_small_threshold', 20);
  61. variable_set('privatemsg_cron_recipient_per_run', 20);
  62. // Create 26 users (more than are allowed to be process directly).
  63. $users = array();
  64. for ($i = 0; $i < 25; $i++) {
  65. $users[$i] = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  66. // Add user to group1.
  67. og_group('node', $this->group1->nid, array('entity' => $users[$i]->uid));
  68. }
  69. $recipient = clone $this->group1;
  70. $recipient->recipient = $this->group1->nid;
  71. $recipient->type = _privatemsg_groups_get_group_recipient_type('node');
  72. privatemsg_new_thread(array($recipient), $subject = $this->randomName(10), $body = $this->randomName(50), array('author' => $this->group1_manager));
  73. // Run cron.
  74. privatemsg_cron();
  75. // Test a few recipients to see if they received the message.
  76. foreach (array(0, 5, 17) as $uid) {
  77. $this->drupalLogin($users[$uid]);
  78. $this->drupalGet('messages');
  79. $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  80. $this->clickLink($subject);
  81. $this->assertText($body, t('Thread starter body displayed.'));
  82. }
  83. // Make sure that user 20 has not yet received the message.
  84. $this->drupalLogin($users[20]);
  85. $this->drupalGet('messages');
  86. $this->assertNoText($subject, t('Message is not yet displayed for this user'));
  87. // Run cron again.
  88. privatemsg_cron();
  89. // Test that the remaining recipients do now see the message too.
  90. foreach (array(20, 24) as $uid) {
  91. $this->drupalLogin($users[$uid]);
  92. $this->drupalGet('messages');
  93. $this->assertRaw($subject . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  94. $this->clickLink($subject);
  95. $this->assertText($body, t('Thread starter body displayed.'));
  96. }
  97. }
  98. /**
  99. * Test batch sending from GUI.
  100. */
  101. function testSendMessagetoGroupBatch() {
  102. variable_set('privatemsg_recipient_small_threshold', 20);
  103. // Create 25 users (more than are allowed to be process directly).
  104. $users = array();
  105. for ($i = 0; $i < 25; $i++) {
  106. $users[$i] = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  107. // Add user to group1.
  108. og_group('node', $this->group1->nid, array('entity' => $users[$i]->uid));
  109. }
  110. $this->drupalLogin($this->group1_manager);
  111. // Send a message to the group shared by all users.
  112. $edit = array(
  113. 'recipient' => $this->group1->title . '[group]',
  114. 'subject' => $this->randomName(10),
  115. 'body[value]' => $this->randomName(50),
  116. );
  117. $this->drupalPost('messages/new', $edit, t('Send message'));
  118. $this->assertText(t('A message has been sent to @group (group).', array('@group' => $this->group1->title)));
  119. // Test a few recipients to see if they all received the message.
  120. foreach ($users as $user) {
  121. $this->drupalLogin($user);
  122. $this->drupalGet('messages');
  123. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  124. $this->clickLink($edit['subject']);
  125. $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
  126. }
  127. }
  128. /**
  129. * Test global permissions and group permission.
  130. */
  131. function testPermission() {
  132. // Attempt to send message to group1 with no permission.
  133. $this->drupalLogin($this->user1);
  134. $edit = array(
  135. 'recipient' => $this->group1->title . '[group]',
  136. 'subject' => $this->randomName(10),
  137. 'body[value]' => $this->randomName(50),
  138. );
  139. $this->drupalPost('messages/new', $edit, t('Send message'));
  140. $this->assertText(t('You must include at least one valid recipient.'));
  141. $this->drupalLogout();
  142. // Attempt to send message to group1 with write to all permission but
  143. // no group permission.
  144. $user2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to all organic groups'));
  145. og_group('node', $this->group1->nid, array('entity' => $user2->uid));
  146. $this->drupalLogin($user2);
  147. $this->drupalPost('messages/new', $edit, t('Send message'));
  148. $this->assertText(t('A message has been sent to @group (group).', array('@group' => $this->group1->title)));
  149. $this->drupalLogout();
  150. // Attempt to send message to group1 with write to own permission but
  151. // no group permission.
  152. $user3 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to own organic groups'));
  153. og_group('node', $this->group1->nid, array('entity' => $user3->uid));
  154. $this->drupalLogin($user3);
  155. $this->drupalPost('messages/new', $edit, t('Send message'));
  156. $this->assertText(t('You must include at least one valid recipient.'));
  157. $this->drupalLogout();
  158. // Attempt to send message to group1 with write to own permission and
  159. // group permission.
  160. $roles = array_flip(og_roles('node', 'page', $this->group1->nid));
  161. og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], array(
  162. 'write privatemsg to group' => 1,
  163. ));
  164. $user4 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'write privatemsg to own organic groups'));
  165. og_group('node', $this->group1->nid, array('entity' => $user4->uid));
  166. $this->drupalLogin($user4);
  167. $this->drupalPost('messages/new', $edit, t('Send message'));
  168. $this->assertText(t('A message has been sent to @group (group).', array('@group' => $this->group1->title)));
  169. // Attempt to send message to group2 with write to own permission
  170. $edit['recipient'] = $this->group2->title . '[group]';
  171. $this->drupalPost('messages/new', $edit, t('Send message'));
  172. $this->assertText(t('You must include at least one valid recipient.'));
  173. }
  174. /**
  175. * Test sending message to group using GUI.
  176. */
  177. function testSendMessageToGroup() {
  178. $user2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'view organic groups recipients'));
  179. og_group('node', $this->group1->nid, array('entity' => $user2->uid));
  180. $group_name = $this->group1->title;
  181. $this->drupalLogin($this->group1_manager);
  182. // Verify autocomplete feature.
  183. $json = $this->drupalGet('messages/autocomplete/' . drupal_substr($group_name, 0, 2));
  184. $autocomplete = (array)json_decode($json);
  185. $this->assertEqual($autocomplete[$group_name . ', '], $group_name);
  186. // Access the form through a url that pre-fills the recipient field.
  187. $this->drupalGet('messages/new/'._privatemsg_groups_get_group_recipient_type('node').'_'.$this->group1->nid);
  188. // Send a message to group1.
  189. $edit = array(
  190. 'subject' => $this->randomName(10),
  191. 'body[value]' => $this->randomName(50),
  192. );
  193. $this->drupalPost(NULL, $edit, t('Send message'));
  194. $this->assertText(t('A message has been sent to @group (group).', array('@group' => $group_name)));
  195. // Log in as user1 and check that the message is listed, is marked as new
  196. // and can be marked as read.
  197. $this->drupalLogin($this->user1);
  198. $this->drupalGet('messages');
  199. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  200. $this->clickLink($edit['subject']);
  201. $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
  202. // Make sure that the user does not see the group.
  203. $this->assertNoText(t('@group (group)', array('@group' => $group_name)));
  204. // Reply to the message, only admin should see this.
  205. $reply1 = array(
  206. 'body[value]' => $this->randomName(50),
  207. );
  208. $this->drupalPost(NULL, $reply1, t('Send message'));
  209. $this->drupalGet('messages');
  210. $this->assertNoRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as read'));
  211. // Login as admin again and check if he sees the response.
  212. $this->drupalLogin($this->group1_manager);
  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($reply1['body[value]'], t('Reply1 is displayed'));
  217. // Reply to the message, all recipients should see this.
  218. $reply2 = array(
  219. 'body[value]' => $this->randomName(50),
  220. );
  221. $this->drupalPost(NULL, $reply2, t('Send message'));
  222. // Log in as user2 and check that he only sees the messages from admin.
  223. $this->drupalLogin($user2);
  224. $this->drupalGet('messages');
  225. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  226. $this->clickLink($edit['subject']);
  227. $this->assertText(t('@group (group)', array('@group' => $group_name)));
  228. $this->assertText($edit['body[value]'], t('Thread starter body is displayed'));
  229. $this->assertNoText($reply1['body[value]'], t('Reply1 is not displayed'));
  230. $this->assertText($reply2['body[value]'], t('Reply2 is displayed'));
  231. }
  232. /**
  233. * Test sending message to group when some members blocked the sender.
  234. */
  235. function testSendMessageToGroupBlocked() {
  236. $user2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  237. // Block admin as user 2.
  238. $this->drupalLogin($user2);
  239. $this->drupalPost('messages/blocked', array('name' => $this->group1_manager->name), t('Block user'));
  240. $this->drupalLogin($this->group1_manager);
  241. // Send a message to the group of user 1 and 2.
  242. $edit = array(
  243. 'recipient' => $this->group1->title. '[group]',
  244. 'subject' => $this->randomName(10),
  245. 'body[value]' => $this->randomName(50),
  246. );
  247. $this->drupalPost('messages/new', $edit, t('Send message'));
  248. $this->assertText(t('A message has been sent to @group (group).', array('@group' => $this->group1->title)));
  249. // Log in as user1 and check that the message is listed, is marked as new
  250. // and can be marked as read.
  251. $this->drupalLogin($this->user1);
  252. $this->drupalGet('messages');
  253. $this->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  254. $this->clickLink($edit['subject']);
  255. $this->assertText($edit['body[value]'], t('Thread starter body displayed.'));
  256. // Make sure that the user doesn't see the group recipient.
  257. $this->assertNoText(t('@group (group)', array('@group' => $this->group1->title)));
  258. // Log in as user2 and make sure that he didn't received the messages
  259. // as he blocks admin.
  260. $this->drupalLogin($user2);
  261. $this->drupalGet('messages');
  262. $this->assertNoText($edit['subject'], t('Message is not displayed'));
  263. }
  264. /**
  265. * Test to make sure newly added group member doesn't see old messages.
  266. */
  267. function testNewUser() {
  268. // Send a message to the group of user 1.
  269. $this->drupalLogin($this->group1_manager);
  270. $edit = array(
  271. 'recipient' => $this->group1->title . '[group]',
  272. 'subject' => $this->randomName(10),
  273. 'body[value]' => $this->randomName(50),
  274. );
  275. $this->drupalPost('messages/new', $edit, t('Send message'));
  276. $this->assertText(t('A message has been sent to @group (group).', array('@group' => $this->group1->title)));
  277. // Add user 2 to the same group now. The user should not see the message.
  278. $user2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
  279. og_group('node', $this->group1->nid, array('entity' => $user2->uid));
  280. $this->drupalLogin($user2);
  281. $this->drupalGet('messages');
  282. $this->assertNoText($edit['subject'], t('Newly added user does not see the old message sent to his group'));
  283. }
  284. /**
  285. * Verify that deleting a user does not leave stale data behind.
  286. */
  287. function testDeleteUser() {
  288. // Send a message to group 1.
  289. $this->drupalLogin($this->group1_manager);
  290. $edit = array(
  291. 'recipient' => $this->group1->title . '[group]',
  292. 'subject' => $this->randomName(10),
  293. 'body[value]' => $this->randomName(50),
  294. );
  295. $this->drupalPost('messages/new', $edit, t('Send message'));
  296. $this->assertText(t('A message has been sent to @group (group).', array('@group' => $this->group1->title)));
  297. $this->drupalLogin($this->user1);
  298. $this->drupalGet('messages');
  299. $this->assertText($edit['subject'], t('Message sent to group is visible.'));
  300. // Delete user and make sure that no recipient entries of him are left in
  301. // table.
  302. user_delete($this->user1->uid);
  303. $this->assertFalse(db_query_range("SELECT 1 FROM {pm_index} WHERE recipient = :recipient AND type IN ('user', 'hidden')", 0, 1, array(':recipient' => $this->user1->uid))->fetchField());
  304. }
  305. }

Classes

Namesort descending Description
PrivatemsgGroupsTestCase Test cases for the privatemsg_groups module.