Skip to content

Commit

Permalink
Merge 4.x into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI authored May 3, 2024
2 parents 9b7a118 + 3745ebb commit 3aa9865
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,17 @@ public function batchAction(Request $request): Response
$extension->preBatchAction($this->admin, $action, $query, $idx, $allElements);
}

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

0 comments on commit 3aa9865

Please sign in to comment.