You are here

public function PasswordPolicyUITestCase::testPasswordSet in Password Policy 7.2

Tests setting passwords.

File

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

Class

PasswordPolicyUITestCase
Test case to verify accuracy of each available policy condition.

Code

public function testPasswordSet() {

  // Use password_policy_test module form_alter to prevent extraneous policy
  // text on user edit page. @todo figure out another way to do this.
  variable_set('password_policy_test_no_description', TRUE);

  // Set up strong policy.
  $this
    ->createStrongPolicy();
  $user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user);

  // Hold onto first password.
  $password0 = $user->pass_raw;

  // Try weak password.
  $edit = array();
  $edit['current_pass'] = $password0;
  $edit['pass[pass1]'] = 'pass';
  $edit['pass[pass2]'] = 'pass';
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertText('Password must have at least 1 digit(s).');
  $this
    ->assertText('Password must have at least 8 character(s).');

  // Try username.
  $edit = array();
  $edit['current_pass'] = $password0;
  $edit['pass[pass1]'] = $user->name;
  $edit['pass[pass2]'] = $user->name;
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertText('Password must not contain their username');

  // Change password twice.
  $password1 = $this
    ->randomName() . '9';
  $edit = array();
  $edit['current_pass'] = $password0;
  $edit['pass[pass1]'] = $password1;
  $edit['pass[pass2]'] = $password1;
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $password2 = $this
    ->randomName() . '9';
  $edit = array();
  $edit['current_pass'] = $password1;
  $edit['pass[pass1]'] = $password2;
  $edit['pass[pass2]'] = $password2;
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertText('The changes have been saved.');

  // Try to save old $password1.
  $edit = array();
  $edit['current_pass'] = $password2;
  $edit['pass[pass1]'] = $password1;
  $edit['pass[pass2]'] = $password1;
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertNoText('The changes have been saved.');
  $this
    ->assertText('Password cannot match 2 past passwords.');

  // Save a new random password.
  $password3 = $this
    ->randomName() . '9';
  $edit = array();
  $edit['current_pass'] = $password2;
  $edit['pass[pass1]'] = $password3;
  $edit['pass[pass2]'] = $password3;
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertText('The changes have been saved.');

  // Try and save $password1.
  $edit = array();
  $edit['current_pass'] = $password3;
  $edit['pass[pass1]'] = $password1;
  $edit['pass[pass2]'] = $password1;
  $this
    ->drupalPost("user/{$user->uid}/edit", $edit, t('Save'));
  $this
    ->assertText('The changes have been saved.');
}