You are here

function PrivatemsgBlockUserCase::testRoleBlockRecipient in Privatemsg 7

Same name and namespace in other branches
  1. 6.2 pm_block_user/pm_block_user.test \PrivatemsgBlockUserCase::testRoleBlockRecipient()
  2. 6 pm_block_user/pm_block_user.test \PrivatemsgBlockUserCase::testRoleBlockRecipient()
  3. 7.2 pm_block_user/pm_block_user.test \PrivatemsgBlockUserCase::testRoleBlockRecipient()

Test role blocking rules.

File

pm_block_user/pm_block_user.test, line 26
Test file for pm_block_user.module

Class

PrivatemsgBlockUserCase
@file Test file for pm_block_user.module

Code

function testRoleBlockRecipient() {

  // User id 3, Role id 4.
  $author_user = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
    'access user profiles',
  ));

  // User id 4, Role id 5.
  $blocked_user = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
    'access user profiles',
  ));

  // User id 5, Role id 6.
  $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/config/messaging/privatemsg/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 4 and recipent role is 5, 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[value]' => $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[value]' => $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.'));
}