This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
FormTest.php
1428 lines (1235 loc) · 45.8 KB
/
FormTest.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
997
998
999
1000
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Form
*/
namespace ZendTest\Form;
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Zend\Form\Element;
use Zend\Form\Factory;
use Zend\Form\Fieldset;
use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFilterFactory;
use Zend\Stdlib\Hydrator;
use ZendTest\Form\TestAsset\Entity;
class FormTest extends TestCase
{
/**
* @var Form
*/
protected $form;
public function setUp()
{
$this->form = new Form();
}
public function getComposedEntity()
{
$address = new TestAsset\Entity\Address();
$address->setStreet('1 Rue des Champs Elysées');
$city = new TestAsset\Entity\City();
$city->setName('Paris');
$city->setZipCode('75008');
$country = new TestAsset\Entity\Country();
$country->setName('France');
$country->setContinent('Europe');
$city->setCountry($country);
$address->setCity($city);
return $address;
}
public function getOneToManyEntity()
{
$product = new TestAsset\Entity\Product();
$product->setName('Chair');
$product->setPrice(10);
$firstCategory = new TestAsset\Entity\Category();
$firstCategory->setName('Office');
$secondCategory = new TestAsset\Entity\Category();
$secondCategory->setName('Armchair');
$product->setCategories(array($firstCategory, $secondCategory));
return $product;
}
public function populateHydratorStrategyForm()
{
$this->form->add(new Element('entities'));
}
public function populateForm()
{
$this->form->add(new Element('foo'));
$this->form->add(new Element('bar'));
$fieldset = new Fieldset('foobar');
$fieldset->add(new Element('foo'));
$fieldset->add(new Element('bar'));
$this->form->add($fieldset);
$inputFilterFactory = new InputFilterFactory();
$inputFilter = $inputFilterFactory->createInputFilter(array(
'foo' => array(
'name' => 'foo',
'required' => false,
'validators' => array(
array(
'name' => 'not_empty',
),
array(
'name' => 'string_length',
'options' => array(
'min' => 3,
'max' => 5,
),
),
),
),
'bar' => array(
'allow_empty' => true,
'filters' => array(
array(
'name' => 'string_trim',
),
array(
'name' => 'string_to_lower',
'options' => array(
'encoding' => 'ISO-8859-1',
),
),
),
),
'foobar' => array(
'type' => 'Zend\InputFilter\InputFilter',
'foo' => array(
'name' => 'foo',
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
),
array(
'name' => 'string_length',
'options' => array(
'min' => 3,
'max' => 5,
),
),
),
),
'bar' => array(
'allow_empty' => true,
'filters' => array(
array(
'name' => 'string_trim',
),
array(
'name' => 'string_to_lower',
'options' => array(
'encoding' => 'ISO-8859-1',
),
),
),
),
),
));
$this->form->setInputFilter($inputFilter);
}
public function testInputFilterPresentByDefault()
{
$this->assertNotNull($this->form->getInputFilter());
}
public function testCanComposeAnInputFilter()
{
$filter = new InputFilter();
$this->form->setInputFilter($filter);
$this->assertSame($filter, $this->form->getInputFilter());
}
public function testDefaultNonRequiredInputFilterIsSet()
{
$this->form->add(new Element('foo'));
$inputFilter = $this->form->getInputFilter();
$fooInput = $inputFilter->get('foo');
$this->assertFalse($fooInput->isRequired());
}
public function testInputProviderInterfaceAddsInputFilters()
{
$form = new TestAsset\InputFilterProvider();
$inputFilter = $form->getInputFilter();
$fooInput = $inputFilter->get('foo');
$this->assertTrue($fooInput->isRequired());
}
public function testCallingIsValidRaisesExceptionIfNoDataSet()
{
$this->setExpectedException('Zend\Form\Exception\DomainException');
$this->form->isValid();
}
public function testHasValidatedFlag()
{
$form = new TestAsset\NewProductForm();
$this->assertFalse($form->hasValidated());
$form->setData(array());
$form->isValid();
$this->assertTrue($form->hasValidated());
}
public function testValidatesEntireDataSetByDefault()
{
$this->populateForm();
$invalidSet = array(
'foo' => 'a',
'bar' => 'always valid',
'foobar' => array(
'foo' => 'a',
'bar' => 'always valid',
),
);
$this->form->setData($invalidSet);
$this->assertFalse($this->form->isValid());
$validSet = array(
'foo' => 'abcde',
'bar' => 'always valid',
'foobar' => array(
'foo' => 'abcde',
'bar' => 'always valid',
),
);
$this->form->setData($validSet);
$this->assertTrue($this->form->isValid());
}
public function testSpecifyingValidationGroupForcesPartialValidation()
{
$this->populateForm();
$invalidSet = array(
'foo' => 'a',
);
$this->form->setValidationGroup('foo');
$this->form->setData($invalidSet);
$this->assertFalse($this->form->isValid());
$validSet = array(
'foo' => 'abcde',
);
$this->form->setData($validSet);
$this->assertTrue($this->form->isValid());
}
public function testSpecifyingValidationGroupForNestedFieldsetsForcesPartialValidation()
{
$form = new TestAsset\NewProductForm();
$form->setData(array(
'product' => array(
'name' => 'Chair'
)
));
$this->assertFalse($form->isValid());
$form->setValidationGroup(array(
'product' => array(
'name'
)
));
$this->assertTrue($form->isValid());
}
public function testSettingValidateAllFlagAfterPartialValidationForcesFullValidation()
{
$this->populateForm();
$this->form->setValidationGroup('foo');
$validSet = array(
'foo' => 'abcde',
);
$this->form->setData($validSet);
$this->form->setValidationGroup(Form::VALIDATE_ALL);
$this->assertFalse($this->form->isValid());
$messages = $this->form->getMessages();
$this->assertArrayHasKey('foobar', $messages, var_export($messages, 1));
}
public function testSetValidationGroupWithNoArgumentsRaisesException()
{
$this->setExpectedException('Zend\Form\Exception\InvalidArgumentException');
$this->form->setValidationGroup();
}
public function testCallingGetDataPriorToValidationRaisesException()
{
$this->setExpectedException('Zend\Form\Exception\DomainException');
$this->form->getData();
}
public function testAttemptingToValidateWithNoInputFilterAttachedRaisesException()
{
$this->setExpectedException('Zend\Form\Exception\DomainException');
$this->form->isValid();
}
public function testCanRetrieveDataWithoutErrorsFollowingValidation()
{
$this->populateForm();
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid',
),
);
$this->form->setData($validSet);
$this->form->isValid();
$data = $this->form->getData();
$this->assertInternalType('array', $data);
}
/**
* @group ZF2-336
*/
public function testCanAddFileEnctypeAttribute()
{
$file = new Element\File('file_resource');
$file
->setOptions(array())
->setLabel('File');
$this->form->add($file);
$this->form->prepare();
$enctype = $this->form->getAttribute('enctype');
$this->assertNotEmpty($enctype);
$this->assertEquals($enctype, 'multipart/form-data');
}
/**
* @group ZF2-336
*/
public function testCanAddFileEnctypeFromCollectionAttribute()
{
$file = new Element\File('file_resource');
$file
->setOptions(array())
->setLabel('File');
$fileCollection = new Element\Collection('collection');
$fileCollection->setOptions(array(
'count' => 2,
'allow_add' => false,
'allow_remove' => false,
'target_element' => $file,
));
$this->form->add($fileCollection);
$this->form->prepare();
$enctype = $this->form->getAttribute('enctype');
$this->assertNotEmpty($enctype);
$this->assertEquals($enctype, 'multipart/form-data');
}
public function testCallingGetDataReturnsNormalizedDataByDefault()
{
$this->populateForm();
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid',
),
);
$this->form->setData($validSet);
$this->form->isValid();
$data = $this->form->getData();
$expected = array(
'foo' => 'abcde',
'bar' => 'always valid',
'foobar' => array(
'foo' => 'abcde',
'bar' => 'always valid',
),
);
$this->assertEquals($expected, $data);
}
public function testAllowsReturningRawValuesViaGetData()
{
$this->populateForm();
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid',
),
);
$this->form->setData($validSet);
$this->form->isValid();
$data = $this->form->getData(Form::VALUES_RAW);
$this->assertEquals($validSet, $data);
}
public function testGetDataReturnsBoundModel()
{
$model = new stdClass;
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->populateForm();
$this->form->setData(array());
$this->form->bind($model);
$this->form->isValid();
$data = $this->form->getData();
$this->assertSame($model, $data);
}
public function testGetDataCanReturnValuesAsArrayWhenModelIsBound()
{
$model = new stdClass;
$validSet = array(
'foo' => 'abcde',
'bar' => 'always valid',
'foobar' => array(
'foo' => 'abcde',
'bar' => 'always valid',
),
);
$this->populateForm();
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->form->bind($model);
$this->form->setData($validSet);
$this->form->isValid();
$data = $this->form->getData(Form::VALUES_AS_ARRAY);
$this->assertEquals($validSet, $data);
}
public function testValuesBoundToModelAreNormalizedByDefault()
{
$model = new stdClass;
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' always VALID',
),
);
$this->populateForm();
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->form->bind($model);
$this->form->setData($validSet);
$this->form->isValid();
$this->assertObjectHasAttribute('foo', $model);
$this->assertEquals($validSet['foo'], $model->foo);
$this->assertObjectHasAttribute('bar', $model);
$this->assertEquals('always valid', $model->bar);
$this->assertObjectHasAttribute('foobar', $model);
$this->assertEquals(array(
'foo' => 'abcde',
'bar' => 'always valid',
), $model->foobar);
}
public function testCanBindRawValuesToModel()
{
$model = new stdClass;
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' always VALID',
),
);
$this->populateForm();
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->form->bind($model, Form::VALUES_RAW);
$this->form->setData($validSet);
$this->form->isValid();
$this->assertObjectHasAttribute('foo', $model);
$this->assertEquals($validSet['foo'], $model->foo);
$this->assertObjectHasAttribute('bar', $model);
$this->assertEquals(' ALWAYS valid ', $model->bar);
$this->assertObjectHasAttribute('foobar', $model);
$this->assertEquals(array(
'foo' => 'abcde',
'bar' => ' always VALID',
), $model->foobar);
}
public function testGetDataReturnsSubsetOfDataWhenValidationGroupSet()
{
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' always VALID',
),
);
$this->populateForm();
$this->form->setValidationGroup('foo');
$this->form->setData($validSet);
$this->form->isValid();
$data = $this->form->getData();
$this->assertInternalType('array', $data);
$this->assertEquals(1, count($data));
$this->assertArrayHasKey('foo', $data);
$this->assertEquals('abcde', $data['foo']);
}
public function testSettingValidationGroupBindsOnlyThoseValuesToModel()
{
$model = new stdClass;
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' always VALID',
),
);
$this->populateForm();
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->form->bind($model);
$this->form->setData($validSet);
$this->form->setValidationGroup('foo');
$this->form->isValid();
$this->assertObjectHasAttribute('foo', $model);
$this->assertEquals('abcde', $model->foo);
$this->assertObjectNotHasAttribute('bar', $model);
$this->assertObjectNotHasAttribute('foobar', $model);
}
public function testCanBindModelsToArraySerializableObjects()
{
$model = new TestAsset\Model();
$validSet = array(
'foo' => 'abcde',
'bar' => 'always valid',
'foobar' => array(
'foo' => 'abcde',
'bar' => 'always valid',
),
);
$this->populateForm();
$this->form->setHydrator(new Hydrator\ArraySerializable());
$this->form->bind($model);
$this->form->setData($validSet);
$this->form->isValid();
$this->assertEquals('abcde', $model->foo);
$this->assertEquals('always valid', $model->bar);
$this->assertEquals(array(
'foo' => 'abcde',
'bar' => 'always valid',
), $model->foobar);
}
public function testSetsInputFilterToFilterFromBoundModelIfModelImplementsInputLocatorAware()
{
$model = new TestAsset\ValidatingModel();
$model->setInputFilter(new InputFilter());
$this->populateForm();
$this->form->bind($model);
$this->assertSame($model->getInputFilter(), $this->form->getInputFilter());
}
public function testSettingDataShouldSetElementValues()
{
$this->populateForm();
$data = array(
'foo' => 'abcde',
'bar' => 'always valid',
'foobar' => array(
'foo' => 'abcde',
'bar' => 'always valid',
),
);
$this->form->setData($data);
$fieldset = $this->form->get('foobar');
foreach (array('foo', 'bar') as $name) {
$element = $this->form->get($name);
$this->assertEquals($data[$name], $element->getValue());
$element = $fieldset->get($name);
$this->assertEquals($data[$name], $element->getValue());
}
}
public function testElementValuesArePopulatedFollowingBind()
{
$this->populateForm();
$object = new stdClass();
$object->foo = 'foobar';
$object->bar = 'barbaz';
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->form->bind($object);
$foo = $this->form->get('foo');
$this->assertEquals('foobar', $foo->getValue());
$bar = $this->form->get('bar');
$this->assertEquals('barbaz', $bar->getValue());
}
public function testUsesBoundObjectAsDataSourceWhenNoDataSet()
{
$this->populateForm();
$object = new stdClass();
$object->foo = 'foos';
$object->bar = 'bar';
$object->foobar = array(
'foo' => 'foos',
'bar' => 'bar',
);
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->form->bind($object);
$this->assertTrue($this->form->isValid());
}
public function testBindOnValidateIsTrueByDefault()
{
$this->assertTrue($this->form->bindOnValidate());
}
public function testCanDisableBindOnValidateFunctionality()
{
$this->form->setBindOnValidate(false);
$this->assertFalse($this->form->bindOnValidate());
}
public function testCallingBindValuesWhenBindOnValidateIsDisabledPopulatesBoundObject()
{
$model = new stdClass;
$validSet = array(
'foo' => 'abcde',
'bar' => ' ALWAYS valid ',
'foobar' => array(
'foo' => 'abcde',
'bar' => ' always VALID',
),
);
$this->populateForm();
$this->form->setHydrator(new Hydrator\ObjectProperty());
$this->form->setBindOnValidate(false);
$this->form->bind($model);
$this->form->setData($validSet);
$this->form->isValid();
$this->assertObjectNotHasAttribute('foo', $model);
$this->assertObjectNotHasAttribute('bar', $model);
$this->assertObjectNotHasAttribute('foobar', $model);
$this->form->bindValues();
$this->assertObjectHasAttribute('foo', $model);
$this->assertEquals($validSet['foo'], $model->foo);
$this->assertObjectHasAttribute('bar', $model);
$this->assertEquals('always valid', $model->bar);
$this->assertObjectHasAttribute('foobar', $model);
$this->assertEquals(array(
'foo' => 'abcde',
'bar' => 'always valid',
), $model->foobar);
}
public function testHasFactoryComposedByDefault()
{
$factory = $this->form->getFormFactory();
$this->assertInstanceOf('Zend\Form\Factory', $factory);
}
public function testCanComposeFactory()
{
$factory = new Factory();
$this->form->setFormFactory($factory);
$this->assertSame($factory, $this->form->getFormFactory());
}
public function testCanAddElementsUsingSpecs()
{
$this->form->add(array(
'name' => 'foo',
'attributes' => array(
'type' => 'text',
'class' => 'foo-class',
'data-js-type' => 'my.form.text',
),
));
$this->assertTrue($this->form->has('foo'));
$element = $this->form->get('foo');
$this->assertInstanceOf('Zend\Form\ElementInterface', $element);
$this->assertEquals('foo', $element->getName());
$this->assertEquals('text', $element->getAttribute('type'));
$this->assertEquals('foo-class', $element->getAttribute('class'));
$this->assertEquals('my.form.text', $element->getAttribute('data-js-type'));
}
public function testCanAddFieldsetsUsingSpecs()
{
$this->form->add(array(
'type' => 'Zend\Form\Fieldset',
'name' => 'foo',
'attributes' => array(
'type' => 'fieldset',
'class' => 'foo-class',
'data-js-type' => 'my.form.fieldset',
),
));
$this->assertTrue($this->form->has('foo'));
$fieldset = $this->form->get('foo');
$this->assertInstanceOf('Zend\Form\FieldsetInterface', $fieldset);
$this->assertEquals('foo', $fieldset->getName());
$this->assertEquals('fieldset', $fieldset->getAttribute('type'));
$this->assertEquals('foo-class', $fieldset->getAttribute('class'));
$this->assertEquals('my.form.fieldset', $fieldset->getAttribute('data-js-type'));
}
public function testFormAsFieldsetWillBindValuesToObject()
{
$parentForm = new Form('parent');
$parentFormObject = new \ArrayObject(array('parentId' => null));
$parentFormElement = new Element('parentId');
$parentForm->setObject($parentFormObject);
$parentForm->add($parentFormElement);
$childForm = new Form('child');
$childFormObject = new \ArrayObject(array('childId' => null));
$childFormElement = new Element('childId');
$childForm->setObject($childFormObject);
$childForm->add($childFormElement);
$parentForm->add($childForm);
$data = array(
'parentId' => 'mpinkston was here',
'child' => array(
'childId' => 'testing 123'
)
);
$parentForm->setData($data);
$this->assertTrue($parentForm->isValid());
$this->assertEquals($data['parentId'], $parentFormObject['parentId']);
$this->assertEquals($data['child']['childId'], $childFormObject['childId']);
}
public function testWillUseInputSpecificationFromElementInInputFilterIfNoMatchingInputFound()
{
$element = new TestAsset\ElementWithFilter('foo');
$filter = new InputFilter();
$this->form->setInputFilter($filter);
$this->form->add($element);
$test = $this->form->getInputFilter();
$this->assertSame($filter, $test);
$this->assertTrue($filter->has('foo'));
$input = $filter->get('foo');
$filters = $input->getFilterChain();
$this->assertEquals(1, count($filters));
$validators = $input->getValidatorChain();
$this->assertEquals(2, count($validators));
$this->assertTrue($input->isRequired());
$this->assertEquals('foo', $input->getName());
}
public function testWillUseInputFilterSpecificationFromFieldsetInInputFilterIfNoMatchingInputFilterFound()
{
$fieldset = new TestAsset\FieldsetWithInputFilter('set');
$filter = new InputFilter();
$this->form->setInputFilter($filter);
$this->form->add($fieldset);
$test = $this->form->getInputFilter();
$this->assertSame($filter, $test);
$this->assertTrue($filter->has('set'));
$input = $filter->get('set');
$this->assertInstanceOf('Zend\InputFilter\InputFilterInterface', $input);
$this->assertEquals(2, count($input));
$this->assertTrue($input->has('foo'));
$this->assertTrue($input->has('bar'));
}
public function testWillPopulateSubInputFilterFromInputSpecificationsOnFieldsetElements()
{
$element = new TestAsset\ElementWithFilter('foo');
$fieldset = new Fieldset('set');
$filter = new InputFilter();
$fieldsetFilter = new InputFilter();
$fieldset->add($element);
$filter->add($fieldsetFilter, 'set');
$this->form->setInputFilter($filter);
$this->form->add($fieldset);
$test = $this->form->getInputFilter();
$this->assertSame($filter, $test);
$test = $filter->get('set');
$this->assertSame($fieldsetFilter, $test);
$this->assertEquals(1, count($fieldsetFilter));
$this->assertTrue($fieldsetFilter->has('foo'));
$input = $fieldsetFilter->get('foo');
$this->assertInstanceOf('Zend\InputFilter\InputInterface', $input);
$filters = $input->getFilterChain();
$this->assertEquals(1, count($filters));
$validators = $input->getValidatorChain();
$this->assertEquals(2, count($validators));
$this->assertTrue($input->isRequired());
$this->assertEquals('foo', $input->getName());
}
public function testWillUseFormInputFilterOverrideOverInputSpecificationFromElement()
{
$element = new TestAsset\ElementWithFilter('foo');
$filter = new InputFilter();
$filterFactory = new InputFilterFactory();
$filter = $filterFactory->createInputFilter(array(
'foo' => array(
'name' => 'foo',
'required' => false,
),
));
$this->form->setPreferFormInputFilter(true);
$this->form->setInputFilter($filter);
$this->form->add($element);
$test = $this->form->getInputFilter();
$this->assertSame($filter, $test);
$this->assertTrue($filter->has('foo'));
$input = $filter->get('foo');
$filters = $input->getFilterChain();
$this->assertEquals(0, count($filters));
$validators = $input->getValidatorChain();
$this->assertEquals(0, count($validators));
$this->assertFalse($input->isRequired());
$this->assertEquals('foo', $input->getName());
}
public function testDisablingUseInputFilterDefaultsFlagDisablesInputFilterScanning()
{
$element = new TestAsset\ElementWithFilter('foo');
$fieldset = new Fieldset('set');
$filter = new InputFilter();
$fieldsetFilter = new InputFilter();
$fieldset->add($element);
$filter->add($fieldsetFilter, 'set');
$this->form->setInputFilter($filter);
$this->form->add($fieldset);
$this->form->setUseInputFilterDefaults(false);
$test = $this->form->getInputFilter();
$this->assertSame($filter, $test);
$this->assertSame($fieldsetFilter, $test->get('set'));
$this->assertEquals(0, count($fieldsetFilter));
}
public function testCallingPrepareEnsuresInputFilterRetrievesDefaults()
{
$element = new TestAsset\ElementWithFilter('foo');
$filter = new InputFilter();
$this->form->setInputFilter($filter);
$this->form->add($element);
$this->form->prepare();
$this->assertTrue($filter->has('foo'));
$input = $filter->get('foo');
$filters = $input->getFilterChain();
$this->assertEquals(1, count($filters));
$validators = $input->getValidatorChain();
$this->assertEquals(2, count($validators));
$this->assertTrue($input->isRequired());
$this->assertEquals('foo', $input->getName());
// Issue #2586 Ensure default filters aren't added twice
$filter = $this->form->getInputFilter();
$this->assertTrue($filter->has('foo'));
$input = $filter->get('foo');
$filters = $input->getFilterChain();
$this->assertEquals(1, count($filters));
$validators = $input->getValidatorChain();
$this->assertEquals(2, count($validators));
}
public function testCanProperlyPrepareNestedFieldsets()
{
$this->form->add(array(
'name' => 'foo',
'attributes' => array(
'type' => 'text'
)
));
$this->form->add(array(
'type' => 'ZendTest\Form\TestAsset\BasicFieldset'
));
$this->form->prepare();
$this->assertEquals('foo', $this->form->get('foo')->getName());
$basicFieldset = $this->form->get('basic_fieldset');
$this->assertEquals('basic_fieldset[field]', $basicFieldset->get('field')->getName());
$nestedFieldset = $basicFieldset->get('nested_fieldset');
$this->assertEquals('basic_fieldset[nested_fieldset][anotherField]', $nestedFieldset->get('anotherField')
->getName());
}
public function testCanCorrectlyExtractDataFromComposedEntities()
{
$address = $this->getComposedEntity();
$form = new TestAsset\CreateAddressForm();
$form->bind($address);
$form->setBindOnValidate(false);
$this->assertEquals(true, $form->isValid());
$this->assertEquals($address, $form->getData());
}
public function testCanCorrectlyPopulateDataToComposedEntities()
{
$address = $this->getComposedEntity();
$emptyAddress = new TestAsset\Entity\Address();
$form = new TestAsset\CreateAddressForm();
$form->bind($emptyAddress);
$data = array(
'address' => array(
'street' => '1 Rue des Champs Elysées',
'city' => array(
'name' => 'Paris',
'zipCode' => '75008',
'country' => array(
'name' => 'France',
'continent' => 'Europe'
)
)
)
);
$form->setData($data);
$this->assertEquals(true, $form->isValid());
$this->assertEquals($address, $emptyAddress, var_export($address, 1) . "\n\n" . var_export($emptyAddress, 1));
}
public function testCanCorrectlyExtractDataFromOneToManyRelationship()
{
$product = $this->getOneToManyEntity();
$form = new TestAsset\NewProductForm();
$form->bind($product);
$form->setBindOnValidate(false);
$this->assertEquals(true, $form->isValid());
$this->assertEquals($product, $form->getData());
}
public function testCanCorrectlyPopulateDataToOneToManyEntites()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped("The Intl extension is not loaded");
}
$product = $this->getOneToManyEntity();
$emptyProduct = new TestAsset\Entity\Product();
$form = new TestAsset\NewProductForm();
$form->bind($emptyProduct);
$data = array(
'product' => array(
'name' => 'Chair',
'price' => 10,
'categories' => array(
array(
'name' => 'Office'
),
array(
'name' => 'Armchair'
)
)
)
);
$form->setData($data);
$this->assertEquals(true, $form->isValid());
$this->assertEquals($product, $emptyProduct, var_export($product, 1) . "\n\n" . var_export($emptyProduct, 1));
}
public function testAssertElementsNamesAreNotWrappedAroundFormNameByDefault()
{
$form = new \ZendTest\Form\TestAsset\FormCollection();