You are here

user_relationship_privatemsg.test in User Relationships 7

User relationship privatemsg tests.

File

user_relationship_privatemsg/user_relationship_privatemsg.test
View source
<?php

/**
 * @file
 * User relationship privatemsg tests.
 */

/**
 * Tests cases to send messages to related users.
 */
class UserRelationshipsPrivatemsgRecipientTestCase extends DrupalWebTestCase {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('User Relationships Privatemsg recipients'),
      'description' => t('Check relationship recipient type for privatemsg'),
      'group' => t('User Relationships'),
      'dependencies' => array(
        'privatemsg',
      ),
    );
  }

  /**
   * Enable required modules.
   */
  public function setUp() {
    return parent::setUp('privatemsg', 'user_relationships', 'user_relationships_ui', 'user_relationship_privatemsg');
  }

  /**
   * Tests sending a message to related users.
   */
  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.');
  }

  /**
   * Test a relationship with bath api processing.
   */
  public function testManyRelations() {

    // 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',
    ));
    $this
      ->drupalLogin($admin);

    // Create 25 users (more than are allowed to be process directly) and relate
    // them to the admin.
    $users = array();
    for ($i = 0; $i < 25; $i++) {
      $users[$i] = $this
        ->drupalCreateUser(array(
        'read privatemsg',
        'write privatemsg',
        $have_permission,
        $request_permission,
        'view relationship recipients',
        'write privatemsg to relationships',
      ));

      // Become related to that user.
      $this
        ->drupalGet('relationship/' . $users[$i]->uid . '/request/1');
      $this
        ->drupalPost(NULL, array(), t('Send'));
    }
    variable_set('privatemsg_recipient_small_threshold', 20);

    // 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'],
    )));

    // Test if all recipients see the message.
    foreach ($users as $user) {
      $this
        ->drupalLogin($user);
      $this
        ->drupalGet('messages');
      $this
        ->clickLink($message['subject']);
      $this
        ->assertText($message['body[value]']);
    }
  }

}

Classes

Namesort descending Description
UserRelationshipsPrivatemsgRecipientTestCase Tests cases to send messages to related users.