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

Bugfix/issue 8168 fix batch consistency #8177

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
18 changes: 10 additions & 8 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,17 @@ public function batchAction(Request $request): Response
}
}

if (\count($idx) > 0) {
$this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
} elseif (!$allElements) {
$this->addFlash(
'sonata_flash_info',
$this->trans('flash_batch_no_elements_processed', [], 'SonataAdminBundle')
);
if (!$allElements) {
if (\count($idx) > 0) {
$this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
} else {
$this->addFlash(
'sonata_flash_info',
$this->trans('flash_batch_no_elements_processed', [], 'SonataAdminBundle')
);

return $this->redirectToList();
return $this->redirectToList();
}
}

return \call_user_func($batchActionExecutable, $query, $forwardedRequest);
Expand Down
47 changes: 47 additions & 0 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4512,6 +4512,53 @@ public function testBatchActionWithRequesData(): void
static::assertSame('bar', $this->request->request->get('foo'));
}

public function testBatchActionWithRequesDataIdsAndAllItems(): void
{
$batchActions = ['delete' => ['label' => 'Foo Bar', 'ask_confirmation' => false]];

$this->admin->expects(static::exactly(2))
->method('getBatchActions')
->willReturn($batchActions);

$this->expectGetController();

$datagrid = $this->createMock(DatagridInterface::class);

$query = $this->createMock(ProxyQueryInterface::class);
$datagrid->expects(static::once())
->method('getQuery')
->willReturn($query);

$this->admin->expects(static::once())
->method('getDatagrid')
->willReturn($datagrid);

$this->httpKernel->expects(static::once())
->method('handle')
->willReturn($response = new Response());

$modelManager = $this->createMock(ModelManagerInterface::class);

$this->admin
->method('getModelManager')
->willReturn($modelManager);

$this->admin
->method('getClass')
->willReturn('Foo');

$modelManager->expects(static::never())
->method('addIdentifiersToQuery');

$this->request->setMethod(Request::METHOD_POST);
$this->request->request->set('data', json_encode(['action' => 'delete', 'idx' => ['123', '456'], 'all_elements' => true]));
$this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.batch');

$result = $this->controller->batchAction($this->request);

static::assertSame($response, $result);
}

/**
* @phpstan-return iterable<array-key, array{string, string}>
*/
Expand Down
Loading