Jelajahi Sumber

[Form] Fixed a couple of failing tests

Bernhard Schussek 14 tahun lalu
induk
melakukan
fb8efaba67

+ 1 - 0
src/Symfony/Component/Form/Config/ChoiceFieldConfig.php

@@ -68,6 +68,7 @@ class ChoiceFieldConfig extends AbstractFieldConfig
             'expanded' => false,
             'choices' => array(),
             'preferred_choices' => array(),
+            'csrf_protection' => false,
         );
 
         $options = array_replace($defaultOptions, $options);

+ 6 - 14
src/Symfony/Component/Form/Config/CollectionFieldConfig.php

@@ -19,12 +19,10 @@ class CollectionFieldConfig extends AbstractFieldConfig
     public function configure(FieldInterface $field, array $options)
     {
         if ($options['modifiable']) {
-            $child = clone $options['prototype'];
-            $child->setKey('$$key$$');
-            $child->setPropertyPath(null);
-            // TESTME
-            $child->setRequired(false);
-            $field->add($child);
+            $field->add($options['prototype'], '$$key$$', array(
+                'property_path' => null,
+                'required' => false,
+            ));
         }
 
         $field->addEventListener(new ResizeFormListener($field,
@@ -33,18 +31,12 @@ class CollectionFieldConfig extends AbstractFieldConfig
 
     public function getDefaultOptions(array $options)
     {
-        $defaultOptions = array(
+        return array(
             'template' => 'collection',
             'prototype' => null,
             'modifiable' => false,
+            'prototype' => 'text',
         );
-
-        // Lazy creation of the prototype
-        if (!isset($options['prototype'])) {
-            $defaultOptions['prototype'] = $this->getInstance('text');
-        }
-
-        return $defaultOptions;
     }
 
     public function getParent(array $options)

+ 1 - 0
src/Symfony/Component/Form/Config/DateFieldConfig.php

@@ -111,6 +111,7 @@ class DateFieldConfig extends AbstractFieldConfig
             'format' => \IntlDateFormatter::MEDIUM,
             'data_timezone' => date_default_timezone_get(),
             'user_timezone' => date_default_timezone_get(),
+            'csrf_protection' => false,
         );
     }
 

+ 1 - 0
src/Symfony/Component/Form/Config/FileFieldConfig.php

@@ -56,6 +56,7 @@ class FileFieldConfig extends AbstractFieldConfig
         return array(
             'template' => 'file',
             'type' => 'string',
+            'csrf_protection' => false,
         );
     }
 

+ 1 - 0
src/Symfony/Component/Form/Config/RepeatedFieldConfig.php

@@ -34,6 +34,7 @@ class RepeatedFieldConfig extends AbstractFieldConfig
             'options' => array(),
             'first_key' => 'first',
             'second_key' => 'second',
+            'csrf_protection' => false,
         );
     }
 

+ 1 - 0
src/Symfony/Component/Form/Config/TimeFieldConfig.php

@@ -103,6 +103,7 @@ class TimeFieldConfig extends AbstractFieldConfig
             'pattern' => null,
             'data_timezone' => date_default_timezone_get(),
             'user_timezone' => date_default_timezone_get(),
+            'csrf_protection' => false,
         );
     }
 

+ 7 - 12
src/Symfony/Component/Form/EventListener/ResizeFormListener.php

@@ -24,7 +24,7 @@ class ResizeFormListener implements EventListenerInterface
 
     private $resizeOnBind;
 
-    public function __construct(FormInterface $form, FieldInterface $prototype, $resizeOnBind = false)
+    public function __construct(FormInterface $form, $prototype, $resizeOnBind = false)
     {
         $this->form = $form;
         $this->prototype = $prototype;
@@ -56,7 +56,9 @@ class ResizeFormListener implements EventListenerInterface
         }
 
         foreach ($collection as $name => $value) {
-            $this->form->add($this->newField($name));
+            $this->form->add($this->prototype, $name, array(
+                'property_path' => '['.$name.']',
+            ));
         }
     }
 
@@ -77,17 +79,10 @@ class ResizeFormListener implements EventListenerInterface
 
         foreach ($data as $name => $value) {
             if (!$this->form->has($name) && $this->resizeOnBind) {
-                $this->form->add($this->newField($name));
+                $this->form->add($this->prototype, $name, array(
+                    'property_path' => '['.$name.']',
+                ));
             }
         }
     }
-
-    protected function newField($key)
-    {
-        $field = clone $this->prototype;
-        $field->setKey($key);
-        $field->setPropertyPath('['.$key.']');
-
-        return $field;
-    }
 }

+ 15 - 12
tests/Symfony/Tests/Component/Form/CollectionFieldTest.php

@@ -19,18 +19,21 @@ use Symfony\Component\Form\Field;
 
 class CollectionFieldTest extends TestCase
 {
-    public function testContainsNoFieldsByDefault()
+    public function testContainsOnlyCsrfTokenByDefault()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
+            'csrf_field_name' => 'abc',
         ));
-        $this->assertEquals(0, count($field));
+
+        $this->assertTrue($field->has('abc'));
+        $this->assertEquals(1, count($field));
     }
 
     public function testSetDataAdjustsSize()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
         ));
         $field->setData(array('foo@foo.com', 'foo@bar.com'));
 
@@ -50,7 +53,7 @@ class CollectionFieldTest extends TestCase
     public function testSetDataAdjustsSizeIfModifiable()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
             'modifiable' => true,
         ));
         $field->setData(array('foo@foo.com', 'foo@bar.com'));
@@ -70,7 +73,7 @@ class CollectionFieldTest extends TestCase
     public function testThrowsExceptionIfObjectIsNotTraversable()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
         ));
         $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
         $field->setData(new \stdClass());
@@ -79,7 +82,7 @@ class CollectionFieldTest extends TestCase
     public function testModifiableCollectionsContainExtraField()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
             'modifiable' => true,
         ));
         $field->setData(array('foo@bar.com'));
@@ -92,7 +95,7 @@ class CollectionFieldTest extends TestCase
     public function testNotResizedIfSubmittedWithMissingData()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
         ));
         $field->setData(array('foo@foo.com', 'bar@bar.com'));
         $field->submit(array('foo@bar.com'));
@@ -106,7 +109,7 @@ class CollectionFieldTest extends TestCase
     public function testResizedIfSubmittedWithMissingDataAndModifiable()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
             'modifiable' => true,
         ));
         $field->setData(array('foo@foo.com', 'bar@bar.com'));
@@ -120,7 +123,7 @@ class CollectionFieldTest extends TestCase
     public function testNotResizedIfSubmittedWithExtraData()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
         ));
         $field->setData(array('foo@bar.com'));
         $field->submit(array('foo@foo.com', 'bar@bar.com'));
@@ -133,7 +136,7 @@ class CollectionFieldTest extends TestCase
     public function testResizedUpIfSubmittedWithExtraDataAndModifiable()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
             'modifiable' => true,
         ));
         $field->setData(array('foo@bar.com'));
@@ -149,7 +152,7 @@ class CollectionFieldTest extends TestCase
     public function testResizedDownIfSubmittedWithLessDataAndModifiable()
     {
         $field = $this->factory->getInstance('collection', 'emails', array(
-            'prototype' => new Field(),
+            'prototype' => 'field',
             'modifiable' => true,
         ));
         $field->setData(array('foo@bar.com', 'bar@bar.com'));