You are here

function PrivatemsgRolesTestCase::testSendMessageToRole in Privatemsg 6.2

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

File

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

Class

PrivatemsgRolesTestCase
Test cases for the privatemsg_roles module.

Code

function testSendMessageToRole() {
  $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);

  // Update permissions.
  $this
    ->checkPermissions(array(), TRUE);
  $admin = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
    'view roles recipients',
    'write privatemsg to role ' . $user1->roles[3],
  ));
  $this
    ->drupalLogin($admin);

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

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

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

  // 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'], t('Thread starter body displayed.'));

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

  // Reply to the message, only admin should see this.
  $reply1 = array(
    'body' => $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'], t('Reply1 is displayed'));

  // Reply to the message, all recipients should see this.
  $reply2 = array(
    'body' => $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' => $user1->roles[3],
  )));
  $this
    ->assertText($edit['body'], t('Thread starter body is displayed'));
  $this
    ->assertNoText($reply1['body'], t('Reply1 is not displayed'));
  $this
    ->assertText($reply2['body'], t('Reply2 is displayed'));
}