| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CakeValidationRuleTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
|
|
14 |
* @package Cake.Test.Case.Model.Validator
|
|
|
15 |
* @since CakePHP(tm) v 2.2.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('CakeValidationRule', 'Model/Validator');
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* CakeValidationRuleTest
|
|
|
23 |
*
|
|
|
24 |
* @package Cake.Test.Case.Model.Validator
|
|
|
25 |
*/
|
|
|
26 |
class CakeValidationRuleTest extends CakeTestCase {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Auxiliary method to test custom validators
|
|
|
30 |
*
|
|
|
31 |
* @return boolean
|
|
|
32 |
*/
|
|
|
33 |
public function myTestRule() {
|
|
|
34 |
return false;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Auxiliary method to test custom validators
|
|
|
39 |
*
|
|
|
40 |
* @return boolean
|
|
|
41 |
*/
|
|
|
42 |
public function myTestRule2() {
|
|
|
43 |
return true;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Auxiliary method to test custom validators
|
|
|
48 |
*
|
|
|
49 |
* @return string
|
|
|
50 |
*/
|
|
|
51 |
public function myTestRule3() {
|
|
|
52 |
return 'string';
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Test isValid method
|
|
|
57 |
*
|
|
|
58 |
* @return void
|
|
|
59 |
*/
|
|
|
60 |
public function testIsValid() {
|
|
|
61 |
$def = array('rule' => 'notEmpty', 'message' => 'Can not be empty');
|
|
|
62 |
$data = array(
|
|
|
63 |
'fieldName' => ''
|
|
|
64 |
);
|
|
|
65 |
$methods = array();
|
|
|
66 |
|
|
|
67 |
$Rule = new CakeValidationRule($def);
|
|
|
68 |
$Rule->process('fieldName', $data, $methods);
|
|
|
69 |
$this->assertFalse($Rule->isValid());
|
|
|
70 |
|
|
|
71 |
$data = array('fieldName' => 'not empty');
|
|
|
72 |
$Rule->process('fieldName', $data, $methods);
|
|
|
73 |
$this->assertTrue($Rule->isValid());
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* tests that passing custom validation methods work
|
|
|
78 |
*
|
|
|
79 |
* @return void
|
|
|
80 |
*/
|
|
|
81 |
public function testCustomMethods() {
|
|
|
82 |
$def = array('rule' => 'myTestRule');
|
|
|
83 |
$data = array(
|
|
|
84 |
'fieldName' => 'some data'
|
|
|
85 |
);
|
|
|
86 |
$methods = array('mytestrule' => array($this, 'myTestRule'));
|
|
|
87 |
|
|
|
88 |
$Rule = new CakeValidationRule($def);
|
|
|
89 |
$Rule->process('fieldName', $data, $methods);
|
|
|
90 |
$this->assertFalse($Rule->isValid());
|
|
|
91 |
|
|
|
92 |
$methods = array('mytestrule' => array($this, 'myTestRule2'));
|
|
|
93 |
$Rule->process('fieldName', $data, $methods);
|
|
|
94 |
$this->assertTrue($Rule->isValid());
|
|
|
95 |
|
|
|
96 |
$methods = array('mytestrule' => array($this, 'myTestRule3'));
|
|
|
97 |
$Rule->process('fieldName', $data, $methods);
|
|
|
98 |
$this->assertFalse($Rule->isValid());
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Make sure errors are triggered when validation is missing.
|
|
|
103 |
*
|
|
|
104 |
* @expectedException PHPUnit_Framework_Error_Warning
|
|
|
105 |
* @expectedExceptionMessage Could not find validation handler totallyMissing for fieldName
|
|
|
106 |
* @return void
|
|
|
107 |
*/
|
|
|
108 |
public function testCustomMethodMissingError() {
|
|
|
109 |
$def = array('rule' => array('totallyMissing'));
|
|
|
110 |
$data = array(
|
|
|
111 |
'fieldName' => 'some data'
|
|
|
112 |
);
|
|
|
113 |
$methods = array('mytestrule' => array($this, 'myTestRule'));
|
|
|
114 |
|
|
|
115 |
$Rule = new CakeValidationRule($def);
|
|
|
116 |
$Rule->process('fieldName', $data, $methods);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Test isRequired method
|
|
|
121 |
*
|
|
|
122 |
* @return void
|
|
|
123 |
*/
|
|
|
124 |
public function testIsRequired() {
|
|
|
125 |
$def = array('rule' => 'notEmpty', 'required' => true);
|
|
|
126 |
$Rule = new CakeValidationRule($def);
|
|
|
127 |
$this->assertTrue($Rule->isRequired());
|
|
|
128 |
|
|
|
129 |
$def = array('rule' => 'notEmpty', 'required' => false);
|
|
|
130 |
$Rule = new CakeValidationRule($def);
|
|
|
131 |
$this->assertFalse($Rule->isRequired());
|
|
|
132 |
|
|
|
133 |
$def = array('rule' => 'notEmpty', 'required' => 'create');
|
|
|
134 |
$Rule = new CakeValidationRule($def);
|
|
|
135 |
$this->assertTrue($Rule->isRequired());
|
|
|
136 |
|
|
|
137 |
$def = array('rule' => 'notEmpty', 'required' => 'update');
|
|
|
138 |
$Rule = new CakeValidationRule($def);
|
|
|
139 |
$this->assertFalse($Rule->isRequired());
|
|
|
140 |
|
|
|
141 |
$Rule->isUpdate(true);
|
|
|
142 |
$this->assertTrue($Rule->isRequired());
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Test isEmptyAllowed method
|
|
|
147 |
*
|
|
|
148 |
* @return void
|
|
|
149 |
*/
|
|
|
150 |
public function testIsEmptyAllowed() {
|
|
|
151 |
$def = array('rule' => 'aRule', 'allowEmpty' => true);
|
|
|
152 |
$Rule = new CakeValidationRule($def);
|
|
|
153 |
$this->assertTrue($Rule->isEmptyAllowed());
|
|
|
154 |
|
|
|
155 |
$def = array('rule' => 'aRule', 'allowEmpty' => false);
|
|
|
156 |
$Rule = new CakeValidationRule($def);
|
|
|
157 |
$this->assertFalse($Rule->isEmptyAllowed());
|
|
|
158 |
|
|
|
159 |
$def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'update');
|
|
|
160 |
$Rule = new CakeValidationRule($def);
|
|
|
161 |
$this->assertTrue($Rule->isEmptyAllowed());
|
|
|
162 |
|
|
|
163 |
$Rule->isUpdate(true);
|
|
|
164 |
$this->assertFalse($Rule->isEmptyAllowed());
|
|
|
165 |
|
|
|
166 |
$def = array('rule' => 'notEmpty', 'allowEmpty' => false, 'on' => 'create');
|
|
|
167 |
$Rule = new CakeValidationRule($def);
|
|
|
168 |
$this->assertFalse($Rule->isEmptyAllowed());
|
|
|
169 |
|
|
|
170 |
$Rule->isUpdate(true);
|
|
|
171 |
$this->assertTrue($Rule->isEmptyAllowed());
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Test checkRequired method
|
|
|
176 |
*
|
|
|
177 |
* @return void
|
|
|
178 |
*/
|
|
|
179 |
public function testCheckRequiredWhenRequiredAndAllowEmpty() {
|
|
|
180 |
$Rule = $this->getMock('CakeValidationRule', array('isRequired'));
|
|
|
181 |
$Rule->expects($this->any())
|
|
|
182 |
->method('isRequired')
|
|
|
183 |
->will($this->returnValue(true));
|
|
|
184 |
$Rule->allowEmpty = true;
|
|
|
185 |
|
|
|
186 |
$fieldname = 'field';
|
|
|
187 |
$data = array(
|
|
|
188 |
$fieldname => null
|
|
|
189 |
);
|
|
|
190 |
|
|
|
191 |
$this->assertFalse($Rule->checkRequired($fieldname, $data), "A null but present field should not fail requirement check if allowEmpty is true");
|
|
|
192 |
|
|
|
193 |
$Rule->allowEmpty = false;
|
|
|
194 |
|
|
|
195 |
$this->assertTrue($Rule->checkRequired($fieldname, $data), "A null but present field should fail requirement check if allowEmpty is false");
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
}
|