You are here

function PrivatemsgBlockUserCase::testBlockAndUnblock in Privatemsg 7.2

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

File

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

Class

PrivatemsgBlockUserCase
@file Test file for pm_block_user.module

Code

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 receive 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);
}