Skip to content

Commit

Permalink
AbstractAdmin::setSubject() should use getModelClass() instead of…
Browse files Browse the repository at this point in the history
… `getClass()`

`getClass()` will check the class of the current subject. So setting the
subject with an instance of a parent class when the current subject is
of a child class, this would failf before.

Resolves: #7944
  • Loading branch information
7ochem authored and VincentLanglet committed Nov 1, 2022
1 parent c3259e4 commit 0ced12b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Admin/AbstractAdmin.php
Expand Up @@ -1056,12 +1056,12 @@ final public function hasParentFieldDescription(): bool

final public function setSubject(?object $subject): void
{
if (null !== $subject && !is_a($subject, $this->getClass(), true)) {
if (null !== $subject && !is_a($subject, $this->getModelClass(), true)) {
throw new \LogicException(sprintf(
'Admin "%s" does not allow this subject: %s, use the one register with this admin class %s',
static::class,
\get_class($subject),
$this->getClass()
$this->getModelClass()
));
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Admin/AdminTest.php
Expand Up @@ -64,6 +64,7 @@
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost;
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Comment;
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\CommentVote;
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\NewsPost;
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\PostCategory;
use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
Expand Down Expand Up @@ -1731,6 +1732,34 @@ public function testGetSubject($id): void
static::assertSame($model, $admin->getSubject()); // model manager must be used only once
}

public function testSetSubject(): void
{
$this->expectNotToPerformAssertions();

$admin = new PostAdmin();
$admin->setModelClass(Post::class);

$admin->setSubject(new BlogPost());
$admin->setSubject(new NewsPost());
$admin->setSubject(new Post());
$admin->setSubject(null);
}

public function testSetSubjectNotAllowed(): void
{
$admin = new PostAdmin();
$admin->setModelClass(Post::class);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage(sprintf(
'Admin "%s" does not allow this subject: %s, use the one register with this admin class %s',
PostAdmin::class,
Comment::class,
Post::class
));
$admin->setSubject(new Comment());
}

public function testGetSubjectWithParentDescription(): void
{
$adminId = 1;
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/Bundle/Entity/NewsPost.php
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity;

final class NewsPost extends Post
{
}

0 comments on commit 0ced12b

Please sign in to comment.