cakephp2-php8/lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php

199 lines
5.2 KiB
PHP
Raw Normal View History

2012-04-23 11:02:26 +00:00
<?php
/**
* 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>
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2012-04-23 11:02:26 +00:00
*
* Licensed under The MIT License
* 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
*
* @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
* @license https://opensource.org/licenses/mit-license.php MIT License
2012-04-23 11:02:26 +00:00
*/
App::uses('CakeValidationRule', 'Model/Validator');
2012-04-23 11:02:26 +00:00
/**
* CakeValidationRuleTest
2012-04-23 11:02:26 +00:00
*
* @package Cake.Test.Case.Model.Validator
*/
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
*
* @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
*
* @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() {
$def = array('rule' => 'notBlank', 'message' => 'Can not be empty');
2012-04-30 00:26:24 +00:00
$data = array(
'fieldName' => ''
);
$methods = array();
$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-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'));
$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());
}
/**
* Make sure errors are triggered when validation is missing.
*
* @return void
*/
public function testCustomMethodMissingError() {
$this->expectWarning();
$this->expectWarningMessage('Could not find validation handler totallyMissing for fieldName');
$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() {
$def = array('rule' => 'notBlank', 'required' => true);
$Rule = new CakeValidationRule($def);
2012-04-30 00:26:24 +00:00
$this->assertTrue($Rule->isRequired());
$def = array('rule' => 'notBlank', 'required' => false);
$Rule = new CakeValidationRule($def);
2012-04-30 00:26:24 +00:00
$this->assertFalse($Rule->isRequired());
$def = array('rule' => 'notBlank', 'required' => 'create');
$Rule = new CakeValidationRule($def);
2012-04-30 00:26:24 +00:00
$this->assertTrue($Rule->isRequired());
$def = array('rule' => 'notBlank', 'required' => 'update');
$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
}
/**
* Test isEmptyAllowed method
*
* @return void
*/
public function testIsEmptyAllowed() {
$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());
$def = array('rule' => 'notBlank', 'allowEmpty' => false, 'on' => 'update');
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isEmptyAllowed());
$Rule->isUpdate(true);
$this->assertFalse($Rule->isEmptyAllowed());
$def = array('rule' => 'notBlank', 'allowEmpty' => false, 'on' => 'create');
$Rule = new CakeValidationRule($def);
$this->assertFalse($Rule->isEmptyAllowed());
$Rule->isUpdate(true);
$this->assertTrue($Rule->isEmptyAllowed());
}
/**
* 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
}