From 7980d2178e0882f7301f94d371779b017a9e7613 Mon Sep 17 00:00:00 2001 From: Daniel Gohlke Date: Fri, 20 Nov 2026 14:15:53 +0100 Subject: [PATCH] [BUGFIX] Pass errors as FlashMessages in RetrieveProductsFromRequest Co-Authored-By: Claude Opus 5.5 --- .../RetrieveProductsFromRequest.php | 32 ++-- Resources/Private/Language/locallang.xlf | 6 + .../RetrieveProductsFromRequestTest.php | 137 ++++++++++++++++++ 3 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 Tests/Unit/EventListener/RetrieveProductsFromRequestTest.php diff --git a/Classes/EventListener/RetrieveProductsFromRequest.php b/Classes/EventListener/RetrieveProductsFromRequest.php index 07044cd..b0cf9f2 100644 --- a/Classes/EventListener/RetrieveProductsFromRequest.php +++ b/Classes/EventListener/RetrieveProductsFromRequest.php @@ -15,6 +15,7 @@ use Extcode\Cart\Event\RetrieveProductsFromRequestEvent; use Extcode\CartBooks\Domain\Model\Book; use Extcode\CartBooks\Domain\Repository\BookRepository; +use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; @@ -43,6 +44,12 @@ public function __invoke(RetrieveProductsFromRequestEvent $event): void } $book = $this->bookRepository->findByUid((int)$requestArguments['book']); + if (!$book instanceof Book) { + $event->setErrors([ + $this->createError('tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR), + ]); + return; + } $quantity = (int)$requestArguments['quantity']; @@ -69,28 +76,33 @@ protected function getCartProductFromBook( ); } + /** + * @return FlashMessage[] + */ protected function checkRequestArguments(array $requestArguments): array { if (!(int)$requestArguments['book']) { return [ - 'messageBody' => LocalizationUtility::translate( - 'tx_cartbooks.error.book_not_found', - 'CartBooks' - ), - ContextualFeedbackSeverity::ERROR, + $this->createError('tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR), ]; } if ((int)$requestArguments['quantity'] < 0) { return [ - 'messageBody' => LocalizationUtility::translate( - 'tx_cart.error.invalid_quantity', - 'CartBooks' - ), - 'severity' => ContextualFeedbackSeverity::WARNING, + $this->createError('tx_cartbooks.error.invalid_quantity', ContextualFeedbackSeverity::WARNING), ]; } return []; } + + private function createError(string $key, ContextualFeedbackSeverity $severity): FlashMessage + { + return new FlashMessage( + LocalizationUtility::translate($key, 'CartBooks') ?? $key, + '', + $severity, + false + ); + } } diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf index 8e863ae..6cee461 100644 --- a/Resources/Private/Language/locallang.xlf +++ b/Resources/Private/Language/locallang.xlf @@ -25,6 +25,12 @@ Not enough Books available. + + The book could not be found. + + + The quantity is invalid. + Stock keeping unit diff --git a/Tests/Unit/EventListener/RetrieveProductsFromRequestTest.php b/Tests/Unit/EventListener/RetrieveProductsFromRequestTest.php new file mode 100644 index 0000000..b6f16bc --- /dev/null +++ b/Tests/Unit/EventListener/RetrieveProductsFromRequestTest.php @@ -0,0 +1,137 @@ +createEvent(['productType' => 'CartProducts']); + + $this->createSubject()($event); + + self::assertSame([], $event->getErrors()); + self::assertSame([], $event->getProducts()); + } + + #[Test] + public function setsErrorAsFlashMessageForMissingBookArgument(): void + { + $event = $this->createEvent(['productType' => 'CartBooks', 'book' => '0', 'quantity' => '1']); + + $this->mockTranslation(); + $this->createSubject()($event); + + $this->assertSingleError($event, 'tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR); + } + + #[Test] + public function setsErrorAsFlashMessageForInvalidQuantity(): void + { + $event = $this->createEvent(['productType' => 'CartBooks', 'book' => '1', 'quantity' => '-1']); + + $this->mockTranslation(); + $this->createSubject()($event); + + $this->assertSingleError($event, 'tx_cartbooks.error.invalid_quantity', ContextualFeedbackSeverity::WARNING); + } + + #[Test] + public function setsErrorAsFlashMessageForNotExistingBook(): void + { + $event = $this->createEvent(['productType' => 'CartBooks', 'book' => '42', 'quantity' => '1']); + + $bookRepository = self::createStub(BookRepository::class); + $bookRepository->method('findByUid')->willReturn(null); + + $this->mockTranslation(); + (new RetrieveProductsFromRequest($bookRepository))($event); + + $this->assertSingleError($event, 'tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR); + self::assertSame([], $event->getProducts()); + } + + /** + * Lets LocalizationUtility::translate() return the given key. + */ + private function mockTranslation(): void + { + $cacheManager = self::createStub(CacheManager::class); + $cacheManager->method('getCache')->willReturn(new VariableFrontend('runtime', new TransientMemoryBackend('Testing'))); + GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManager); + + $locales = self::createStub(Locales::class); + $locales->method('createLocaleFromRequest')->willReturn(new Locale()); + GeneralUtility::setSingletonInstance(Locales::class, $locales); + + $languageService = self::createStub(LanguageService::class); + $languageService->method('sL')->willReturnCallback( + static fn(string $input): string => substr($input, (int)strrpos($input, ':') + 1) + ); + $languageServiceFactory = self::createStub(LanguageServiceFactory::class); + $languageServiceFactory->method('create')->willReturn($languageService); + GeneralUtility::addInstance(LanguageServiceFactory::class, $languageServiceFactory); + } + + /** + * @param array $arguments + */ + private function createEvent(array $arguments): RetrieveProductsFromRequestEvent + { + $request = self::createStub(Request::class); + $request->method('getArguments')->willReturn($arguments); + + $cart = self::createStub(Cart::class); + $cart->method('getTaxClasses')->willReturn([]); + + return new RetrieveProductsFromRequestEvent($request, $cart); + } + + private function createSubject(): RetrieveProductsFromRequest + { + return new RetrieveProductsFromRequest(self::createStub(BookRepository::class)); + } + + private function assertSingleError( + RetrieveProductsFromRequestEvent $event, + string $message, + ContextualFeedbackSeverity $severity + ): void { + $errors = $event->getErrors(); + + self::assertCount(1, $errors); + self::assertSame($message, $errors[0]->getMessage()); + self::assertSame($severity, $errors[0]->getSeverity()); + } +}