You are here

function UserRelationshipUserSettings::testAutoApprovalSettings in User Relationships 7

Test the various user settings.

File

user_relationships_ui/user_relationships_ui.test, line 88
User Relationships UI tests

Class

UserRelationshipUserSettings
Tests for user settings.

Code

function testAutoApprovalSettings() {
  $permissions = array(
    'can have ' . $this->rtypes['oneway']->machine_name . ' relationships',
    'can request ' . $this->rtypes['oneway']->machine_name . ' relationships',
    'can have ' . $this->rtypes['twoway']->machine_name . ' relationships',
    'can request ' . $this->rtypes['twoway']->machine_name . ' relationships',
    'view own twoway relationships',
    'view own oneway relationships',
  );

  // User without maintain permission.
  $u1 = $this
    ->drupalCreateUser($permissions);

  // User without permission for an approval relationship.
  $permissions[] = 'maintain twoway relationships';
  $permissions[] = 'maintain oneway relationships';
  $u2 = $this
    ->drupalCreateUser($permissions);

  // Two users with all required permissions.
  $permissions[] = 'can have ' . $this->rtypes['approval_oneway']->machine_name . ' relationships';
  $permissions[] = 'can request ' . $this->rtypes['approval_oneway']->machine_name . ' relationships';
  $permissions[] = 'can have ' . $this->rtypes['approval']->machine_name . ' relationships';
  $permissions[] = 'can request ' . $this->rtypes['approval']->machine_name . ' relationships';
  $permissions[] = 'maintain approval relationships';
  $permissions[] = 'maintain approval_oneway relationships';
  $permissions[] = 'view own approval relationships';
  $permissions[] = 'view own approval_oneway relationships';
  $u3 = $this
    ->drupalCreateUser($permissions);
  $u4 = $this
    ->drupalCreateUser($permissions);

  // First user does not have maintain permission, should not see any
  // Relationships options.
  $this
    ->drupalLogin($u1);
  $this
    ->drupalGet('user/' . $u1->uid . '/edit');
  $this
    ->assertNoText(t('Automatically approve relationship requests from other users'));
  $this
    ->assertNoText(t('Receive e-mail notification of relationship activity'));

  // Make sure that there is no empty relationship fieldset.
  // Because the relationships tab is always there, use unique text to ensure
  // that no other text like this is present.
  // @todo: Find a better way to deal with this.
  $this
    ->assertUniqueText(T('Relationship'));

  // Second user is not allowed to have relationships of types which require
  // approval, no options should be displayed. He is however allowed to
  // disable e-mail notifications.
  $this
    ->drupalLogin($u2);
  $this
    ->drupalGet('user/' . $u2->uid . '/edit');
  $this
    ->assertNoText(t('Automatically approve relationship requests from other users'));
  $this
    ->assertText(t('Receive e-mail notification of relationship activity'));

  // Disable e-mail notifications of this user.
  $edit = array(
    'user_relationship_mailer_send_mail' => FALSE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // The third user is allowed to see the settings, but only for the types
  // which require approval
  $this
    ->drupalLogin($u3);
  $this
    ->drupalGet('user/' . $u3->uid . '/edit');
  $this
    ->assertText(t('Automatically approve relationship requests from other users'));
  $this
    ->assertText(t('Receive e-mail notification of relationship activity'));
  $this
    ->assertFieldByName('user_relationships_ui_auto_approve[' . $this->rtypes['approval']->rtid . ']');
  $this
    ->assertFieldByName('user_relationships_ui_auto_approve[' . $this->rtypes['approval_oneway']->rtid . ']');
  $this
    ->assertNoFieldByName('user_relationships_ui_auto_approve[' . $this->rtypes['oneway']->rtid . ']');
  $this
    ->assertNoFieldByName('user_relationships_ui_auto_approve[' . $this->rtypes['twoway']->rtid . ']');

  // Enable auto approval for the approval_oneway type.
  $edit = array(
    'user_relationships_ui_auto_approve[' . $this->rtypes['approval_oneway']->rtid . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // Verify that the defaults are set correctly.
  $this
    ->assertFieldChecked('edit-user-relationships-ui-auto-approve-' . $this->rtypes['approval_oneway']->rtid);
  $this
    ->assertNoFieldChecked('edit-user-relationships-ui-auto-approve-' . $this->rtypes['approval']->rtid);

  // Finally, actually verify that the settings work.
  $this
    ->drupalLogin($u4);

  // Request approval relationship, should not approve automatically.
  $this
    ->drupalPost('relationship/' . $u3->uid . '/request/' . $this->rtypes['approval']->rtid, array(), t('Send'));
  $this
    ->assertText(t('Your approval request has been sent to @user.', array(
    '@user' => $u3->name,
  )));

  // Request approval_oneway relationships, should approve automatically.
  $this
    ->drupalPost('relationship/' . $u3->uid . '/request/' . $this->rtypes['approval_oneway']->rtid, array(), t('Send'));
  $this
    ->assertText(t("You are @user's newest approval-oneway.", array(
    '@user' => $u3->name,
  )));

  // Load relationships between these two users.
  $relationships = user_relationships_load(array(
    'between' => array(
      $u3->uid,
      $u4->uid,
    ),
  ));
  $this
    ->assertEqual(count($relationships), 2);
  foreach ($relationships as $relationship) {
    if ($relationship->rtid == $this->rtypes['approval_oneway']->rtid) {
      $this
        ->assertTrue((bool) $relationship->approved);
    }
    elseif ($relationship->rtid == $this->rtypes['approval']->rtid) {
      $this
        ->assertFalse((bool) $relationship->approved);
    }
    else {
      $this
        ->fail('Unexpected relationship type @type', array(
        '@type' => $relationship->rtid,
      ));
    }
  }

  // Verify that two mails have been sent.
  $this
    ->assertEqual(count($this
    ->drupalGetMails()), 2);

  // Request a oneway relationship with user 2, this should not generate a
  // mail notification.
  $this
    ->drupalPost('relationship/' . $u2->uid . '/request/' . $this->rtypes['oneway']->rtid, array(), t('Send'));
  $this
    ->assertText(t("You are @user's newest oneway.", array(
    '@user' => $u2->name,
  )));

  // Verify that no additional mails have been sent.
  $this
    ->assertEqual(count($this
    ->drupalGetMails()), 2);
}