From b1a4467e83f1f1d33fdb32358544b0c9279ea190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Wed, 22 Jun 2022 19:10:23 -0400 Subject: [PATCH] Fix issue 6904 by removing the subclass parameter from model auto complete --- .../Type/sonata_type_model_autocomplete.html.twig | 5 ----- tests/App/Admin/FooAdmin.php | 15 ++++++++++++++- tests/App/Model/Foo.php | 8 ++++++++ .../Functional/Controller/CRUDControllerTest.php | 9 ++++++++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Resources/views/Form/Type/sonata_type_model_autocomplete.html.twig b/src/Resources/views/Form/Type/sonata_type_model_autocomplete.html.twig index 7973e723969..38e162b7f5c 100644 --- a/src/Resources/views/Form/Type/sonata_type_model_autocomplete.html.twig +++ b/src/Resources/views/Form/Type/sonata_type_model_autocomplete.html.twig @@ -114,11 +114,6 @@ file that was distributed with this source code. '_sonata_admin': '{{ admin_code|e('js') }}', {% endif %} - // subclass - {% if app.request.query.get('subclass') %} - 'subclass': '{{ app.request.query.get('subclass') }}', - {% endif %} - {% if context == 'filter' %} 'field': '{{ full_name|replace({'filter[': '', '][value]': '', '__':'.'}) }}', '_context': 'filter' diff --git a/tests/App/Admin/FooAdmin.php b/tests/App/Admin/FooAdmin.php index 9caa5038621..3718a7d38e4 100644 --- a/tests/App/Admin/FooAdmin.php +++ b/tests/App/Admin/FooAdmin.php @@ -19,8 +19,10 @@ use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface; use Sonata\AdminBundle\Form\FormMapper; +use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; use Sonata\AdminBundle\Form\Type\TemplateType; use Sonata\AdminBundle\Show\ShowMapper; +use Sonata\AdminBundle\Tests\App\Model\Bar; use Sonata\AdminBundle\Tests\App\Model\Foo; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -51,7 +53,18 @@ protected function configureFormFields(FormMapper $form): void ->add('customField', TemplateType::class, [ 'template' => 'foo/custom_field.html.twig', 'parameters' => ['number' => 42], - ]); + ]) + ->add( + 'referenced', + ModelAutocompleteType::class, + [ + 'class' => Bar::class, + 'property' => ['name', 'title'], + ], + [ + 'admin_code' => 'sonata_bar_admin', + ] + ); } protected function configureShowFields(ShowMapper $show): void diff --git a/tests/App/Model/Foo.php b/tests/App/Model/Foo.php index 5dcc03ce9fe..e542182d753 100644 --- a/tests/App/Model/Foo.php +++ b/tests/App/Model/Foo.php @@ -24,6 +24,8 @@ final class Foo implements EntityInterface */ private array $elements; + private ?Bar $referenced; + /** * @param string[] $elements */ @@ -32,6 +34,7 @@ public function __construct(string $id, string $name, array $elements = []) $this->id = $id; $this->name = $name; $this->elements = $elements; + $this->referenced = null; } public function getId(): string @@ -44,6 +47,11 @@ public function getName(): string return $this->name; } + public function getReferenced(): ?Bar + { + return $this->referenced; + } + /** * @return string[] */ diff --git a/tests/Functional/Controller/CRUDControllerTest.php b/tests/Functional/Controller/CRUDControllerTest.php index 74df9828463..3904057819f 100644 --- a/tests/Functional/Controller/CRUDControllerTest.php +++ b/tests/Functional/Controller/CRUDControllerTest.php @@ -34,8 +34,9 @@ public function testList(): void public function testCreate(): void { + $subclass = uniqid('subclass'); $client = static::createClient(); - $crawler = $client->request(Request::METHOD_GET, '/admin/tests/app/foo/create'); + $crawler = $client->request(Request::METHOD_GET, '/admin/tests/app/foo/create?subclass=' . $subclass); static::assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); static::assertCount( @@ -46,6 +47,12 @@ public function testCreate(): void 1, $crawler->filter('.help-block.sonata-ba-field-help:contains("Help me!")') ); + + static::assertStringNotContainsString( + $subclass, + $crawler->filter('div[id$=_referenced]')->text(), + 'The subclass parameter must no be present in referenced model auto complete ajax call' + ); } public function testShow(): void