Jelajahi Sumber

[Form] Added test coverage for expanded choice fields

Bernhard Schussek 14 tahun lalu
induk
melakukan
4907c7d81d

+ 3 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget.html.php

@@ -1,8 +1,10 @@
 <?php if ($expanded): ?>
+    <div<?php echo $view['form']->attributes() ?>>
     <?php foreach ($context as $choice => $child): ?>
         <?php echo $view['form']->widget($child) ?>
         <?php echo $view['form']->label($child) ?>
     <?php endforeach ?>
+    </div>
 <?php else: ?>
     <select
         <?php echo $view['form']->attributes() ?>
@@ -11,7 +13,7 @@
         <?php if ($multiple): ?> multiple="multiple"<?php endif ?>
         <?php if ($class): ?> class="<?php echo $class ?>"<?php endif ?>
     >
-        <?php if (!$required): ?><option value=""><?php echo $empty_value; ?></option><?php endif; ?>
+        <?php if (!$multiple && !$required): ?><option value=""><?php echo $empty_value; ?></option><?php endif; ?>
         <?php if (count($preferred_choices) > 0): ?>
             <?php foreach ($preferred_choices as $choice => $label): ?>
                 <?php if ($context->isChoiceGroup($label)): ?>

+ 3 - 2
src/Symfony/Bundle/TwigBundle/Resources/views/div_layout.html.twig

@@ -103,14 +103,15 @@
 {% block choice__widget %}
 {% spaceless %}
     {% if expanded %}
+        <div {{ block('attributes') }}>
         {% for choice, child in context %}
             {{ form_widget(child) }}
-            {{ child.setVar('label', context.choiceLabel(choice)) }}
             {{ form_label(child) }}
         {% endfor %}
+        </div>
     {% else %}
     <select {{ block('attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
-        {% if not required %}
+        {% if not multiple and not required %}
             <option value="">{{ empty_value }}</option>
         {% endif %}
         {% if preferred_choices|length > 0 %}

+ 103 - 4
tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

@@ -210,11 +210,13 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    public function testChoice()
+    public function testSingleChoice()
     {
         $form = $this->factory->create('choice', 'name', array(
             'choices' => array('a' => 'A', 'b' => 'B'),
             'data' => 'a',
+            'multiple' => false,
+            'expanded' => false,
         ));
 
         $this->assertWidgetMatchesXpath($form, array(),
@@ -229,12 +231,14 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    public function testChoiceWithPreferred()
+    public function testSingleChoiceWithPreferred()
     {
         $form = $this->factory->create('choice', 'name', array(
             'choices' => array('a' => 'A', 'b' => 'B'),
             'preferred_choices' => array('b'),
             'data' => 'a',
+            'multiple' => false,
+            'expanded' => false,
         ));
 
         $this->assertWidgetMatchesXpath($form, array('separator' => '-- sep --'),
@@ -250,12 +254,14 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    public function testNonRequiredChoice()
+    public function testSingleChoiceNonRequired()
     {
         $form = $this->factory->create('choice', 'name', array(
             'choices' => array('a' => 'A', 'b' => 'B'),
             'required' => false,
             'data' => 'a',
+            'multiple' => false,
+            'expanded' => false,
         ));
 
         $this->assertWidgetMatchesXpath($form, array(),
@@ -271,7 +277,7 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    public function testGroupedChoice()
+    public function testSingleChoiceGrouped()
     {
         $form = $this->factory->create('choice', 'name', array(
             'choices' => array(
@@ -279,6 +285,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
                 'Group2' => array('c' => 'C'),
             ),
             'data' => 'a',
+            'multiple' => false,
+            'expanded' => false,
         ));
 
         $this->assertWidgetMatchesXpath($form, array(),
@@ -300,6 +308,97 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
+    public function testMultipleChoice()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'choices' => array('a' => 'A', 'b' => 'B'),
+            'data' => array('a'),
+            'multiple' => true,
+            'expanded' => false,
+        ));
+
+        $this->assertWidgetMatchesXpath($form, array(),
+'/select
+    [@name="name[]"]
+    [@multiple="multiple"]
+    [
+        ./option[@value="a"][@selected="selected"][.="A"]
+        /following-sibling::option[@value="b"][not(@selected)][.="B"]
+    ]
+    [count(./option)=2]
+'
+        );
+    }
+
+    public function testMultipleChoiceNonRequired()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'choices' => array('a' => 'A', 'b' => 'B'),
+            'data' => array('a'),
+            'required' => false,
+            'multiple' => true,
+            'expanded' => false,
+        ));
+
+        $this->assertWidgetMatchesXpath($form, array(),
+'/select
+    [@name="name[]"]
+    [@multiple="multiple"]
+    [
+        ./option[@value="a"][@selected="selected"][.="A"]
+        /following-sibling::option[@value="b"][not(@selected)][.="B"]
+    ]
+    [count(./option)=2]
+'
+        );
+    }
+
+    public function testSingleChoiceExpanded()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'choices' => array('a' => 'A', 'b' => 'B'),
+            'data' => 'a',
+            'multiple' => false,
+            'expanded' => true,
+        ));
+
+        $this->assertWidgetMatchesXpath($form, array(),
+'/div
+    [
+        ./input[@type="radio"][@name="name"][@id="name_a"][@checked]
+        /following-sibling::label[@for="name_a"][.="[trans]A[/trans]"]
+        /following-sibling::input[@type="radio"][@name="name"][@id="name_b"][not(@checked)]
+        /following-sibling::label[@for="name_b"][.="[trans]B[/trans]"]
+    ]
+    [count(./input)=2]
+'
+        );
+    }
+
+    public function testMultipleChoiceExpanded()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'choices' => array('a' => 'A', 'b' => 'B', 'c' => 'C'),
+            'data' => array('a', 'c'),
+            'multiple' => true,
+            'expanded' => true,
+        ));
+
+        $this->assertWidgetMatchesXpath($form, array(),
+'/div
+    [
+        ./input[@type="checkbox"][@name="name[a]"][@id="name_a"][@checked]
+        /following-sibling::label[@for="name_a"][.="[trans]A[/trans]"]
+        /following-sibling::input[@type="checkbox"][@name="name[b]"][@id="name_b"][not(@checked)]
+        /following-sibling::label[@for="name_b"][.="[trans]B[/trans]"]
+        /following-sibling::input[@type="checkbox"][@name="name[c]"][@id="name_c"][@checked]
+        /following-sibling::label[@for="name_c"][.="[trans]C[/trans]"]
+    ]
+    [count(./input)=3]
+'
+        );
+    }
+
     public function testCountry()
     {
         $form = $this->factory->create('country', 'name', array(