You are here

password_policy_password_tab.test in Password Policy 7

Web tests for Password policy password tab module.

File

contrib/password_tab/password_policy_password_tab.test
View source
<?php

/**
 * @file
 * Web tests for Password policy password tab module.
 */

/**
 * Tests of password tab.
 */
class PasswordPolicyPasswordTabTestCase extends DrupalWebTestCase {

  /**
   * Get info about the test case.
   */
  public static function getInfo() {
    return array(
      'name' => 'Password Change Tab',
      'description' => 'Test Password Change Tab.',
      'group' => 'Password Policy',
    );
  }

  /**
   * Set up the test.
   */
  public function setUp() {
    parent::setUp('password_policy', 'password_policy_password_tab');
    $this->user = $this
      ->drupalCreateUser();
  }

  /**
   * Test the password tab.
   */
  public function testPasswordTab() {
    $this
      ->drupalLogin($this->user);

    // Check tab is working.
    $this
      ->drupalGet('user/' . $this->user->uid . '/password');

    // Attempt password change.
    $edit = array(
      'current_pass' => $this->user->pass_raw,
      'pass[pass1]' => $this->user->pass_raw . $this->user->pass_raw,
      'pass[pass2]' => $this->user->pass_raw . $this->user->pass_raw,
    );
    $this
      ->drupalPost('user/' . $this->user->uid . '/password', $edit, 'Save');
    $this
      ->assertText(t('Password has been changed.'));
  }

  /**
   * Test custom redirect path with %uid variable.
   *
   * @todo Deduplicate code versus testPasswordTab().
   */
  public function testCustomRedirectPath() {

    // Log in.
    $user = $this
      ->drupalCreateUser(array(
      'administer password policies',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($user);

    // Set custom redirect path.
    $redirect_path = 'user/%uid/edit';
    $edit = array(
      'password_policy_password_tab_redirect' => $redirect_path,
    );
    $this
      ->drupalPost('admin/config/people/password_policy/password_tab', $edit, t('Save configuration'));
    $this
      ->assertText(t('The configuration options have been saved.'));

    // Check tab is working.
    $this
      ->drupalGet('user/' . $user->uid . '/password');

    // Attempt password change.
    $edit = array(
      'current_pass' => $user->pass_raw,
      'pass[pass1]' => $user->pass_raw . $user->pass_raw,
      'pass[pass2]' => $user->pass_raw . $user->pass_raw,
    );
    $this
      ->drupalPost('user/' . $user->uid . '/password', $edit, 'Save');
    $this
      ->assertText(t('Password has been changed.'));

    // Check that user was correctly redirected.
    $this
      ->assertUrl('user/' . $user->uid . '/edit');
  }

  /**
   * Test history constraint when using password tab and custom redirect path.
   *
   * This test prevents regression of bug where history constraint did not work
   * under these conditions.
   *
   * @todo Deduplicate code versus
   *   PasswordPolicyTestCase::testHistoryConstraint().
   */
  public function testHistorySave() {
    module_load_include('inc', 'password_policy', 'constraints/constraint_history');

    // Log in.
    $user = $this
      ->drupalCreateUser(array(
      'administer password policies',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($user);

    // Create a policy.
    $policy_name = $this
      ->randomName();
    $edit = array(
      'name' => $policy_name,
      'constraint_history' => t('2'),
      'roles[2]' => '2',
    );
    $this
      ->drupalPost('admin/config/people/password_policy/add', $edit, t('Create'));
    $this
      ->assertText('Policy ' . $policy_name . ' has been created.', 'Policy ' . $policy_name . ' has been created');

    // Enable newly created policy.
    $pid = db_query('SELECT pid FROM {password_policy} WHERE name = :name', array(
      ':name' => $policy_name,
    ))
      ->fetchField();
    $constraints = unserialize(db_query('SELECT constraints FROM {password_policy} WHERE pid = :pid', array(
      ':pid' => $pid,
    ))
      ->fetchField());
    $this
      ->assertTrue($constraints['history'] == 2, t('Verified history constraint set.'));
    $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.'), t('Form submitted successfully.'));
    $enabled = db_query('SELECT enabled FROM {password_policy} WHERE pid = :pid', array(
      ':pid' => $pid,
    ))
      ->fetchField();
    $this
      ->assertTrue($enabled == 1, t('Policy enabled.'));

    // Set custom redirect path.
    $redirect_path = 'node';
    $edit = array(
      'password_policy_password_tab_redirect' => $redirect_path,
    );
    $this
      ->drupalPost('admin/config/people/password_policy/password_tab', $edit, t('Save configuration'));
    $this
      ->assertText(t('The configuration options have been saved.'));

    // Change password.
    $pass1 = 'aaaaaa';
    $edit = array(
      'current_pass' => $user->pass_raw,
      'pass[pass1]' => $pass1,
      'pass[pass2]' => $pass1,
    );
    $this
      ->drupalPost("user/{$user->uid}/password", $edit, t('Save'));
    $this
      ->assertText(t('Password has been changed.'), t('1st password change: !pass1', array(
      '!pass1' => $pass1,
    )));

    // Change password second time.
    $edit = array(
      'current_pass' => $pass1,
      'pass[pass1]' => $pass1,
      'pass[pass2]' => $pass1,
    );
    $this
      ->drupalPost("user/{$user->uid}/password", $edit, t('Save'));
    $this
      ->assertText(t('Your password has not met the following requirement(s):'), t('2nd password change should fail: !pass1', array(
      '!pass1' => $pass1,
    )));
  }

  /**
   * Test force password change.
   *
   * @todo Deduplicate code versus
   *   PasswordPolicyForcePasswordChangeTestCase::testSingleUser().
   */
  public function testForcePasswordChange() {
    $admin = $this
      ->drupalCreateUser(array(
      'force password change',
      'administer users',
    ));
    $user = $this
      ->drupalCreateUser();
    $this
      ->drupalLogin($admin);
    $edit = array(
      'force_password_change' => TRUE,
    );
    $this
      ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
    $this
      ->assertRaw(t('!user will be required to change their password the next time they log in.', array(
      '!user' => $user->name,
    )), t('User flagged for password change.'));
    $force_change = db_query('SELECT force_change FROM {password_policy_force_change} WHERE uid = :uid', array(
      ':uid' => $user->uid,
    ))
      ->fetchField();
    $this
      ->assertTrue($force_change == 1, t('Force change flag set to %d for %s.', array(
      '%d' => $force_change,
      '%s' => $user->name,
    )));

    // Verify user is forced to change password.
    $this
      ->drupalLogin($user);
    $this
      ->assertFieldByName('current_pass', NULL, t('User redirected correctly.'));
    $this
      ->assertRaw(t('Your password has expired. You must change your password to proceed on the site.'), t('User presented with error instructing them to change their password.'));

    // Attempt to change password.
    $edit = array(
      'current_pass' => $user->pass_raw,
      'pass[pass1]' => 'random_string',
      'pass[pass2]' => 'random_string',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->assertRaw(t('Password has been changed.'), t('Password change successful.'));

    // Verify user not prompted to change password a 2nd time.
    $this
      ->drupalGet('node');
    $this
      ->assertNoFieldByName('current_pass', NULL, t('User not forced to change password a 2nd time.'));
    $this
      ->drupalLogout();
  }

}

Classes

Namesort descending Description
PasswordPolicyPasswordTabTestCase Tests of password tab.