diff --git a/password_policy.test b/password_policy.test index ea5d606..243153d 100644 --- a/password_policy.test +++ b/password_policy.test @@ -420,6 +420,106 @@ class PasswordPolicyConditionsTestCase extends PasswordPolicyBaseTestCase { } +/** + * Tests of restriction on password length. + */ +class PasswordPolicyPasswordLengthRestrictionTestCase extends PasswordPolicyBaseTestCase { + protected $admin; + + /** + * Implements DrupalWebTestCase::getInfo(). + */ + public static function getInfo() { + return array( + 'name' => 'Password length restriction', + 'description' => 'Test that overlong passwords are disallowed.', + 'group' => 'Password Policy', + ); + } + + /** + * Set up the test. + */ + public function setUp() { + parent::setUp('password_policy'); + $this->createAdmin(); + } + + /** + * Creates a usable admin (UID=1) user. + * + * SimpleTest creates an admin user, but it cannot log in since it has no + * password set. So, we give it a password. We also give it a valid email + * address so its user edit form can be submitted. + */ + protected function createAdmin() { + global $user; + $pass = user_password(); + $edit = array( + 'pass' => $pass, + 'mail' => 'foo@example.com', + ); + user_save($user, $edit); + $user->pass_raw = $pass; + $this->admin = $user; + } + + /** + * Sets a password policy that applies to the authenticated user role. + * + * This is just a minimal policy to apply to the admin (UID=1) user, which is + * being used for this test. + */ + public function testOverlongPasswordSubmission() { + $config = array( + 'alpha_count' => array( + 'alpha_count' => 1, + ), + 'role' => array( + 'roles' => array( + DRUPAL_AUTHENTICATED_RID => 1, + ), + ), + ); + $this->createPolicy($config); + $this->drupalLogin($this->admin); + + $this->submitOverlongPassword(); + $this->submitNotOverlongPassword(); + } + + /** + * Submits a password that is overlong. + */ + protected function submitOverlongPassword() { + $pass = str_repeat('a', 513); + $edit = array( + 'current_pass' => $this->admin->pass_raw, + 'pass[pass1]' => $pass, + 'pass[pass2]' => $pass, + ); + $this->drupalPost('user/' . $this->admin->uid . '/edit', $edit, t('Save')); + $this->assertText(t('Password exceeds maximum length.'), 'Overlong password causes form error.'); + $this->assertNoText(t('The changes have been saved.'), 'Overlong password is not saved.'); + } + + /** + * Submits a password that is not overlong. + */ + protected function submitNotOverlongPassword() { + $pass = str_repeat('a', 512); + $edit = array( + 'current_pass' => $this->admin->pass_raw, + 'pass[pass1]' => $pass, + 'pass[pass2]' => $pass, + ); + $this->drupalPost('user/' . $this->admin->uid . '/edit', $edit, t('Save')); + $this->assertNoText(t('Password exceeds maximum length.'), 'Not-overlong password does not cause form error.'); + $this->assertText(t('The changes have been saved.'), 'Not-overlong password is saved.'); + } + +} + /** * Test case to verify accuracy of each available policy condition. */