/
ValidatorTest.php
996 lines (890 loc) · 31.5 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
<?php
/**
* 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 information, 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 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Validation;
use Cake\TestSuite\TestCase;
use Cake\Validation\ValidationSet;
use Cake\Validation\Validator;
/**
* Tests Validator class
*
*/
class ValidatorTest extends TestCase
{
/**
* Testing you can dynamically add rules to a field
*
* @return void
*/
public function testAddingRulesToField()
{
$validator = new Validator;
$validator->add('title', 'not-blank', ['rule' => 'notBlank']);
$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);
}
/**
* Testing addNested field rules
*
* @return void
*/
public function testAddNestedSingle()
{
$validator = new Validator();
$inner = new Validator();
$inner->add('username', 'not-blank', ['rule' => 'notBlank']);
$this->assertSame($validator, $validator->addNested('user', $inner));
$this->assertCount(1, $validator->field('user'));
}
/**
* Testing addNested connects providers
*
* @return void
*/
public function testAddNestedSingleProviders()
{
$validator = new Validator();
$validator->provider('test', $this);
$inner = new Validator();
$inner->add('username', 'not-blank', ['rule' => function () use ($inner, $validator) {
$this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
return false;
}]);
$validator->addNested('user', $inner);
$result = $validator->errors(['user' => ['username' => 'example']]);
$this->assertNotEmpty($result, 'Validation should fail');
}
/**
* Testing addNestedMany field rules
*
* @return void
*/
public function testAddNestedMany()
{
$validator = new Validator();
$inner = new Validator();
$inner->add('comment', 'not-blank', ['rule' => 'notBlank']);
$this->assertSame($validator, $validator->addNestedMany('comments', $inner));
$this->assertCount(1, $validator->field('comments'));
}
/**
* Testing addNestedMany connects providers
*
* @return void
*/
public function testAddNestedManyProviders()
{
$validator = new Validator();
$validator->provider('test', $this);
$inner = new Validator();
$inner->add('comment', 'not-blank', ['rule' => function () use ($inner, $validator) {
$this->assertSame($validator->providers(), $inner->providers(), 'Providers should match');
return false;
}]);
$validator->addNestedMany('comments', $inner);
$result = $validator->errors(['comments' => [['comment' => 'example']]]);
$this->assertNotEmpty($result, 'Validation should fail');
}
/**
* Tests that calling field will create a default validation set for it
*
* @return void
*/
public function testFieldDefault()
{
$validator = new Validator;
$this->assertFalse($validator->hasField('foo'));
$field = $validator->field('foo');
$this->assertInstanceOf('Cake\Validation\ValidationSet', $field);
$this->assertCount(0, $field);
$this->assertTrue($validator->hasField('foo'));
}
/**
* 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-blank', ['rule' => 'notBlank']);
$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-blank', ['rule' => 'notBlank']);
$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 requirePresence method
*
* @return void
*/
public function testRequirePresence()
{
$validator = new Validator;
$this->assertSame($validator, $validator->requirePresence('title'));
$this->assertTrue($validator->field('title')->isPresenceRequired());
$validator->requirePresence('title', false);
$this->assertFalse($validator->field('title')->isPresenceRequired());
$validator->requirePresence('title', 'create');
$this->assertEquals('create', $validator->field('title')->isPresenceRequired());
$validator->requirePresence('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->requirePresence('title'));
$this->assertTrue($validator->isPresenceRequired('title', true));
$this->assertTrue($validator->isPresenceRequired('title', false));
$validator->requirePresence('title', false);
$this->assertFalse($validator->isPresenceRequired('title', true));
$this->assertFalse($validator->isPresenceRequired('title', false));
$validator->requirePresence('title', 'create');
$this->assertTrue($validator->isPresenceRequired('title', true));
$this->assertFalse($validator->isPresenceRequired('title', false));
$validator->requirePresence('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->requirePresence('title');
$errors = $validator->errors(['foo' => 'something']);
$expected = ['title' => ['_required' => 'This field is required']];
$this->assertEquals($expected, $errors);
$this->assertEmpty($validator->errors(['title' => 'bar']));
$validator->requirePresence('title', false);
$this->assertEmpty($validator->errors(['foo' => 'bar']));
}
/**
* Test that errors() can work with nested data.
*
* @return void
*/
public function testErrorsWithNestedFields()
{
$validator = new Validator();
$user = new Validator();
$user->add('username', 'letter', ['rule' => 'alphanumeric']);
$comments = new Validator();
$comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
$validator->addNested('user', $user);
$validator->addNestedMany('comments', $comments);
$data = [
'user' => [
'username' => 'is wrong'
],
'comments' => [
['comment' => 'is wrong']
]
];
$errors = $validator->errors($data);
$expected = [
'user' => [
'username' => ['letter' => 'The provided value is invalid']
],
'comments' => [
0 => ['comment' => ['letter' => 'The provided value is invalid']]
]
];
$this->assertEquals($expected, $errors);
}
/**
* Test nested fields with many, but invalid data.
*
* @return void
*/
public function testErrorsWithNestedSingleInvalidType()
{
$validator = new Validator();
$user = new Validator();
$user->add('user', 'letter', ['rule' => 'alphanumeric']);
$validator->addNested('user', $user);
$data = [
'user' => 'a string',
];
$errors = $validator->errors($data);
$expected = [
'user' => ['_nested' => 'The provided value is invalid'],
];
$this->assertEquals($expected, $errors);
}
/**
* Test nested fields with many, but invalid data.
*
* @return void
*/
public function testErrorsWithNestedManyInvalidType()
{
$validator = new Validator();
$comments = new Validator();
$comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
$validator->addNestedMany('comments', $comments);
$data = [
'comments' => 'a string',
];
$errors = $validator->errors($data);
$expected = [
'comments' => ['_nested' => 'The provided value is invalid'],
];
$this->assertEquals($expected, $errors);
}
/**
* Test nested fields with many, but invalid data.
*
* @return void
*/
public function testErrorsWithNestedManySomeInvalid()
{
$validator = new Validator();
$comments = new Validator();
$comments->add('comment', 'letter', ['rule' => 'alphanumeric']);
$validator->addNestedMany('comments', $comments);
$data = [
'comments' => [
'a string',
['comment' => 'letters'],
['comment' => 'more invalid']
]
];
$errors = $validator->errors($data);
$expected = [
'comments' => [
'_nested' => 'The provided value is invalid',
],
];
$this->assertEquals($expected, $errors);
}
/**
* Tests custom error messages generated when a field presence is required
*
* @return void
*/
public function testCustomErrorsWithPresenceRequired()
{
$validator = new Validator;
$validator->requirePresence('title', true, 'Custom message');
$errors = $validator->errors(['foo' => 'something']);
$expected = ['title' => ['_required' => 'Custom message']];
$this->assertEquals($expected, $errors);
}
/**
* 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', 'create');
$this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
$validator->allowEmpty('title', 'update');
$this->assertEquals('update', $validator->field('title')->isEmptyAllowed());
}
/**
* Tests the allowEmpty method with date/time fields.
*
* @return void
*/
public function testAllowEmptyDateTime()
{
$validator = new Validator;
$validator->allowEmpty('created')
->add('created', 'date', ['rule' => 'date']);
$data = [
'created' => [
'year' => '',
'month' => '',
'day' => ''
]
];
$result = $validator->errors($data);
$this->assertEmpty($result, 'No errors on empty date');
$data = [
'created' => [
'year' => '',
'month' => '',
'day' => '',
'hour' => '',
'minute' => '',
'second' => '',
'meridian' => '',
]
];
$result = $validator->errors($data);
$this->assertEmpty($result, 'No errors on empty datetime');
$data = [
'created' => [
'hour' => '',
'minute' => '',
'meridian' => '',
]
];
$result = $validator->errors($data);
$this->assertEmpty($result, 'No errors on empty time');
}
/**
* Tests the allowEmpty method with file fields.
*
* @return void
*/
public function testAllowEmptyFileFields()
{
$validator = new Validator;
$validator->allowEmpty('picture')
->add('picture', 'file', ['rule' => 'uploadedFile']);
$data = [
'picture' => [
'name' => '',
'type' => '',
'tmp_name' => '',
'error' => UPLOAD_ERR_NO_FILE,
]
];
$result = $validator->errors($data);
$this->assertEmpty($result, 'No errors on empty date');
$data = [
'picture' => [
'name' => 'fake.png',
'type' => '',
'tmp_name' => '',
'error' => UPLOAD_ERR_OK,
]
];
$result = $validator->errors($data);
$this->assertNotEmpty($result, 'Invalid file should be caught still.');
}
/**
* Test the notEmpty() method.
*
* @return void
*/
public function testNotEmpty()
{
$validator = new Validator;
$validator->notEmpty('title');
$this->assertFalse($validator->field('title')->isEmptyAllowed());
$validator->allowEmpty('title');
$this->assertTrue($validator->field('title')->isEmptyAllowed());
}
/**
* Test the notEmpty() method.
*
* @return void
*/
public function testNotEmptyModes()
{
$validator = new Validator;
$validator->notEmpty('title', 'Need a title', 'create');
$this->assertFalse($validator->isEmptyAllowed('title', true));
$this->assertTrue($validator->isEmptyAllowed('title', false));
$validator->notEmpty('title', 'Need a title', 'update');
$this->assertTrue($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
$validator->notEmpty('title', 'Need a title');
$this->assertFalse($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
$validator->notEmpty('title');
$this->assertFalse($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
}
/**
* Test interactions between notEmpty() and isAllowed().
*
* @return void
*/
public function testNotEmptyAndIsAllowed()
{
$validator = new Validator;
$validator->allowEmpty('title')
->notEmpty('title', 'Need it', 'update');
$this->assertTrue($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
$validator->allowEmpty('title')
->notEmpty('title');
$this->assertFalse($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
$validator->notEmpty('title')
->allowEmpty('title', 'create');
$this->assertTrue($validator->isEmptyAllowed('title', true));
$this->assertFalse($validator->isEmptyAllowed('title', false));
}
/**
* Tests the allowEmpty method when passing a callback
*
* @return void
*/
public function testAllowEmptyCallback()
{
$validator = new Validator;
$allow = true;
$validator->allowEmpty('title', function ($context) use (&$allow) {
$this->assertEquals([], $context['data']);
$this->assertEquals([], $context['providers']);
$this->assertTrue($context['newRecord']);
return $allow;
});
$this->assertTrue($validator->isEmptyAllowed('title', true));
$allow = false;
$this->assertFalse($validator->isEmptyAllowed('title', true));
}
/**
* Tests the notEmpty method when passing a callback
*
* @return void
*/
public function testNotEmptyCallback()
{
$validator = new Validator;
$prevent = true;
$validator->notEmpty('title', 'error message', function ($context) use (&$prevent) {
$this->assertEquals([], $context['data']);
$this->assertEquals([], $context['providers']);
$this->assertFalse($context['newRecord']);
return $prevent;
});
$this->assertFalse($validator->isEmptyAllowed('title', false));
$prevent = false;
$this->assertTrue($validator->isEmptyAllowed('title', false));
}
/**
* 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->notEmpty('title');
$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->notEmpty('title');
$errors = $validator->errors(['title' => '']);
$expected = ['title' => ['_empty' => 'This field cannot be left empty']];
$this->assertEquals($expected, $errors);
$errors = $validator->errors(['title' => []]);
$expected = ['title' => ['_empty' => 'This field cannot be left empty']];
$this->assertEquals($expected, $errors);
$errors = $validator->errors(['title' => null]);
$expected = ['title' => ['_empty' => '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 custom error mesages generated when a field is not allowed to be empty
*
* @return void
*/
public function testCustomErrorsWithEmptyNotAllowed()
{
$validator = new Validator;
$validator->notEmpty('title', 'Custom message');
$errors = $validator->errors(['title' => '']);
$expected = ['title' => ['_empty' => 'Custom message']];
$this->assertEquals($expected, $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.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(new \Cake\Validation\RulesProvider, $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', 'notBlank', ['rule' => 'notBlank'])
->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, $context) use ($thing) {
$this->assertEquals('bar', $data);
$expected = [
'default' => new \Cake\Validation\RulesProvider,
'thing' => $thing
];
$expected = [
'newRecord' => true,
'providers' => $expected,
'data' => [
'email' => '!',
'title' => 'bar'
],
'field' => 'title'
];
$this->assertEquals($expected, $context);
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, $context) use ($thing) {
$this->assertEquals('bar', $data);
$this->assertEquals('and', $a);
$this->assertEquals('awesome', $b);
$expected = [
'default' => new \Cake\Validation\RulesProvider,
'thing' => $thing
];
$expected = [
'newRecord' => true,
'providers' => $expected,
'data' => [
'email' => '!',
'title' => 'bar'
],
'field' => 'title'
];
$this->assertEquals($expected, $context);
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);
}
/**
* Tests adding rules via alternative syntax
*
* @return void
*/
public function testAddMulitple()
{
$validator = new Validator;
$validator->add('title', [
'notBlank' => [
'rule' => 'notBlank'
],
'length' => [
'rule' => ['minLength', 10],
'message' => 'Titles need to be at least 10 characters long'
]
]);
$set = $validator->field('title');
$this->assertInstanceOf('Cake\Validation\ValidationSet', $set);
$this->assertCount(2, $set);
}
/**
* Integration test for compareWith validator.
*
* @return void
*/
public function testCompareWithIntegration()
{
$validator = new Validator;
$validator->add('password', [
'compare' => [
'rule' => ['compareWith', 'password_compare']
],
]);
$data = [
'password' => 'test',
'password_compare' => 'not the same'
];
$this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
}
/**
* Test debugInfo helper method.
*
* @return void
*/
public function testDebugInfo()
{
$validator = new Validator();
$validator->provider('test', $this);
$validator->add('title', 'not-empty', ['rule' => 'notEmpty']);
$validator->requirePresence('body');
$validator->allowEmpty('published');
$result = $validator->__debugInfo();
$expected = [
'_providers' => ['test'],
'_fields' => [
'title' => [
'isPresenceRequired' => false,
'isEmptyAllowed' => false,
'rules' => ['not-empty'],
],
'body' => [
'isPresenceRequired' => true,
'isEmptyAllowed' => false,
'rules' => [],
],
'published' => [
'isPresenceRequired' => false,
'isEmptyAllowed' => true,
'rules' => [],
],
],
'_presenceMessages' => [],
'_allowEmptyMessages' => [],
'_useI18n' => true,
];
$this->assertEquals($expected, $result);
}
}