2012-04-23 11:02:26 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2012-05-05 18:13:40 +00:00
|
|
|
* CakeValidationSetTest 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-05 18:13:40 +00:00
|
|
|
App::uses('CakeValidationSet', 'Model/Validator');
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-05 18:13:40 +00:00
|
|
|
* CakeValidationSetTest
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Model.Validator
|
|
|
|
*/
|
2012-11-21 14:39:03 +00:00
|
|
|
class CakeValidationSetTest extends CakeTestCase {
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2013-06-11 18:03:05 +00:00
|
|
|
/**
|
|
|
|
* override locale to the default (eng).
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
Configure::write('Config.language', 'eng');
|
|
|
|
}
|
|
|
|
|
2012-04-23 11:02:26 +00:00
|
|
|
/**
|
|
|
|
* testValidate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidate() {
|
2015-05-17 20:27:16 +00:00
|
|
|
$Field = new CakeValidationSet('title', 'notBlank');
|
2012-04-29 21:48:33 +00:00
|
|
|
$data = array(
|
|
|
|
'title' => '',
|
|
|
|
'body' => 'a body'
|
|
|
|
);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-29 21:48:33 +00:00
|
|
|
$result = $Field->validate($data);
|
|
|
|
$expected = array('This field cannot be left blank');
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$Field = new CakeValidationSet('body', 'notBlank');
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-29 21:48:33 +00:00
|
|
|
$result = $Field->validate($data);
|
|
|
|
$this->assertEmpty($result);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-05-24 01:02:41 +00:00
|
|
|
$Field = new CakeValidationSet('nothere', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array(
|
|
|
|
'rule' => 'notBlank',
|
2012-05-24 01:02:41 +00:00
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
));
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2012-04-29 21:48:33 +00:00
|
|
|
$result = $Field->validate($data);
|
2015-05-17 20:27:16 +00:00
|
|
|
$expected = array('notBlank');
|
2012-04-29 21:48:33 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2012-05-24 01:02:41 +00:00
|
|
|
|
|
|
|
$Field = new CakeValidationSet('body', array(
|
|
|
|
'inList' => array(
|
|
|
|
'rule' => array('inList', array('test'))
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$result = $Field->validate($data);
|
|
|
|
$expected = array('inList');
|
|
|
|
$this->assertEquals($expected, $result);
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testGetRule method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetRule() {
|
2015-05-17 20:27:16 +00:00
|
|
|
$rules = array('notBlank' => array('rule' => 'notBlank', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rules);
|
2015-05-17 20:27:16 +00:00
|
|
|
$result = $Field->getRule('notBlank');
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $result);
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals('notBlank', $result->rule);
|
2012-04-23 11:02:26 +00:00
|
|
|
$this->assertEquals(null, $result->required);
|
|
|
|
$this->assertEquals(false, $result->allowEmpty);
|
|
|
|
$this->assertEquals(null, $result->on);
|
|
|
|
$this->assertEquals(true, $result->last);
|
|
|
|
$this->assertEquals('Can not be empty', $result->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testGetRules method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGetRules() {
|
2015-05-17 20:27:16 +00:00
|
|
|
$rules = array('notBlank' => array('rule' => 'notBlank', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rules);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$result = $Field->getRules();
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals(array('notBlank'), array_keys($result));
|
|
|
|
$this->assertInstanceOf('CakeValidationRule', $result['notBlank']);
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSetRule method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSetRule() {
|
2015-05-17 20:27:16 +00:00
|
|
|
$rules = array('notBlank' => array('rule' => 'notBlank', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rules);
|
2015-05-17 20:27:16 +00:00
|
|
|
$Rule = new CakeValidationRule($rules['notBlank']);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals($Rule, $Field->getRule('notBlank'));
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($rules['validEmail']);
|
2012-04-23 11:02:26 +00:00
|
|
|
$Field->setRule('validEmail', $Rule);
|
|
|
|
$result = $Field->getRules();
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals(array('notBlank', 'validEmail'), array_keys($result));
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
|
2012-05-11 01:31:44 +00:00
|
|
|
$Rule = new CakeValidationRule($rules['validEmail']);
|
2012-04-23 11:02:26 +00:00
|
|
|
$Field->setRule('validEmail', $Rule);
|
|
|
|
$result = $Field->getRules();
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals(array('notBlank', 'validEmail'), array_keys($result));
|
2012-04-23 11:02:26 +00:00
|
|
|
$result = $Field->getRule('validEmail');
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $result);
|
2012-04-23 11:02:26 +00:00
|
|
|
$this->assertEquals('email', $result->rule);
|
|
|
|
$this->assertEquals(null, $result->required);
|
|
|
|
$this->assertEquals(false, $result->allowEmpty);
|
|
|
|
$this->assertEquals(null, $result->on);
|
|
|
|
$this->assertEquals(true, $result->last);
|
|
|
|
$this->assertEquals('Other message', $result->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSetRules method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSetRules() {
|
2015-05-17 20:27:16 +00:00
|
|
|
$rule = array('notBlank' => array('rule' => 'notBlank', 'message' => 'Can not be empty'));
|
2012-05-05 18:13:40 +00:00
|
|
|
$Field = new CakeValidationSet('title', $rule);
|
2015-05-17 20:27:16 +00:00
|
|
|
$RuleEmpty = new CakeValidationRule($rule['notBlank']);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
|
2012-05-11 01:31:44 +00:00
|
|
|
$RuleEmail = new CakeValidationRule($rule['validEmail']);
|
2012-04-23 11:02:26 +00:00
|
|
|
|
|
|
|
$rules = array('validEmail' => $RuleEmail);
|
|
|
|
$Field->setRules($rules, false);
|
|
|
|
$result = $Field->getRules();
|
|
|
|
$this->assertEquals(array('validEmail'), array_keys($result));
|
|
|
|
|
2012-11-13 16:43:15 +00:00
|
|
|
$Field->setRules(array('validEmail' => $rule), false);
|
|
|
|
$result = $Field->getRules();
|
|
|
|
$this->assertEquals(array('validEmail'), array_keys($result));
|
|
|
|
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$rules = array('notBlank' => $RuleEmpty);
|
2012-04-23 11:02:26 +00:00
|
|
|
$Field->setRules($rules, true);
|
|
|
|
$result = $Field->getRules();
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals(array('validEmail', 'notBlank'), array_keys($result));
|
2012-11-13 16:43:15 +00:00
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$rules = array('notBlank' => array('rule' => 'notBlank'));
|
2012-11-13 16:43:15 +00:00
|
|
|
$Field->setRules($rules, true);
|
|
|
|
$result = $Field->getRules();
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals(array('validEmail', 'notBlank'), array_keys($result));
|
2012-11-13 16:43:15 +00:00
|
|
|
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
|
|
|
|
$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
|
2012-04-23 11:02:26 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 01:30:40 +00:00
|
|
|
/**
|
|
|
|
* Tests getting a rule from the set using array access
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessGet() {
|
|
|
|
$Set = new CakeValidationSet('title', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array('rule' => 'notBlank', 'required' => true),
|
2012-05-06 01:30:40 +00:00
|
|
|
'numeric' => array('rule' => 'numeric'),
|
|
|
|
'other' => array('rule' => array('other', 1)),
|
|
|
|
));
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$rule = $Set['notBlank'];
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $rule);
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals('notBlank', $rule->rule);
|
2012-05-06 01:30:40 +00:00
|
|
|
|
|
|
|
$rule = $Set['numeric'];
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $rule);
|
2012-05-06 01:30:40 +00:00
|
|
|
$this->assertEquals('numeric', $rule->rule);
|
|
|
|
|
|
|
|
$rule = $Set['other'];
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $rule);
|
2012-05-06 01:30:40 +00:00
|
|
|
$this->assertEquals(array('other', 1), $rule->rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests checking a rule from the set using array access
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessExists() {
|
|
|
|
$Set = new CakeValidationSet('title', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array('rule' => 'notBlank', 'required' => true),
|
2012-05-06 01:30:40 +00:00
|
|
|
'numeric' => array('rule' => 'numeric'),
|
|
|
|
'other' => array('rule' => array('other', 1)),
|
|
|
|
));
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertTrue(isset($Set['notBlank']));
|
2012-05-06 01:30:40 +00:00
|
|
|
$this->assertTrue(isset($Set['numeric']));
|
|
|
|
$this->assertTrue(isset($Set['other']));
|
|
|
|
$this->assertFalse(isset($Set['fail']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests setting a rule in the set using array access
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessSet() {
|
|
|
|
$Set = new CakeValidationSet('title', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array('rule' => 'notBlank', 'required' => true),
|
2012-05-06 01:30:40 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFalse(isset($Set['other']));
|
|
|
|
$Set['other'] = array('rule' => array('other', 1));
|
|
|
|
$rule = $Set['other'];
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $rule);
|
2012-05-06 01:30:40 +00:00
|
|
|
$this->assertEquals(array('other', 1), $rule->rule);
|
|
|
|
|
|
|
|
$this->assertFalse(isset($Set['numeric']));
|
2012-05-11 01:31:44 +00:00
|
|
|
$Set['numeric'] = new CakeValidationRule(array('rule' => 'numeric'));
|
2012-05-06 01:30:40 +00:00
|
|
|
$rule = $Set['numeric'];
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $rule);
|
2012-05-06 01:30:40 +00:00
|
|
|
$this->assertEquals('numeric', $rule->rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests unseting a rule from the set using array access
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayAccessUnset() {
|
|
|
|
$Set = new CakeValidationSet('title', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array('rule' => 'notBlank', 'required' => true),
|
2012-05-06 01:30:40 +00:00
|
|
|
'numeric' => array('rule' => 'numeric'),
|
|
|
|
'other' => array('rule' => array('other', 1)),
|
|
|
|
));
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
unset($Set['notBlank']);
|
|
|
|
$this->assertFalse(isset($Set['notBlank']));
|
2012-05-06 01:30:40 +00:00
|
|
|
|
|
|
|
unset($Set['numeric']);
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertFalse(isset($Set['notBlank']));
|
2012-05-06 01:30:40 +00:00
|
|
|
|
|
|
|
unset($Set['other']);
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertFalse(isset($Set['notBlank']));
|
2012-05-06 01:30:40 +00:00
|
|
|
}
|
2012-05-06 01:43:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests it is possible to iterate a validation set object
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testIterator() {
|
|
|
|
$Set = new CakeValidationSet('title', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array('rule' => 'notBlank', 'required' => true),
|
2012-05-06 01:43:09 +00:00
|
|
|
'numeric' => array('rule' => 'numeric'),
|
|
|
|
'other' => array('rule' => array('other', 1)),
|
|
|
|
));
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach ($Set as $name => $rule) {
|
|
|
|
if ($i === 0) {
|
2015-05-17 20:27:16 +00:00
|
|
|
$this->assertEquals('notBlank', $name);
|
2012-05-06 01:43:09 +00:00
|
|
|
}
|
|
|
|
if ($i === 1) {
|
|
|
|
$this->assertEquals('numeric', $name);
|
|
|
|
}
|
|
|
|
if ($i === 2) {
|
|
|
|
$this->assertEquals('other', $name);
|
|
|
|
}
|
2012-05-11 01:31:44 +00:00
|
|
|
$this->assertInstanceOf('CakeValidationRule', $rule);
|
2012-05-06 01:43:09 +00:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
$this->assertEquals(3, $i);
|
|
|
|
}
|
|
|
|
|
2012-05-06 05:37:13 +00:00
|
|
|
/**
|
|
|
|
* Tests countable interface
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCount() {
|
|
|
|
$Set = new CakeValidationSet('title', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array('rule' => 'notBlank', 'required' => true),
|
2012-05-06 05:37:13 +00:00
|
|
|
'numeric' => array('rule' => 'numeric'),
|
|
|
|
'other' => array('rule' => array('other', 1)),
|
|
|
|
));
|
|
|
|
$this->assertCount(3, $Set);
|
|
|
|
|
|
|
|
unset($Set['other']);
|
|
|
|
$this->assertCount(2, $Set);
|
|
|
|
}
|
|
|
|
|
2012-05-07 03:00:07 +00:00
|
|
|
/**
|
|
|
|
* Test removeRule method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRemoveRule() {
|
|
|
|
$Set = new CakeValidationSet('title', array(
|
2015-05-17 20:27:16 +00:00
|
|
|
'notBlank' => array('rule' => 'notBlank', 'required' => true),
|
2012-05-07 03:00:07 +00:00
|
|
|
'numeric' => array('rule' => 'numeric'),
|
|
|
|
'other' => array('rule' => array('other', 1)),
|
|
|
|
));
|
|
|
|
|
2015-05-17 20:27:16 +00:00
|
|
|
$Set->removeRule('notBlank');
|
|
|
|
$this->assertFalse(isset($Set['notBlank']));
|
2012-05-07 03:00:07 +00:00
|
|
|
|
|
|
|
$Set->removeRule('numeric');
|
|
|
|
$this->assertFalse(isset($Set['numeric']));
|
|
|
|
|
|
|
|
$Set->removeRule('other');
|
|
|
|
$this->assertFalse(isset($Set['other']));
|
|
|
|
}
|
|
|
|
|
2012-05-24 01:02:41 +00:00
|
|
|
}
|