You are here

public function PasswordPolicyForcePasswordChangeTestCase::testRoleChange in Password Policy 7

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

Tests role-based password change.

File

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

Class

PasswordPolicyForcePasswordChangeTestCase
Tests of forcing password changes.

Code

public function testRoleChange() {
  $admin = $this
    ->drupalCreateUser(array(
    'administer users',
    'force password change',
  ));
  $user1 = $this
    ->drupalCreateUser();
  $user2 = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($admin);
  $edit = array(
    'password_policy_force_change_roles[2]' => 2,
  );
  $this
    ->drupalPost('admin/config/people/password_policy/password_change', $edit, t('Save changes'));
  $this
    ->assertText(t('Users in the following roles will be required to immediately change their password: authenticated user'), 'Authenticated users role selected.');
  $this
    ->assertTrue($admin->uid != 1, format_string('Admin uid not 1: !admin_uid', array(
    'admin_uid' => $admin->uid,
  )));
  $this
    ->assertRaw(t('Your password has expired. You must change your password to proceed on the site.'), 'Admin (not uid 1) correctly forced to change password.');

  // Test db flags for individual users.
  $entry_1 = db_query('SELECT uid FROM {password_policy_force_change} WHERE uid = :uid', array(
    ':uid' => $user1->uid,
  ))
    ->fetchField();
  $this
    ->assertTrue($entry_1 == $user1->uid, format_string('Entry created in password_policy_force_change for user !uid.', array(
    '!uid' => $user1->uid,
  )));
  $flag_1 = db_query('SELECT force_change FROM {password_policy_force_change} WHERE uid = :uid', array(
    ':uid' => $user1->uid,
  ))
    ->fetchField();
  $this
    ->assertTrue($flag_1 == 1, format_string("User !uid flagged: !flag.", array(
    '!uid' => $user1->uid,
    '!flag' => $flag_1,
  )));
  $this
    ->drupalLogout();

  // Test individual users.
  $this
    ->drupalLogin($user1);
  $this
    ->drupalGet('node');
  $this
    ->assertRaw(t('Your password has expired. You must change your password to proceed on the site.'), 'First test user forced to change password.');
  $this
    ->drupalLogout();

  // Test 2nd user.
  $this
    ->drupalLogin($user2);
  $this
    ->assertRaw(t('Your password has expired. You must change your password to proceed on the site.'), 'Second test user forced to change password.');
  $this
    ->drupalLogout();
}