Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly cast types in CRUDController::batchAction() #6529

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -437,14 +437,15 @@ 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');

$forwardedRequest->request->set('idx', $idx);
$forwardedRequest->request->set('all_elements', $allElements);

$data = $forwardedRequest->request->all();
$data['all_elements'] = $allElements;

unset($data['_sonata_csrf_token']);
}
Expand Down
14 changes: 11 additions & 3 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3590,21 +3590,29 @@ 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why the legacy group is here. Is there any instruction that will be resolved in the next major about these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests are copy/paste of CRUDControllerTest::testBatchActionWithConfirmation().

*
* @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]];

$this->admin->expects($this->once())
->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');
Expand Down