From 6b0a06aecaecee2b3d9856bc10e26981482f6b9f Mon Sep 17 00:00:00 2001 From: mandan2 Date: Mon, 28 Aug 2023 16:54:04 +0300 Subject: [PATCH] improved validations --- controllers/front/ajax.php | 4 -- mollie.php | 5 -- subscription/Exception/ExceptionCode.php | 4 ++ .../Exception/ProductValidationException.php | 10 ---- ...SubscriptionProductValidationException.php | 1 - .../Factory/CreateSubscriptionDataFactory.php | 2 - .../CanProductBeAddedToCartValidator.php | 50 ++----------------- .../Validator/SubscriptionOrderValidator.php | 6 +-- .../SubscriptionProductValidator.php | 1 + ... CanProductBeAddedToCartValidatorTest.php} | 15 +++--- ...php => SubscriptionOrderValidatorTest.php} | 10 ++-- ...p => SubscriptionProductValidatorTest.php} | 9 ++-- 12 files changed, 33 insertions(+), 84 deletions(-) delete mode 100644 subscription/Exception/ProductValidationException.php rename tests/Integration/Subscription/Validator/{SubscriptionCartTest.php => CanProductBeAddedToCartValidatorTest.php} (93%) rename tests/Integration/Subscription/Validator/{SubscriptionOrderTest.php => SubscriptionOrderValidatorTest.php} (94%) rename tests/Integration/Subscription/Validator/{SubscriptionProductTest.php => SubscriptionProductValidatorTest.php} (93%) diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 6c6ff8376..cc93f30a7 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -16,7 +16,6 @@ use Mollie\Exception\FailedToProvidePaymentFeeException; use Mollie\Provider\PaymentFeeProviderInterface; use Mollie\Repository\CurrencyRepositoryInterface; -use Mollie\Subscription\Exception\ProductValidationException; use Mollie\Subscription\Exception\SubscriptionProductValidationException; use Mollie\Subscription\Validator\CanProductBeAddedToCartValidator; use Mollie\Utility\NumberUtility; @@ -192,9 +191,6 @@ private function validateProduct(): void try { $cartValidation->validate((int) $product['id_product_attribute']); - } catch (ProductValidationException $e) { - $productCanBeAdded = false; - $message = $this->module->l('Product cannot be added because you have subscription product in your cart', self::FILE_NAME); } catch (SubscriptionProductValidationException $e) { $productCanBeAdded = false; $message = $this->module->l('Subscription product cannot be added if you have other products in your cart', self::FILE_NAME); diff --git a/mollie.php b/mollie.php index 58e6542db..0b419b8d2 100755 --- a/mollie.php +++ b/mollie.php @@ -26,7 +26,6 @@ use Mollie\Repository\PaymentMethodRepositoryInterface; use Mollie\Service\ExceptionService; use Mollie\ServiceProvider\LeagueServiceContainerProvider; -use Mollie\Subscription\Exception\ProductValidationException; use Mollie\Subscription\Exception\SubscriptionProductValidationException; use Mollie\Subscription\Handler\CustomerAddressUpdateHandler; use Mollie\Subscription\Install\AttributeInstaller; @@ -999,10 +998,6 @@ public function hookActionCartUpdateQuantityBefore($params) } catch (SubscriptionProductValidationException $e) { $product = $this->makeProductNotOrderable($params['product']); - $params['product'] = $product; - } catch (ProductValidationException $e) { - $product = $this->makeProductNotOrderable($params['product']); - $params['product'] = $product; } } diff --git a/subscription/Exception/ExceptionCode.php b/subscription/Exception/ExceptionCode.php index 2fabb1bcd..7820f0276 100644 --- a/subscription/Exception/ExceptionCode.php +++ b/subscription/Exception/ExceptionCode.php @@ -8,4 +8,8 @@ class ExceptionCode public const ORDER_FAILED_TO_CREATE_ORDER_PAYMENT_FEE = 1001; public const ORDER_FAILED_TO_UPDATE_ORDER_TOTAL_WITH_PAYMENT_FEE = 1002; + + //Cart error codes starts from 2000 + + public const CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT = 2001; } diff --git a/subscription/Exception/ProductValidationException.php b/subscription/Exception/ProductValidationException.php deleted file mode 100644 index 52b90e556..000000000 --- a/subscription/Exception/ProductValidationException.php +++ /dev/null @@ -1,10 +0,0 @@ -getCartProducts(); $subscriptionProduct = []; - // TODO add shipping option that is selected in BO - foreach ($products as $product) { if (!$this->subscriptionProductValidator->validate((int) $product['id_product_attribute'])) { continue; diff --git a/subscription/Validator/CanProductBeAddedToCartValidator.php b/subscription/Validator/CanProductBeAddedToCartValidator.php index 5fdd828cd..94382bc21 100644 --- a/subscription/Validator/CanProductBeAddedToCartValidator.php +++ b/subscription/Validator/CanProductBeAddedToCartValidator.php @@ -6,7 +6,7 @@ use Mollie\Adapter\CartAdapter; use Mollie\Adapter\ToolsAdapter; -use Mollie\Subscription\Exception\ProductValidationException; +use Mollie\Subscription\Exception\ExceptionCode; use Mollie\Subscription\Exception\SubscriptionProductValidationException; class CanProductBeAddedToCartValidator @@ -38,6 +38,8 @@ public function __construct( * @param int $productAttributeId * * @return bool + * + * @throws SubscriptionProductValidationException */ public function validate(int $productAttributeId): bool { @@ -49,13 +51,7 @@ public function validate(int $productAttributeId): bool $isNewSubscriptionProduct = $this->subscriptionProduct->validate($productAttributeId); - if ($isNewSubscriptionProduct) { - return $this->validateIfSubscriptionProductCanBeAdded($productAttributeId); - } - - return true; -// -// return $this->validateIfProductCanBeAdded(); + return !$isNewSubscriptionProduct || $this->validateIfSubscriptionProductCanBeAdded($productAttributeId); } /** @@ -78,43 +74,7 @@ private function validateIfSubscriptionProductCanBeAdded(int $productAttributeId continue; } - // TODO new exception to show that only single subscription product can be added - throw new SubscriptionProductValidationException('Cart has multiple products', SubscriptionProductValidationException::MULTTIPLE_PRODUCTS_IN_CART); - } - -// $numberOfProductsInCart = count($cartProducts); -// // we can only have 1 product in cart if its subscription product -// if ($numberOfProductsInCart > 1) { -// throw new SubscriptionProductValidationException('Cart has multiple products', SubscriptionProductValidationException::MULTTIPLE_PRODUCTS_IN_CART); -// } -// -// // if it's the same product we can add more of the same product -// if ($numberOfProductsInCart === 1) { -// $cartProduct = reset($cartProducts); -// -// $isTheSameProduct = $productAttributeId === (int) $cartProduct['id_product_attribute']; -// -// if (!$isTheSameProduct) { -// throw new SubscriptionProductValidationException('Cart has multiple products', SubscriptionProductValidationException::MULTTIPLE_PRODUCTS_IN_CART); -// } -// } - - return true; - } - - /** - * @return bool - * - * @throws ProductValidationException - */ - private function validateIfProductCanBeAdded(): bool - { - $cartProducts = $this->cart->getProducts(); - foreach ($cartProducts as $cartProduct) { - $isSubscriptionProduct = $this->subscriptionProduct->validate((int) $cartProduct['id_product_attribute']); - if ($isSubscriptionProduct) { - throw new ProductValidationException('Cart has subscription products', ProductValidationException::SUBSCRIPTTION_PRODUCTS_IN_CART); - } + throw new SubscriptionProductValidationException('Cart already has subscription product', ExceptionCode::CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT); } return true; diff --git a/subscription/Validator/SubscriptionOrderValidator.php b/subscription/Validator/SubscriptionOrderValidator.php index 33bdea498..7adb56572 100644 --- a/subscription/Validator/SubscriptionOrderValidator.php +++ b/subscription/Validator/SubscriptionOrderValidator.php @@ -21,15 +21,15 @@ public function validate(Cart $cart): bool { $products = $cart->getProducts(); - // TODO add exception handling scenario where subscription fails to be created due to multiple subscription products but flow continues + $subscriptionProductCount = 0; // checks if one of cart products is subscription product foreach ($products as $product) { if ($this->subscriptionProduct->validate((int) $product['id_product_attribute'])) { - return true; + ++$subscriptionProductCount; } } - return false; + return $subscriptionProductCount > 0 && $subscriptionProductCount < 2; } } diff --git a/subscription/Validator/SubscriptionProductValidator.php b/subscription/Validator/SubscriptionProductValidator.php index 3c24ec80a..e00f345f2 100644 --- a/subscription/Validator/SubscriptionProductValidator.php +++ b/subscription/Validator/SubscriptionProductValidator.php @@ -43,6 +43,7 @@ public function validate(int $productAttributeId): bool { $combination = $this->combination->getById($productAttributeId); $attributeIds = $this->combinationRepository->getIds((int) $combination->id); + foreach ($attributeIds as $attributeId) { if ($this->isSubscriptionAttribute((int) $attributeId['id_attribute'])) { return true; diff --git a/tests/Integration/Subscription/Validator/SubscriptionCartTest.php b/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php similarity index 93% rename from tests/Integration/Subscription/Validator/SubscriptionCartTest.php rename to tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php index 248217403..319709425 100644 --- a/tests/Integration/Subscription/Validator/SubscriptionCartTest.php +++ b/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php @@ -1,9 +1,10 @@ randomAttributeId = self::NORMAL_PRODUCT_ATTRIBUTE_ID; @@ -58,7 +60,6 @@ protected function setUp(): void */ public function testValidate(string $combinationReference, bool $hasExtraAttribute, array $cartProducts, $expectedResult): void { - $language = new Language(1); $cartAdapterMock = $this->createMock(CartAdapter::class); $cartProducts = array_map(function (array $product) { @@ -114,7 +115,7 @@ public function productDataProvider(): array 'id_product_attribute' => self::NORMAL_PRODUCT_ATTRIBUTE_ID, ], ], - 'expected result' => SubscriptionProductValidationException::class, + 'expected result' => true, ], 'Add subscription product but already have another subscription product in cart' => [ 'subscription reference' => Config::SUBSCRIPTION_ATTRIBUTE_DAILY, @@ -134,7 +135,7 @@ public function productDataProvider(): array 'id_product_attribute' => Config::SUBSCRIPTION_ATTRIBUTE_MONTHLY, ], ], - 'expected result' => ProductValidationException::class, + 'expected result' => true, ], 'Add normal product but already have another normal product in cart' => [ 'subscription reference' => '', @@ -159,6 +160,6 @@ private function getCombination(string $combinationReference, bool $hasExtraAttr ]) : $this->randomAttributeId; } - return (int) Combination::getIdByReference(1, $reference); + return (int) \Combination::getIdByReference(1, $reference); } } diff --git a/tests/Integration/Subscription/Validator/SubscriptionOrderTest.php b/tests/Integration/Subscription/Validator/SubscriptionOrderValidatorTest.php similarity index 94% rename from tests/Integration/Subscription/Validator/SubscriptionOrderTest.php rename to tests/Integration/Subscription/Validator/SubscriptionOrderValidatorTest.php index df246ecc5..dd2467b2d 100644 --- a/tests/Integration/Subscription/Validator/SubscriptionOrderTest.php +++ b/tests/Integration/Subscription/Validator/SubscriptionOrderValidatorTest.php @@ -1,19 +1,21 @@ randomAttributeId = self::NORMAL_PRODUCT_ATTRIBUTE_ID; @@ -64,7 +66,7 @@ public function testValidate(array $orderProducts, $expectedResult): void $combinationMock = $this->createMock(CombinationAdapter::class); $combinationMock ->method('getById') - ->willReturn(new Combination(1)); + ->willReturn(new \Combination(1)); $subscriptionProductMock = $this->createMock(SubscriptionProductValidator::class); $mockedValidation = [ @@ -106,7 +108,7 @@ public function productDataProvider(): array Config::SUBSCRIPTION_ATTRIBUTE_DAILY, self::NORMAL_PRODUCT_ATTRIBUTE_ID, ], - 'expected result' => false, + 'expected result' => true, ], 'Only normal product' => [ 'order products' => [ diff --git a/tests/Integration/Subscription/Validator/SubscriptionProductTest.php b/tests/Integration/Subscription/Validator/SubscriptionProductValidatorTest.php similarity index 93% rename from tests/Integration/Subscription/Validator/SubscriptionProductTest.php rename to tests/Integration/Subscription/Validator/SubscriptionProductValidatorTest.php index 0b50b6c68..7448e07cb 100644 --- a/tests/Integration/Subscription/Validator/SubscriptionProductTest.php +++ b/tests/Integration/Subscription/Validator/SubscriptionProductValidatorTest.php @@ -1,15 +1,18 @@ randomAttributeId = 1; @@ -90,6 +93,6 @@ private function getCombination(string $combinationReference, bool $hasExtraAttr ]) : $this->randomAttributeId; } - return (int) Combination::getIdByReference(1, $reference); + return (int) \Combination::getIdByReference(1, $reference); } }