diff --git a/src/Component/Symfony/Session/Flash/FlashHelper.php b/src/Component/Symfony/Session/Flash/FlashHelper.php index 66b69bf41..e6cfe595d 100644 --- a/src/Component/Symfony/Session/Flash/FlashHelper.php +++ b/src/Component/Symfony/Session/Flash/FlashHelper.php @@ -16,6 +16,7 @@ use Sylius\Component\Resource\Context\Context; use Sylius\Component\Resource\Context\Option\RequestOption; use Sylius\Component\Resource\Humanizer\StringHumanizer; +use Sylius\Component\Resource\Metadata\BulkOperationInterface; use Sylius\Component\Resource\Metadata\Operation; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\Translation\TranslatorBagInterface; @@ -55,11 +56,6 @@ private function buildMessage(Operation $operation, string $type): string $resource = $operation->getResource(); Assert::notNull($resource); - $keys = [ - sprintf('%s.%s.%s', $resource->getApplicationName() ?? '', $resource->getName() ?? '', $operation->getShortName() ?? ''), - sprintf('sylius.resource.%s', $operation->getShortName() ?? ''), - ]; - $specifyKey = sprintf('%s.%s.%s', $resource->getApplicationName() ?? '', $resource->getName() ?? '', $operation->getShortName() ?? ''); $defaultKey = sprintf('sylius.resource.%s', $operation->getShortName() ?? ''); @@ -84,11 +80,13 @@ private function getTranslationParameters(Operation $operation): array return []; } - // TODO plural name with Bulk operation -// if ($operation instanceof BulkOperationInterface) { -// return ['%resources%' => ucfirst($resource->getPluralName() ?? '')]; -// } + $resourceName = $operation instanceof BulkOperationInterface ? $resource->getPluralName() : $resource->getName(); + $humanizedName = ucfirst(StringHumanizer::humanize($resourceName ?? '')); + + if ($operation instanceof BulkOperationInterface) { + return ['%resources%' => $humanizedName]; + } - return ['%resource%' => ucfirst(StringHumanizer::humanize($resource->getName() ?? ''))]; + return ['%resource%' => $humanizedName]; } } diff --git a/src/Component/spec/State/EventDispatcherProviderSpec.php b/src/Component/spec/State/EventDispatcherProviderSpec.php index ac7cc8f7f..6a1c0b4bf 100644 --- a/src/Component/spec/State/EventDispatcherProviderSpec.php +++ b/src/Component/spec/State/EventDispatcherProviderSpec.php @@ -49,6 +49,9 @@ function it_dispatches_events_for_index_operation( $decorated->provide($operation, $context)->shouldBeCalled(); $operationEventDispatcher->dispatch(null, $operation, $context)->willReturn($operationEvent)->shouldBeCalled(); + $decorated->provide($operation, $context)->shouldBeCalled(); + + $operationEventDispatcher->dispatch(null, $operation, $context)->shouldBeCalled(); $this->provide($operation, $context); } @@ -65,6 +68,9 @@ function it_dispatches_events_for_show_operation( $decorated->provide($operation, $context)->shouldBeCalled(); $operationEventDispatcher->dispatch(null, $operation, $context)->willReturn($operationEvent)->shouldBeCalled(); + $decorated->provide($operation, $context)->shouldBeCalled(); + + $operationEventDispatcher->dispatch(null, $operation, $context)->shouldBeCalled(); $this->provide($operation, $context); } diff --git a/src/Component/spec/Symfony/Session/Flash/FlashHelperSpec.php b/src/Component/spec/Symfony/Session/Flash/FlashHelperSpec.php index 6e6785541..87d8d5fa0 100644 --- a/src/Component/spec/Symfony/Session/Flash/FlashHelperSpec.php +++ b/src/Component/spec/Symfony/Session/Flash/FlashHelperSpec.php @@ -16,6 +16,7 @@ use PhpSpec\ObjectBehavior; use Sylius\Component\Resource\Context\Context; use Sylius\Component\Resource\Context\Option\RequestOption; +use Sylius\Component\Resource\Metadata\BulkDelete; use Sylius\Component\Resource\Metadata\Create; use Sylius\Component\Resource\Metadata\Resource; use Sylius\Component\Resource\Symfony\Session\Flash\FlashHelper; @@ -45,7 +46,7 @@ function it_adds_success_flashes_with_specific_message( TranslatorBagInterface $translator, MessageCatalogueInterface $messageCatalogue, ): void { - $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'dummy')); + $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'dummy', applicationName: 'app')); $context = new Context(new RequestOption($request->getWrappedObject())); $request->getSession()->willReturn($session); @@ -70,7 +71,7 @@ function it_adds_success_flashes_with_default_message( TranslatorBagInterface $translator, MessageCatalogueInterface $messageCatalogue, ): void { - $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'dummy')); + $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'dummy', applicationName: 'app')); $context = new Context(new RequestOption($request->getWrappedObject())); $request->getSession()->willReturn($session); @@ -94,7 +95,7 @@ function it_adds_success_flashes_with_default_message_when_translator_is_not_a_b FlashBagInterface $flashBag, TranslatorInterface $translator, ): void { - $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'dummy')); + $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'dummy', applicationName: 'app')); $context = new Context(new RequestOption($request->getWrappedObject())); $request->getSession()->willReturn($session); @@ -115,7 +116,7 @@ function it_adds_success_flashes_with_humanized_message( TranslatorBagInterface $translator, MessageCatalogueInterface $messageCatalogue, ): void { - $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', applicationName: 'app', name: 'admin_user')); + $operation = (new Create())->withResource(new Resource(alias: 'app.dummy', name: 'admin_user', applicationName: 'app')); $context = new Context(new RequestOption($request->getWrappedObject())); $request->getSession()->willReturn($session); @@ -126,9 +127,34 @@ function it_adds_success_flashes_with_humanized_message( $messageCatalogue->has('app.admin_user.create', 'flashes')->willReturn(true)->shouldBeCalled(); - $translator->trans('app.admin_user.create', ['%resource%' => 'Admin user'], 'flashes')->willReturn('Dummy was created successfully.')->shouldBeCalled(); + $translator->trans('app.admin_user.create', ['%resource%' => 'Admin user'], 'flashes')->willReturn('Admin user was created successfully.')->shouldBeCalled(); - $flashBag->add('success', 'Dummy was created successfully.')->shouldBeCalled(); + $flashBag->add('success', 'Admin user was created successfully.')->shouldBeCalled(); + + $this->addSuccessFlash($operation, $context); + } + + function it_adds_success_flashes_with_humanized_message_and_plural_name_on_bulk_operation( + Request $request, + SessionInterface $session, + FlashBagInterface $flashBag, + TranslatorBagInterface $translator, + MessageCatalogueInterface $messageCatalogue, + ): void { + $operation = (new BulkDelete())->withResource(new Resource(alias: 'app.dummy', name: 'admin_user', pluralName: 'admin_users', applicationName: 'app')); + $context = new Context(new RequestOption($request->getWrappedObject())); + + $request->getSession()->willReturn($session); + + $session->getBag('flashes')->willReturn($flashBag); + + $translator->getCatalogue()->willReturn($messageCatalogue); + + $messageCatalogue->has('app.admin_user.bulk_delete', 'flashes')->willReturn(true)->shouldBeCalled(); + + $translator->trans('app.admin_user.bulk_delete', ['%resources%' => 'Admin users'], 'flashes')->willReturn('Admin users was removed successfully.')->shouldBeCalled(); + + $flashBag->add('success', 'Admin users was removed successfully.')->shouldBeCalled(); $this->addSuccessFlash($operation, $context); }