2012-04-23 11:02:26 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-05-11 01:31:44 +00:00
|
|
|
* CakeValidationRuleTest file
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/view/1196/Testing>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2012-04-23 11:02:26 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2012-04-23 11:02:26 +00:00
|
|
|
* @package Cake.Test.Case.Model.Validator
|
|
|
|
* @since CakePHP(tm) v 2.2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2012-04-23 11:02:26 +00:00
|
|
|
*/
|
|
|
|
|
2012-05-11 01:31:44 +00:00
|
|
|
App::uses('CakeValidationRule', 'Model/Validator');
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-11 01:31:44 +00:00
|
|
|
* CakeValidationRuleTest
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Model.Validator
|
|
|
|
*/
|
2012-05-11 01:31:44 +00:00
|
|
|
class CakeValidationRuleTest extends CakeTestCase {
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
/**
|
2012-04-30 00:26:24 +00:00
|
|
|
* Auxiliary method to test custom validators
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-04-30 00:26:24 +00:00
|
|
|
public function myTestRule() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auxiliary method to test custom validators
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-04-30 00:26:24 +00:00
|
|
|
public function myTestRule2() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auxiliary method to test custom validators
|
|
|
|
*
|
|
|
|
* @return string
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-04-30 00:26:24 +00:00
|
|
|
public function myTestRule3() {
|
|
|
|
return 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test isValid method
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIsValid() {
|
2015-05-17 20:27:16 +00:00
|
|
|
$def = array('rule' => 'notBlank', 'message' => 'Can not be empty');
|
2012-04-30 00:26:24 +00:00
|
|
|
$data = array(
|
|
|
|
'fieldName' => ''
|
|
|
|
);
|
|
|
|
$methods = array();
|
|
|
|
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
2012-04-30 00:26:24 +00:00
|
|
|
$Rule->process('fieldName', $data, $methods);
|
|
|
|
$this->assertFalse($Rule->isValid());
|
|
|
|
|
|
|
|
$data = array('fieldName' => 'not empty');
|
|
|
|
$Rule->process('fieldName', $data, $methods);
|
|
|
|
$this->assertTrue($Rule->isValid());
|
|
|
|
}
|
2012-07-15 23:45:10 +00:00
|
|
|
|
2012-04-30 00:26:24 +00:00
|
|
|
/**
|
|
|
|
* tests that passing custom validation methods work
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCustomMethods() {
|
|
|
|
$def = array('rule' => 'myTestRule');
|
|
|
|
$data = array(
|
|
|
|
'fieldName' => 'some data'
|
|
|
|
);
|
|
|
|
$methods = array('mytestrule' => array($this, 'myTestRule'));
|
|
|
|
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
2012-04-30 00:26:24 +00:00
|
|
|
$Rule->process('fieldName', $data, $methods);
|
|
|
|
$this->assertFalse($Rule->isValid());
|
|
|
|
|
|
|
|
$methods = array('mytestrule' => array($this, 'myTestRule2'));
|
|
|
|
$Rule->process('fieldName', $data, $methods);
|
|
|
|
$this->assertTrue($Rule->isValid());
|
|
|
|
|
|
|
|
$methods = array('mytestrule' => array($this, 'myTestRule3'));
|
|
|
|
$Rule->process('fieldName', $data, $methods);
|
|
|
|
$this->assertFalse($Rule->isValid());
|
|
|
|
}
|
|
|
|
|
2012-07-15 23:45:10 +00:00
|
|
|
/**
|
|
|
|
* Make sure errors are triggered when validation is missing.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCustomMethodMissingError() {
|
2022-12-31 00:08:28 +00:00
|
|
|
$this->expectWarning();
|
|
|
|
$this->expectWarningMessage('Could not find validation handler totallyMissing for fieldName');
|
2012-07-15 23:45:10 +00:00
|
|
|
$def = array('rule' => array('totallyMissing'));
|
|
|
|
$data = array(
|
|
|
|
'fieldName' => 'some data'
|
|
|
|
);
|
|
|
|
$methods = array('mytestrule' => array($this, 'myTestRule'));
|
|
|
|
|
|
|
|
$Rule = new CakeValidationRule($def);
|
|
|
|
$Rule->process('fieldName', $data, $methods);
|
|
|
|
}
|
|
|
|
|
2012-04-30 00:26:24 +00:00
|
|
|
/**
|
|
|
|
* Test isRequired method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIsRequired() {
|
2015-05-17 20:27:16 +00:00
|
|
|
$def = array('rule' => 'notBlank', 'required' => true);
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
2012-04-30 00:26:24 +00:00
|
|
|
$this->assertTrue($Rule->isRequired());
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$def = array('rule' => 'notBlank', 'required' => false);
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
2012-04-30 00:26:24 +00:00
|
|
|
$this->assertFalse($Rule->isRequired());
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$def = array('rule' => 'notBlank', 'required' => 'create');
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
2012-04-30 00:26:24 +00:00
|
|
|
$this->assertTrue($Rule->isRequired());
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$def = array('rule' => 'notBlank', 'required' => 'update');
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
2012-04-30 00:26:24 +00:00
|
|
|
$this->assertFalse($Rule->isRequired());
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-30 00:26:24 +00:00
|
|
|
$Rule->isUpdate(true);
|
|
|
|
$this->assertTrue($Rule->isRequired());
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|
2012-05-11 03:43:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test isEmptyAllowed method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-06-25 00:06:14 +00:00
|
|
|
public function testIsEmptyAllowed() {
|
2012-05-11 03:43:45 +00:00
|
|
|
$def = array('rule' => 'aRule', 'allowEmpty' => true);
|
|
|
|
$Rule = new CakeValidationRule($def);
|
|
|
|
$this->assertTrue($Rule->isEmptyAllowed());
|
|
|
|
|
|
|
|
$def = array('rule' => 'aRule', 'allowEmpty' => false);
|
|
|
|
$Rule = new CakeValidationRule($def);
|
|
|
|
$this->assertFalse($Rule->isEmptyAllowed());
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$def = array('rule' => 'notBlank', 'allowEmpty' => false, 'on' => 'update');
|
2012-05-11 03:43:45 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
|
|
|
$this->assertTrue($Rule->isEmptyAllowed());
|
|
|
|
|
|
|
|
$Rule->isUpdate(true);
|
|
|
|
$this->assertFalse($Rule->isEmptyAllowed());
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$def = array('rule' => 'notBlank', 'allowEmpty' => false, 'on' => 'create');
|
2012-05-11 03:43:45 +00:00
|
|
|
$Rule = new CakeValidationRule($def);
|
|
|
|
$this->assertFalse($Rule->isEmptyAllowed());
|
|
|
|
|
|
|
|
$Rule->isUpdate(true);
|
|
|
|
$this->assertTrue($Rule->isEmptyAllowed());
|
|
|
|
}
|
2012-11-02 13:35:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkRequired method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCheckRequiredWhenRequiredAndAllowEmpty() {
|
|
|
|
$Rule = $this->getMock('CakeValidationRule', array('isRequired'));
|
|
|
|
$Rule->expects($this->any())
|
|
|
|
->method('isRequired')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$Rule->allowEmpty = true;
|
|
|
|
|
|
|
|
$fieldname = 'field';
|
|
|
|
$data = array(
|
|
|
|
$fieldname => null
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse($Rule->checkRequired($fieldname, $data), "A null but present field should not fail requirement check if allowEmpty is true");
|
|
|
|
|
|
|
|
$Rule->allowEmpty = false;
|
|
|
|
|
|
|
|
$this->assertTrue($Rule->checkRequired($fieldname, $data), "A null but present field should fail requirement check if allowEmpty is false");
|
|
|
|
}
|
2012-11-03 21:22:00 +00:00
|
|
|
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|