8000 [Form] Improved test coverage of widget_attributes and widget_contain… · symfony/symfony@b85577b · GitHub
[go: up one dir, main page]

Skip to content

Commit b85577b

Browse files
committed
[Form] Improved test coverage of widget_attributes and widget_container_attributes blocks
1 parent 8e4c2a7 commit b85577b

File tree

8 files changed

+223
-50
lines changed

8 files changed

+223
-50
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -373,47 +373,53 @@
373373

374374
{% block widget_attributes %}
375375
{% spaceless %}
376-
id="{{ id }}"
377-
name="{{ full_name }}"
378-
{% if read_only %} readonly="readonly"{% endif %}
379-
{% if disabled %} disabled="disabled"{% endif %}
380-
{% if required %} required="required"{% endif %}
381-
{% if max_length %} maxlength="{{ max_length }}"{% endif %}
382-
{% if pattern %} pattern="{{ pattern }}"{% endif %}
383-
{% for attrname, attrvalue in attr %}
384-
{% if attrname in ['placeholder', 'title'] %}
385-
{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
386-
{% else %}
387-
{% if attrvalue is sameas(true) %}
388-
{{ attrname }}="{{ attrname }}"
389-
{% elseif attrvalue is not sameas(false) %}
390-
{{ attrname }}="{{ attrvalue }}"
391-
{% endif %}
392-
{% endif %}
393-
{% endfor %}
376+
id="{{ id }}" name="{{ full_name }}"
377+
{%- if read_only %} readonly="readonly"{% endif -%}
378+
{%- if disabled %} disabled="disabled"{% endif -%}
379+
{%- if required %} required="required"{% endif -%}
380+
{%- if max_length %} maxlength="{{ max_length }}"{% endif -%}
381+
{%- if pattern %} pattern="{{ pattern }}"{% endif -%}
382+
{%- for attrname, attrvalue in attr -%}
383+
{{- " " -}}
384+
{%- if attrname in ['placeholder', 'title'] -%}
385+
{{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
386+
{%- elseif attrvalue is sameas(true) -%}
387+
{{- attrname }}="{{ attrname }}"
388+
{%- elseif attrvalue is not sameas(false) -%}
389+
{{- attrname }}="{{ attrvalue }}"
390+
{%- endif -%}
391+
{%- endfor -%}
394392
{% endspaceless %}
395393
{% endblock widget_attributes %}
396394

397395
{% block widget_container_attributes %}
398396
{% spaceless %}
399-
{% if id is not empty %}id="{{ id }}" {% endif %}
400-
{% for attrname, attrvalue in attr %}
401-
{% if attrname in ['placeholder', 'title'] %}
402-
{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
403-
{% else %}
404-
{% if attrvalue is sameas(true) %}
405-
{{ attrname }}="{{ attrname }}"
406-
{% elseif attrvalue is not sameas(false) %}
407-
{{ attrname }}="{{ attrvalue }}"
408-
{% endif %}
409-
{% endif %}
410-
{% endfor %}
397+
{%- if id is not empty %}id="{{ id }}"{% endif -%}
398+
{%- for attrname, attrvalue in attr -%}
399+
{{- " " -}}
400+
{%- if attrname in ['placeholder', 'title'] -%}
401+
{{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
402+
{%- elseif attrvalue is sameas(true) -%}
403+
{{- attrname }}="{{ attrname }}"
404+
{%- elseif attrvalue is not sameas(false) -%}
405+
{{- attrname }}="{{ attrvalue }}"
406+
{%- endif -%}
407+
{%- endfor -%}
411408
{% endspaceless %}
412409
{% endblock widget_container_attributes %}
413410

414411
{% block button_attributes %}
415412
{% spaceless %}
416-
id="{{ id }}" name="{{ full_name }}"{% if disabled %} disabled="disabled"{% endif %}
417-
{% for attrname, attrvalue in attr %}{{ attrname }}="{{ attrvalue }}" {% endfor %}
413+
id="{{ id }}" name="{{ full_name }}"{% if disabled %} disabled="disabled"{% endif -%}
414+
{%- for attrname, attrvalue in attr -%}
415+
{{- " " -}}
416+
{%- if attrname in ['placeholder', 'title'] -%}
417+
{{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
418+
{%- elseif attrvalue is sameas(true) -%}
419+
{{- attrname }}="{{ attrname }}"
420+
{%- elseif attrvalue is not sameas(false) -%}
421+
{{- attrname }}="{{ attrvalue }}"
422+
{%- endif -%}
423+
{%- endfor -%}
418424
{% endspaceless %}
419425
{% endblock button_attributes %}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
id="<?php echo $view->escape($id) ?>"
2-
name="<?php echo $view->escape($full_name) ?>"
3-
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
1+
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($disabled): ?>disabled="disabled" <?php endif ?>
42
<?php foreach ($attr as $k => $v): ?>
5-
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
6-
<?php endforeach; ?>
3+
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
4+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>
5+
<?php elseif ($v === true): ?>
6+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
7+
<?php elseif ($v !== false): ?>
8+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
9+
<?php endif ?>
10+
<?php endforeach ?>
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
<input
2-
type="<?php echo isset($type) ? $view->escape($type) : 'text' ?>"
3-
<?php if (!empty($value)): ?>value="<?php echo $view->escape($value) ?>"<?php endif ?>
4-
<?php echo $view['form']->block($form, 'widget_attributes') ?>
5-
/>
1+
<input type="<?php echo isset($type) ? $view->escape($type) : 'text' ?>" <?php echo $view['form']->block($form, 'widget_attributes') ?><?php if (!empty($value)): ?> value="<?php echo $view->escape($value) ?>"<?php endif ?> />
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
id="<?php echo $view->escape($id) ?>"
2-
name="<?php echo $view->escape($full_name) ?>"
3-
<?php if ($read_only): ?>readonly="readonly" <?php endif ?>
1+
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($read_only): ?>readonly="readonly" <?php endif ?>
42
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
53
<?php if ($required): ?>required="required" <?php endif ?>
64
<?php if ($max_length): ?>maxlength="<?php echo $view->escape($max_length) ?>" <?php endif ?>
75
<?php if ($pattern): ?>pattern="<?php echo $view->escape($pattern) ?>" <?php endif ?>
86
<?php foreach ($attr as $k => $v): ?>
9-
<?php printf('%s="%s" ', $view->escape($k), $view->escape(in_array($v, array('placeholder', 'title')) ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
10-
<?php endforeach; ?>
7+
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
8+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>
9+
<?php elseif ($v === true): ?>
10+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
11+
<?php elseif ($v !== false): ?>
12+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
13+
<?php endif ?>
14+
<?php endforeach ?>
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
<?php if (!empty($id)): ?>id="<?php echo $view->escape($id) ?>" <?php endif; ?>
2-
<?php foreach ($attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>
1+
<?php if (!empty($id)): ?>id="<?php echo $view->escape($id) ?>" <?php endif ?>
2+
<?php foreach ($attr as $k => $v): ?>
3+
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
4+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>
5+
<?php elseif ($v === true): ?>
6+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
7+
<?php elseif ($v !== false): ?>
8+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
9+
<?php endif ?>
10+
<?php endforeach ?>

src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,4 +729,42 @@ public function testFormEndWithoutRest()
729729

730730
$this->assertEquals('</form>', $html);
731731
}
732+
733+
public function testWidgetContainerAttributes()
734+
{
735+
$form = $this->factory->createNamed('form', 'form', null, array(
736+
'attr' => array('class' => 'foobar', 'data-foo' => 'bar'),
737+
));
738+
739+
$form->add('text', 'text');
740+
741+
$html = $this->renderWidget($form->createView());
742+
743+
// compare plain HTML to check the whitespace
744+
$this->assertContains('<div id="form" class="foobar" data-foo="bar">', $html);
745+
}
746+
747+
public function testWidgetContainerAttributeNameRepeatedIfTrue()
748+
{
749+
$form = $this->factory->createNamed('form', 'form', null, array(
750+
'attr' => array('foo' => true),
751+
));
752+
753+
$html = $this->renderWidget($form->createView());
754+
755+
// foo="foo"
756+
$this->assertContains('<div id="form" foo="foo">', $html);
757+
}
758+
759+
public function testWidgetContainerAttributeHiddenIfFalse()
760+
{
761+
$form = $this->factory->createNamed('form', 'form', null, array(
762+
'attr' => array('foo' => false),
763+
));
764+
765+
$html = $this->renderWidget($form->createView());
766+
767+
// no foo
768+
$this->assertContains('<div id="form">', $html);
769+
}
732770
}

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ public function testErrors()
302302
);
303303
}
304304

305-
public function testWidgetById()
305+
public function testOverrideWidgetBlock()
306306
{
307+
// see custom_widgets.html.twig
307308
$form = $this->factory->createNamed('text_id', 'text');
308309
$html = $this->renderWidget($form->createView());
309310

@@ -1891,4 +1892,82 @@ public function testStartTagWithExtraAttributes()
18911892

18921893
$this->assertSame('<form name="form" method="get" action="http://example.com/directory" class="foobar">', $html);
18931894
}
1895+
1896+
public function testWidgetAttributes()
1897+
{
1898+
$form = $this->factory->createNamed('text', 'text', 'value', array(
1899+
'required' => true,
1900+
'disabled' => true,
1901+
'read_only' => true,
1902+
'max_length' => 10,
1903+
'pattern' => '\d+',
1904+
'attr' => array('class' => 'foobar', 'data-foo' => 'bar'),
1905+
));
1906+
1907+
$html = $this->renderWidget($form->createView());
1908+
1909+
// compare plain HTML to check the whitespace
1910+
$this->assertSame('<input type="text" id="text" name="text" readonly="readonly" disabled="disabled" required="required" maxlength="10" pattern="\d+" class="foobar" data-foo="bar" value="value" />', $html);
1911+
}
1912+
1913+
public function testWidgetAttributeNameRepeatedIfTrue()
1914+
{
1915+
$form = $this->factory->createNamed('text', 'text', 'value', array(
1916+
'attr' => array('foo' => true),
1917+
));
1918+
1919+
$html = $this->renderWidget($form->createView());
1920+
1921+
// foo="foo"
1922+
$this->assertSame('<input type="text" id="text" name="text" required="required" foo="foo" value="value" />', $html);
1923+
}
1924+
1925+
public function testWidgetAttributeHiddenIfFalse()
1926+
{
1927+
$form = $this->factory->createNamed('text', 'text', 'value', array(
1928+
'attr' => array('foo' => false),
1929+
));
1930+
1931+
$html = $this->renderWidget($form->createView());
1932+
1933+
// no foo
1934+
$this->assertSame('<input type="text" id="text" name="text" required="required" value="value" />', $html);
1935+
}
1936+
1937+
public function testButtonAttributes()
1938+
{
1939+
$form = $this->factory->createNamed('button', 'button', null, array(
1940+
'disabled' => true,
1941+
'attr' => array('class' => 'foobar', 'data-foo' => 'bar'),
1942+
));
1943+
1944+
$html = $this->renderWidget($form->createView());
1945+
1946+
// compare plain HTML to check the whitespace
1947+
$this->assertSame('<button type="button" id="button" name="button" disabled="disabled" class="foobar" data-foo="bar">[trans]Button[/trans]</button>', $html);
1948+
}
1949+
1950+
public function testButtonAttributeNameRepeatedIfTrue()
1951+
{
1952+
$form = $this->factory->createNamed('button', 'button', null, array(
1953+
'attr' => array('foo' => true),
1954+
));
1955+
1956+
$html = $this->renderWidget($form->createView());
1957+
1958+
// foo="foo"
1959+
$this->assertSame('<button type="button" id="button" name="button" foo="foo">[trans]Button[/trans]</button>', $html);
1960+
}
1961+
1962+
public function testButtonAttributeHiddenIfFalse()
1963+
{
1964+
$form = $this->factory->createNamed('button', 'button', null, array(
1965+
'attr' => array('foo' => false),
1966+
));
1967+
1968+
$html = $this->renderWidget($form->createView());
1969+
1970+
// no foo
1971+
$this->assertSame('<button type="button" id="button" name="button">[trans]Button[/trans]</button>', $html);
1972+
}
18941973
}

src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,42 @@ public function testFormEndWithoutRest()
506506

507507
$this->assertEquals('</form>', $html);
508508
}
509+
510+
public function testWidgetContainerAttributes()
511+
{
512+
$form = $this->factory->createNamed('form', 'form', null, array(
513+
'attr' => array('class' => 'foobar', 'data-foo' => 'bar'),
514+
));
515+
516+
$form->add('text', 'text');
517+
518+
$html = $this->renderWidget($form->createView());
519+
520+
// compare plain HTML to check the whitespace
521+
$this->assertContains('<table id="form" class="foobar" data-foo="bar">', $html);
522+
}
523+
524+
public function testWidgetContainerAttributeNameRepeatedIfTrue()
525+
{
526+
$form = $this->factory->createNamed('form', 'form', null, array(
527+
'attr' => array('foo' => true),
528+
));
529+
530+
$html = $this->renderWidget($form->createView());
531+
532+
// foo="foo"
533+
$this->assertContains('<table id="form" foo="foo">', $html);
534+
}
535+
536+
public function testWidgetContainerAttributeHiddenIfFalse()
537+
{
538+
$form = $this 6786 ->factory->createNamed('form', 'form', null, array(
539+
'attr' => array('foo' => false),
540+
));
541+
542+
$html = $this->renderWidget($form->createView());
543+
544+
// no foo
545+
$this->assertContains('<table id="form">', $html);
546+
}
509547
}

0 commit comments

Comments
 (0)
0