pm_block_user.test

  1. 1 pm_block_user/pm_block_user.test
  2. 7-1 pm_block_user/pm_block_user.test
  3. 7-2 pm_block_user/pm_block_user.test
  4. 6-2 pm_block_user/pm_block_user.test

Test file for pm_block_user.module

File

pm_block_user/pm_block_user.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Test file for pm_block_user.module
  5. */
  6. class PrivatemsgBlockUserCase extends PrivatemsgBaseTestCase {
  7. /**
  8. * Implements getInfo().
  9. */
  10. public static function getInfo() {
  11. return array(
  12. 'name' => t('User blocking functionality.'),
  13. 'description' => t('Test blocking and unblocking of users'),
  14. 'group' => t('Privatemsg'),
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('privatemsg', 'pm_block_user');
  19. }
  20. /**
  21. * Test role blocking rules.
  22. */
  23. function testRoleBlockRecipient() {
  24. // User id 3, Role id 4.
  25. $author_user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
  26. // User id 4, Role id 5.
  27. $blocked_user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
  28. // User id 5, Role id 6.
  29. $admin_user = $this->drupalCreateUser(array('administer privatemsg settings'));
  30. // Login the admin user and set up the role blocking rules.
  31. $this->drupalLogin($admin_user);
  32. $this->drupalPost('admin/config/messaging/privatemsg/block', array(), t('More'));
  33. // If this fails we still have the default empty block user form.
  34. $this->assertNoText(t('No rules have been added'), t('The block form now allows adding rules.'));
  35. // If author role is 4 and recipient role is 5, disallow sending of messages.
  36. $edit = array(
  37. 'block_actions[0][author]' => 3,
  38. 'block_actions[0][recipient]' => 4,
  39. 'block_actions[0][action]' => 1,
  40. 'block_actions[0][enabled]' => 1,
  41. );
  42. $this->drupalPost(NULL, $edit, t('Save configuration'));
  43. // Verify that the user doesn't show up in the autocomplete.
  44. // Login the user that will write a message to the user with the blocked role.
  45. $this->drupalLogin($author_user);
  46. $this->drupalGet('messages/new');
  47. $this->assertText(t('Write new message'), t('Author user can write messages.'));
  48. $this->drupalGet('messages/autocomplete/' . $blocked_user->name);
  49. $this->assertNoText($blocked_user->name, t('User with blocked role does not appear in autocomplete results.'));
  50. // Verify that link to send private message to blocked user is not shown on their profile page.
  51. $this->drupalGet('user/' . $blocked_user->uid);
  52. $this->assertNoText(t('Send this user a private message'), t("Author user cannot see link to send blocked user a message on blocked user's profile."));
  53. // Verify that it is not possible to manually write a message to blocked user.
  54. $this->drupalGet('messages/new');
  55. $message = array(
  56. 'recipient' => $blocked_user->name,
  57. 'subject' => $this->randomName(20),
  58. 'body[value]' => $this->randomName(50),
  59. );
  60. $this->drupalPost('messages/new', $message, t('Send message'));
  61. $this->assertText(t('You are not permitted to send messages to @recipient.' , array('@recipient' => $blocked_user->name)), t('Author user message to blocked user has been blocked.'));
  62. // Verify that unblocked user can receive message from a multi-recipient message that includes blocked user.
  63. $this->drupalGet('messages/new');
  64. $message = array(
  65. 'recipient' => $admin_user->name . ', ' . $blocked_user->name,
  66. 'subject' => $this->randomName(20),
  67. 'body[value]' => $this->randomName(50),
  68. );
  69. $this->drupalPost('messages/new', $message, t('Send message'));
  70. $this->assertText(t('You are not permitted to send messages to @recipient.' , array('@recipient' => $blocked_user->name)), t('Author user message to blocked user has been blocked.'));
  71. $this->assertText(t('A message has been sent to @recipient.', array('@recipient' => $admin_user->name)), t('Author user message sent to admin user.'));
  72. }
  73. function testBlockAndUnblock() {
  74. // Create needed users.
  75. $user1 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
  76. $user2 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
  77. $user3 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
  78. // Set up a simple conversation.
  79. $return = privatemsg_new_thread(array($user2, $user3), $subject = $this->randomName(20), $this->randomString(50), array('author' => $user1));
  80. privatemsg_reply($return['message']->thread_id, $this->randomString(50), array('author' => $user2));
  81. privatemsg_reply($return['message']->thread_id, $this->randomString(50), array('author' => $user3));
  82. $this->drupalLogin($user1);
  83. // Visit profile page of user 2 and verify that there is a link to write a
  84. // message.
  85. $this->drupalGet('user/' . $user2->uid);
  86. $this->assertText(t('Send this user a private message'));
  87. $this->drupalGet('messages');
  88. $this->clickLink($subject);
  89. // Block user2.
  90. $this->clickLink(t('Block'));
  91. $this->drupalPost(NULL, array(), t('Block @user', array('@user' => $user2->name)));
  92. $this->assertText(t('@user has been blocked from sending you any further messages.', array('@user' => $user2->name)), t('Confirmation message displayed'));
  93. // Block user3.
  94. $this->clickLink(t('Block'));
  95. $this->drupalPost(NULL, array(), t('Block @user', array('@user' => $user3->name)));
  96. $this->assertText(t('@user has been blocked from sending you any further messages.', array('@user' => $user3->name)), t('Confirmation message displayed'));
  97. // Visit blocked users list.
  98. $this->drupalGet('messages/blocked');
  99. $this->assertText($user2->name, t('User 2 displayed as blocked'));
  100. $this->assertText($user3->name, t('User 3 displayed as blocked'));
  101. // Unblock user2.
  102. // Blocked users are sorted alphabetically, check which one is the first
  103. // one in the list and set index based on that.
  104. $rows = $this->xpath('//tbody/tr');
  105. $index = (int) !($user2->name == (string)$rows[0]->td[0]->a);
  106. $this->clickLink(t('unblock'), $index);
  107. $this->drupalPost(NULL, array(), t('Unblock @user', array('@user' => $user2->name)));
  108. $this->assertText(t('@user is now allowed to send you new messages.', array('@user' => $user2->name)), t('Confirmation message displayed'));
  109. $this->assertText($user3->name, t('User 3 still displayed as blocked'));
  110. // Unblock user3.
  111. $this->clickLink(t('unblock'));
  112. $this->drupalPost(NULL, array(), t('Unblock @user', array('@user' => $user3->name)));
  113. $this->assertText(t('@user is now allowed to send you new messages.', array('@user' => $user3->name)), t('Confirmation message displayed'));
  114. $this->drupalGet('messages/blocked');
  115. $this->assertNoText($user2->name, t('User 2 not displayed as blocked anymore'));
  116. $this->assertNoText($user3->name, t('User 3 not displayed as blocked anymore'));
  117. // Block users again.
  118. $edit = array('name' => $user2->name . ', ' . $user3->name);
  119. $this->drupalPost(NULL, $edit, t('Block user'));
  120. $this->assertText(t('@user has been blocked from sending you any further messages.', array('@user' => $user2->name)), t('Confirmation message displayed'));
  121. $this->assertText(t('@user has been blocked from sending you any further messages.', array('@user' => $user3->name)), t('Confirmation message displayed'));
  122. $this->drupalGet('messages');
  123. $this->clickLink($subject);
  124. $this->assertNoLink(t('Block'), t('No "Block user" links displayed.'));
  125. // Log in as user2 and try to send messages to user1.
  126. $this->drupalLogin($user2);
  127. // Access profile to see if there is a write message link.
  128. $this->drupalGet('user/' . $user1->uid);
  129. $this->assertNoText(t('Send this user a private message'));
  130. $edit = array(
  131. 'recipient' => $user1->name,
  132. 'subject' => $subject2 = $this->randomName(20),
  133. 'body[value]' => $this->randomName(50),
  134. );
  135. $this->drupalPost('messages/new', $edit, t('Send message'));
  136. $this->assertRaw(t('%user has chosen to block messages from you.', array('%user' => $user1->name)), t('User 1 blocks user 2 message displayed'));
  137. $this->assertText(t('You are not allowed to send this message because all recipients are blocked.'));
  138. $edit = array(
  139. 'recipient' => $user1->name . ', ' . $user3->name,
  140. 'subject' => $subject3 = $this->randomName(20),
  141. 'body[value]' => $this->randomName(50),
  142. );
  143. $this->drupalPost('messages/new', $edit, t('Send message'));
  144. $this->assertRaw(t('%user has chosen to block messages from you.', array('%user' => $user1->name)), t('User 1 blocks user 2 message displayed'));
  145. $this->assertText(t('A message has been sent to @user.', array('@user' => $user3->name)), t('Message sent to user 3'));
  146. // Try to reply to an existing thread.
  147. $this->drupalGet('messages');
  148. $this->clickLink($subject);
  149. $edit = array('body[value]' => $reply = $this->randomName(50));
  150. $this->drupalPost(NULL, $edit, t('Send message'));
  151. $this->assertText(t('@user has chosen to block messages from you.', array('@user' => $user1->name)));
  152. $this->assertText(t('A message has been sent to @user.', array('@user' => $user3->name)), t('Message sent to user 3'));
  153. // Login as user1 again and check that we didn't receive the messages.
  154. $this->drupalLogin($user1);
  155. $this->drupalGet('messages');
  156. // Check that we didn't get the new messages.
  157. $this->assertNoLink($subject2);
  158. $this->assertNoLink($subject3);
  159. // Check that we don't see the new messages.
  160. $this->clickLink($subject);
  161. $this->assertNoText($reply);
  162. }
  163. }

Classes