Przeglądaj źródła

AdminType doubles $fieldDescription and property_path (#4429)

Michael Hirschler 8 lat temu
rodzic
commit
0761127cfb
2 zmienionych plików z 42 dodań i 1 usunięć
  1. 1 1
      Form/Type/AdminType.php
  2. 41 0
      Tests/Form/Type/AdminTypeTest.php

+ 1 - 1
Form/Type/AdminType.php

@@ -79,7 +79,7 @@ class AdminType extends AbstractType
                         // for PropertyAccessor >= 2.5
                         $subject = $p->getValue(
                             $parentSubject,
-                            $this->getFieldDescription($options)->getFieldName().$options['property_path']
+                            $options['property_path']
                         );
                     }
                     $builder->setData($subject);

+ 41 - 0
Tests/Form/Type/AdminTypeTest.php

@@ -88,6 +88,47 @@ class AdminTypeTest extends TypeTestCase
         $this->assertTrue($form->isSynchronized());
     }
 
+    public function testDotFields()
+    {
+        if (!method_exists('Symfony\Component\PropertyAccess\PropertyAccessor', 'isReadable')) {
+            return $this->markTestSkipped('Testing ancient versions would be more complicated.');
+        }
+
+        $parentSubject = new \stdClass();
+        $parentSubject->foo = 1;
+
+        $parentAdmin = $this->prophesize('Sonata\AdminBundle\Admin\AdminInterface');
+        $parentAdmin->getSubject()->shouldBeCalled()->willReturn($parentSubject);
+        $parentField = $this->prophesize('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $parentField->getAdmin()->shouldBeCalled()->willReturn($parentAdmin->reveal());
+
+        $modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
+
+        $admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
+        $admin->hasParentFieldDescription()->shouldBeCalled()->willReturn(false);
+        $admin->getParentFieldDescription()->shouldBeCalled()->willReturn($parentField->reveal());
+        $admin->setSubject(1)->shouldBeCalled();
+        $admin->defineFormBuilder(new AnyValueToken())->shouldBeCalled();
+        $admin->getModelManager()->shouldBeCalled()->willReturn($modelManager);
+        $admin->getClass()->shouldBeCalled()->willReturn('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo');
+
+        $field = $this->prophesize('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $field->getAssociationAdmin()->shouldBeCalled()->willReturn($admin->reveal());
+
+        $this->builder->add('foo.bar');
+
+        try {
+            $type = new AdminType();
+            $type->buildForm($this->builder, array(
+                'sonata_field_description' => $field->reveal(),
+                'delete' => false, // not needed
+                'property_path' => 'foo', // actual test case
+            ));
+        } catch (\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException $exception) {
+            $this->fail($exception->getMessage());
+        }
+    }
+
     protected function getExtensions()
     {
         $extensions = parent::getExtensions();