Browse Source

Fixed setting 'compound' option of sonata_type_model

Andrej Hudec 11 years ago
parent
commit
d74b680652
2 changed files with 57 additions and 1 deletions
  1. 17 1
      Form/Type/ModelType.php
  2. 40 0
      Tests/Form/Type/ModelTypeTest.php

+ 17 - 1
Form/Type/ModelType.php

@@ -65,7 +65,23 @@ class ModelType extends AbstractType
     {
         $resolver->setDefaults(array(
             'compound'          => function (Options $options) {
-                return isset($options['multiple']) ? $options['multiple'] : false;
+                if (isset($options['multiple']) && $options['multiple']) {
+                    if (isset($options['expanded']) && $options['expanded']) {
+                        //checkboxes
+                        return true;
+                    }
+
+                    //select tag (with multiple attribute)
+                    return false;
+                }
+
+                if (isset($options['expanded']) && $options['expanded']) {
+                    //radio buttons
+                    return true;
+                }
+
+                //select tag
+                return false;
             },
 
             'template'          => 'choice',

+ 40 - 0
Tests/Form/Type/ModelTypeTest.php

@@ -44,4 +44,44 @@ class ModelTypeTest extends TypeTestCase
         $this->assertEquals('SonataAdminBundle', $options['btn_catalogue']);
         $this->assertInstanceOf('Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList', $options['choice_list']);
     }
+
+    /**
+     * @dataProvider getCompundOptionTests
+     */
+    public function testCompundOption($expectedCompound, $multiple, $expanded)
+    {
+        $type = new ModelType();
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $optionResolver = new OptionsResolver();
+
+        $type->setDefaultOptions($optionResolver);
+
+        $options = $optionResolver->resolve(array('model_manager' => $modelManager, 'choices' => array(), 'multiple'=>$multiple, 'expanded'=>$expanded));
+
+        $this->assertEquals($expectedCompound, $options['compound']);
+        $this->assertEquals('choice', $options['template']);
+        $this->assertEquals($multiple, $options['multiple']);
+        $this->assertEquals($expanded, $options['expanded']);
+        $this->assertInstanceOf('Sonata\AdminBundle\Model\ModelManagerInterface', $options['model_manager']);
+        $this->assertNull($options['class']);
+        $this->assertNull($options['property']);
+        $this->assertNull($options['query']);
+        $this->assertEquals(0, count($options['choices']));
+        $this->assertEquals(0, count($options['preferred_choices']));
+        $this->assertEquals('link_add', $options['btn_add']);
+        $this->assertEquals('link_list', $options['btn_list']);
+        $this->assertEquals('link_delete', $options['btn_delete']);
+        $this->assertEquals('SonataAdminBundle', $options['btn_catalogue']);
+        $this->assertInstanceOf('Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList', $options['choice_list']);
+    }
+
+    public function getCompundOptionTests()
+    {
+        return array(
+            array(true, true, true), //checkboxes
+            array(false, true, false), //select tag (with multiple attribute)
+            array(true, false, true), //radio buttons
+            array(false, false, false), //select tag
+        );
+    }
 }