Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 561 lines (496 sloc) 15.26 kb
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
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;
13
14 use Symfony\Component\Form\FormError;
15
16 abstract class AbstractDivLayoutTest extends AbstractLayoutTest
17 {
18 public function testRow()
19 {
20 $form = $this->factory->createNamed('text', 'name');
21 $form->addError(new FormError('Error!'));
22 $view = $form->createView();
23 $html = $this->renderRow($view);
24
25 $this->assertMatchesXpath($html,
26 '/div
27 [
28 ./label[@for="name"]
29 /following-sibling::ul
30 [./li[.="[trans]Error![/trans]"]]
31 [count(./li)=1]
32 /following-sibling::input[@id="name"]
33 ]
34 '
35 );
36 }
37
38 public function testRowOverrideVariables()
39 {
40 $view = $this->factory->createNamed('text', 'name')->createView();
41 $html = $this->renderRow($view, array('label' => 'foo&bar'));
42
43 $this->assertMatchesXpath($html,
44 '/div
45 [
46 ./label[@for="name"][.="[trans]foo&bar[/trans]"]
47 /following-sibling::input[@id="name"]
48 ]
49 '
50 );
51 }
52
53 public function testRepeatedRow()
54 {
55 $form = $this->factory->createNamed('repeated', 'name');
56 $form->addError(new FormError('Error!'));
57 $view = $form->createView();
58 $html = $this->renderRow($view);
59
60 $this->assertMatchesXpath($html,
61 '/ul
62 [./li[.="[trans]Error![/trans]"]]
63 [count(./li)=1]
64 /following-sibling::div
65 [
66 ./label[@for="name_first"]
67 /following-sibling::input[@id="name_first"]
68 ]
69 /following-sibling::div
70 [
71 ./label[@for="name_second"]
72 /following-sibling::input[@id="name_second"]
73 ]
74 '
75 );
76 }
77
78 public function testRest()
79 {
80 $view = $this->factory->createNamedBuilder('form', 'name')
81 ->add('field1', 'text')
82 ->add('field2', 'repeated')
83 ->add('field3', 'text')
84 ->add('field4', 'text')
85 ->getForm()
86 ->createView();
87
88 // Render field2 row -> does not implicitly call renderWidget because
89 // it is a repeated field!
90 $this->renderRow($view['field2']);
91
92 // Render field3 widget
93 $this->renderWidget($view['field3']);
94
95 // Rest should only contain field1 and field4
96 $html = $this->renderRest($view);
97
98 $this->assertMatchesXpath($html,
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
99 '/div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
100 [
101 ./label[@for="name_field1"]
102 /following-sibling::input[@type="text"][@id="name_field1"]
103 ]
104 /following-sibling::div
105 [
106 ./label[@for="name_field4"]
107 /following-sibling::input[@type="text"][@id="name_field4"]
108 ]
109 [count(../div)=2]
110 [count(..//label)=2]
111 [count(..//input)=3]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
112 /following-sibling::input
113 [@type="hidden"]
114 [@id="name__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
115 '
116 );
117 }
118
119 public function testRestWithChildrenForms()
120 {
121 $child1 = $this->factory->createNamedBuilder('form', 'child1')
122 ->add('field1', 'text')
123 ->add('field2', 'text')
124 ->getForm();
125
126 $child2 = $this->factory->createNamedBuilder('form', 'child2')
127 ->add('field1', 'text')
128 ->add('field2', 'text')
129 ->getForm();
130
131 $view = $this->factory->createNamedBuilder('form', 'parent')
132 ->getForm()
133 ->add($child1)
134 ->add($child2)
135 ->createView();
136
137 // Render child1.field1 row
138 $this->renderRow($view['child1']['field1']);
139
140 // Render child2.field2 widget (remember that widget don't render label)
141 $this->renderWidget($view['child2']['field2']);
142
143 // Rest should only contain child1.field2 and child2.field1
144 $html = $this->renderRest($view);
145
146 $this->assertMatchesXpath($html,
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
147 '/div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
148 [
149 ./label[not(@for)]
150 /following-sibling::div[@id="parent_child1"]
151 [
152 ./div
153 [
154 ./label[@for="parent_child1_field2"]
155 /following-sibling::input[@id="parent_child1_field2"]
156 ]
157 ]
158 ]
159
160 /following-sibling::div
161 [
162 ./label[not(@for)]
163 /following-sibling::div[@id="parent_child2"]
164 [
165 ./div
166 [
167 ./label[@for="parent_child2_field1"]
168 /following-sibling::input[@id="parent_child2_field1"]
169 ]
170 ]
171 ]
172 [count(//label)=4]
173 [count(//input[@type="text"])=2]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
174 /following-sibling::input[@type="hidden"][@id="parent__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
175 '
176 );
177 }
178
179 public function testRestAndRepeatedWithRow()
180 {
181 $view = $this->factory->createNamedBuilder('form', 'name')
182 ->add('first', 'text')
183 ->add('password', 'repeated')
184 ->getForm()
185 ->createView();
186
187 $this->renderRow($view['password']);
188
189 $html = $this->renderRest($view);
190
191 $this->assertMatchesXpath($html,
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
192 '/div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
193 [
194 ./label[@for="name_first"]
195 /following-sibling::input[@type="text"][@id="name_first"]
196 ]
197 [count(.//input)=1]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
198 /following-sibling::input
199 [@type="hidden"]
200 [@id="name__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
201 '
202 );
203 }
204
9482389 @webmozart [Form] Deprecated FieldType, which has been merged into FormType
webmozart authored
205 public function testRestAndRepeatedWithRowPerChild()
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
206 {
207 $view = $this->factory->createNamedBuilder('form', 'name')
208 ->add('first', 'text')
209 ->add('password', 'repeated')
210 ->getForm()
211 ->createView();
212
213 $this->renderRow($view['password']['first']);
214 $this->renderRow($view['password']['second']);
215
216 $html = $this->renderRest($view);
217
218 $this->assertMatchesXpath($html,
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
219 '/div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
220 [
221 ./label[@for="name_first"]
222 /following-sibling::input[@type="text"][@id="name_first"]
223 ]
224 [count(.//input)=1]
225 [count(.//label)=1]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
226 /following-sibling::input
227 [@type="hidden"]
228 [@id="name__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
229 '
230 );
231 }
232
9482389 @webmozart [Form] Deprecated FieldType, which has been merged into FormType
webmozart authored
233 public function testRestAndRepeatedWithWidgetPerChild()
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
234 {
235 $view = $this->factory->createNamedBuilder('form', 'name')
236 ->add('first', 'text')
237 ->add('password', 'repeated')
238 ->getForm()
239 ->createView();
240
241 // The password form is considered as rendered as all its children
242 // are rendered
243 $this->renderWidget($view['password']['first']);
244 $this->renderWidget($view['password']['second']);
245
246 $html = $this->renderRest($view);
247
248 $this->assertMatchesXpath($html,
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
249 '/div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
250 [
251 ./label[@for="name_first"]
252 /following-sibling::input[@type="text"][@id="name_first"]
253 ]
254 [count(//input)=2]
255 [count(//label)=1]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
256 /following-sibling::input
257 [@type="hidden"]
258 [@id="name__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
259 '
260 );
261 }
262
263 public function testCollection()
264 {
265 $form = $this->factory->createNamed('collection', 'name', array('a', 'b'), array(
266 'type' => 'text',
267 ));
268
269 $this->assertWidgetMatchesXpath($form->createView(), array(),
270 '/div
271 [
272 ./div[./input[@type="text"][@value="a"]]
273 /following-sibling::div[./input[@type="text"][@value="b"]]
274 ]
275 [count(./div[./input])=2]
276 '
277 );
278 }
279
956bbad @webmozart [Form] Fixed results of the FieldType+FormType merge.
webmozart authored
280 public function testEmptyCollection()
281 {
282 $form = $this->factory->createNamed('collection', 'name', array(), array(
283 'type' => 'text',
284 ));
285
286 $this->assertWidgetMatchesXpath($form->createView(), array(),
287 '/div
0211130 @webmozart [Form] Fixed: CSRF token was not displayed on empty complex forms
webmozart authored
288 [./input[@type="hidden"][@id="name__token"]]
956bbad @webmozart [Form] Fixed results of the FieldType+FormType merge.
webmozart authored
289 [count(./div)=0]
290 '
291 );
292 }
293
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
294 public function testCollectionRow()
295 {
296 $collection = $this->factory->createNamedBuilder(
297 'collection',
298 'collection',
299 array('a', 'b'),
300 array('type' => 'text')
301 );
302
303 $form = $this->factory->createNamedBuilder('form', 'form')
304 ->add($collection)
305 ->getForm();
306
307 $this->assertWidgetMatchesXpath($form->createView(), array(),
308 '/div
309 [
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
310 ./div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
311 [
312 ./label[not(@for)]
313 /following-sibling::div
314 [
315 ./div
316 [
317 ./label[@for="form_collection_0"]
318 /following-sibling::input[@type="text"][@value="a"]
319 ]
320 /following-sibling::div
321 [
322 ./label[@for="form_collection_1"]
323 /following-sibling::input[@type="text"][@value="b"]
324 ]
325 ]
326 ]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
327 /following-sibling::input[@type="hidden"][@id="form__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
328 ]
329 [count(.//input)=3]
330 '
331 );
332 }
333
334 public function testForm()
335 {
336 $form = $this->factory->createNamedBuilder('form', 'name')
337 ->add('firstName', 'text')
338 ->add('lastName', 'text')
339 ->getForm();
340
341 $this->assertWidgetMatchesXpath($form->createView(), array(),
342 '/div
343 [
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
344 ./div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
345 [
346 ./label[@for="name_firstName"]
347 /following-sibling::input[@type="text"][@id="name_firstName"]
348 ]
349 /following-sibling::div
350 [
351 ./label[@for="name_lastName"]
352 /following-sibling::input[@type="text"][@id="name_lastName"]
353 ]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
354 /following-sibling::input[@type="hidden"][@id="name__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
355 ]
356 [count(.//input)=3]
357 '
358 );
359 }
360
361 // https://github.com/symfony/symfony/issues/2308
362 public function testNestedFormError()
363 {
364 $form = $this->factory->createNamedBuilder('form', 'name')
9482389 @webmozart [Form] Deprecated FieldType, which has been merged into FormType
webmozart authored
365 ->add($this->factory
366 ->createNamedBuilder('form', 'child', null, array('error_bubbling' => false))
367 ->add('grandChild', 'form')
368 )
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
369 ->getForm();
370
371 $form->get('child')->addError(new FormError('Error!'));
372
373 $this->assertWidgetMatchesXpath($form->createView(), array(),
374 '/div
375 [
376 ./div/div[@id="name_child"][./ul/li[.="[trans]Error![/trans]"]]
377 ]
378 [count(.//li[.="[trans]Error![/trans]"])=1]
379 '
380 );
381 }
382
dbdb461 @webmozart [Form] Simplified CSRF mechanism and removed "csrf" type
webmozart authored
383 public function testCsrf()
384 {
385 $this->csrfProvider->expects($this->any())
386 ->method('generateCsrfToken')
387 ->will($this->returnValue('foo&bar'));
388
389 $form = $this->factory->createNamedBuilder('form', 'name')
390 ->add($this->factory
391 // No CSRF protection on nested forms
392 ->createNamedBuilder('form', 'child')
393 ->add($this->factory->createNamedBuilder('text', 'grandchild'))
394 )
395 ->getForm();
396
397 $this->assertWidgetMatchesXpath($form->createView(), array(),
398 '/div
399 [
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
400 ./div
401 /following-sibling::input[@type="hidden"][@id="name__token"][@value="foo&bar"]
dbdb461 @webmozart [Form] Simplified CSRF mechanism and removed "csrf" type
webmozart authored
402 ]
403 [count(.//input[@type="hidden"])=1]
404 '
405 );
406 }
407
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
408 public function testRepeated()
409 {
410 $form = $this->factory->createNamed('repeated', 'name', 'foobar', array(
411 'type' => 'text',
412 ));
413
414 $this->assertWidgetMatchesXpath($form->createView(), array(),
415 '/div
416 [
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
417 ./div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
418 [
419 ./label[@for="name_first"]
420 /following-sibling::input[@type="text"][@id="name_first"]
421 ]
422 /following-sibling::div
423 [
424 ./label[@for="name_second"]
425 /following-sibling::input[@type="text"][@id="name_second"]
426 ]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
427 /following-sibling::input[@type="hidden"][@id="name__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
428 ]
dbdb461 @webmozart [Form] Simplified CSRF mechanism and removed "csrf" type
webmozart authored
429 [count(.//input)=3]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
430 '
431 );
432 }
433
434 public function testRepeatedWithCustomOptions()
435 {
436 $form = $this->factory->createNamed('repeated', 'name', null, array(
437 // the global required value cannot be overriden
438 'first_options' => array('label' => 'Test', 'required' => false),
439 'second_options' => array('label' => 'Test2')
440 ));
441
442 $this->assertWidgetMatchesXpath($form->createView(), array(),
443 '/div
444 [
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
445 ./div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
446 [
447 ./label[@for="name_first"][.="[trans]Test[/trans]"]
448 /following-sibling::input[@type="text"][@id="name_first"][@required="required"]
449 ]
450 /following-sibling::div
451 [
452 ./label[@for="name_second"][.="[trans]Test2[/trans]"]
453 /following-sibling::input[@type="text"][@id="name_second"][@required="required"]
454 ]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
455 /following-sibling::input[@type="hidden"][@id="name__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
456 ]
dbdb461 @webmozart [Form] Simplified CSRF mechanism and removed "csrf" type
webmozart authored
457 [count(.//input)=3]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
458 '
459 );
460 }
461
462 public function testSearchInputName()
463 {
464 $form = $this->factory->createNamedBuilder('form', 'full')
465 ->add('name', 'search')
466 ->getForm();
467
468 $this->assertWidgetMatchesXpath($form->createView(), array(),
469 '/div
470 [
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
471 ./div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
472 [
473 ./label[@for="full_name"]
474 /following-sibling::input[@type="search"][@id="full_name"][@name="full[name]"]
475 ]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
476 /following-sibling::input[@type="hidden"][@id="full__token"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
477 ]
478 [count(//input)=2]
479 '
480 );
481 }
482
483 public function testLabelHasNoId()
484 {
485 $form = $this->factory->createNamed('text', 'name');
486 $html = $this->renderRow($form->createView());
487
488 $this->assertMatchesXpath($html,
489 '/div
490 [
491 ./label[@for="name"][not(@id)]
492 /following-sibling::input[@id="name"]
493 ]
494 '
495 );
496 }
497
498 /**
499 * @dataProvider themeBlockInheritanceProvider
500 */
501 public function testThemeBlockInheritance($theme)
502 {
503 $view = $this->factory
504 ->createNamed('email', 'name')
505 ->createView()
506 ;
507
508 $this->setTheme($view, $theme);
509
510 $this->assertMatchesXpath(
511 $this->renderWidget($view),
512 '/input[@type="email"][@rel="theme"]'
513 );
514 }
515
516 /**
517 * @dataProvider themeInheritanceProvider
518 */
519 public function testThemeInheritance($parentTheme, $childTheme)
520 {
521 $child = $this->factory->createNamedBuilder('form', 'child')
522 ->add('field', 'text')
523 ->getForm();
524
525 $view = $this->factory->createNamedBuilder('form', 'parent')
526 ->add('field', 'text')
527 ->getForm()
528 ->add($child)
529 ->createView()
530 ;
531
532 $this->setTheme($view, $parentTheme);
533 $this->setTheme($view['child'], $childTheme);
534
535 $this->assertWidgetMatchesXpath($view, array(),
536 '/div
537 [
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
538 ./div
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
539 [
540 ./label[.="parent"]
541 /following-sibling::input[@type="text"]
542 ]
543 /following-sibling::div
544 [
545 ./label
546 /following-sibling::div
547 [
548 ./div
549 [
550 ./label[.="child"]
551 /following-sibling::input[@type="text"]
552 ]
553 ]
554 ]
6dc30c7 @webmozart [Form] Removed extra CSRF field on collection prototype
webmozart authored
555 /following-sibling::input[@type="hidden"]
0a54c1e @fabpot moved component and bridge unit tests to the src/ directory
fabpot authored
556 ]
557 '
558 );
559 }
560 }
Something went wrong with that request. Please try again.