From ddbc55bfa05987144028f4ad8783037c6772d391 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Fri, 23 Oct 2020 15:32:12 +0300 Subject: [PATCH] explicitly cast types in CRUDController::batchAction() add tests comment idx in tests use data provider in tests --- src/Controller/CRUDController.php | 7 ++++--- tests/Controller/CRUDControllerTest.php | 14 +++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Controller/CRUDController.php b/src/Controller/CRUDController.php index 4d642b6f84..a8fb3a036f 100644 --- a/src/Controller/CRUDController.php +++ b/src/Controller/CRUDController.php @@ -426,8 +426,8 @@ public function batchAction() if ($data = json_decode((string) $request->get('data'), true)) { $action = $data['action']; - $idx = $data['idx']; - $allElements = (bool) $data['all_elements']; + $idx = (array) ($data['idx'] ?? []); + $allElements = (bool) ($data['all_elements'] ?? false); $forwardedRequest->request->replace(array_merge($forwardedRequest->request->all(), $data)); } else { $action = $forwardedRequest->request->get('action'); @@ -437,7 +437,7 @@ public function batchAction() // symfony 5.1+ $idx = $bag->all('idx'); } else { - $idx = $bag->get('idx', []); + $idx = (array) $bag->get('idx', []); } $allElements = $forwardedRequest->request->getBoolean('all_elements'); @@ -445,6 +445,7 @@ public function batchAction() $forwardedRequest->request->set('all_elements', $allElements); $data = $forwardedRequest->request->all(); + $data['all_elements'] = $allElements; unset($data['_sonata_csrf_token']); } diff --git a/tests/Controller/CRUDControllerTest.php b/tests/Controller/CRUDControllerTest.php index 78b609d5d8..a816fba3ef 100644 --- a/tests/Controller/CRUDControllerTest.php +++ b/tests/Controller/CRUDControllerTest.php @@ -3602,12 +3602,22 @@ public function testBatchActionWithoutConfirmation2(): void $this->assertSame('list', $result->getTargetUrl()); } + public function provideConfirmationData(): iterable + { + yield 'normal data' => [['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]]; + yield 'without all elements' => [['action' => 'delete', 'idx' => ['123', '456']]]; + yield 'all elements' => [['action' => 'delete', 'all_elements' => true]]; + yield 'idx is null' => [['action' => 'delete', 'idx' => null, 'all_elements' => true]]; + yield 'all_elements is null' => [['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => null]]; + } + /** * NEXT_MAJOR: Remove this legacy group. * + * @dataProvider provideConfirmationData * @group legacy */ - public function testBatchActionWithConfirmation(): void + public function testBatchActionWithConfirmation(array $data): void { $batchActions = ['delete' => ['label' => 'Foo Bar', 'translation_domain' => 'FooBarBaz', 'ask_confirmation' => true]]; @@ -3615,8 +3625,6 @@ public function testBatchActionWithConfirmation(): void ->method('getBatchActions') ->willReturn($batchActions); - $data = ['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => false]; - $this->request->setMethod(Request::METHOD_POST); $this->request->request->set('data', json_encode($data)); $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');