You are here

function PrivatemsgAPITestCase::testGetLink in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.test \PrivatemsgAPITestCase::testGetLink()
  2. 7 privatemsg.test \PrivatemsgAPITestCase::testGetLink()

Test various use cases for privatemsg_get_link().

File

./privatemsg.test, line 1435
Test file for privatemsg.module

Class

PrivatemsgAPITestCase
Privatemsg API tests

Code

function testGetLink() {
  $author = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
    'write privatemsg to all roles',
  ));
  $author2 = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
  ));
  $recipient1 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
  ));
  $recipient2 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
  ));
  $recipient3 = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'allow disabling privatemsg',
  ));
  $recipient4 = $this
    ->drupalCreateUser();

  // Create role with the same name as recipient 2.
  $role = $this
    ->drupalCreateRole(array(
    'read privatemsg',
  ), $recipient2->name);
  $this
    ->drupalLogin($author);
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient1,
  )));
  $this
    ->assertFieldByName('recipient', $recipient1->name);

  // Because of there is a role with the same name like recipient 2,
  // add the [user] for recipient 2.
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient1,
    $recipient2,
  )));
  $this
    ->assertFieldByName('recipient', $recipient1->name . ', ' . $recipient2->name . ' [user]');
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient1,
    $recipient2,
  ), $author));
  $this
    ->assertFieldByName('recipient', $recipient1->name . ', ' . $recipient2->name . ' [user]');
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient1,
    $recipient2,
  ), $author, $subject = 'Str/"ang\\w3//'));
  $this
    ->assertFieldByName('recipient', $recipient1->name . ', ' . $recipient2->name . ' [user]');
  $this
    ->assertFieldByName('subject', $subject);

  // Disable privatemsg for recipient 3.
  privatemsg_set_setting('user', $recipient3->uid, 'disabled', 1);
  $this
    ->assertFalse(privatemsg_get_link(array(
    $recipient3,
  ), $author));
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient1,
    $recipient3,
  ), $author));
  $this
    ->assertFieldByName('recipient', $recipient1->name);

  // Disable links to self, verify that a link is only returned when the
  // author is not the only recipient.
  variable_set('privatemsg_display_link_self', FALSE);
  $this
    ->assertFalse(privatemsg_get_link(array(
    $author,
  ), $author));
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient1,
    $author,
  ), $author));
  $this
    ->assertFieldByName('recipient', $recipient1->name);

  // Verify that link is not shown when recipient doesn't have read
  // permission.
  $this
    ->assertFalse(privatemsg_get_link(array(
    $recipient4,
  ), $author));
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient1,
    $recipient4,
  ), $author));
  $this
    ->assertFieldByName('recipient', $recipient1->name);

  // Verify that link is not shown when author does not have write permission.
  $this
    ->drupalLogin($recipient1);
  $this
    ->assertFalse(privatemsg_get_link(array(
    $author,
  ), $recipient1));

  // For user that has no permission to send message to role,
  // the [user] suffix will not appear although there is a role with the same name as user.
  $this
    ->drupalLogin($author2);
  $this
    ->drupalGet(privatemsg_get_link(array(
    $recipient2,
  )));
  $this
    ->assertFieldByName('recipient', $recipient2->name);
}