PHP Classes

File: tests/PasswordTest.php

Recommend this page to a friend!
  Classes of Igor Zaporozhets   PHP Password Validation   tests/PasswordTest.php   Download  
File: tests/PasswordTest.php
Role: Unit test script
Content type: text/plain
Description: Test File
Class: PHP Password Validation
Generate and validate passwords based in rules
Author: By
Last change:
Date: 9 years ago
Size: 12,023 bytes
 

Contents

Class file image Download
<?php namespace Security; require_once __DIR__ . '/../Password.php'; /** * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-05-29 at 23:55:28. */ class PasswordTest extends \PHPUnit_Framework_TestCase { /** * @var Password */ protected $object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new Password; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @covers Security\Password::factory */ public function testFactory() { $obj = Password::factory(); $this->assertTrue($obj instanceof Password, 'Factory method failed.'); print get_class($obj); } /** * @covers Security\Password::password_policy * @todo Implement testPassword_policy(). */ // public function testPassword_policy() { // // Remove the following lines when you implement this test. // $this->markTestIncomplete( // 'This test has not been implemented yet.' // ); // } /** * @covers Security\Password::min_length setting */ public function test_min_length() { $res = $this->object ->min_length(1) ->password('12345678') ->numbers(array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) ->check(); $this->assertTrue($res, 'Password min length failed'); print '1'; } /** * @covers Security\Password::min_length Exception * @expectedException Exception * @expectedExceptionMessage Password may not be empty */ public function test_empty_min_length() { $this->object ->min_length(0) ->numbers(array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) ->password('12345678') ->check(); print($this->object->password()); print('Password may not be empty'); } public function test_letter_password() { $min_length = 7; $pass = $this->object->letters(array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' )) ->min_length($min_length) ->letters_count(7) ->symbols_count(0) ->capitals_count(0) ->numbers_count(0) ->generate(); print $pass; $this->assertTrue(strlen($pass) >= $min_length, 'Incorrect password'); } /** * @covers Security\Password::generate() numeric password */ public function test_number_password() { $pass = $this->object ->min_length(7) ->numbers(array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) ->letters_count(0) ->symbols_count(0) ->capitals_count(0) ->numbers_count(10) ->generate(); print $pass; $this->assertTrue(strlen($pass) >= 7, 'Incorrect password'); } /** * @covers Security\Password::generate() string password */ public function test_symbol_password() { $min_length = 5; $pass = $this->object ->min_length($min_length) ->symbols(array( "$", "-", "_", ".", "+", "!", "*", "\\", "'", "(", ")", ",", "{", "}", "|", "^", "~", "[", "]", "`", "<", ">", "#", "%", "\"", ";", "/", "?", ":", "@", "&", "=", "." )) ->letters_count(0) ->symbols_count(5) ->capitals_count(0) ->numbers_count(0) ->generate(); print $pass; $this->assertTrue(strlen($pass) >= $min_length, 'Incorrect password'); } /** * @covers Security\Password::generate() unicode string password * Used Ukrainian lower case letters unicode codes for regex '\x{0430}', '\x{0431}', '\x{0432}', '\x{0433}', '\x{0491}', '\x{0434}', '\x{0435}', '\x{0454}', '\x{0436}', '\x{0437}', '\x{0438}', '\x{0456}', '\x{0457}', '\x{0439}', '\x{043a}', '\x{043b}', '\x{043c}', '\x{043d}', '\x{043e}', '\x{043f}', '\x{0440}', '\x{0441}', '\x{0442}', '\x{0443}', '\x{0444}', '\x{0445}', '\x{0446}', '\x{0447}', '\x{0448}', '\x{0449}', '\x{044c}', '\x{044e}', '\x{044f}' */ public function test_unicode_symbol_password_generate() { $min_length = 7; $pass = $this->object->letters(array( 'а', 'б', 'в', 'г', 'ґ', 'д ', 'е', 'є', 'ж', 'з', 'и', 'і', 'ї', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ю', 'я', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' )) ->symbols(array( "$", "-", "_", ".", "+", "!", "*", "\\", "'", "(", ")", ",", "{", "}", "|", "^", "~", "[", "]", "`", "<", ">", "#", "%", "\"", ";", "/", "?", ":", "@", "&", "=", "." )) ->numbers(array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) ->min_length($min_length) ->letters_count(12) ->symbols_count(0) ->capitals_count(0) ->numbers_count(0) ->generate(); $check = $this->object->check(); $this->assertTrue(strlen($pass) >= $min_length && $check, 'Incorrect password'); print $pass; } /** * password in national chars is: * "Slava Ukrayini" - transliterated * "Glory to Ukraine" - is translation into EN * When build non-latin regex classes use PCRE unicode syntax: \x{FFFF} */ public function test_unicode_symbol_password_check() { $min_length = 7; $password='Слава Україні'; $check = $this->object->letters(array( 'а', 'б', 'в', 'г', 'ґ', 'д ', 'е', 'є', 'ж', 'з', 'и', 'і', 'ї', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ю', 'я', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' )) ->min_length($min_length) ->letters_count(12) ->symbols_count(0) ->capitals_count(3) ->numbers_count(0) ->password($password) ->password_policy(array( 'contains_lowercase' => array( 'regex' => '[a-z\x{0430}-\x{045f}]+'),) ) ->check(); $this->assertTrue($check, 'Password should contain lowercase letters and valid regex providing this password policy'); print $password; } public function test_complex_password_policy() { $min_length = 8; $gen = $this->object->letters(array( 'а', 'б', 'в', 'г', 'ґ', 'д ', 'е', 'є', 'ж', 'з', 'и', 'і', 'ї', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ю', 'я', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' )) ->symbols(array( "$", "-", "_", ".", "+", "!", "*", "\\", "'", "(", ")", ",", "{", "}", "|", "^", "~", "[", "]", "`", "<", ">", "#", "%", "\"", ";", "/", "?", ":", "@", "&", "=", ".")) ->numbers(array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) ->min_length($min_length) ->letters_count(12) ->symbols_count(3) ->capitals_count(3) ->numbers_count(3) ->password_policy(array( 'contains_lowercase' => array( 'regex' => '[a-z\x{0430}-\x{045f}]+'), 'contains_uppercase' =>array( 'regex' => '[A-Z\x{0400}-\x{042f}]'), 'contains_numbers' => array( 'regex' => '\d+'), 'balanced_digits' => array( 'regex' => '^\d+?([^\s])*\d+?$|^\D+([^\s])*\D+$')) ) ->generate(); $check = $this->object->check(); $this->assertTrue($check, 'Password should contain lowercase, capital letters, digits and don\'t contain digits at the start or end position only.'); } public function test_black_list_password() { $BlackList = array('bigdigpotato', '123456789', '987654321','АбіїЇієЄ'); $min_length = 8; $check = $this->object->letters(array( 'а', 'б', 'в', 'г', 'ґ', 'д ', 'е', 'є', 'ж', 'з', 'и', 'і', 'ї', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ю', 'я', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' )) ->symbols(array( "$", "-", "_", ".", "+", "!", "*", "\\", "'", "(", ")", ",", "{", "}", "|", "^", "~", "[", "]", "`", "<", ">", "#", "%", "\"", ";", "/", "?", ":", "@", "&", "=", ".")) ->numbers(array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) ->min_length($min_length) ->letters_count(12) ->symbols_count(3) ->capitals_count(3) ->numbers_count(3) ->password_policy(array( 'contains_lowercase' => array( 'regex' => '[a-z\x{0430}-\x{045f}]+'), 'contains_uppercase' =>array( 'regex' => '[A-Z\x{0400}-\x{042f}]'), 'contains_numbers' => array( 'regex' => '\d+'), 'balanced_digits' => array( 'regex' => '^\d+?([^\s])*\d+?$|^\D+([^\s])*\D+$')) ) ->black_list($BlackList) ->password('bigdigpotato'); $result1 = $check->check(); $result2 = $check->password('123456789')->check(); $result3 = $check->password('АбіїЇієЄ')->check(); $result4 = $check->generate(); print $result4; $this->assertTrue(($result1 && $result2 && $result3) === false, 'Blacklist check failed.'); } }