You are here

password_policy_authmap.test in Password Policy 7

Functional tests for excluding authentication modules from policies.

File

tests/password_policy_authmap.test
View source
<?php

/**
 * @file
 * Functional tests for excluding authentication modules from policies.
 */

/**
 * Tests of basic Password Policy constraints.
 */
class PasswordPolicyAuthmapTestCase extends DrupalWebTestCase {
  protected $policyMaker;
  protected $userNoAuthenticationModule;
  protected $userAuthenticationModuleOne;
  protected $userAuthenticationModuleTwo;

  /**
   * Get info about the test case.
   */
  public static function getInfo() {
    return array(
      'name' => 'Authentication modules exclusion',
      'description' => 'Test excluding authentication modules from policies.',
      'group' => 'Password Policy',
    );
  }

  /**
   * Set up the test.
   */
  public function setUp() {
    parent::setUp('password_policy');

    // Create user for setting test policy.
    $this
      ->createPolicyMaker();

    // Create user authenticated by no authentication modules.
    $account = $this
      ->drupalCreateUser();
    $this->userNoAuthenticationModule = $account;

    // Create user authenticated by authentication module "auth_module_one".
    $account = $this
      ->drupalCreateUser();
    $authmaps = array(
      'authname_auth_module_one' => $account->name,
    );
    user_set_authmaps($account, $authmaps);
    $this->userAuthenticationModuleOne = $account;

    // Create user authenticated by authentication module "auth_module_two".
    $account = $this
      ->drupalCreateUser();
    $authmaps = array(
      'authname_auth_module_two' => $account->name,
    );
    user_set_authmaps($account, $authmaps);
    $this->userAuthenticationModuleTwo = $account;

    // Set policy for testing.
    $this
      ->setTestPolicy();
  }

  /**
   * Creates policy maker.
   */
  protected function createPolicyMaker() {
    $this->policyMaker = $this
      ->drupalCreateUser(array(
      'administer users',
      'administer permissions',
      'administer password policies',
      'unblock expired accounts',
    ));
  }

  /**
   * Tests excluding authentication modules.
   */
  public function testExcludingAuthenticationModules() {
    $account = $this->userNoAuthenticationModule;
    $this
      ->drupalLogin($account);
    $pass = 'foo';
    $edit = array(
      'current_pass' => $account->pass_raw,
      'pass[pass1]' => $pass,
      'pass[pass2]' => $pass,
    );
    $this
      ->drupalPost('user/' . $account->uid . '/edit', $edit, t('Save'));
    $this
      ->assertText(t('Password must be at least 4 characters in length.'), 'Password not accepted for user not authenticated by authentication module.');
    $this
      ->drupalLogout();
    $account = $this->userAuthenticationModuleOne;
    $this
      ->drupalLogin($account);
    $pass = 'foo';
    $edit = array(
      'current_pass' => $account->pass_raw,
      'pass[pass1]' => $pass,
      'pass[pass2]' => $pass,
    );
    $this
      ->drupalPost('user/' . $account->uid . '/edit', $edit, t('Save'));
    $this
      ->assertText(t('The changes have been saved.'), 'Password accept for user whose authentication module is excluded from policy.');
    $this
      ->drupalLogout();
    $account = $this->userAuthenticationModuleTwo;
    $this
      ->drupalLogin($account);
    $pass = 'foo';
    $edit = array(
      'current_pass' => $account->pass_raw,
      'pass[pass1]' => $pass,
      'pass[pass2]' => $pass,
    );
    $this
      ->drupalPost('user/' . $account->uid . '/edit', $edit, t('Save'));
    $this
      ->assertText(t('Password must be at least 4 characters in length.'), 'Password not accepted for user authentication whose module is not excluded from policy.');
    $this
      ->drupalLogout();
  }

  /**
   * Sets test policy.
   *
   * Has Policy Maker user create and enable a test policy.
   */
  protected function setTestPolicy() {
    $policy_maker = $this->policyMaker;
    $this
      ->drupalLogin($policy_maker);
    $name = $this
      ->createTestPolicy();
    $this
      ->enablePolicy($name);
    $this
      ->drupalLogout();
  }

  /**
   * Creates test policy.
   */
  protected function createTestPolicy() {
    $policy_name = $this
      ->randomName();
    $rid = DRUPAL_AUTHENTICATED_RID;
    $edit = array(
      'name' => $policy_name,
      "roles[{$rid}]" => $rid,
      'excluded_authentication_modules[auth_module_one]' => 'auth_module_one',
      'constraint_length' => 4,
    );
    $this
      ->drupalPost('admin/config/people/password_policy/add', $edit, t('Create'));
    $created_text = "Policy {$policy_name} has been created.";
    $this
      ->assertText($created_text, $created_text);
    return $policy_name;
  }

  /**
   * Enables policy.
   *
   * @param string $policy_name
   *   Name of policy to be enabled.
   */
  protected function enablePolicy($policy_name) {
    $pid = db_query('SELECT pid FROM {password_policy} WHERE name = :name', array(
      ':name' => $policy_name,
    ))
      ->fetchField();
    $edit = array(
      "policies[{$pid}][enabled]" => $pid,
    );
    $this
      ->drupalPost('admin/config/people/password_policy/list', $edit, t('Save changes'));
    $this
      ->assertText(t('The changes have been saved.'), 'Form submitted successfully.');
    $this
      ->drupalGet('admin/config/people/password_policy');
    $enabled = db_query('SELECT enabled FROM {password_policy} WHERE pid = :pid', array(
      ':pid' => $pid,
    ))
      ->fetchField();
    $this
      ->assertTrue($enabled == 1, 'Policy enabled.');
  }

}

Classes

Namesort descending Description
PasswordPolicyAuthmapTestCase Tests of basic Password Policy constraints.