You are here

pm_block_user.test in Privatemsg 7

Test file for pm_block_user.module

File

pm_block_user/pm_block_user.test
View source
<?php

/**
 * @file
 * Test file for pm_block_user.module
 */
class PrivatemsgBlockUserCase extends PrivatemsgBaseTestCase {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('User blocking functionality.'),
      'description' => t('Test blocking and unblocking of users'),
      'group' => t('Privatemsg'),
    );
  }
  function setUp() {
    parent::setUp('privatemsg', 'pm_block_user');
  }

  /**
   * Test role blocking rules.
   */
  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.'));
  }
  function testBlockAndUnblock() {

    // Create needed users.
    $user1 = $this
      ->drupalCreateUser(array(
      'write privatemsg',
      'read privatemsg',
      'access user profiles',
    ));
    $user2 = $this
      ->drupalCreateUser(array(
      'write privatemsg',
      'read privatemsg',
      'access user profiles',
    ));
    $user3 = $this
      ->drupalCreateUser(array(
      'write privatemsg',
      'read privatemsg',
      'access user profiles',
    ));

    // Set up a simple conversation.
    $return = privatemsg_new_thread(array(
      $user2,
      $user3,
    ), $subject = $this
      ->randomName(20), $this
      ->randomString(50), array(
      'author' => $user1,
    ));
    privatemsg_reply($return['message']->thread_id, $this
      ->randomString(50), array(
      'author' => $user2,
    ));
    privatemsg_reply($return['message']->thread_id, $this
      ->randomString(50), array(
      'author' => $user3,
    ));
    $this
      ->drupalLogin($user1);

    // Visit profile page of user 2 and verify that there is a link to write a
    // message.
    $this
      ->drupalGet('user/' . $user2->uid);
    $this
      ->assertText(t('Send this user a private message'));
    $this
      ->drupalGet('messages');
    $this
      ->clickLink($subject);

    // Block user2.
    $this
      ->clickLink(t('Block'));
    $this
      ->drupalPost(NULL, array(), t('Block @user', array(
      '@user' => $user2->name,
    )));
    $this
      ->assertText(t('@user has been blocked from sending you any further messages.', array(
      '@user' => $user2->name,
    )), t('Confirmation message displayed'));

    // Block user3.
    $this
      ->clickLink(t('Block'));
    $this
      ->drupalPost(NULL, array(), t('Block @user', array(
      '@user' => $user3->name,
    )));
    $this
      ->assertText(t('@user has been blocked from sending you any further messages.', array(
      '@user' => $user3->name,
    )), t('Confirmation message displayed'));

    // Visit blocked users list.
    $this
      ->drupalGet('messages/blocked');
    $this
      ->assertText($user2->name, t('User 2 displayed as blocked'));
    $this
      ->assertText($user3->name, t('User 3 displayed as blocked'));

    // Unblock user2.
    // Blocked users are sorted alphabetically, check which one is the first
    // one in the list and set index based on that.
    $rows = $this
      ->xpath('//tbody/tr');
    $index = (int) (!($user2->name == (string) $rows[0]->td[0]->a));
    $this
      ->clickLink(t('unblock'), $index);
    $this
      ->drupalPost(NULL, array(), t('Unblock @user', array(
      '@user' => $user2->name,
    )));
    $this
      ->assertText(t('@user is now allowed to send you new messages.', array(
      '@user' => $user2->name,
    )), t('Confirmation message displayed'));
    $this
      ->assertText($user3->name, t('User 3 still displayed as blocked'));

    // Unblock user3.
    $this
      ->clickLink(t('unblock'));
    $this
      ->drupalPost(NULL, array(), t('Unblock @user', array(
      '@user' => $user3->name,
    )));
    $this
      ->assertText(t('@user is now allowed to send you new messages.', array(
      '@user' => $user3->name,
    )), t('Confirmation message displayed'));
    $this
      ->drupalGet('messages/blocked');
    $this
      ->assertNoText($user2->name, t('User 2 not displayed as blocked anymore'));
    $this
      ->assertNoText($user3->name, t('User 3 not displayed as blocked anymore'));

    // Block users again.
    $edit = array(
      'name' => $user2->name . ', ' . $user3->name,
    );
    $this
      ->drupalPost(NULL, $edit, t('Block user'));
    $this
      ->assertText(t('@user has been blocked from sending you any further messages.', array(
      '@user' => $user2->name,
    )), t('Confirmation message displayed'));
    $this
      ->assertText(t('@user has been blocked from sending you any further messages.', array(
      '@user' => $user3->name,
    )), t('Confirmation message displayed'));
    $this
      ->drupalGet('messages');
    $this
      ->clickLink($subject);
    $this
      ->assertNoLink(t('Block'), t('No "Block user" links displayed.'));

    // Log in as user2 and try to send messages to user1.
    $this
      ->drupalLogin($user2);

    // Access profile to see if there is a write message link.
    $this
      ->drupalGet('user/' . $user1->uid);
    $this
      ->assertNoText(t('Send this user a private message'));
    $edit = array(
      'recipient' => $user1->name,
      'subject' => $subject2 = $this
        ->randomName(20),
      'body[value]' => $this
        ->randomName(50),
    );
    $this
      ->drupalPost('messages/new', $edit, t('Send message'));
    $this
      ->assertRaw(t('%user has chosen to block messages from you.', array(
      '%user' => $user1->name,
    )), t('User 1 blocks user 2 message displayed'));
    $this
      ->assertText(t('You are not allowed to send this message because all recipients are blocked.'));
    $edit = array(
      'recipient' => $user1->name . ', ' . $user3->name,
      'subject' => $subject3 = $this
        ->randomName(20),
      'body[value]' => $this
        ->randomName(50),
    );
    $this
      ->drupalPost('messages/new', $edit, t('Send message'));
    $this
      ->assertRaw(t('%user has chosen to block messages from you.', array(
      '%user' => $user1->name,
    )), t('User 1 blocks user 2 message displayed'));
    $this
      ->assertText(t('A message has been sent to @user.', array(
      '@user' => $user3->name,
    )), t('Message sent to user 3'));

    // Try to reply to an existing thread.
    $this
      ->drupalGet('messages');
    $this
      ->clickLink($subject);
    $edit = array(
      'body[value]' => $reply = $this
        ->randomName(50),
    );
    $this
      ->drupalPost(NULL, $edit, t('Send message'));
    $this
      ->assertText(t('@user has chosen to block messages from you.', array(
      '@user' => $user1->name,
    )));
    $this
      ->assertText(t('A message has been sent to @user.', array(
      '@user' => $user3->name,
    )), t('Message sent to user 3'));

    // Login as user1 again and check that we didn't recieve the messages.
    $this
      ->drupalLogin($user1);
    $this
      ->drupalGet('messages');

    // Check that we didn't get the new messages.
    $this
      ->assertNoLink($subject2);
    $this
      ->assertNoLink($subject3);

    // Check that we don't see the new messages.
    $this
      ->clickLink($subject);
    $this
      ->assertNoText($reply);
  }

}

Classes

Namesort descending Description
PrivatemsgBlockUserCase @file Test file for pm_block_user.module