? blocked_role_test3.patch
Index: pm_block_user/pm_block_user.test
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/pm_block_user/pm_block_user.test,v
retrieving revision 1.9
diff -u -p -r1.9 pm_block_user.test
--- pm_block_user/pm_block_user.test	25 Dec 2010 15:27:19 -0000	1.9
+++ pm_block_user/pm_block_user.test	28 Apr 2011 15:34:26 -0000
@@ -22,6 +22,73 @@ class PrivatemsgBlockUserCase extends Dr
     parent::setUp('privatemsg', 'pm_block_user');
   }
 
+  /**
+   * Test role blocking rules.
+   */
+  function testRoleBlockRecipient() {
+    // User id 3, Role id 3.
+    $author_user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
+    // User id 4, Role id 4.
+    $blocked_user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
+    // User id 5, Role id 5.
+    $admin_user = $this->drupalCreateUser(array('administer privatemsg settings'));
+
+    // Login the admin user and set up the role blocking rules.
+    $this->drupalLogin($admin_user);
+
+    $this->drupalPost('admin/settings/messages/block', array(), t('More'));
+    // If this fails we still have the default empty block user form.
+    $this->assertNoText(t('No rules have been added'), t('The block form now allows adding rules.'));
+
+    // If author role is 3 and recipent role is 4, disallow sending of messages.
+    $edit = array(
+      'block_actions[0][author]' => 3,
+      'block_actions[0][recipient]' => 4,
+      'block_actions[0][action]' => 1,
+      'block_actions[0][enabled]' => 1,
+    );
+    $this->drupalPost(NULL, $edit, t('Save configuration'));
+
+    // Verify that the user doesn't show up in the autocomplete.
+    // Login the user that will write a message to the user with the blocked role.
+    $this->drupalLogin($author_user);
+
+    $this->drupalGet('messages/new');
+    $this->assertText(t('Write new message'), t('Author user can write messages.'));
+
+    $this->drupalGet('messages/autocomplete/' . $blocked_user->name);
+    $this->assertNoText($blocked_user->name, t('User with blocked role does not appear in autocomplete results.'));
+
+    // Verify that link to send private message to blocked user is not shown on their profile page.
+    $this->drupalGet('user/' . $blocked_user->uid);
+    $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."));
+
+    // Verify that it is not possible to manually write a message to blocked user.
+    $this->drupalGet('messages/new');
+    $message = array(
+      'recipient'   => $blocked_user->name,
+      'subject'     => $this->randomName(20),
+      'body'        => $this->randomName(50),
+    );
+
+    $this->drupalPost('messages/new', $message, t('Send message'));
+    $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.'));
+
+    // Verify that unblocked user can receive message from a multi-recipient message that includes blocked user.
+    $this->drupalGet('messages/new');
+    $message = array(
+      'recipient'   => $admin_user->name . ', ' . $blocked_user->name,
+      'subject'     => $this->randomName(20),
+      'body'        => $this->randomName(50),
+    );
+
+    $this->drupalPost('messages/new', $message, t('Send message'));
+    $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.'));
+    $this->assertText(t('A message has been sent to @recipient.', array('@recipient' => $admin_user->name)), t('Author user message sent to admin user.'));
+
+  }
+
+
   function testBlockAndUnblock() {
     // Create needed users.
     $user1 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'access user profiles'));
@@ -138,4 +205,4 @@ class PrivatemsgBlockUserCase extends Dr
     $this->clickLink($subject);
     $this->assertNoText($reply);
   }
-}
\ No newline at end of file
+}
