You are here

domain_integration_login_restrict.test in Domain Integration (Drupal 7) 7

File

modules/domain_integration_login_restrict/domain_integration_login_restrict.test
View source
<?php

/**
 * Class DomainIntegrationLoginRestrictTest
 */
class DomainIntegrationLoginRestrictTest extends DomainTestCase {

  /**
   * @var array
   *   The list of domains.
   */
  protected $domains;

  /**
   * @var object
   *   User to test.
   */
  protected $user;
  public static function getInfo() {
    return array(
      'name' => 'Check login restrictions',
      'description' => 'Check that login restrictions apply.',
      'group' => 'Domain Access',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp($list = []) {
    $list[] = 'domain_integration_login_restrict';
    parent::setUp($list);
    $this->domains = $this
      ->domainCreateDomains();
    $this->user = $this
      ->drupalCreateUser();
  }

  /**
   * Tests the restrictions on login.
   */
  public function testRestrictLogin() {

    // Assign the user to domain one and try to log in.
    $user = user_save($this->user, array(
      'domain_user' => array(
        1 => 1,
      ),
    ));
    $this
      ->drupalLogin($user);

    // Assign the user to domain two and try to log in.
    $user = user_save($this->user, array(
      'domain_user' => array(
        $this->domains['two']['domain_id'] => $this->domains['two']['domain_id'],
      ),
    ));
    $this
      ->drupalLoginNoSuccessCheck($user);
    $this
      ->assertText('Sorry, you cannot log in on this domain.');
    $this
      ->drupalLogout();

    // Try to obtain a one time login on a domain where the user has no access.
    // Try using a username.
    $edit = array(
      'name' => $this->user->name,
    );
    $this
      ->drupalPost('user/password', $edit, 'E-mail new password');
    $this
      ->assertText('Sorry, you cannot log in on this domain.');

    // Try using a email.
    $edit = array(
      'name' => $this->user->mail,
    );
    $this
      ->drupalPost('user/password', $edit, 'E-mail new password');
    $this
      ->assertText('Sorry, you cannot log in on this domain.');

    // Use a one-time login link to log into the main domain when the user has
    // no access to the domain.
    $one_time_login = user_pass_reset_url($user);
    $this
      ->drupalGet($one_time_login);
    $this
      ->drupalPost(NULL, array(), t('Log in'));
    $this
      ->assertNoText('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.');

    // Make sure we've been redirected to the front page.
    $this
      ->assertText('Sorry, you cannot log in on this domain.');
    $this
      ->assertText('Welcome to TestDomainSitename');

    // For a logged-out user, expect no secondary links.
    $element = $this
      ->xpath('//ul[@id=:menu_id]', array(
      ':menu_id' => 'secondary-menu-links',
    ));
    $this
      ->assertEqual(count($element), 0, 'No secondary-menu for logged-out users.');
  }

  /**
   * Does a login without a success check.
   *
   * @param \stdClass $account
   */
  protected function drupalLoginNoSuccessCheck(stdClass $account) {
    if ($this->loggedInUser) {
      $this
        ->drupalLogout();
    }
    $edit = array(
      'name' => $account->name,
      'pass' => $account->pass_raw,
    );
    $this
      ->drupalPost('user', $edit, t('Log in'));
  }

}

Classes

Namesort descending Description
DomainIntegrationLoginRestrictTest Class DomainIntegrationLoginRestrictTest