You are here

public function PasswordPolicyPasswordTabTestCase::testForcePasswordChange in Password Policy 7

Test force password change.

@todo Deduplicate code versus PasswordPolicyForcePasswordChangeTestCase::testSingleUser().

File

contrib/password_tab/password_policy_password_tab.test, line 157
Web tests for Password policy password tab module.

Class

PasswordPolicyPasswordTabTestCase
Tests of password tab.

Code

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();
}