From 2ee045a73918c680c95684022da15b21f6c40f2c Mon Sep 17 00:00:00 2001 From: mandan2 <61560082+mandan2@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:11:27 +0200 Subject: [PATCH] PIPRES-349: Restrict subscription product add to cart based on subscription settings (#850) * PIPRES-349: Restrict subscription product add to cart based on subscription settings * subtracted validation to a single service --- controllers/front/ajax.php | 45 +++++---- mollie.php | 11 +-- src/Config/Config.php | 2 +- src/Install/Installer.php | 2 +- src/Install/Uninstall.php | 2 +- .../CouldNotValidateSubscriptionSettings.php | 36 +++++++ subscription/Exception/ExceptionCode.php | 5 +- ...SubscriptionProductValidationException.php | 15 +++ .../SubscriptionOptionsConfiguration.php | 4 +- .../CanProductBeAddedToCartValidator.php | 38 ++++++-- .../SubscriptionSettingsValidator.php | 73 ++++++++++++++ .../CanProductBeAddedToCartValidatorTest.php | 97 +++++++++++++++++-- .../SubscriptionSettingsValidatorTest.php | 82 ++++++++++++++++ tests/Unit/Subscription/Validator/index.php | 20 ++++ upgrade/Upgrade-6.0.6.php | 2 +- views/js/front/subscription/product.js | 16 ++- 16 files changed, 400 insertions(+), 50 deletions(-) create mode 100644 subscription/Exception/CouldNotValidateSubscriptionSettings.php create mode 100644 subscription/Validator/SubscriptionSettingsValidator.php create mode 100644 tests/Unit/Subscription/Validator/SubscriptionSettingsValidatorTest.php create mode 100644 tests/Unit/Subscription/Validator/index.php diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 12c637541..67e2c44d1 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -13,10 +13,12 @@ use Mollie\Adapter\ConfigurationAdapter; use Mollie\Adapter\ToolsAdapter; use Mollie\Controller\AbstractMollieController; +use Mollie\Errors\Http\HttpStatusCode; use Mollie\Exception\FailedToProvidePaymentFeeException; +use Mollie\Infrastructure\Response\JsonResponse; use Mollie\Provider\PaymentFeeProviderInterface; use Mollie\Shared\Infrastructure\Repository\CurrencyRepositoryInterface; -use Mollie\Subscription\Exception\SubscriptionProductValidationException; +use Mollie\Subscription\Exception\ExceptionCode; use Mollie\Subscription\Validator\CanProductBeAddedToCartValidator; use Mollie\Utility\NumberUtility; @@ -185,30 +187,35 @@ private function displayCheckoutError(): void private function validateProduct(): void { - /** @var CanProductBeAddedToCartValidator $cartValidation */ - $cartValidation = $this->module->getService(CanProductBeAddedToCartValidator::class); + /** @var CanProductBeAddedToCartValidator $canProductBeAddedToCartValidator */ + $canProductBeAddedToCartValidator = $this->module->getService(CanProductBeAddedToCartValidator::class); $product = Tools::getValue('product'); - $productCanBeAdded = true; - $message = ''; - try { - $cartValidation->validate((int) $product['id_product_attribute']); - } catch (SubscriptionProductValidationException $e) { - $productCanBeAdded = false; - $message = $this->module->l('Please note: Only one subscription product can be added to the cart at a time.', self::FILE_NAME); + $canProductBeAddedToCartValidator->validate((int) ($product['id_product_attribute'] ?? 0)); + } catch (\Throwable $exception) { + if ($exception->getCode() === ExceptionCode::CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT) { + $this->ajaxResponse(JsonResponse::error( + $this->module->l('Please note: Only one subscription product can be added to the cart at a time.', self::FILE_NAME), + HttpStatusCode::HTTP_BAD_REQUEST + )); + } + + if ($exception->getCode() === ExceptionCode::CART_INVALID_SUBSCRIPTION_SETTINGS) { + $this->ajaxResponse(JsonResponse::error( + $this->module->l('Subscription service is disabled. Please change the attribute to Subscription: none.', self::FILE_NAME), + HttpStatusCode::HTTP_BAD_REQUEST + )); + } + + $this->ajaxResponse(JsonResponse::error( + $this->module->l('Unknown error. Try again or change the attribute to Subscription: none.', self::FILE_NAME), + HttpStatusCode::HTTP_BAD_REQUEST + )); } - $this->ajaxRender( - json_encode( - [ - 'success' => true, - 'isValid' => $productCanBeAdded, - 'message' => $message, - ] - ) - ); + $this->ajaxResponse(JsonResponse::success([])); } private function returnDefaultOrderSummaryBlock(Cart $cart, array $errorData = [], array $presentedCart = null): void diff --git a/mollie.php b/mollie.php index 67d595207..7c20f5feb 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\SubscriptionProductValidationException; use Mollie\Subscription\Handler\CustomerAddressUpdateHandler; use Mollie\Subscription\Install\AttributeInstaller; use Mollie\Subscription\Install\DatabaseTableInstaller; @@ -1014,14 +1013,14 @@ public function hookDisplayProductAdditionalInfo() return ''; } - public function hookActionCartUpdateQuantityBefore($params) + public function hookActionCartUpdateQuantityBefore($params): void { - /** @var CanProductBeAddedToCartValidator $cartValidation */ - $cartValidation = $this->getService(CanProductBeAddedToCartValidator::class); + /** @var CanProductBeAddedToCartValidator $canProductBeAddedToCartValidator */ + $canProductBeAddedToCartValidator = $this->getService(CanProductBeAddedToCartValidator::class); try { - $cartValidation->validate((int) $params['id_product_attribute']); - } catch (SubscriptionProductValidationException $e) { + $canProductBeAddedToCartValidator->validate((int) $params['id_product_attribute']); + } catch (\Throwable $exception) { $product = $this->makeProductNotOrderable($params['product']); $params['product'] = $product; diff --git a/src/Config/Config.php b/src/Config/Config.php index 611240862..b5fb41220 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -162,7 +162,7 @@ class Config const MOLLIE_CARRIER_CUSTOM_URL = 'MOLLIE_CARRIER_CUSTOM_URL_'; const MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID = 'MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID'; - const MOLLIE_SUBSCRIPTION_ENABLE = 'MOLLIE_SUBSCRIPTION_ENABLE'; + const MOLLIE_SUBSCRIPTION_ENABLED = 'MOLLIE_SUBSCRIPTION_ENABLED'; const MOLLIE_METHOD_ENABLED = 'MOLLIE_METHOD_ENABLED_'; const MOLLIE_METHOD_TITLE = 'MOLLIE_METHOD_TITLE_'; diff --git a/src/Install/Installer.php b/src/Install/Installer.php index 663a0fb2f..aec5a7671 100644 --- a/src/Install/Installer.php +++ b/src/Install/Installer.php @@ -215,7 +215,7 @@ protected function initConfig() $this->configurationAdapter->updateValue(Config::MOLLIE_BANCONTACT_QR_CODE_ENABLED, 0); $this->configurationAdapter->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, 0); - $this->configurationAdapter->updateValue(Config::MOLLIE_SUBSCRIPTION_ENABLE, 0); + $this->configurationAdapter->updateValue(Config::MOLLIE_SUBSCRIPTION_ENABLED, 0); } public function setDefaultCarrierStatuses() diff --git a/src/Install/Uninstall.php b/src/Install/Uninstall.php index dd7cd8be3..e92cc9dd2 100644 --- a/src/Install/Uninstall.php +++ b/src/Install/Uninstall.php @@ -97,7 +97,7 @@ private function deleteConfig() Config::MOLLIE_MAIL_WHEN_COMPLETED, Config::MOLLIE_API_KEY_TEST, Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, - Config::MOLLIE_SUBSCRIPTION_ENABLE, + Config::MOLLIE_SUBSCRIPTION_ENABLED, ]; $this->deleteConfigurations($configurations); diff --git a/subscription/Exception/CouldNotValidateSubscriptionSettings.php b/subscription/Exception/CouldNotValidateSubscriptionSettings.php new file mode 100644 index 000000000..4f2d800f9 --- /dev/null +++ b/subscription/Exception/CouldNotValidateSubscriptionSettings.php @@ -0,0 +1,36 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Subscription\Exception; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class CouldNotValidateSubscriptionSettings extends MollieSubscriptionException +{ + public static function subscriptionServiceDisabled(): self + { + return new self( + 'Subscription service disabled.', + ExceptionCode::CART_SUBSCRIPTION_SERVICE_DISABLED + ); + } + + public static function subscriptionCarrierInvalid(): self + { + return new self( + 'Subscription carrier invalid.', + ExceptionCode::CART_SUBSCRIPTION_CARRIER_INVALID + ); + } +} diff --git a/subscription/Exception/ExceptionCode.php b/subscription/Exception/ExceptionCode.php index c8eada765..acd4fecdc 100644 --- a/subscription/Exception/ExceptionCode.php +++ b/subscription/Exception/ExceptionCode.php @@ -39,7 +39,10 @@ class ExceptionCode //Cart error codes starts from 2000 - public const CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT = 2001; + public const CART_INVALID_SUBSCRIPTION_SETTINGS = 2001; + public const CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT = 2002; + public const CART_SUBSCRIPTION_SERVICE_DISABLED = 2003; + public const CART_SUBSCRIPTION_CARRIER_INVALID = 2004; //Recurring order error codes starts from 3000 diff --git a/subscription/Exception/SubscriptionProductValidationException.php b/subscription/Exception/SubscriptionProductValidationException.php index 439ef2c3e..03c6615f3 100644 --- a/subscription/Exception/SubscriptionProductValidationException.php +++ b/subscription/Exception/SubscriptionProductValidationException.php @@ -20,4 +20,19 @@ class SubscriptionProductValidationException extends MollieSubscriptionException { + public static function invalidSubscriptionSettings(): self + { + return new self( + 'Invalid subscription settings', + ExceptionCode::CART_INVALID_SUBSCRIPTION_SETTINGS + ); + } + + public static function cartAlreadyHasSubscriptionProduct(): self + { + return new self( + 'Cart already has subscription product', + ExceptionCode::CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT + ); + } } diff --git a/subscription/Form/Options/SubscriptionOptionsConfiguration.php b/subscription/Form/Options/SubscriptionOptionsConfiguration.php index 87de28a4e..21df3a522 100644 --- a/subscription/Form/Options/SubscriptionOptionsConfiguration.php +++ b/subscription/Form/Options/SubscriptionOptionsConfiguration.php @@ -38,7 +38,7 @@ public function __construct(Configuration $configuration) public function getConfiguration(): array { return [ - 'enable_subscriptions' => $this->configuration->getBoolean(Config::MOLLIE_SUBSCRIPTION_ENABLE), + 'enable_subscriptions' => $this->configuration->getBoolean(Config::MOLLIE_SUBSCRIPTION_ENABLED), 'carrier' => $this->configuration->getInt(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID), ]; } @@ -53,7 +53,7 @@ public function updateConfiguration(array $configuration): array } $this->configuration->set( - Config::MOLLIE_SUBSCRIPTION_ENABLE, + Config::MOLLIE_SUBSCRIPTION_ENABLED, (int) $configuration['enable_subscriptions'] ); diff --git a/subscription/Validator/CanProductBeAddedToCartValidator.php b/subscription/Validator/CanProductBeAddedToCartValidator.php index 7cbd29f62..6ae2da714 100644 --- a/subscription/Validator/CanProductBeAddedToCartValidator.php +++ b/subscription/Validator/CanProductBeAddedToCartValidator.php @@ -16,7 +16,7 @@ use Mollie\Adapter\CartAdapter; use Mollie\Adapter\ToolsAdapter; -use Mollie\Subscription\Exception\ExceptionCode; +use Mollie\Subscription\Exception\CouldNotValidateSubscriptionSettings; use Mollie\Subscription\Exception\SubscriptionProductValidationException; if (!defined('_PS_VERSION_')) { @@ -33,15 +33,19 @@ class CanProductBeAddedToCartValidator /** @var ToolsAdapter */ private $tools; + /** @var SubscriptionSettingsValidator */ + private $subscriptionSettingsValidator; public function __construct( CartAdapter $cart, SubscriptionProductValidator $subscriptionProductValidator, - ToolsAdapter $tools + ToolsAdapter $tools, + SubscriptionSettingsValidator $subscriptionSettingsValidator ) { $this->cart = $cart; $this->subscriptionProductValidator = $subscriptionProductValidator; $this->tools = $tools; + $this->subscriptionSettingsValidator = $subscriptionSettingsValidator; } /** @@ -55,14 +59,21 @@ public function validate(int $productAttributeId): bool return true; } - $isNewSubscriptionProduct = $this->subscriptionProductValidator->validate($productAttributeId); + if (!$this->subscriptionProductValidator->validate($productAttributeId)) { + return true; + } + + if (!$this->validateSubscriptionSettings()) { + throw SubscriptionProductValidationException::invalidSubscriptionSettings(); + } - return !$isNewSubscriptionProduct || $this->validateIfSubscriptionProductCanBeAdded($productAttributeId); + if (!$this->validateIfSubscriptionProductCanBeAdded($productAttributeId)) { + throw SubscriptionProductValidationException::cartAlreadyHasSubscriptionProduct(); + } + + return true; } - /** - * @throws SubscriptionProductValidationException - */ private function validateIfSubscriptionProductCanBeAdded(int $productAttributeId): bool { $cartProducts = $this->cart->getProducts(); @@ -76,7 +87,18 @@ private function validateIfSubscriptionProductCanBeAdded(int $productAttributeId continue; } - throw new SubscriptionProductValidationException('Cart already has subscription product', ExceptionCode::CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT); + return false; + } + + return true; + } + + private function validateSubscriptionSettings(): bool + { + try { + $this->subscriptionSettingsValidator->validate(); + } catch (CouldNotValidateSubscriptionSettings $exception) { + return false; } return true; diff --git a/subscription/Validator/SubscriptionSettingsValidator.php b/subscription/Validator/SubscriptionSettingsValidator.php new file mode 100644 index 000000000..e3fbe3235 --- /dev/null +++ b/subscription/Validator/SubscriptionSettingsValidator.php @@ -0,0 +1,73 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Subscription\Validator; + +use Mollie\Adapter\ConfigurationAdapter; +use Mollie\Config\Config; +use Mollie\Repository\CarrierRepositoryInterface; +use Mollie\Subscription\Exception\CouldNotValidateSubscriptionSettings; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class SubscriptionSettingsValidator +{ + /** @var ConfigurationAdapter */ + private $configuration; + /** @var CarrierRepositoryInterface */ + private $carrierRepository; + + public function __construct( + ConfigurationAdapter $configuration, + CarrierRepositoryInterface $carrierRepository + ) { + $this->configuration = $configuration; + $this->carrierRepository = $carrierRepository; + } + + /** + * @throws CouldNotValidateSubscriptionSettings + */ + public function validate(): bool + { + if (!$this->isSubscriptionActive()) { + throw CouldNotValidateSubscriptionSettings::subscriptionServiceDisabled(); + } + + if (!$this->isSubscriptionCarrierValid()) { + throw CouldNotValidateSubscriptionSettings::subscriptionCarrierInvalid(); + } + + return true; + } + + private function isSubscriptionActive(): bool + { + return (bool) $this->configuration->get(Config::MOLLIE_SUBSCRIPTION_ENABLED); + } + + private function isSubscriptionCarrierValid(): bool + { + $carrierId = (int) $this->configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID); + + /** @var \Carrier|null $carrier */ + $carrier = $this->carrierRepository->findOneBy([ + 'id_carrier' => $carrierId, + 'active' => 1, + 'deleted' => 0, + ]); + + return (bool) $carrier; + } +} diff --git a/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php b/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php index 56937101e..03d429289 100644 --- a/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php +++ b/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php @@ -13,17 +13,32 @@ namespace Mollie\Tests\Integration\Subscription\Validator; use Mollie\Adapter\CartAdapter; +use Mollie\Adapter\ConfigurationAdapter; use Mollie\Adapter\ToolsAdapter; +use Mollie\Config\Config as SettingsConfig; use Mollie\Subscription\Config\Config; +use Mollie\Subscription\Exception\ExceptionCode; use Mollie\Subscription\Exception\SubscriptionProductValidationException; use Mollie\Subscription\Validator\CanProductBeAddedToCartValidator; use Mollie\Subscription\Validator\SubscriptionProductValidator; +use Mollie\Subscription\Validator\SubscriptionSettingsValidator; use Mollie\Tests\Integration\BaseTestCase; +use Mollie\Tests\Integration\Factory\CarrierFactory; class CanProductBeAddedToCartValidatorTest extends BaseTestCase { private const NORMAL_PRODUCT_ATTRIBUTE_ID = 1; + /** @var \Carrier */ + private $validCarrier; + + public function __construct($name = null, array $data = [], $dataName = '') + { + parent::__construct($name, $data, $dataName); + + $this->validCarrier = CarrierFactory::create(); + } + protected function setUp(): void { parent::setUp(); @@ -64,8 +79,16 @@ protected function setUp(): void /** * @dataProvider productDataProvider */ - public function testValidate(string $combinationReference, bool $hasExtraAttribute, array $cartProducts, $expectedResult): void - { + public function testValidate( + string $combinationReference, + bool $hasExtraAttribute, + array $cartProducts, + bool $subscriptionEnabled, + int $subscriptionCarrierId, + bool $expectedResult, + string $expectedException, + int $expectedExceptionCode + ): void { // TODO cart factory $cart = $this->createMock(CartAdapter::class); @@ -79,14 +102,22 @@ public function testValidate(string $combinationReference, bool $hasExtraAttribu $combination = $this->getCombination($combinationReference, $hasExtraAttribute); + /** @var ConfigurationAdapter $configuration */ + $configuration = $this->getService(ConfigurationAdapter::class); + + $configuration->updateValue(SettingsConfig::MOLLIE_SUBSCRIPTION_ENABLED, $subscriptionEnabled); + $configuration->updateValue(SettingsConfig::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, $subscriptionCarrierId); + $subscriptionCartValidator = new CanProductBeAddedToCartValidator( $cart, $this->getService(SubscriptionProductValidator::class), - $this->getService(ToolsAdapter::class) + $this->getService(ToolsAdapter::class), + $this->getService(SubscriptionSettingsValidator::class) ); - if ($expectedResult !== true) { - $this->expectException($expectedResult); + if (!$expectedResult) { + $this->expectException($expectedException); + $this->expectExceptionCode($expectedExceptionCode); } $canBeAdded = $subscriptionCartValidator->validate($combination); @@ -101,13 +132,51 @@ public function productDataProvider(): array 'subscription reference' => Config::SUBSCRIPTION_ATTRIBUTE_DAILY, 'has extra attribute' => false, 'cart products' => [], + 'subscription enabled' => true, + 'subscription carrier ID' => $this->validCarrier->id, 'expected result' => true, + 'expected exception' => '', + 'expected exception code' => 0, + ], + 'One subscription product disabled subscription' => [ + 'subscription reference' => Config::SUBSCRIPTION_ATTRIBUTE_DAILY, + 'has extra attribute' => false, + 'cart products' => [], + 'subscription enabled' => false, + 'subscription carrier ID' => $this->validCarrier->id, + 'expected result' => false, + 'expected exception' => SubscriptionProductValidationException::class, + 'expected exception code' => ExceptionCode::CART_INVALID_SUBSCRIPTION_SETTINGS, + ], + 'One subscription product invalid carrier' => [ + 'subscription reference' => Config::SUBSCRIPTION_ATTRIBUTE_DAILY, + 'has extra attribute' => false, + 'cart products' => [], + 'subscription enabled' => true, + 'subscription carrier ID' => 0, + 'expected result' => false, + 'expected exception' => SubscriptionProductValidationException::class, + 'expected exception code' => ExceptionCode::CART_INVALID_SUBSCRIPTION_SETTINGS, ], 'One normal product' => [ 'subscription reference' => '', 'has extra attribute' => true, 'cart products' => [], + 'subscription enabled' => true, + 'subscription carrier ID' => $this->validCarrier->id, + 'expected result' => true, + 'expected exception' => '', + 'expected exception code' => 0, + ], + 'One normal product disabled subscription and invalid carrier' => [ + 'subscription reference' => '', + 'has extra attribute' => true, + 'cart products' => [], + 'subscription enabled' => false, + 'subscription carrier ID' => 0, 'expected result' => true, + 'expected exception' => '', + 'expected exception code' => 0, ], 'Add subscription product but already have normal product in cart' => [ 'subscription reference' => Config::SUBSCRIPTION_ATTRIBUTE_DAILY, @@ -117,7 +186,11 @@ public function productDataProvider(): array 'id_product_attribute' => self::NORMAL_PRODUCT_ATTRIBUTE_ID, ], ], + 'subscription enabled' => true, + 'subscription carrier ID' => $this->validCarrier->id, 'expected result' => true, + 'expected exception' => '', + 'expected exception code' => 0, ], 'Add subscription product but already have another subscription product in cart' => [ 'subscription reference' => Config::SUBSCRIPTION_ATTRIBUTE_DAILY, @@ -127,7 +200,11 @@ public function productDataProvider(): array 'id_product_attribute' => Config::SUBSCRIPTION_ATTRIBUTE_MONTHLY, ], ], - 'expected result' => SubscriptionProductValidationException::class, + 'subscription enabled' => true, + 'subscription carrier ID' => $this->validCarrier->id, + 'expected result' => false, + 'expected exception' => SubscriptionProductValidationException::class, + 'expected exception code' => ExceptionCode::CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT, ], 'Add normal product but already have another subscription product in cart' => [ 'subscription reference' => '', @@ -137,7 +214,11 @@ public function productDataProvider(): array 'id_product_attribute' => Config::SUBSCRIPTION_ATTRIBUTE_MONTHLY, ], ], + 'subscription enabled' => true, + 'subscription carrier ID' => $this->validCarrier->id, 'expected result' => true, + 'expected exception' => '', + 'expected exception code' => 0, ], 'Add normal product but already have another normal product in cart' => [ 'subscription reference' => '', @@ -147,7 +228,11 @@ public function productDataProvider(): array 'id_product_attribute' => self::NORMAL_PRODUCT_ATTRIBUTE_ID, ], ], + 'subscription enabled' => true, + 'subscription carrier ID' => $this->validCarrier->id, 'expected result' => true, + 'expected exception' => '', + 'expected exception code' => 0, ], ]; } diff --git a/tests/Unit/Subscription/Validator/SubscriptionSettingsValidatorTest.php b/tests/Unit/Subscription/Validator/SubscriptionSettingsValidatorTest.php new file mode 100644 index 000000000..97a66dbe1 --- /dev/null +++ b/tests/Unit/Subscription/Validator/SubscriptionSettingsValidatorTest.php @@ -0,0 +1,82 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Tests\Unit\Subscription\Validator; + +use Mollie\Repository\CarrierRepositoryInterface; +use Mollie\Subscription\Exception\CouldNotValidateSubscriptionSettings; +use Mollie\Subscription\Exception\ExceptionCode; +use Mollie\Subscription\Validator\SubscriptionSettingsValidator; +use Mollie\Tests\Unit\BaseTestCase; + +class SubscriptionSettingsValidatorTest extends BaseTestCase +{ + /** @var CarrierRepositoryInterface */ + private $carrieRepository; + + public function setUp(): void + { + $this->carrieRepository = $this->mock(CarrierRepositoryInterface::class); + + parent::setUp(); + } + + public function testItSuccessfullyValidatesSubscriptionSettings(): void + { + $this->configuration->expects($this->exactly(2))->method('get')->willReturnOnConsecutiveCalls(true, 1); + + $carrier = $this->mock(\Carrier::class); + + $this->carrieRepository->expects($this->once())->method('findOneBy')->willReturn($carrier); + + $subscriptionSettingsValidator = new SubscriptionSettingsValidator( + $this->configuration, + $this->carrieRepository + ); + + $result = $subscriptionSettingsValidator->validate(); + + $this->assertEquals(true, $result); + } + + public function testItUnsuccessfullyValidatesSubscriptionSettingsSubscriptionInactive(): void + { + $this->configuration->expects($this->once())->method('get')->willReturn(false); + + $subscriptionSettingsValidator = new SubscriptionSettingsValidator( + $this->configuration, + $this->carrieRepository + ); + + $this->expectException(CouldNotValidateSubscriptionSettings::class); + $this->expectExceptionCode(ExceptionCode::CART_SUBSCRIPTION_SERVICE_DISABLED); + + $subscriptionSettingsValidator->validate(); + } + + public function testItUnsuccessfullyValidatesSubscriptionSettingsSubscriptionCarrierInvalid(): void + { + $this->configuration->expects($this->exactly(2))->method('get')->willReturnOnConsecutiveCalls(true, 1); + + $this->carrieRepository->expects($this->once())->method('findOneBy')->willReturn(null); + + $subscriptionSettingsValidator = new SubscriptionSettingsValidator( + $this->configuration, + $this->carrieRepository + ); + + $this->expectException(CouldNotValidateSubscriptionSettings::class); + $this->expectExceptionCode(ExceptionCode::CART_SUBSCRIPTION_CARRIER_INVALID); + + $subscriptionSettingsValidator->validate(); + } +} diff --git a/tests/Unit/Subscription/Validator/index.php b/tests/Unit/Subscription/Validator/index.php new file mode 100644 index 000000000..d3343995f --- /dev/null +++ b/tests/Unit/Subscription/Validator/index.php @@ -0,0 +1,20 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; diff --git a/upgrade/Upgrade-6.0.6.php b/upgrade/Upgrade-6.0.6.php index afbe4d9a3..206269a22 100644 --- a/upgrade/Upgrade-6.0.6.php +++ b/upgrade/Upgrade-6.0.6.php @@ -28,5 +28,5 @@ function updateConfigurationValues606(Mollie $module) /** @var ConfigurationAdapter $configuration */ $configuration = $module->getService(ConfigurationAdapter::class); - $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ENABLE, '0'); + $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ENABLED, '0'); } diff --git a/views/js/front/subscription/product.js b/views/js/front/subscription/product.js index 07977b18d..b7f8c6ad8 100644 --- a/views/js/front/subscription/product.js +++ b/views/js/front/subscription/product.js @@ -62,12 +62,20 @@ $(document).ready(function () { action: 'validateProduct', product: product }, - success: function (response) { - response = jQuery.parseJSON(response); + error: function (error) { + const response = jQuery.parseJSON(error.responseText); - if (!response.isValid) { - noticeMessage(response.message); + const responseErrors = response.errors; + + if ($.isArray(responseErrors)) { + console.error(responseErrors[0]) + noticeMessage(responseErrors[0]); + + return; } + + console.error(responseErrors) + noticeMessage(responseErrors); } }) }