You are here

function PasswordPolicyTestCase::testHistoryConstraint in Password Policy 6

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

File

tests/password_policy.test, line 158
Unit tests for Password policy module.

Class

PasswordPolicyTestCase
@file Unit tests for Password policy module.

Code

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

  // Log in
  $user = $this
    ->drupalCreateUser(array(
    '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/settings/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_result(db_query('SELECT pid FROM {password_policy} WHERE name="%s"', $policy_name));
  $constraints = unserialize(db_result(db_query('SELECT policy FROM {password_policy} WHERE pid=%d', $pid)));
  $this
    ->assertTrue($constraints['history'] == 2, t('Verified history constraint set.'));
  $edit = array(
    "enabled[{$pid}]" => $pid,
  );
  $this
    ->drupalPost('admin/settings/password_policy/list', $edit, t('Save changes'));
  $this
    ->assertText(t('The changes have been saved.'), t('Form submitted successfully.'));
  $enabled = db_result(db_query('SELECT enabled FROM {password_policy} WHERE pid=%d', $pid));
  $this
    ->assertTrue($enabled == 1, t('Policy enabled.'));

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

  // Change password second time
  $pass2 = "bbbbbb";
  $edit = array(
    '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):'), t("2nd password change should fail: {$pass1}"));

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

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

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