You are here

function PrivatemsgRolesTestCase::testSendMessageToRole in Privatemsg 7

Same name and namespace in other branches
  1. 6.2 privatemsg_roles/privatemsg_roles.test \PrivatemsgRolesTestCase::testSendMessageToRole()
  2. 7.2 privatemsg_roles/privatemsg_roles.test \PrivatemsgRolesTestCase::testSendMessageToRole()

File

privatemsg_roles/privatemsg_roles.test, line 149
This file contains tests for the privatemsg roles module

Class

PrivatemsgRolesTestCase
Test cases for the privatemsg_roles module.

Code

function testSendMessageToRole() {
  $admin = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
    'write privatemsg to roles',
  ));
  $user1 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
  ));
  $user2 = $this
    ->drupalCreateUser(array(
    'view roles recipients',
  ));

  // Add role of user 1 to user 2;
  $edit = array(
    'roles' => $user2->roles + $user1->roles,
  );
  user_save($user2, $edit);
  $this
    ->drupalLogin($admin);

  // Verify autocomplete feature.
  $role_name = user_role_load($user1->roles[4])->name;
  $json = $this
    ->drupalGet('messages/autocomplete/' . drupal_substr($role_name, 0, 2));
  $autocomplete = (array) json_decode($json);
  $this
    ->assertEqual($autocomplete[$role_name . ', '], $role_name);

  // Access the form through a url that pre-fills the recipient field.
  $this
    ->drupalGet('messages/new/role_4');

  // Send a message to the role of user 1 and 2.
  $edit = array(
    'subject' => $this
      ->randomName(10),
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $edit, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @role (role).', array(
    '@role' => user_role_load($user1->roles[4])->name,
  )));

  // Log in as user1 and check that the message is listed, is marked as new
  // and can be marked as read.
  $this
    ->drupalLogin($user1);
  $this
    ->drupalGet('messages');
  $this
    ->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  $this
    ->clickLink($edit['subject']);
  $this
    ->assertText($edit['body[value]'], t('Thread starter body displayed.'));

  // Make sure that the user does not see the role.
  $this
    ->assertNoText(t('@role (role)', array(
    '@role' => $role_name,
  )));

  // Reply to the message, only admin should see this.
  $reply1 = array(
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $reply1, t('Send message'));
  $this
    ->drupalGet('messages');
  $this
    ->assertNoRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as read'));

  // Login as admin again and check if he sees the response.
  $this
    ->drupalLogin($admin);
  $this
    ->drupalGet('messages');
  $this
    ->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  $this
    ->clickLink($edit['subject']);
  $this
    ->assertText($reply1['body[value]'], t('Reply1 is displayed'));

  // Reply to the message, all recipients should see this.
  $reply2 = array(
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $reply2, t('Send message'));

  // Log in as user2 and check that he only sees the messages from admin.
  $this
    ->drupalLogin($user2);
  $this
    ->drupalGet('messages');
  $this
    ->assertRaw($edit['subject'] . '</a> <span class="marker">new</span>', t('Message is displayed as new'));
  $this
    ->clickLink($edit['subject']);
  $this
    ->assertText(t('@role (role)', array(
    '@role' => $role_name,
  )));
  $this
    ->assertText($edit['body[value]'], t('Thread starter body is displayed'));
  $this
    ->assertNoText($reply1['body[value]'], t('Reply1 is not displayed'));
  $this
    ->assertText($reply2['body[value]'], t('Reply2 is displayed'));
}