/
ValidatorTest.php
474 lines (417 loc) · 14.3 KB
/
ValidatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license infValidationation, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Validation;
use \Cake\Validation\ValidationSet;
use \Cake\Validation\Validator;
/**
* Tests Validator class
*
*/
class ValidatorTest extends \Cake\TestSuite\TestCase {
/**
* Testing you can dynamically add rules to a field
*
* @return void
*/
public function testAddingRulesToField() {
$validator = new Validator;
$validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
$set = $validator->field('title');
$this->assertInstanceOf('\Cake\Validation\ValidationSet', $set);
$this->assertCount(1, $set);
$validator->add('title', 'another', ['rule' => 'alphanumeric']);
$this->assertCount(2, $set);
$validator->add('body', 'another', ['rule' => 'crazy']);
$this->assertCount(1, $validator->field('body'));
$this->assertCount(2, $validator);
}
/**
* Tests that calling field will create a default validation set for it
*
* @return void
*/
public function testFieldDefault() {
$validator = new Validator;
$field = $validator->field('foo');
$this->assertInstanceOf('\Cake\Validation\ValidationSet', $field);
$this->assertCount(0, $field);
}
/**
* Tests that field method can be used as a setter
*
* @return void
*/
public function testFieldSetter() {
$validator = new Validator;
$validationSet = new ValidationSet;
$validator->field('thing', $validationSet);
$this->assertSame($validationSet, $validator->field('thing'));
}
/**
* Tests the remove method
*
* @return void
*/
public function testRemove() {
$validator = new Validator;
$validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
$validator->add('title', 'foo', ['rule' => 'bar']);
$this->assertCount(2, $validator->field('title'));
$validator->remove('title');
$this->assertCount(0, $validator->field('title'));
$validator->remove('title');
$validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
$validator->add('title', 'foo', ['rule' => 'bar']);
$this->assertCount(2, $validator->field('title'));
$validator->remove('title', 'foo');
$this->assertCount(1, $validator->field('title'));
$this->assertNull($validator->field('title')->rule('foo'));
}
/**
* Tests the validatePresence method
*
* @return void
*/
public function testValidatePresence() {
$validator = new Validator;
$this->assertSame($validator, $validator->validatePresence('title'));
$this->assertTrue($validator->field('title')->isPresenceRequired());
$validator->validatePresence('title', false);
$this->assertFalse($validator->field('title')->isPresenceRequired());
$validator->validatePresence('title', 'create');
$this->assertEquals('create', $validator->field('title')->isPresenceRequired());
$validator->validatePresence('title', 'update');
$this->assertEquals('update', $validator->field('title')->isPresenceRequired());
}
/**
* Tests the isPresenceRequired method
*
* @return void
*/
public function testIsPresenceRequired() {
$validator = new Validator;
$this->assertSame($validator, $validator->validatePresence('title'));
$this->assertTrue($validator->isPresenceRequired('title', true));
$this->assertTrue($validator->isPresenceRequired('title', false));
$validator->validatePresence('title', false);
$this->assertFalse($validator->isPresenceRequired('title', true));
$this->assertFalse($validator->isPresenceRequired('title', false));
$validator->validatePresence('title', 'create');
$this->assertTrue($validator->isPresenceRequired('title', true));
$this->assertFalse($validator->isPresenceRequired('title', false));
$validator->validatePresence('title', 'update');
$this->assertTrue($validator->isPresenceRequired('title', false));
$this->assertFalse($validator->isPresenceRequired('title', true));
}
/**
* Tests errors generated when a field presence is required
*
* @return void
*/
public function testErrorsWithPresenceRequired() {
$validator = new Validator;
$validator->validatePresence('title');
$errors = $validator->errors(['foo' => 'something']);
$expected = ['title' => ['This field is required']];
$this->assertEquals($expected, $errors);
$this->assertEmpty($validator->errors(['title' => 'bar']));
$validator->validatePresence('title', false);
$this->assertEmpty($validator->errors(['foo' => 'bar']));
}
/**
* Tests the allowEmpty method
*
* @return void
*/
public function testAllowEmpty() {
$validator = new Validator;
$this->assertSame($validator, $validator->allowEmpty('title'));
$this->assertTrue($validator->field('title')->isEmptyAllowed());
$validator->allowEmpty('title', false);
$this->assertFalse($validator->field('title')->isEmptyAllowed());
$validator->allowEmpty('title', 'created');
$this->assertEquals('created', $validator->field('title')->isEmptyAllowed());
$validator->allowEmpty('title', 'updated');
$this->assertEquals('updated', $validator->field('title')->isEmptyAllowed());
}
/**
* Tests the isEmptyAllowed method
*
* @return void
*/
public function testIsEmptyAllowed() {
$validator = new Validator;
$this->assertSame($validator, $validator->allowEmpty('title'));
$this->assertTrue($validator->isEmptyAllowed('title', true));
$this->assertTrue($validator->isEmptyAllowed('title', false));
$validator->allowEmpty('title', false);
$this->assertFalse($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
$validator->allowEmpty('title', 'create');
$this->assertTrue($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
$validator->allowEmpty('title', 'update');
$this->assertTrue($validator->isEmptyAllowed('title', false));
$this->assertFalse($validator->isEmptyAllowed('title', true));
}
/**
* Tests errors generated when a field is not allowed to be empty
*
* @return void
*/
public function testErrorsWithEmptyNotAllowed() {
$validator = new Validator;
$validator->allowEmpty('title', false);
$errors = $validator->errors(['title' => '']);
$expected = ['title' => ['This field cannot be left empty']];
$this->assertEquals($expected, $errors);
$errors = $validator->errors(['title' => []]);
$expected = ['title' => ['This field cannot be left empty']];
$this->assertEquals($expected, $errors);
$errors = $validator->errors(['title' => null]);
$expected = ['title' => ['This field cannot be left empty']];
$this->assertEquals($expected, $errors);
$errors = $validator->errors(['title' => 0]);
$this->assertEmpty($errors);
$errors = $validator->errors(['title' => '0']);
$this->assertEmpty($errors);
$errors = $validator->errors(['title' => false]);
$this->assertEmpty($errors);
}
/**
* Tests errors generated when a field is allowed to be empty
*
* @return void
*/
public function testErrorsWithEmptyAllowed() {
$validator = new Validator;
$validator->allowEmpty('title');
$errors = $validator->errors(['title' => '']);
$this->assertEmpty($errors);
$errors = $validator->errors(['title' => []]);
$this->assertEmpty($errors);
$errors = $validator->errors(['title' => null]);
$this->assertEmpty($errors);
$errors = $validator->errors(['title' => 0]);
$this->assertEmpty($errors);
$errors = $validator->errors(['title' => '0']);
$this->assertEmpty($errors);
$errors = $validator->errors(['title' => false]);
$this->assertEmpty($errors);
}
/**
* Test the provider() method
*
* @return void
*/
public function testProvider() {
$validator = new Validator;
$object = new \stdClass;
$this->assertSame($validator, $validator->provider('foo', $object));
$this->assertSame($object, $validator->provider('foo'));
$this->assertNull($validator->provider('bar'));
$another = new \stdClass;
$this->assertSame($validator, $validator->provider('bar', $another));
$this->assertSame($another, $validator->provider('bar'));
$this->assertEquals('\Cake\Validation\Validation', $validator->provider('default'));
}
/**
* Tests errors() method when using validators from the default provider, this proves
* that it returns a default validation message and the custom one set in the rule
*
* @return void
*/
public function testErrorsFromDefaultProvider() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('email', 'notEmpty', ['rule' => 'notEmpty'])
->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
$errors = $validator->errors(['email' => 'not an email!']);
$expected = [
'email' => [
'alpha' => 'The provided value is invalid',
'email' => 'Y u no write email?'
]
];
$this->assertEquals($expected, $errors);
}
/**
* Tests using validation methods from different providers and returning the error
* as a string
*
* @return void
*/
public function testErrorsFromCustomProvider() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$thing = $this->getMock('\stdClass', ['isCool']);
$thing->expects($this->once())->method('isCool')
->will($this->returnCallback(function($data, $providers) use ($thing) {
$this->assertEquals('bar', $data);
$expected = [
'default' => '\Cake\Validation\Validation',
'thing' => $thing
];
$this->assertEquals($expected, $providers);
return "That ain't cool, yo";
}));
$validator->provider('thing', $thing);
$errors = $validator->errors(['email' => '!', 'title' => 'bar']);
$expected = [
'email' => ['alpha' => 'The provided value is invalid'],
'title' => ['cool' => "That ain't cool, yo"]
];
$this->assertEquals($expected, $errors);
}
/**
* Tests that it is possible to pass extra arguments to the validation function
* and it still gets the providers as last argument
*
* @return void
*/
public function testMethodsWithExtraArguments() {
$validator = new Validator;
$validator->add('title', 'cool', [
'rule' => ['isCool', 'and', 'awesome'],
'provider' => 'thing'
]);
$thing = $this->getMock('\stdClass', ['isCool']);
$thing->expects($this->once())->method('isCool')
->will($this->returnCallback(function($data, $a, $b, $providers) use ($thing) {
$this->assertEquals('bar', $data);
$this->assertEquals('and', $a);
$this->assertEquals('awesome', $b);
$expected = [
'default' => '\Cake\Validation\Validation',
'thing' => $thing
];
$this->assertEquals($expected, $providers);
return "That ain't cool, yo";
}));
$validator->provider('thing', $thing);
$errors = $validator->errors(['email' => '!', 'title' => 'bar']);
$expected = [
'title' => ['cool' => "That ain't cool, yo"]
];
$this->assertEquals($expected, $errors);
}
/**
* Tests that it is possible to use a closure as a rule
*
* @return void
*/
public function testUsingClosureAsRule() {
$validator = new Validator;
$validator->add('name', 'myRule', [
'rule' => function($data, $provider) {
$this->assertEquals('foo', $data);
return 'You fail';
}
]);
$expected = ['name' => ['myRule' => 'You fail']];
$this->assertEquals($expected, $validator->errors(['name' => 'foo']));
}
/**
* Tests that setting last to a rule will stop validating the rest of the rules
*
* @return void
*/
public function testErrorsWithLastRule() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])
->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
$errors = $validator->errors(['email' => 'not an email!']);
$expected = [
'email' => [
'alpha' => 'The provided value is invalid'
]
];
$this->assertEquals($expected, $errors);
}
/**
* Tests it is possible to get validation sets for a field using an array interface
*
* @return void
*/
public function testArrayAccessGet() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertSame($validator['email'], $validator->field('email'));
$this->assertSame($validator['title'], $validator->field('title'));
}
/**
* Tests it is possible to check for validation sets for a field using an array inteface
*
* @return void
*/
public function testArrayAccessExists() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertTrue(isset($validator['email']));
$this->assertTrue(isset($validator['title']));
$this->assertFalse(isset($validator['foo']));
}
/**
* Tests it is possible to set validation rules for a field using an array inteface
*
* @return void
*/
public function testArrayAccessSet() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$validator['name'] = $validator->field('title');
$this->assertSame($validator->field('title'), $validator->field('name'));
$validator['name'] = ['alpha' => ['rule' => 'alphanumeric']];
$this->assertEquals($validator->field('email'), $validator->field('email'));
}
/**
* Tests it is possible to unset validation rules
*
* @return void
*/
public function testArrayAccessUset() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertTrue(isset($validator['title']));
unset($validator['title']);
$this->assertFalse(isset($validator['title']));
}
/**
* Tests the countable interface
*
* @return void
*/
public function testCount() {
$validator = new Validator;
$validator
->add('email', 'alpha', ['rule' => 'alphanumeric'])
->add('title', 'cool', ['rule' => 'isCool', 'provider' => 'thing']);
$this->assertCount(2, $validator);
}
}