You are here

public function UserRelationshipsPrivatemsgRecipientTestCase::testSendToRelationship in User Relationships 7

Tests sending a message to related users.

File

user_relationship_privatemsg/user_relationship_privatemsg.test, line 34
User relationship privatemsg tests.

Class

UserRelationshipsPrivatemsgRecipientTestCase
Tests cases to send messages to related users.

Code

public function testSendToRelationship() {

  // Create relationship.
  $relationship = array(
    'name' => $this
      ->randomName(),
    'machine_name' => strtolower($this
      ->randomName()),
    'plural_name' => $this
      ->randomName(),
    'requires_approval' => FALSE,
    'expires_val' => 0,
  );
  user_relationships_type_save((object) $relationship);

  // Flush permission cache.
  drupal_static_reset('user_relationships_types_load');
  $this
    ->checkPermissions(array(), TRUE);
  $have_permission = 'can have ' . $relationship['machine_name'] . ' relationships';
  $request_permission = 'can request ' . $relationship['machine_name'] . ' relationships';
  $maintain_permission = 'maintain ' . $relationship['machine_name'] . ' relationships';
  $admin = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
    $have_permission,
    $request_permission,
    'view relationship recipients',
    'administer user relationships',
    $maintain_permission,
    'write privatemsg to relationships',
    'access user profiles',
  ));
  $userA = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
    $have_permission,
    $request_permission,
    'view relationship recipients',
    'write privatemsg to relationships',
  ));
  $userB = $this
    ->drupalCreateUser(array(
    'read privatemsg',
    'write privatemsg',
    $have_permission,
    $request_permission,
    'view relationship recipients',
    $maintain_permission,
  ));
  $this
    ->drupalLogin($admin);

  // Only allow messages between related users.
  $edit = array(
    'user_relationships_restrict_privatemsg' => 'relationships',
  );
  $this
    ->drupalPost('admin/config/people/relationships/settings', $edit, t('Save configuration'));

  // Become related to user A and B.
  $this
    ->drupalGet('relationship/' . $userA->uid . '/request/1');
  $this
    ->drupalPost(NULL, array(), t('Send'));
  $this
    ->drupalGet('relationship/' . $userB->uid . '/request/1');
  $this
    ->drupalPost(NULL, array(), t('Send'));

  // Send a message to related users.
  $this
    ->drupalGet('messages/new');
  $this
    ->assertText(t('Enter the name of a user relationship to write a message to all related users. Example: @relationship.', array(
    '@relationship' => $relationship['plural_name'],
  )), t('Description is displayed.'));
  $message = array(
    'recipient' => $relationship['plural_name'],
    'subject' => $this
      ->randomName(),
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $message, t('Send message'));
  $this
    ->assertText(t('A message has been sent to your @relationship.', array(
    '@relationship' => $relationship['plural_name'],
  )));

  // Check if userA received the message and is able to respond.
  $this
    ->drupalLogin($userA);
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($message['subject']);
  $this
    ->assertText($message['body[value]']);

  // Send a reply
  $reply = array(
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $reply, t('Send message'));

  // Try to send a message to non-related userB.
  $this
    ->drupalGet('messages/new');
  $message_to_b = array(
    'recipient' => $userB->name,
    'subject' => $this
      ->randomName(),
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $message_to_b, t('Send message'));
  $this
    ->assertText(t('@name does not have an established relationship with you.', array(
    '@name' => $userB->name,
  )), 'Sending to non-related user should fail.');

  // Login as userB and make sure the original message is displayed but not the message nor the username of userA.
  $this
    ->drupalLogin($userB);
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($message['subject']);
  $this
    ->assertText($message['body[value]']);
  $this
    ->assertNoText($userA->name);

  // Log in as admin and check that the response of user A is visible.
  $this
    ->drupalLogin($admin);
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($message['subject']);
  $this
    ->assertText($reply['body[value]']);
  $reply2 = array(
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $reply2, t('Send message'));

  // Log in as user B again and check that the reply but not user a is visible.
  $this
    ->drupalLogin($userB);
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($message['subject']);
  $this
    ->assertText($reply2['body[value]']);

  // @todo: does not yet work.

  //$this->assertNoText($userA->name);

  // Test reverse relationship.
  $this
    ->drupalLogin($userA);
  $this
    ->drupalGet('messages/new');
  $this
    ->assertText(t('Enter the name of a user relationship to write a message to all related users. Example: @relationship.', array(
    '@relationship' => $relationship['plural_name'],
  )), t('Description is displayed.'));
  $message = array(
    'recipient' => $relationship['plural_name'],
    'subject' => $this
      ->randomName(),
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $message, t('Send message'));
  $this
    ->assertText(t('A message has been sent to your @relationship.', array(
    '@relationship' => $relationship['plural_name'],
  )));

  // Log in as admin and check that the response of user A is visible.
  $this
    ->drupalLogin($admin);
  $this
    ->drupalGet('messages');
  $this
    ->clickLink($message['subject']);
  $this
    ->assertText($message['body[value]']);

  // Allow messages to unrelated users.
  $edit = array(
    'user_relationships_restrict_privatemsg' => 'all',
  );
  $this
    ->drupalPost('admin/config/people/relationships/settings', $edit, t('Save configuration'));

  // Switch to user A again.
  $this
    ->drupalLogin($userA);

  // Try to send a message to non-related userB.
  $this
    ->drupalGet('messages/new');
  $message_to_b = array(
    'recipient' => $userB->name,
    'subject' => $this
      ->randomName(),
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $message_to_b, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @name.', array(
    '@name' => $userB->name,
  )), 'Sending to non-related user should succeed.');

  // Block messages to unrelated users.
  $this
    ->drupalLogin($admin);
  $edit = array(
    'user_relationships_restrict_privatemsg' => 'relationships',
  );
  $this
    ->drupalPost('admin/config/people/relationships/settings', $edit, t('Save configuration'));

  // Change the relationship to make approval required.
  $relationship = user_relationships_type_load(array(
    'name' => $relationship['name'],
  ));
  $relationship->requires_approval = TRUE;
  user_relationships_type_save($relationship);

  // Reset static caches.
  user_relationships_types_load(TRUE);

  // Switch to user A again.
  $this
    ->drupalLogin($userA);

  // Send a relationship request from userA to userB.,
  $this
    ->drupalGet('relationship/' . $userB->uid . '/request/' . $userA->uid);
  $this
    ->drupalPost(NULL, array(), t('Send'));

  // Try to send a message to not-yet-related userB.
  $this
    ->drupalGet('messages/new');
  $message_to_b = array(
    'recipient' => $userB->name,
    'subject' => $this
      ->randomName(),
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $message_to_b, t('Send message'));
  $this
    ->assertText(t('@name does not have an established relationship with you.', array(
    '@name' => $userB->name,
  )), 'Sending to non-approved user should fail.');

  // Switch to user B again to approve the request.
  $this
    ->drupalLogin($userB);
  $this
    ->drupalGet('user/' . $userB->uid . '/relationships/received');
  $this
    ->clickLink(t('Approve'));
  $this
    ->drupalPost(NULL, array(), t('Yes'));

  // Now user A should be able to send a message.
  $this
    ->drupalLogin($userA);

  // Try to send a message to userB.
  $this
    ->drupalGet('messages/new');
  $message_to_b = array(
    'recipient' => $userB->name,
    'subject' => $this
      ->randomName(),
    'body[value]' => $this
      ->randomName(50),
  );
  $this
    ->drupalPost(NULL, $message_to_b, t('Send message'));
  $this
    ->assertText(t('A message has been sent to @name.', array(
    '@name' => $userB->name,
  )), 'Sending to approved related user should succeed.');
}