bringing the validator to 93% coverage,

adding default regexes for Validator::phone(), Validator::postal() and Validator::ssn() if no default regex is supplied, but also no country

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6927 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
DarkAngelBGE 2008-05-18 15:47:35 +00:00
parent 45ed8ffeff
commit 032990a364
2 changed files with 138 additions and 20 deletions

View file

@ -488,7 +488,7 @@ class Validation extends Object {
return ($check === $comparedTo);
}
/**
* Check that value has a valid file extenstion.
* Check that value has a valid file extension.
*
* @param mixed $check Value to check
* @param array $extensions file extenstions to allow
@ -511,14 +511,22 @@ class Validation extends Object {
return false;
}
/**
* Check that value is a file type.
* Check that value is a file name
*
* @param mixed $check Value to check
* @access public
* @todo Implement
*/
function file($check) {
// if (is_array($check)) {
// foreach ($check as $value) {
// if (!Validation::file($value)) {
// return false;
// }
// }
// return true;
// }
//
// return preg_match('/[\w| |_]+\.[\w]+/', $check);
}
/**
* Validation of an IPv4 address.
@ -617,7 +625,7 @@ class Validation extends Object {
* @return boolean Success
* @access public
*/
function phone($check, $regex= null, $country = 'all') {
function phone($check, $regex = null, $country = 'all') {
$_this =& Validation::getInstance();
if (is_array($check)) {
$_this->_extract($check);
@ -630,6 +638,7 @@ class Validation extends Object {
if (is_null($_this->regex)) {
switch ($_this->country) {
case 'us':
default:
$_this->regex = '/^1?[-. ]?\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/';
break;
}
@ -657,9 +666,6 @@ class Validation extends Object {
if (is_null($_this->regex)) {
switch ($_this->country) {
case 'us':
$_this->regex = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i';
break;
case 'uk':
$_this->regex = '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i';
break;
@ -670,6 +676,10 @@ class Validation extends Object {
case 'de':
$_this->regex = '/^[0-9]{5}$/i';
break;
case 'us':
default:
$_this->regex = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i';
break;
}
}
return $_this->_check();
@ -721,15 +731,16 @@ class Validation extends Object {
if (is_null($_this->regex)) {
switch ($_this->country) {
case 'us':
$_this->regex = '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i';
break;
case 'dk':
$_this->regex = '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i';
break;
case 'nl':
$_this->regex = '/\\A\\b[0-9]{9}\\b\\z/i';
break;
case 'us':
default:
$_this->regex = '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i';
break;
}
}
return $_this->_check();

View file

@ -26,7 +26,21 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
uses('validation');
App::import('Core', 'Validation');
class CustomValidator {
/**
* Makes sure that a given $email address is valid and unique
*
* @param string $email
* @return boolean
* @access public
*/
function customValidate($check) {
return preg_match('/^[0-9]{3}$/', $check);
}
}
/**
* Short description for class.
*
@ -1368,14 +1382,107 @@ class ValidationTestCase extends UnitTestCase {
$this->assertFalse(Validation::money('100.1€', 'right'));
$this->assertFalse(Validation::money('100.1111€', 'right'));
}
/*
function TestFile() {
$this->assertTrue(Validation::file(WWW_ROOT . 'img' . DS . 'cake.icon.gif'));
$this->assertTrue(Validation::file(WWW_ROOT. 'favicon.ico'));
$this->assertTrue(Validation::file(WWW_ROOT. 'index.php'));
$this->assertTrue(Validation::file(WWW_ROOT. 'css' . DS . 'cake.generic.css'));
$this->assertTrue(Validation::file(TEST_CAKE_CORE_INCLUDE_PATH. 'VERSION.txt'));
function testNumeric() {
$this->assertFalse(Validation::numeric('teststring'));
$this->assertFalse(Validation::numeric('1.1test'));
$this->assertFalse(Validation::numeric('2test'));
$this->assertTrue(Validation::numeric('2'));
$this->assertTrue(Validation::numeric(2));
$this->assertTrue(Validation::numeric(2.2));
$this->assertTrue(Validation::numeric('2.2'));
}
*/
function testPhone() {
$this->assertFalse(Validation::phone('teststring'));
$this->assertFalse(Validation::phone('1-(33)-(333)-(4444)'));
$this->assertFalse(Validation::phone('1-(33)-3333-4444'));
$this->assertFalse(Validation::phone('1-(33)-33-4444'));
$this->assertFalse(Validation::phone('1-(33)-3-44444'));
$this->assertFalse(Validation::phone('1-(33)-3-444'));
$this->assertFalse(Validation::phone('1-(33)-3-44'));
$this->assertTrue(Validation::phone('(999) 999-9999'));
$this->assertTrue(Validation::phone('1-(333)-333-4444'));
$this->assertTrue(Validation::phone('1.(333)-333-4444'));
$this->assertTrue(Validation::phone('1.(333).333-4444'));
$this->assertTrue(Validation::phone('1.(333).333.4444'));
$this->assertTrue(Validation::phone('1-333-333-4444'));
}
function testPostal() {
$this->assertFalse(Validation::postal('111', null, 'de'));
$this->assertFalse(Validation::postal('1111', null, 'de'));
$this->assertTrue(Validation::postal('13089', null, 'de'));
$this->assertFalse(Validation::postal('111', null, 'it'));
$this->assertFalse(Validation::postal('1111', null, 'it'));
$this->assertTrue(Validation::postal('13089', null, 'it'));
$this->assertFalse(Validation::postal('111', null, 'uk'));
$this->assertFalse(Validation::postal('1111', null, 'uk'));
$this->assertFalse(Validation::postal('AZA 0AB', null, 'uk'));
$this->assertFalse(Validation::postal('X0A 0ABC', null, 'uk'));
$this->assertTrue(Validation::postal('X0A 0AB', null, 'uk'));
$this->assertTrue(Validation::postal('AZ0A 0AA', null, 'uk'));
$this->assertTrue(Validation::postal('A89 2DD', null, 'uk'));
$this->assertFalse(Validation::postal('111', null, 'ca'));
$this->assertFalse(Validation::postal('1111', null, 'ca'));
$this->assertFalse(Validation::postal('D2A 0A0', null, 'ca'));
$this->assertFalse(Validation::postal('BAA 0ABC', null, 'ca'));
$this->assertFalse(Validation::postal('B2A AABC', null, 'ca'));
$this->assertFalse(Validation::postal('B2A 2AB', null, 'ca'));
$this->assertTrue(Validation::postal('X0A 0A2', null, 'ca'));
$this->assertTrue(Validation::postal('G4V 4C3', null, 'ca'));
$this->assertFalse(Validation::postal('111', null, 'us'));
$this->assertFalse(Validation::postal('1111', null, 'us'));
$this->assertFalse(Validation::postal('130896', null, 'us'));
$this->assertFalse(Validation::postal('13089-33333', null, 'us'));
$this->assertFalse(Validation::postal('13089-333', null, 'us'));
$this->assertFalse(Validation::postal('13A89-4333', null, 'us'));
$this->assertTrue(Validation::postal('13089-3333', null, 'us'));
$this->assertFalse(Validation::postal('111'));
$this->assertFalse(Validation::postal('1111'));
$this->assertFalse(Validation::postal('130896'));
$this->assertFalse(Validation::postal('13089-33333'));
$this->assertFalse(Validation::postal('13089-333'));
$this->assertFalse(Validation::postal('13A89-4333'));
$this->assertTrue(Validation::postal('13089-3333'));
}
function testSsn() {
$this->assertFalse(Validation::ssn('111-333', null, 'dk'));
$this->assertFalse(Validation::ssn('111111-333', null, 'dk'));
$this->assertTrue(Validation::ssn('111111-3334', null, 'dk'));
$this->assertFalse(Validation::ssn('1118333', null, 'nl'));
$this->assertFalse(Validation::ssn('1234567890', null, 'nl'));
$this->assertFalse(Validation::ssn('12345A789', null, 'nl'));
$this->assertTrue(Validation::ssn('123456789', null, 'nl'));
$this->assertFalse(Validation::ssn('11-33-4333', null, 'us'));
$this->assertFalse(Validation::ssn('113-3-4333', null, 'us'));
$this->assertFalse(Validation::ssn('111-33-333', null, 'us'));
$this->assertTrue(Validation::ssn('111-33-4333', null, 'us'));
}
function testUserDefined() {
$validator = new CustomValidator;
$this->assertFalse(Validation::userDefined('33', $validator, 'customValidate'));
$this->assertFalse(Validation::userDefined('3333', $validator, 'customValidate'));
$this->assertTrue(Validation::userDefined('333', $validator, 'customValidate'));
}
// function testFile() {
// $this->assertTrue(Validation::file(WWW_ROOT . 'img' . DS . 'cake.icon.gif'));
// $this->assertTrue(Validation::file(WWW_ROOT. 'favicon.ico'));
// $this->assertTrue(Validation::file(WWW_ROOT. 'index.php'));
// $this->assertTrue(Validation::file(WWW_ROOT. 'css' . DS . 'cake.generic.css'));
// $this->assertTrue(Validation::file(TEST_CAKE_CORE_INCLUDE_PATH. 'VERSION.txt'));
// }
}
?>