View source
<?php
class PasswordPolicyTest extends DrupalTestCase {
function get_info() {
return array(
'name' => 'Password policy test',
'desc' => t('Tests the core functionality of the password policy module.'),
'group' => 'Password policy tests',
);
}
function testConstraints() {
_password_policy_load_constraint_definitions();
$this
->assertTrue(class_exists('Letter_Constraint'), 'Letter constraint does not exist.');
$this
->assertTrue(class_exists('And_Constraint'), 'And constraint does not exist.');
$this
->assertTrue(class_exists('Character_Constraint'), 'Character constraint does not exist.');
$this
->assertTrue(class_exists('Length_Constraint'), 'Length constraint does not exist.');
$this
->assertTrue(class_exists('Lowercase_Constraint'), 'Lowercase constraint does not exist.');
$this
->assertTrue(class_exists('Uppercase_Constraint'), 'Uppercase constraint does not exist.');
$this
->assertTrue(class_exists('Digit_Constraint'), 'Digit constraint does not exist.');
$this
->assertTrue(class_exists('Punctuation_Constraint'), 'Punctuation constraint does not exist.');
$this
->assertTrue(class_exists('Constraint'), 'Constraint constraint does not exist.');
$c = new Constraint(3);
$this
->assertTrue($c
->validate(''), 'Constraint test failed.');
$this
->assertTrue($c
->getName() != '', 'Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Constraint has no min value.');
$c = new Character_Constraint(4);
$this
->assertTrue(!$c
->validate('abc'), 'Character_Constraint test #1 failed.');
$this
->assertTrue($c
->validate('abcd'), 'Character_Constraint test #2 failed.');
$this
->assertTrue($c
->validate('abcd3'), 'Character_Constraint test #3 failed.');
$this
->assertTrue($c
->getName(), 'Character_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Character_Constraint has no min value.');
$c = new Letter_Constraint(3);
$this
->assertTrue($c
->validate('abc'), 'Letter_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate('ab3'), 'Letter_Constraint test #2 failed.');
$this
->assertTrue($c
->getName(), 'Letter_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Letter_Constraint has no min value.');
$c = new Uppercase_Constraint(3);
$this
->assertTrue($c
->validate('ABC'), 'Uppercase_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate('AbC'), 'Uppercase_Constraint test #2 failed.');
$this
->assertTrue($c
->getName(), 'Uppercase_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Uppercase_Constraint has no min value.');
$c = new Lowercase_Constraint(3);
$this
->assertTrue($c
->validate('abc'), 'Lowercase_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate('aBc'), 'Lowercase_Constraint test #2 failed.');
$this
->assertTrue($c
->getName(), 'Lowercase_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Lowercase_Constraint has no min value.');
$c = new Digit_Constraint(3);
$this
->assertTrue($c
->validate('123'), 'Digit_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate('1b3'), 'Digit_Constraint test #2 failed.');
$this
->assertTrue($c
->getName(), 'Digit_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Digit_Constraint has no min value.');
$c = new Punctuation_Constraint(4);
$this
->assertTrue($c
->validate('*(@"'), 'Punctuation_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate("'b(@"), 'Punctuation_Constraint test #2 failed.');
$this
->assertTrue($c
->getName(), 'Punctuation_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Punctuation_Constraint has no min value.');
$c = new Length_Constraint(4);
$this
->assertTrue($c
->validate('....'), 'Length_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate('...'), 'Length_Constraint test #2 failed.');
$this
->assertTrue($c
->getName(), 'Length_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Length_Constraint has no min value.');
$c = new Letter_Digit_Constraint(4);
$this
->assertTrue($c
->validate('12ab'), 'Letter_Digit_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate('1b3.'), 'Letter_Digit_Constraint test #2 failed.');
$this
->assertTrue($c
->getName(), 'Letter_Digit_Constraint has no name.');
$this
->assertTrue($c
->getMinimumConstraintValue() != '', 'Letter_Digit_Constraint has no min value.');
$c = new And_Constraint();
$c
->addConstraint(new Length_Constraint(4));
$c
->addConstraint(new Digit_Constraint(2));
$c
->addConstraint(new Punctuation_Constraint(2));
$this
->assertTrue(!$c
->validate('aaaa'), 'And_Constraint test #1 failed.');
$this
->assertTrue(!$c
->validate('12ab'), 'And_Constraint test #2 failed.');
$this
->assertTrue(!$c
->validate('..ab'), 'And_Constraint test #3 failed.');
$this
->assertTrue($c
->validate('12..'), 'And_Constraint test #4 failed.');
}
}