Skip to content

Commit

Permalink
explicitly cast types in CRUDController::batchAction()
Browse files Browse the repository at this point in the history
add tests

comment idx in tests

use data provider in tests
  • Loading branch information
peter-gribanov authored and franmomu committed Nov 26, 2020
1 parent 12ed930 commit ddbc55b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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 @@ -3602,21 +3602,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.
*
* @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

0 comments on commit ddbc55b

Please sign in to comment.