You are here

public function PasswordPolicyTestCase::testDelayConstraint in Password Policy 7

Test delay constraint.

File

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

Class

PasswordPolicyTestCase
Tests of basic Password Policy constraints.

Code

public function testDelayConstraint() {
  module_load_include('inc', 'password_policy', 'constraints/constraint_delay');

  // 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_delay' => t('1'),
    '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['delay'] == 1, 'Verified delay 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.');
  $two_hours = 60 * 60 * 2;
  _password_policy_advance_test_clock($two_hours);

  // 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]' => $pass2,
    'pass[pass2]' => $pass2,
  );
  $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,
  )));

  // 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');
}