You are here

public function PasswordPolicyTestCase::testHistoryConstraint in Password Policy 7

Same name and namespace in other branches
  1. 6 tests/password_policy.test \PasswordPolicyTestCase::testHistoryConstraint()

Test history constraint.

File

tests/password_policy.test, line 249
Functional tests for Password policy module.

Class

PasswordPolicyTestCase
Tests of basic Password Policy constraints.

Code

public function testHistoryConstraint() {
  module_load_include('inc', 'password_policy', 'constraints/constraint_history');

  // Log in.
  $user = $this
    ->drupalCreateUser(array(
    'administer password policies',
  ));
  $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, '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.'), 'Form submitted successfully.');
  $enabled = db_query('SELECT enabled FROM {password_policy} WHERE pid = :pid', array(
    ':pid' => $pid,
  ))
    ->fetchField();
  $this
    ->assertTrue($enabled == 1, 'Policy enabled.');

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

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

  // Try changing password with the first one.
  $edit = array(
    'current_pass' => $pass1,
    'pass[pass1]' => $pass2,
    'pass[pass2]' => $pass2,
  );
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'), format_string('3rd password change should pass: !pass2', array(
    '!pass2' => $pass2,
  )));

  // Change password again.
  $pass3 = 'cccccc';
  $edit = array(
    'current_pass' => $pass2,
    'pass[pass1]' => $pass3,
    'pass[pass2]' => $pass3,
  );
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'), format_string('4th password change should pass: !pass3', array(
    '!pass3' => $pass3,
  )));

  // Delete test policy.
  $this
    ->drupalPost('admin/config/people/password_policy/' . $pid . '/delete', array(), t('Delete'));
  $this
    ->assertText('Password policy ' . $policy_name . ' was deleted.', 'Default password policy ' . $policy_name . 'was deleted');
}