diff --git a/Classes/Domain/Finisher/Form/AddToCartFinisher.php b/Classes/Domain/Finisher/Form/AddToCartFinisher.php deleted file mode 100644 index 3360fc2..0000000 --- a/Classes/Domain/Finisher/Form/AddToCartFinisher.php +++ /dev/null @@ -1,88 +0,0 @@ - $formValues['eventDateId'], - 'priceCategory' => $formValues['priceCategoryId'], - 'quantity' => $formValues['quantity'] ?? 1, - ]; - unset($formValues['eventDateId']); - unset($formValues['priceCategoryId']); - unset($formValues['quantity']); - - if (!empty($formValues)) { - $requestArguments['feVariant'] = $this->getFeVariant($formValues); - } - $newProduct = $this->productFactory->createProductFromRequestArguments( - $requestArguments, - $cart->getTaxClasses(), - (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('cart_events', 'inputIsNetPrice') - ); - - $newProduct->setMaxNumberInCart(1); - $newProduct->setMinNumberInCart(1); - - return [$errors, [$newProduct]]; - } - - protected function getFeVariant(array $data): ?FeVariant - { - $feVariant = null; - - if (!empty($data)) { - $feVariants = []; - foreach ($data as $dataKey => $dataValue) { - if (!empty($dataKey) && !empty($dataValue)) { - $feVariants[] = [ - 'sku' => $dataKey, - 'title' => $dataKey, - 'value' => $dataValue, - ]; - } - } - - $feVariant = GeneralUtility::makeInstance( - FeVariant::class, - $feVariants - ); - } - - return $feVariant; - } -} diff --git a/Classes/EventListener/Form/AddToCartFinisher.php b/Classes/EventListener/Form/AddToCartFinisher.php new file mode 100644 index 0000000..032c067 --- /dev/null +++ b/Classes/EventListener/Form/AddToCartFinisher.php @@ -0,0 +1,101 @@ +getFormValues(); + + if (($formValues['productType'] ?? '') !== 'CartEvents') { + return; + } + + $requestArguments = [ + 'eventDate' => $formValues['eventDateId'] ?? null, + 'quantity' => $formValues['quantity'] ?? 1, + ]; + if (!empty($formValues['priceCategoryId'])) { + $requestArguments['priceCategory'] = (int)$formValues['priceCategoryId']; + } + + unset( + $formValues['productType'], + $formValues['eventDateId'], + $formValues['priceCategoryId'], + $formValues['quantity'], + ); + + $feVariant = $this->getFeVariant($formValues); + if ($feVariant instanceof FeVariant) { + $requestArguments['feVariant'] = $feVariant; + } + + try { + $product = $this->productFactory->createProductFromRequestArguments( + $requestArguments, + $event->getCart()->getTaxClasses(), + (bool)$this->extensionConfiguration->get('cart_events', 'inputIsNetPrice'), + ); + } catch (Exception $exception) { + $event->setErrors( + [ + 'messageBody' => LocalizationUtility::translate( + (string)$exception->getCode(), + 'CartEvents' + ) ?? $exception->getMessage(), + 'severity' => ContextualFeedbackSeverity::ERROR, + ] + ); + return; + } + + $product->setMaxNumberInCart(1); + $product->setMinNumberInCart(1); + + $event->setCartProducts([$product]); + } + + private function getFeVariant(array $formValues): ?FeVariant + { + $feVariants = []; + foreach ($formValues as $key => $value) { + if (!empty($key) && !empty($value)) { + $feVariants[] = [ + 'sku' => $key, + 'title' => $key, + 'value' => $value, + ]; + } + } + + if ($feVariants === []) { + return null; + } + + return new FeVariant($feVariants); + } +} diff --git a/Configuration/Services/EventListeners.php b/Configuration/Services/EventListeners.php index 93effde..3d69496 100644 --- a/Configuration/Services/EventListeners.php +++ b/Configuration/Services/EventListeners.php @@ -3,9 +3,11 @@ declare(strict_types=1); use Extcode\Cart\Event\CheckProductAvailabilityEvent; +use Extcode\Cart\Event\Form\AddToCartFinisherEvent; use Extcode\Cart\Event\Order\StockEvent; use Extcode\Cart\Event\RetrieveProductsFromRequestEvent as CartRetrieveProductsFromRequestEvent; use Extcode\CartEvents\EventListener\CheckProductAvailability; +use Extcode\CartEvents\EventListener\Form\AddToCartFinisher; use Extcode\CartEvents\EventListener\Order\Stock\FlushCache; use Extcode\CartEvents\EventListener\Order\Stock\HandleStock; use Extcode\CartEvents\EventListener\RetrieveProductsFromRequest; @@ -62,4 +64,15 @@ ] ) ; + + $services + ->set(AddToCartFinisher::class) + ->tag( + 'event.listener', + [ + 'event' => AddToCartFinisherEvent::class, + 'identifier' => 'cart-events--form--add-to-cart-finisher', + ] + ) + ; }; diff --git a/Tests/Functional/EventListener/Form/AddToCartFinisherTest.php b/Tests/Functional/EventListener/Form/AddToCartFinisherTest.php new file mode 100644 index 0000000..4df9e3a --- /dev/null +++ b/Tests/Functional/EventListener/Form/AddToCartFinisherTest.php @@ -0,0 +1,110 @@ +testExtensionsToLoad[] = 'extcode/cart'; + $this->testExtensionsToLoad[] = 'extcode/cart-events'; + + parent::setUp(); + + $this->importPHPDataSet(__DIR__ . '/../../../Fixtures/PagesDatabase.php'); + $this->importPHPDataSet(__DIR__ . '/../../../Fixtures/EventsDatabase.php'); + } + + #[Test] + public function dispatchedEventContainsProductForBookableEventDate(): void + { + $event = new AddToCartFinisherEvent( + [ + 'productType' => 'CartEvents', + 'eventDateId' => '3', + 'priceCategoryId' => '', + 'firstname' => 'Jane', + ], + $this->getCart() + ); + + $this->get(EventDispatcherInterface::class)->dispatch($event); + + self::assertSame([], $event->getErrors()); + + $products = $event->getCartProducts(); + self::assertCount(1, $products); + + $product = $products[0]; + self::assertInstanceOf(Product::class, $product); + self::assertSame(3, $product->getProductId()); + self::assertSame(1, $product->getQuantity()); + self::assertSame('Event 3 - Eventdate 3.1', $product->getTitle()); + self::assertSame(1, $product->getMaxNumberInCart()); + self::assertSame(1, $product->getMinNumberInCart()); + self::assertSame( + [ + [ + 'sku' => 'firstname', + 'title' => 'firstname', + 'value' => 'Jane', + ], + ], + $product->getFeVariant()?->getVariantData() + ); + } + + #[Test] + public function dispatchedEventContainsErrorForNotBookableEventDate(): void + { + $event = new AddToCartFinisherEvent( + [ + 'productType' => 'CartEvents', + 'eventDateId' => '1', + ], + $this->getCart() + ); + + $this->get(EventDispatcherInterface::class)->dispatch($event); + + self::assertSame([], $event->getCartProducts()); + self::assertNotSame([], $event->getErrors()); + } + + private function getCart(): Cart + { + return new Cart( + [ + 1 => new TaxClass( + 1, + '19 %', + 0.19, + 'normal' + ), + ] + ); + } +} diff --git a/Tests/Unit/EventListener/Form/AddToCartFinisherTest.php b/Tests/Unit/EventListener/Form/AddToCartFinisherTest.php new file mode 100644 index 0000000..d042193 --- /dev/null +++ b/Tests/Unit/EventListener/Form/AddToCartFinisherTest.php @@ -0,0 +1,169 @@ +productFactory = $this->createMock(ProductFactoryInterface::class); + + $this->cart = self::createStub(Cart::class); + $this->cart->method('getTaxClasses')->willReturn([]); + } + + #[Test] + public function ignoresFormsOfOtherProductTypes(): void + { + $this->productFactory->expects(self::never())->method('createProductFromRequestArguments'); + + $event = new AddToCartFinisherEvent(['productType' => 'CartProducts'], $this->cart); + $this->createSubject()($event); + + self::assertSame([], $event->getCartProducts()); + self::assertSame([], $event->getErrors()); + } + + #[Test] + public function addsProductCreatedFromFormValues(): void + { + $product = $this->createProduct(); + + $this->productFactory + ->expects(self::once()) + ->method('createProductFromRequestArguments') + ->with( + [ + 'eventDate' => '3', + 'quantity' => 1, + 'priceCategory' => 5, + ], + [], + true + ) + ->willReturn($product); + + $event = new AddToCartFinisherEvent( + [ + 'productType' => 'CartEvents', + 'eventDateId' => '3', + 'priceCategoryId' => '5', + ], + $this->cart + ); + $this->createSubject()($event); + + self::assertSame([$product], $event->getCartProducts()); + self::assertSame(1, $product->getMaxNumberInCart()); + self::assertSame(1, $product->getMinNumberInCart()); + self::assertSame([], $event->getErrors()); + } + + #[Test] + public function passesNoPriceCategoryForEmptyPriceCategoryId(): void + { + $this->productFactory + ->expects(self::once()) + ->method('createProductFromRequestArguments') + ->with( + [ + 'eventDate' => '3', + 'quantity' => '2', + ] + ) + ->willReturn($this->createProduct()); + + $event = new AddToCartFinisherEvent( + [ + 'productType' => 'CartEvents', + 'eventDateId' => '3', + 'priceCategoryId' => '', + 'quantity' => '2', + ], + $this->cart + ); + $this->createSubject()($event); + } + + #[Test] + public function passesAdditionalNonEmptyFormValuesAsFrontendVariant(): void + { + $this->productFactory + ->expects(self::once()) + ->method('createProductFromRequestArguments') + ->with( + self::callback( + static fn(array $requestArguments): bool => $requestArguments['feVariant'] instanceof FeVariant + && $requestArguments['feVariant']->getVariantData() === [ + [ + 'sku' => 'firstname', + 'title' => 'firstname', + 'value' => 'Jane', + ], + ] + ) + ) + ->willReturn($this->createProduct()); + + $event = new AddToCartFinisherEvent( + [ + 'productType' => 'CartEvents', + 'eventDateId' => '3', + 'firstname' => 'Jane', + 'honeypot' => '', + ], + $this->cart + ); + $this->createSubject()($event); + } + + private function createProduct(): Product + { + return new Product( + 'CartEvents', + 3, + 'event-date', + 'Event date', + 10.0, + new TaxClass(1, '19', 0.19, 'normal'), + 1 + ); + } + + private function createSubject(): AddToCartFinisher + { + $extensionConfiguration = self::createStub(ExtensionConfiguration::class); + $extensionConfiguration->method('get')->willReturn('1'); + + return new AddToCartFinisher($extensionConfiguration, $this->productFactory); + } +} diff --git a/ext_localconf.php b/ext_localconf.php index f41d16f..7ea8b1e 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -2,7 +2,6 @@ use Extcode\CartEvents\Controller\EventController; use Extcode\CartEvents\Controller\EventDateController; -use Extcode\CartEvents\Domain\Finisher\Form\AddToCartFinisher; use Extcode\CartEvents\Hooks\DataHandler; use Extcode\CartEvents\Hooks\DatamapDataHandlerHook; use TYPO3\CMS\Core\Utility\ArrayUtility; @@ -41,15 +40,6 @@ ], ], ], - 'EXTCONF' => [ - 'cart' => [ - 'CartEvents' => [ - 'Form' => [ - 'AddToCartFinisher' => AddToCartFinisher::class, - ], - ], - ], - ], 'SC_OPTIONS' => [ 't3lib/class.t3lib_tcemain.php' => [ 'processDatamapClass' => [