ソースを参照

Fix getSubject method when admin has parentFieldDescription (#4581)

Emmanuel Vella 7 年 前
コミット
4733f4cc89
2 ファイル変更32 行追加1 行削除
  1. 1 1
      Admin/AbstractAdmin.php
  2. 31 0
      Tests/Admin/AdminTest.php

+ 1 - 1
Admin/AbstractAdmin.php

@@ -1688,7 +1688,7 @@ EOT;
      */
     public function getSubject()
     {
-        if ($this->subject === null && $this->request) {
+        if ($this->subject === null && $this->request && !$this->hasParentFieldDescription()) {
             $id = $this->request->get($this->getIdParameter());
             $this->subject = $this->getModelManager()->find($this->class, $id);
         }

+ 31 - 0
Tests/Admin/AdminTest.php

@@ -23,6 +23,7 @@ use Sonata\AdminBundle\Tests\Fixtures\Admin\FilteredAdmin;
 use Sonata\AdminBundle\Tests\Fixtures\Admin\ModelAdmin;
 use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
 use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin;
+use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Comment;
 use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
 use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
 use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
@@ -1651,6 +1652,36 @@ class AdminTest extends PHPUnit_Framework_TestCase
         $this->assertSame($entity, $admin->getSubject()); // model manager must be used only once
     }
 
+    public function testGetSubjectWithParentDescription()
+    {
+        $adminId = 1;
+
+        $comment = new Comment();
+
+        $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager
+            ->expects($this->any())
+            ->method('find')
+            ->with('NewsBundle\Entity\Comment', $adminId)
+            ->will($this->returnValue($comment));
+
+        $request = new Request(array('id' => $adminId));
+
+        $postAdmin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
+        $postAdmin->setRequest($request);
+
+        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
+        $commentAdmin->setRequest($request);
+        $commentAdmin->setModelManager($modelManager);
+
+        $this->assertEquals($comment, $commentAdmin->getSubject());
+
+        $commentAdmin->setSubject(null);
+        $commentAdmin->setParentFieldDescription(new FieldDescription());
+
+        $this->assertNull($commentAdmin->getSubject());
+    }
+
     /**
      * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::configureActionButtons
      */