]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php
a8bdde8a4d3a96339ca21a1d743d7e2875ed8110
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Validator / Constraints / FormValidatorTest.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Tests\Extension\Validator\Constraints;
13
14 use Symfony\Component\Form\FormBuilder;
15 use Symfony\Component\Form\Exception\TransformationFailedException;
16 use Symfony\Component\Form\CallbackTransformer;
17 use Symfony\Component\Form\FormInterface;
18 use Symfony\Component\Form\Extension\Validator\Constraints\Form;
19 use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
20 use Symfony\Component\Form\SubmitButtonBuilder;
21 use Symfony\Component\Validator\Constraint;
22 use Symfony\Component\Validator\Constraints\NotNull;
23 use Symfony\Component\Validator\Constraints\NotBlank;
24
25 /**
26 * @author Bernhard Schussek <bschussek@gmail.com>
27 */
28 class FormValidatorTest extends \PHPUnit_Framework_TestCase
29 {
30 /**
31 * @var \PHPUnit_Framework_MockObject_MockObject
32 */
33 private $dispatcher;
34
35 /**
36 * @var \PHPUnit_Framework_MockObject_MockObject
37 */
38 private $factory;
39
40 /**
41 * @var \PHPUnit_Framework_MockObject_MockObject
42 */
43 private $serverParams;
44
45 /**
46 * @var FormValidator
47 */
48 private $validator;
49
50 protected function setUp()
51 {
52 if (!class_exists('Symfony\Component\EventDispatcher\Event')) {
53 $this->markTestSkipped('The "EventDispatcher" component is not available');
54 }
55
56 $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
57 $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
58 $this->serverParams = $this->getMock(
59 'Symfony\Component\Form\Extension\Validator\Util\ServerParams',
60 array('getNormalizedIniPostMaxSize', 'getContentLength')
61 );
62 $this->validator = new FormValidator($this->serverParams);
63 }
64
65 public function testValidate()
66 {
67 $context = $this->getMockExecutionContext();
68 $object = $this->getMock('\stdClass');
69 $options = array('validation_groups' => array('group1', 'group2'));
70 $form = $this->getBuilder('name', '\stdClass', $options)
71 ->setData($object)
72 ->getForm();
73
74 $context->expects($this->at(0))
75 ->method('validate')
76 ->with($object, 'data', 'group1', true);
77 $context->expects($this->at(1))
78 ->method('validate')
79 ->with($object, 'data', 'group2', true);
80
81 $this->validator->initialize($context);
82 $this->validator->validate($form, new Form());
83 }
84
85 public function testValidateConstraints()
86 {
87 $context = $this->getMockExecutionContext();
88 $object = $this->getMock('\stdClass');
89 $constraint1 = new NotNull(array('groups' => array('group1', 'group2')));
90 $constraint2 = new NotBlank(array('groups' => 'group2'));
91
92 $options = array(
93 'validation_groups' => array('group1', 'group2'),
94 'constraints' => array($constraint1, $constraint2),
95 );
96 $form = $this->getBuilder('name', '\stdClass', $options)
97 ->setData($object)
98 ->getForm();
99
100 // First default constraints
101 $context->expects($this->at(0))
102 ->method('validate')
103 ->with($object, 'data', 'group1', true);
104 $context->expects($this->at(1))
105 ->method('validate')
106 ->with($object, 'data', 'group2', true);
107
108 // Then custom constraints
109 $context->expects($this->at(2))
110 ->method('validateValue')
111 ->with($object, $constraint1, 'data', 'group1');
112 $context->expects($this->at(3))
113 ->method('validateValue')
114 ->with($object, $constraint2, 'data', 'group2');
115
116 $this->validator->initialize($context);
117 $this->validator->validate($form, new Form());
118 }
119
120 public function testDontValidateIfParentWithoutCascadeValidation()
121 {
122 $context = $this->getMockExecutionContext();
123 $object = $this->getMock('\stdClass');
124
125 $parent = $this->getBuilder('parent', null, array('cascade_validation' => false))
126 ->setCompound(true)
127 ->setDataMapper($this->getDataMapper())
128 ->getForm();
129 $options = array('validation_groups' => array('group1', 'group2'));
130 $form = $this->getBuilder('name', '\stdClass', $options)->getForm();
131 $parent->add($form);
132
133 $form->setData($object);
134
135 $context->expects($this->never())
136 ->method('validate');
137
138 $this->validator->initialize($context);
139 $this->validator->validate($form, new Form());
140 }
141
142 public function testValidateConstraintsEvenIfNoCascadeValidation()
143 {
144 $context = $this->getMockExecutionContext();
145 $object = $this->getMock('\stdClass');
146 $constraint1 = new NotNull(array('groups' => array('group1', 'group2')));
147 $constraint2 = new NotBlank(array('groups' => 'group2'));
148
149 $parent = $this->getBuilder('parent', null, array('cascade_validation' => false))
150 ->setCompound(true)
151 ->setDataMapper($this->getDataMapper())
152 ->getForm();
153 $options = array(
154 'validation_groups' => array('group1', 'group2'),
155 'constraints' => array($constraint1, $constraint2),
156 );
157 $form = $this->getBuilder('name', '\stdClass', $options)
158 ->setData($object)
159 ->getForm();
160 $parent->add($form);
161
162 $context->expects($this->at(0))
163 ->method('validateValue')
164 ->with($object, $constraint1, 'data', 'group1');
165 $context->expects($this->at(1))
166 ->method('validateValue')
167 ->with($object, $constraint2, 'data', 'group2');
168
169 $this->validator->initialize($context);
170 $this->validator->validate($form, new Form());
171 }
172
173 public function testDontValidateIfNoValidationGroups()
174 {
175 $context = $this->getMockExecutionContext();
176 $object = $this->getMock('\stdClass');
177
178 $form = $this->getBuilder('name', '\stdClass', array(
179 'validation_groups' => array(),
180 ))
181 ->setData($object)
182 ->getForm();
183
184 $form->setData($object);
185
186 $context->expects($this->never())
187 ->method('validate');
188
189 $this->validator->initialize($context);
190 $this->validator->validate($form, new Form());
191 }
192
193 public function testDontValidateConstraintsIfNoValidationGroups()
194 {
195 $context = $this->getMockExecutionContext();
196 $object = $this->getMock('\stdClass');
197 $constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
198 $constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
199
200 $options = array(
201 'validation_groups' => array(),
202 'constraints' => array($constraint1, $constraint2),
203 );
204 $form = $this->getBuilder('name', '\stdClass', $options)
205 ->setData($object)
206 ->getForm();
207
208 // Launch transformer
209 $form->submit(array());
210
211 $context->expects($this->never())
212 ->method('validate');
213
214 $this->validator->initialize($context);
215 $this->validator->validate($form, new Form());
216 }
217
218 public function testDontValidateIfNotSynchronized()
219 {
220 $context = $this->getMockExecutionContext();
221 $object = $this->getMock('\stdClass');
222
223 $form = $this->getBuilder('name', '\stdClass', array(
224 'invalid_message' => 'invalid_message_key',
225 // Invalid message parameters must be supported, because the
226 // invalid message can be a translation key
227 // see https://github.com/symfony/symfony/issues/5144
228 'invalid_message_parameters' => array('{{ foo }}' => 'bar'),
229 ))
230 ->setData($object)
231 ->addViewTransformer(new CallbackTransformer(
232 function ($data) { return $data; },
233 function () { throw new TransformationFailedException(); }
234 ))
235 ->getForm();
236
237 // Launch transformer
238 $form->submit('foo');
239
240 $context->expects($this->never())
241 ->method('validate');
242
243 $context->expects($this->once())
244 ->method('addViolation')
245 ->with(
246 'invalid_message_key',
247 array('{{ value }}' => 'foo', '{{ foo }}' => 'bar'),
248 'foo'
249 );
250 $context->expects($this->never())
251 ->method('addViolationAt');
252
253 $this->validator->initialize($context);
254 $this->validator->validate($form, new Form());
255 }
256
257 public function testAddInvalidErrorEvenIfNoValidationGroups()
258 {
259 $context = $this->getMockExecutionContext();
260 $object = $this->getMock('\stdClass');
261
262 $form = $this->getBuilder('name', '\stdClass', array(
263 'invalid_message' => 'invalid_message_key',
264 // Invalid message parameters must be supported, because the
265 // invalid message can be a translation key
266 // see https://github.com/symfony/symfony/issues/5144
267 'invalid_message_parameters' => array('{{ foo }}' => 'bar'),
268 'validation_groups' => array(),
269 ))
270 ->setData($object)
271 ->addViewTransformer(new CallbackTransformer(
272 function ($data) { return $data; },
273 function () { throw new TransformationFailedException(); }
274 ))
275 ->getForm();
276
277 // Launch transformer
278 $form->submit('foo');
279
280 $context->expects($this->never())
281 ->method('validate');
282
283 $context->expects($this->once())
284 ->method('addViolation')
285 ->with(
286 'invalid_message_key',
287 array('{{ value }}' => 'foo', '{{ foo }}' => 'bar'),
288 'foo'
289 );
290 $context->expects($this->never())
291 ->method('addViolationAt');
292
293 $this->validator->initialize($context);
294 $this->validator->validate($form, new Form());
295 }
296
297 public function testDontValidateConstraintsIfNotSynchronized()
298 {
299 $context = $this->getMockExecutionContext();
300 $object = $this->getMock('\stdClass');
301 $constraint1 = $this->getMock('Symfony\Component\Validator\Constraint');
302 $constraint2 = $this->getMock('Symfony\Component\Validator\Constraint');
303
304 $options = array(
305 'validation_groups' => array('group1', 'group2'),
306 'constraints' => array($constraint1, $constraint2),
307 );
308 $form = $this->getBuilder('name', '\stdClass', $options)
309 ->setData($object)
310 ->addViewTransformer(new CallbackTransformer(
311 function ($data) { return $data; },
312 function () { throw new TransformationFailedException(); }
313 ))
314 ->getForm();
315
316 // Launch transformer
317 $form->submit(array());
318
319 $context->expects($this->never())
320 ->method('validate');
321
322 $this->validator->initialize($context);
323 $this->validator->validate($form, new Form());
324 }
325
326 // https://github.com/symfony/symfony/issues/4359
327 public function testDontMarkInvalidIfAnyChildIsNotSynchronized()
328 {
329 $context = $this->getMockExecutionContext();
330 $object = $this->getMock('\stdClass');
331
332 $failingTransformer = new CallbackTransformer(
333 function ($data) { return $data; },
334 function () { throw new TransformationFailedException(); }
335 );
336
337 $form = $this->getBuilder('name', '\stdClass')
338 ->setData($object)
339 ->addViewTransformer($failingTransformer)
340 ->setCompound(true)
341 ->setDataMapper($this->getDataMapper())
342 ->add(
343 $this->getBuilder('child')
344 ->addViewTransformer($failingTransformer)
345 )
346 ->getForm();
347
348 // Launch transformer
349 $form->submit(array('child' => 'foo'));
350
351 $context->expects($this->never())
352 ->method('addViolation');
353 $context->expects($this->never())
354 ->method('addViolationAt');
355
356 $this->validator->initialize($context);
357 $this->validator->validate($form, new Form());
358 }
359
360 public function testHandleCallbackValidationGroups()
361 {
362 $context = $this->getMockExecutionContext();
363 $object = $this->getMock('\stdClass');
364 $options = array('validation_groups' => array($this, 'getValidationGroups'));
365 $form = $this->getBuilder('name', '\stdClass', $options)
366 ->setData($object)
367 ->getForm();
368
369 $context->expects($this->at(0))
370 ->method('validate')
371 ->with($object, 'data', 'group1', true);
372 $context->expects($this->at(1))
373 ->method('validate')
374 ->with($object, 'data', 'group2', true);
375
376 $this->validator->initialize($context);
377 $this->validator->validate($form, new Form());
378 }
379
380 public function testDontExecuteFunctionNames()
381 {
382 $context = $this->getMockExecutionContext();
383 $object = $this->getMock('\stdClass');
384 $options = array('validation_groups' => 'header');
385 $form = $this->getBuilder('name', '\stdClass', $options)
386 ->setData($object)
387 ->getForm();
388
389 $context->expects($this->once())
390 ->method('validate')
391 ->with($object, 'data', 'header', true);
392
393 $this->validator->initialize($context);
394 $this->validator->validate($form, new Form());
395 }
396
397 public function testHandleClosureValidationGroups()
398 {
399 $context = $this->getMockExecutionContext();
400 $object = $this->getMock('\stdClass');
401 $options = array('validation_groups' => function(FormInterface $form){
402 return array('group1', 'group2');
403 });
404 $form = $this->getBuilder('name', '\stdClass', $options)
405 ->setData($object)
406 ->getForm();
407
408 $context->expects($this->at(0))
409 ->method('validate')
410 ->with($object, 'data', 'group1', true);
411 $context->expects($this->at(1))
412 ->method('validate')
413 ->with($object, 'data', 'group2', true);
414
415 $this->validator->initialize($context);
416 $this->validator->validate($form, new Form());
417 }
418
419 public function testUseValidationGroupOfClickedButton()
420 {
421 $context = $this->getMockExecutionContext();
422 $object = $this->getMock('\stdClass');
423
424 $parent = $this->getBuilder('parent', null, array('cascade_validation' => true))
425 ->setCompound(true)
426 ->setDataMapper($this->getDataMapper())
427 ->getForm();
428 $form = $this->getForm('name', '\stdClass', array(
429 'validation_groups' => 'form_group',
430 ));
431
432 $parent->add($form);
433 $parent->add($this->getClickedSubmitButton('submit', array(
434 'validation_groups' => 'button_group',
435 )));
436
437 $form->setData($object);
438
439 $context->expects($this->once())
440 ->method('validate')
441 ->with($object, 'data', 'button_group', true);
442
443 $this->validator->initialize($context);
444 $this->validator->validate($form, new Form());
445 }
446
447 public function testDontUseValidationGroupOfUnclickedButton()
448 {
449 $context = $this->getMockExecutionContext();
450 $object = $this->getMock('\stdClass');
451
452 $parent = $this->getBuilder('parent', null, array('cascade_validation' => true))
453 ->setCompound(true)
454 ->setDataMapper($this->getDataMapper())
455 ->getForm();
456 $form = $this->getForm('name', '\stdClass', array(
457 'validation_groups' => 'form_group',
458 ));
459
460 $parent->add($form);
461 $parent->add($this->getSubmitButton('submit', array(
462 'validation_groups' => 'button_group',
463 )));
464
465 $form->setData($object);
466
467 $context->expects($this->once())
468 ->method('validate')
469 ->with($object, 'data', 'form_group', true);
470
471 $this->validator->initialize($context);
472 $this->validator->validate($form, new Form());
473 }
474
475 public function testUseInheritedValidationGroup()
476 {
477 $context = $this->getMockExecutionContext();
478 $object = $this->getMock('\stdClass');
479
480 $parentOptions = array(
481 'validation_groups' => 'group',
482 'cascade_validation' => true,
483 );
484 $parent = $this->getBuilder('parent', null, $parentOptions)
485 ->setCompound(true)
486 ->setDataMapper($this->getDataMapper())
487 ->getForm();
488 $form = $this->getBuilder('name', '\stdClass')->getForm();
489 $parent->add($form);
490
491 $form->setData($object);
492
493 $context->expects($this->once())
494 ->method('validate')
495 ->with($object, 'data', 'group', true);
496
497 $this->validator->initialize($context);
498 $this->validator->validate($form, new Form());
499 }
500
501 public function testUseInheritedCallbackValidationGroup()
502 {
503 $context = $this->getMockExecutionContext();
504 $object = $this->getMock('\stdClass');
505
506 $parentOptions = array(
507 'validation_groups' => array($this, 'getValidationGroups'),
508 'cascade_validation' => true,
509 );
510 $parent = $this->getBuilder('parent', null, $parentOptions)
511 ->setCompound(true)
512 ->setDataMapper($this->getDataMapper())
513 ->getForm();
514 $form = $this->getBuilder('name', '\stdClass')->getForm();
515 $parent->add($form);
516
517 $form->setData($object);
518
519 $context->expects($this->at(0))
520 ->method('validate')
521 ->with($object, 'data', 'group1', true);
522 $context->expects($this->at(1))
523 ->method('validate')
524 ->with($object, 'data', 'group2', true);
525
526 $this->validator->initialize($context);
527 $this->validator->validate($form, new Form());
528 }
529
530 public function testUseInheritedClosureValidationGroup()
531 {
532 $context = $this->getMockExecutionContext();
533 $object = $this->getMock('\stdClass');
534
535 $parentOptions = array(
536 'validation_groups' => function(FormInterface $form){
537 return array('group1', 'group2');
538 },
539 'cascade_validation' => true,
540 );
541 $parent = $this->getBuilder('parent', null, $parentOptions)
542 ->setCompound(true)
543 ->setDataMapper($this->getDataMapper())
544 ->getForm();
545 $form = $this->getBuilder('name', '\stdClass')->getForm();
546 $parent->add($form);
547
548 $form->setData($object);
549
550 $context->expects($this->at(0))
551 ->method('validate')
552 ->with($object, 'data', 'group1', true);
553 $context->expects($this->at(1))
554 ->method('validate')
555 ->with($object, 'data', 'group2', true);
556
557 $this->validator->initialize($context);
558 $this->validator->validate($form, new Form());
559 }
560
561 public function testAppendPropertyPath()
562 {
563 $context = $this->getMockExecutionContext();
564 $object = $this->getMock('\stdClass');
565 $form = $this->getBuilder('name', '\stdClass')
566 ->setData($object)
567 ->getForm();
568
569 $context->expects($this->once())
570 ->method('validate')
571 ->with($object, 'data', 'Default', true);
572
573 $this->validator->initialize($context);
574 $this->validator->validate($form, new Form());
575 }
576
577 public function testDontWalkScalars()
578 {
579 $context = $this->getMockExecutionContext();
580
581 $form = $this->getBuilder()
582 ->setData('scalar')
583 ->getForm();
584
585 $context->expects($this->never())
586 ->method('validate');
587
588 $this->validator->initialize($context);
589 $this->validator->validate($form, new Form());
590 }
591
592 public function testViolationIfExtraData()
593 {
594 $context = $this->getMockExecutionContext();
595
596 $form = $this->getBuilder('parent', null, array('extra_fields_message' => 'Extra!'))
597 ->setCompound(true)
598 ->setDataMapper($this->getDataMapper())
599 ->add($this->getBuilder('child'))
600 ->getForm();
601
602 $form->submit(array('foo' => 'bar'));
603
604 $context->expects($this->once())
605 ->method('addViolation')
606 ->with(
607 'Extra!',
608 array('{{ extra_fields }}' => 'foo'),
609 array('foo' => 'bar')
610 );
611 $context->expects($this->never())
612 ->method('addViolationAt');
613
614 $this->validator->initialize($context);
615 $this->validator->validate($form, new Form());
616 }
617
618 /**
619 * @dataProvider getPostMaxSizeFixtures
620 */
621 public function testPostMaxSizeViolation($contentLength, $iniMax, $nbViolation, array $params = array())
622 {
623 $this->serverParams->expects($this->once())
624 ->method('getContentLength')
625 ->will($this->returnValue($contentLength));
626 $this->serverParams->expects($this->any())
627 ->method('getNormalizedIniPostMaxSize')
628 ->will($this->returnValue($iniMax));
629
630 $context = $this->getMockExecutionContext();
631 $options = array('post_max_size_message' => 'Max {{ max }}!');
632 $form = $this->getBuilder('name', null, $options)->getForm();
633
634 for ($i = 0; $i < $nbViolation; ++$i) {
635 if (0 === $i && count($params) > 0) {
636 $context->expects($this->at($i))
637 ->method('addViolation')
638 ->with($options['post_max_size_message'], $params);
639 } else {
640 $context->expects($this->at($i))
641 ->method('addViolation');
642 }
643 }
644
645 $context->expects($this->never())
646 ->method('addViolationAt');
647
648 $this->validator->initialize($context);
649 $this->validator->validate($form, new Form());
650 }
651
652 public function getPostMaxSizeFixtures()
653 {
654 return array(
655 array(pow(1024, 3) + 1, '1G', 1, array('{{ max }}' => '1G')),
656 array(pow(1024, 3), '1G', 0),
657 array(pow(1024, 2) + 1, '1M', 1, array('{{ max }}' => '1M')),
658 array(pow(1024, 2), '1M', 0),
659 array(1024 + 1, '1K', 1, array('{{ max }}' => '1K')),
660 array(1024, '1K', 0),
661 array(null, '1K', 0),
662 array(1024, '', 0),
663 array(1024, 0, 0),
664 );
665 }
666
667 public function testNoViolationIfNotRoot()
668 {
669 $this->serverParams->expects($this->once())
670 ->method('getContentLength')
671 ->will($this->returnValue(1025));
672 $this->serverParams->expects($this->never())
673 ->method('getNormalizedIniPostMaxSize');
674
675 $context = $this->getMockExecutionContext();
676 $parent = $this->getBuilder()
677 ->setCompound(true)
678 ->setDataMapper($this->getDataMapper())
679 ->getForm();
680 $form = $this->getForm();
681 $parent->add($form);
682
683 $context->expects($this->never())
684 ->method('addViolation');
685 $context->expects($this->never())
686 ->method('addViolationAt');
687
688 $this->validator->initialize($context);
689 $this->validator->validate($form, new Form());
690 }
691
692 /**
693 * Access has to be public, as this method is called via callback array
694 * in {@link testValidateFormDataCanHandleCallbackValidationGroups()}
695 * and {@link testValidateFormDataUsesInheritedCallbackValidationGroup()}
696 */
697 public function getValidationGroups(FormInterface $form)
698 {
699 return array('group1', 'group2');
700 }
701
702 private function getMockExecutionContext()
703 {
704 return $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
705 }
706
707 /**
708 * @param string $name
709 * @param string $dataClass
710 * @param array $options
711 *
712 * @return FormBuilder
713 */
714 private function getBuilder($name = 'name', $dataClass = null, array $options = array())
715 {
716 $options = array_replace(array(
717 'constraints' => array(),
718 'invalid_message_parameters' => array(),
719 ), $options);
720
721 return new FormBuilder($name, $dataClass, $this->dispatcher, $this->factory, $options);
722 }
723
724 private function getForm($name = 'name', $dataClass = null, array $options = array())
725 {
726 return $this->getBuilder($name, $dataClass, $options)->getForm();
727 }
728
729 private function getSubmitButton($name = 'name', array $options = array())
730 {
731 $builder = new SubmitButtonBuilder($name, $options);
732
733 return $builder->getForm();
734 }
735
736 private function getClickedSubmitButton($name = 'name', array $options = array())
737 {
738 return $this->getSubmitButton($name, $options)->submit('');
739 }
740
741 /**
742 * @return \PHPUnit_Framework_MockObject_MockObject
743 */
744 private function getDataMapper()
745 {
746 return $this->getMock('Symfony\Component\Form\DataMapperInterface');
747 }
748 }