From c3624995b2ffba37a694fb1f3ae8f195f3a19648 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 --- .../sonata_type_model_autocomplete.html.twig | 5 ----- tests/App/Admin/FooAdmin.php | 15 ++++++++++++++- tests/App/Model/Foo.php | 8 ++++++++ .../Controller/CRUDControllerTest.php | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 6 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 7973e72396..38e162b7f5 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 9caa503862..3718a7d38e 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 5dcc03ce9f..e542182d75 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 74df982846..b2bab4f407 100644 --- a/tests/Functional/Controller/CRUDControllerTest.php +++ b/tests/Functional/Controller/CRUDControllerTest.php @@ -48,6 +48,24 @@ public function testCreate(): void ); } + /** + * https://github.com/sonata-project/SonataAdminBundle/issues/6904. + */ + public function testCreateModelAutoCompleteNotPassingSubclassParameter(): void + { + $subclass = uniqid('subclass'); + $client = static::createClient(); + $crawler = $client->request(Request::METHOD_GET, '/admin/tests/app/foo/create?subclass='.$subclass); + + static::assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); + + 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 { $client = static::createClient();