Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakeValidationSetTest 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('CakeValidationSet', 'Model/Validator');
20
 
21
/**
22
 * CakeValidationSetTest
23
 *
24
 * @package       Cake.Test.Case.Model.Validator
25
 */
26
class CakeValidationSetTest extends CakeTestCase {
27
 
28
/**
29
 * override locale to the default (eng).
30
 *
31
 * @return void
32
 */
33
	public function setUp() {
34
		parent::setUp();
35
		Configure::write('Config.language', 'eng');
36
	}
37
 
38
/**
39
 * testValidate method
40
 *
41
 * @return void
42
 */
43
	public function testValidate() {
44
		$Field = new CakeValidationSet('title', 'notEmpty');
45
		$data = array(
46
			'title' => '',
47
			'body' => 'a body'
48
		);
49
 
50
		$result = $Field->validate($data);
51
		$expected = array('This field cannot be left blank');
52
		$this->assertEquals($expected, $result);
53
 
54
		$Field = new CakeValidationSet('body', 'notEmpty');
55
 
56
		$result = $Field->validate($data);
57
		$this->assertEmpty($result);
58
 
59
		$Field = new CakeValidationSet('nothere', array(
60
			'notEmpty' => array(
61
				'rule' => 'notEmpty',
62
				'required' => true
63
			)
64
		));
65
 
66
		$result = $Field->validate($data);
67
		$expected = array('notEmpty');
68
		$this->assertEquals($expected, $result);
69
 
70
		$Field = new CakeValidationSet('body', array(
71
			'inList' => array(
72
				'rule' => array('inList', array('test'))
73
			)
74
		));
75
		$result = $Field->validate($data);
76
		$expected = array('inList');
77
		$this->assertEquals($expected, $result);
78
	}
79
 
80
/**
81
 * testGetRule method
82
 *
83
 * @return void
84
 */
85
	public function testGetRule() {
86
		$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
87
		$Field = new CakeValidationSet('title', $rules);
88
		$result = $Field->getRule('notEmpty');
89
		$this->assertInstanceOf('CakeValidationRule', $result);
90
		$this->assertEquals('notEmpty', $result->rule);
91
		$this->assertEquals(null, $result->required);
92
		$this->assertEquals(false, $result->allowEmpty);
93
		$this->assertEquals(null, $result->on);
94
		$this->assertEquals(true, $result->last);
95
		$this->assertEquals('Can not be empty', $result->message);
96
	}
97
 
98
/**
99
 * testGetRules method
100
 *
101
 * @return void
102
 */
103
	public function testGetRules() {
104
		$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
105
		$Field = new CakeValidationSet('title', $rules);
106
 
107
		$result = $Field->getRules();
108
		$this->assertEquals(array('notEmpty'), array_keys($result));
109
		$this->assertInstanceOf('CakeValidationRule', $result['notEmpty']);
110
	}
111
 
112
/**
113
 * testSetRule method
114
 *
115
 * @return void
116
 */
117
	public function testSetRule() {
118
		$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
119
		$Field = new CakeValidationSet('title', $rules);
120
		$Rule = new CakeValidationRule($rules['notEmpty']);
121
 
122
		$this->assertEquals($Rule, $Field->getRule('notEmpty'));
123
 
124
		$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
125
		$Rule = new CakeValidationRule($rules['validEmail']);
126
		$Field->setRule('validEmail', $Rule);
127
		$result = $Field->getRules();
128
		$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
129
 
130
		$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
131
		$Rule = new CakeValidationRule($rules['validEmail']);
132
		$Field->setRule('validEmail', $Rule);
133
		$result = $Field->getRules();
134
		$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
135
		$result = $Field->getRule('validEmail');
136
		$this->assertInstanceOf('CakeValidationRule', $result);
137
		$this->assertEquals('email', $result->rule);
138
		$this->assertEquals(null, $result->required);
139
		$this->assertEquals(false, $result->allowEmpty);
140
		$this->assertEquals(null, $result->on);
141
		$this->assertEquals(true, $result->last);
142
		$this->assertEquals('Other message', $result->message);
143
	}
144
 
145
/**
146
 * testSetRules method
147
 *
148
 * @return void
149
 */
150
	public function testSetRules() {
151
		$rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
152
		$Field = new CakeValidationSet('title', $rule);
153
		$RuleEmpty = new CakeValidationRule($rule['notEmpty']);
154
 
155
		$rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
156
		$RuleEmail = new CakeValidationRule($rule['validEmail']);
157
 
158
		$rules = array('validEmail' => $RuleEmail);
159
		$Field->setRules($rules, false);
160
		$result = $Field->getRules();
161
		$this->assertEquals(array('validEmail'), array_keys($result));
162
 
163
		$Field->setRules(array('validEmail' => $rule), false);
164
		$result = $Field->getRules();
165
		$this->assertEquals(array('validEmail'), array_keys($result));
166
		$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
167
 
168
		$rules = array('notEmpty' => $RuleEmpty);
169
		$Field->setRules($rules, true);
170
		$result = $Field->getRules();
171
		$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
172
 
173
		$rules = array('notEmpty' => array('rule' => 'notEmpty'));
174
		$Field->setRules($rules, true);
175
		$result = $Field->getRules();
176
		$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
177
		$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
178
		$this->assertTrue(array_pop($result) instanceof CakeValidationRule);
179
	}
180
 
181
/**
182
 * Tests getting a rule from the set using array access
183
 *
184
 * @return void
185
 */
186
	public function testArrayAccessGet() {
187
		$Set = new CakeValidationSet('title', array(
188
			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
189
			'numeric' => array('rule' => 'numeric'),
190
			'other' => array('rule' => array('other', 1)),
191
		));
192
 
193
		$rule = $Set['notEmpty'];
194
		$this->assertInstanceOf('CakeValidationRule', $rule);
195
		$this->assertEquals('notEmpty', $rule->rule);
196
 
197
		$rule = $Set['numeric'];
198
		$this->assertInstanceOf('CakeValidationRule', $rule);
199
		$this->assertEquals('numeric', $rule->rule);
200
 
201
		$rule = $Set['other'];
202
		$this->assertInstanceOf('CakeValidationRule', $rule);
203
		$this->assertEquals(array('other', 1), $rule->rule);
204
	}
205
 
206
/**
207
 * Tests checking a rule from the set using array access
208
 *
209
 * @return void
210
 */
211
	public function testArrayAccessExists() {
212
		$Set = new CakeValidationSet('title', array(
213
			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
214
			'numeric' => array('rule' => 'numeric'),
215
			'other' => array('rule' => array('other', 1)),
216
		));
217
 
218
		$this->assertTrue(isset($Set['notEmpty']));
219
		$this->assertTrue(isset($Set['numeric']));
220
		$this->assertTrue(isset($Set['other']));
221
		$this->assertFalse(isset($Set['fail']));
222
	}
223
 
224
/**
225
 * Tests setting a rule in the set using array access
226
 *
227
 * @return void
228
 */
229
	public function testArrayAccessSet() {
230
		$Set = new CakeValidationSet('title', array(
231
			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
232
		));
233
 
234
		$this->assertFalse(isset($Set['other']));
235
		$Set['other'] = array('rule' => array('other', 1));
236
		$rule = $Set['other'];
237
		$this->assertInstanceOf('CakeValidationRule', $rule);
238
		$this->assertEquals(array('other', 1), $rule->rule);
239
 
240
		$this->assertFalse(isset($Set['numeric']));
241
		$Set['numeric'] = new CakeValidationRule(array('rule' => 'numeric'));
242
		$rule = $Set['numeric'];
243
		$this->assertInstanceOf('CakeValidationRule', $rule);
244
		$this->assertEquals('numeric', $rule->rule);
245
	}
246
 
247
/**
248
 * Tests unseting a rule from the set using array access
249
 *
250
 * @return void
251
 */
252
	public function testArrayAccessUnset() {
253
		$Set = new CakeValidationSet('title', array(
254
			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
255
			'numeric' => array('rule' => 'numeric'),
256
			'other' => array('rule' => array('other', 1)),
257
		));
258
 
259
		unset($Set['notEmpty']);
260
		$this->assertFalse(isset($Set['notEmpty']));
261
 
262
		unset($Set['numeric']);
263
		$this->assertFalse(isset($Set['notEmpty']));
264
 
265
		unset($Set['other']);
266
		$this->assertFalse(isset($Set['notEmpty']));
267
	}
268
 
269
/**
270
 * Tests it is possible to iterate a validation set object
271
 *
272
 * @return void
273
 */
274
	public function testIterator() {
275
		$Set = new CakeValidationSet('title', array(
276
			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
277
			'numeric' => array('rule' => 'numeric'),
278
			'other' => array('rule' => array('other', 1)),
279
		));
280
 
281
		$i = 0;
282
		foreach ($Set as $name => $rule) {
283
			if ($i === 0) {
284
				$this->assertEquals('notEmpty', $name);
285
			}
286
			if ($i === 1) {
287
				$this->assertEquals('numeric', $name);
288
			}
289
			if ($i === 2) {
290
				$this->assertEquals('other', $name);
291
			}
292
			$this->assertInstanceOf('CakeValidationRule', $rule);
293
			$i++;
294
		}
295
		$this->assertEquals(3, $i);
296
	}
297
 
298
/**
299
 * Tests countable interface
300
 *
301
 * @return void
302
 */
303
	public function testCount() {
304
		$Set = new CakeValidationSet('title', array(
305
			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
306
			'numeric' => array('rule' => 'numeric'),
307
			'other' => array('rule' => array('other', 1)),
308
		));
309
		$this->assertCount(3, $Set);
310
 
311
		unset($Set['other']);
312
		$this->assertCount(2, $Set);
313
	}
314
 
315
/**
316
 * Test removeRule method
317
 *
318
 * @return void
319
 */
320
	public function testRemoveRule() {
321
		$Set = new CakeValidationSet('title', array(
322
			'notEmpty' => array('rule' => 'notEmpty', 'required' => true),
323
			'numeric' => array('rule' => 'numeric'),
324
			'other' => array('rule' => array('other', 1)),
325
		));
326
 
327
		$Set->removeRule('notEmpty');
328
		$this->assertFalse(isset($Set['notEmpty']));
329
 
330
		$Set->removeRule('numeric');
331
		$this->assertFalse(isset($Set['numeric']));
332
 
333
		$Set->removeRule('other');
334
		$this->assertFalse(isset($Set['other']));
335
	}
336
 
337
}