diff --git a/src/Adapter/Customer/Group/CommandHandler/AddCustomerGroupHandler.php b/src/Adapter/Customer/Group/CommandHandler/AddCustomerGroupHandler.php new file mode 100644 index 0000000000000..3d62f42a50339 --- /dev/null +++ b/src/Adapter/Customer/Group/CommandHandler/AddCustomerGroupHandler.php @@ -0,0 +1,68 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Adapter\Customer\Group\CommandHandler; + +use Group as CustomerGroup; +use PrestaShop\PrestaShop\Adapter\Customer\Group\Repository\GroupRepository; +use PrestaShop\PrestaShop\Adapter\Customer\Group\Validate\CustomerGroupValidator; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\CommandHandler\AddCustomerGroupHandlerInterface; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\ValueObject\GroupId; + +class AddCustomerGroupHandler implements AddCustomerGroupHandlerInterface +{ + /** + * @var CustomerGroupValidator + */ + private $customerGroupValidator; + + /** + * @var GroupRepository + */ + private $customerGroupRepository; + + public function __construct(CustomerGroupValidator $customerGroupValidator, GroupRepository $customerGroupRepository) + { + $this->customerGroupValidator = $customerGroupValidator; + $this->customerGroupRepository = $customerGroupRepository; + } + + public function handle(AddCustomerGroupCommand $command): GroupId + { + $customerGroup = new CustomerGroup(); + $customerGroup->name = $command->getLocalizedNames(); + $customerGroup->reduction = (string) $command->getReductionPercent(); + $customerGroup->price_display_method = (int) $command->displayPriceTaxExcluded(); + $customerGroup->show_prices = $command->showPrice(); + $customerGroup->id_shop_list = $command->getShopIds(); + + $this->customerGroupValidator->validate($customerGroup); + + return $this->customerGroupRepository->create($customerGroup); + } +} diff --git a/src/Adapter/Customer/Group/QueryHandler/GetCustomerGroupForEditingHandler.php b/src/Adapter/Customer/Group/QueryHandler/GetCustomerGroupForEditingHandler.php new file mode 100644 index 0000000000000..ac5efa0dbe26d --- /dev/null +++ b/src/Adapter/Customer/Group/QueryHandler/GetCustomerGroupForEditingHandler.php @@ -0,0 +1,70 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Adapter\Customer\Group\QueryHandler; + +use PrestaShop\Decimal\DecimalNumber; +use PrestaShop\PrestaShop\Adapter\Customer\Group\Repository\GroupRepository; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupNotFoundException; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Query\GetCustomerGroupForEditing; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryHandler\GetCustomerGroupForEditingHandlerInterface; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryResult\EditableCustomerGroup; + +class GetCustomerGroupForEditingHandler implements GetCustomerGroupForEditingHandlerInterface +{ + /** + * @var GroupRepository + */ + private $customerGroupRepository; + + public function __construct(GroupRepository $customerGroupRepository) + { + $this->customerGroupRepository = $customerGroupRepository; + } + + /** + * @param GetCustomerGroupForEditing $query + * + * @throws GroupNotFoundException + * + * @return EditableCustomerGroup + */ + public function handle(GetCustomerGroupForEditing $query): EditableCustomerGroup + { + $customerGroupId = $query->getCustomerGroupId(); + $customerGroup = $this->customerGroupRepository->get($customerGroupId); + + return new EditableCustomerGroup( + $customerGroupId->getValue(), + $customerGroup->name, + new DecimalNumber($customerGroup->reduction), + (bool) $customerGroup->price_display_method, + (bool) $customerGroup->show_prices, + $customerGroup->getAssociatedShops() + ); + } +} diff --git a/src/Adapter/Customer/Group/Repository/GroupRepository.php b/src/Adapter/Customer/Group/Repository/GroupRepository.php index 09e5ebc29fa98..5354760fbdc2b 100644 --- a/src/Adapter/Customer/Group/Repository/GroupRepository.php +++ b/src/Adapter/Customer/Group/Repository/GroupRepository.php @@ -1,5 +1,5 @@ getObjectModel( + $customerGroupId->getValue(), + CustomerGroup::class, + GroupNotFoundException::class + ); + + return $customerGroup; + } + /** * @param GroupId $groupId * @@ -49,4 +72,16 @@ public function assertGroupExists(GroupId $groupId): void GroupNotFoundException::class ); } + + /** + * @param CustomerGroup $customerGroup + * + * @throws CoreException + * + * @return GroupId + */ + public function create(CustomerGroup $customerGroup): GroupId + { + return new GroupId($this->addObjectModelToShops($customerGroup, $customerGroup->id_shop_list, CannotAddGroupException::class)); + } } diff --git a/src/Adapter/Customer/Group/Validate/CustomerGroupValidator.php b/src/Adapter/Customer/Group/Validate/CustomerGroupValidator.php new file mode 100644 index 0000000000000..15e8c2cd52f17 --- /dev/null +++ b/src/Adapter/Customer/Group/Validate/CustomerGroupValidator.php @@ -0,0 +1,153 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +namespace PrestaShop\PrestaShop\Adapter\Customer\Group\Validate; + +use Group as CustomerGroup; +use PrestaShop\PrestaShop\Adapter\AbstractObjectModelValidator; +use PrestaShop\PrestaShop\Adapter\Shop\Repository\ShopRepository; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupConstraintException; + +class CustomerGroupValidator extends AbstractObjectModelValidator +{ + /** + * @var ShopRepository + */ + private $shopRepository; + + public function __construct(ShopRepository $shopRepository) + { + $this->shopRepository = $shopRepository; + } + + /** + * @param CustomerGroup $customerGroup + * + * @throws GroupConstraintException + * + * @return void + */ + public function validate(CustomerGroup $customerGroup): void + { + $this->validateThereIsAtLeastOneShop($customerGroup->id_shop_list); + $this->validateShopsExists($customerGroup->id_shop_list); + $this->validateGroupNames($customerGroup->name); + $this->validatePriceDisplayMethod($customerGroup->price_display_method); + } + + /** + * @param array $shopIds + * + * @throws GroupConstraintException + * + * @return void + */ + private function validateThereIsAtLeastOneShop(array $shopIds): void + { + if (empty($shopIds)) { + throw new GroupConstraintException( + 'Customer group must be associated with at least one shop', + GroupConstraintException::EMPTY_SHOP_LIST + ); + } + } + + /** + * @param array $shopIds + * + * @return void + */ + private function validateShopsExists(array $shopIds): void + { + foreach ($shopIds as $shopId) { + $this->shopRepository->assertShopExists($shopId); + } + } + + /** + * @param int $priceDisplayMethod + * + * @throws GroupConstraintException + * + * @return void + */ + private function validatePriceDisplayMethod(int $priceDisplayMethod): void + { + switch ($priceDisplayMethod) { + case CustomerGroup::PRICE_DISPLAY_METHOD_TAX_INCL: + case CustomerGroup::PRICE_DISPLAY_METHOD_TAX_EXCL: + return; + default: + throw new GroupConstraintException( + sprintf('Invalid price display method "%s"', $priceDisplayMethod), + GroupConstraintException::INVALID_PRICE_DISPLAY_METHOD + ); + } + } + + /** + * @param string[] $names + * + * @throws GroupConstraintException + * + * @return void + */ + private function validateGroupNames(array $names): void + { + if (empty($names)) { + throw new GroupConstraintException( + 'Customer group name cannot be empty', + GroupConstraintException::EMPTY_NAME + ); + } + foreach ($names as $name) { + $this->validateGroupName($name); + } + } + + /** + * @param string $name + * + * @throws GroupConstraintException + * + * @return void + */ + private function validateGroupName(string $name): void + { + if (strlen($name) > 32) { + throw new GroupConstraintException( + sprintf('Customer group name cannot be longer than 32 characters. Got "%s"', $name), + GroupConstraintException::NAME_TOO_LONG + ); + } + if (false === preg_match('/^[^<>={}]*$/u', $name)) { + throw new GroupConstraintException( + 'Customer group name cannot contain these characters: < > = { }', + GroupConstraintException::INVALID_NAME + ); + } + } +} diff --git a/src/Core/Domain/Customer/Group/Command/AddCustomerGroupCommand.php b/src/Core/Domain/Customer/Group/Command/AddCustomerGroupCommand.php new file mode 100644 index 0000000000000..9bad64f0c5ca0 --- /dev/null +++ b/src/Core/Domain/Customer/Group/Command/AddCustomerGroupCommand.php @@ -0,0 +1,135 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command; + +use PrestaShop\Decimal\DecimalNumber; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupConstraintException; +use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopId; + +class AddCustomerGroupCommand +{ + /** + * @var string[] + */ + private $localizedNames; + + /** + * @var DecimalNumber + */ + private $reductionPercent; + + /** + * @var bool + */ + private $displayPriceTaxExcluded; + + /** + * @var bool + */ + private $showPrice; + + /** + * @var ShopId[] + */ + private $shopIds; + + /** + * @param string[] $localizedNames + * @param DecimalNumber $reductionPercent + * @param bool $displayPriceTaxExcluded + * @param bool $showPrice + * @param array $shopIds + */ + public function __construct( + array $localizedNames, + DecimalNumber $reductionPercent, + bool $displayPriceTaxExcluded, + bool $showPrice, + array $shopIds + ) { + $this->assertReductionIsValid($reductionPercent); + + $this->localizedNames = $localizedNames; + $this->reductionPercent = $reductionPercent; + $this->displayPriceTaxExcluded = $displayPriceTaxExcluded; + $this->showPrice = $showPrice; + $this->shopIds = array_map(function (int $shopId) { + return new ShopId($shopId); + }, $shopIds); + } + + /** + * @return string[] + */ + public function getLocalizedNames(): array + { + return $this->localizedNames; + } + + /** + * @return bool + */ + public function displayPriceTaxExcluded(): bool + { + return $this->displayPriceTaxExcluded; + } + + /** + * @return DecimalNumber + */ + public function getReductionPercent(): DecimalNumber + { + return $this->reductionPercent; + } + + /** + * @return bool + */ + public function showPrice(): bool + { + return $this->showPrice; + } + + /** + * @return ShopId[] + */ + public function getShopIds(): array + { + return $this->shopIds; + } + + private function assertReductionIsValid(DecimalNumber $reductionPercent): void + { + if ($reductionPercent->isLowerThanZero() || $reductionPercent->isGreaterThan(new DecimalNumber('100'))) { + throw new GroupConstraintException( + 'Reduction percent must be between 0 and 100', + GroupConstraintException::INVALID_REDUCTION + ); + } + } +} diff --git a/src/Core/Domain/Customer/Group/CommandHandler/AddCustomerGroupHandlerInterface.php b/src/Core/Domain/Customer/Group/CommandHandler/AddCustomerGroupHandlerInterface.php new file mode 100644 index 0000000000000..9c86958792ea8 --- /dev/null +++ b/src/Core/Domain/Customer/Group/CommandHandler/AddCustomerGroupHandlerInterface.php @@ -0,0 +1,41 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Core\Domain\Customer\Group\CommandHandler; + +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\ValueObject\GroupId; + +interface AddCustomerGroupHandlerInterface +{ + /** + * @param AddCustomerGroupCommand $command + * + * @return GroupId + */ + public function handle(AddCustomerGroupCommand $command): GroupId; +} diff --git a/src/Core/Domain/Customer/Group/Exception/CannotAddGroupException.php b/src/Core/Domain/Customer/Group/Exception/CannotAddGroupException.php new file mode 100644 index 0000000000000..f84fd045c08ce --- /dev/null +++ b/src/Core/Domain/Customer/Group/Exception/CannotAddGroupException.php @@ -0,0 +1,32 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception; + +class CannotAddGroupException extends GroupException +{ +} diff --git a/src/Core/Domain/Customer/Group/Exception/GroupConstraintException.php b/src/Core/Domain/Customer/Group/Exception/GroupConstraintException.php index b63a7fd14848f..8aa362f7a0565 100644 --- a/src/Core/Domain/Customer/Group/Exception/GroupConstraintException.php +++ b/src/Core/Domain/Customer/Group/Exception/GroupConstraintException.php @@ -36,4 +36,16 @@ class GroupConstraintException extends GroupException * When invalid groupId value is provided */ public const INVALID_ID = 10; + + public const INVALID_REDUCTION = 20; + + public const EMPTY_SHOP_LIST = 30; + + public const EMPTY_NAME = 40; + + public const NAME_TOO_LONG = 50; + + public const INVALID_NAME = 60; + + public const INVALID_PRICE_DISPLAY_METHOD = 70; } diff --git a/src/Core/Domain/Customer/Group/Query/GetCustomerGroupForEditing.php b/src/Core/Domain/Customer/Group/Query/GetCustomerGroupForEditing.php new file mode 100644 index 0000000000000..1c7a1986dc2f8 --- /dev/null +++ b/src/Core/Domain/Customer/Group/Query/GetCustomerGroupForEditing.php @@ -0,0 +1,51 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Core\Domain\Customer\Group\Query; + +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\ValueObject\GroupId; + +class GetCustomerGroupForEditing +{ + /** + * @var GroupId + */ + private $customerGroupId; + + public function __construct(int $customerGroupId) + { + $this->customerGroupId = new GroupId($customerGroupId); + } + + /** + * @return GroupId + */ + public function getCustomerGroupId(): GroupId + { + return $this->customerGroupId; + } +} diff --git a/src/Core/Domain/Customer/Group/QueryHandler/GetCustomerGroupForEditingHandlerInterface.php b/src/Core/Domain/Customer/Group/QueryHandler/GetCustomerGroupForEditingHandlerInterface.php new file mode 100644 index 0000000000000..ca68940d88b05 --- /dev/null +++ b/src/Core/Domain/Customer/Group/QueryHandler/GetCustomerGroupForEditingHandlerInterface.php @@ -0,0 +1,41 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryHandler; + +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Query\GetCustomerGroupForEditing; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryResult\EditableCustomerGroup; + +interface GetCustomerGroupForEditingHandlerInterface +{ + /** + * @param GetCustomerGroupForEditing $customerForEditingQuery + * + * @return EditableCustomerGroup + */ + public function handle(GetCustomerGroupForEditing $customerForEditingQuery): EditableCustomerGroup; +} diff --git a/src/Core/Domain/Customer/Group/QueryResult/EditableCustomerGroup.php b/src/Core/Domain/Customer/Group/QueryResult/EditableCustomerGroup.php new file mode 100644 index 0000000000000..2665a379e25fb --- /dev/null +++ b/src/Core/Domain/Customer/Group/QueryResult/EditableCustomerGroup.php @@ -0,0 +1,135 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryResult; + +use PrestaShop\Decimal\DecimalNumber; + +class EditableCustomerGroup +{ + /** + * @var int + */ + private $id; + + /** + * @var array + */ + private $localizedNames; + + /** + * @var DecimalNumber + */ + private $reduction; + + /** + * @var bool + */ + private $displayPriceTaxExcluded; + + /** + * @var bool + */ + private $showPrice; + + /** + * @var array + */ + private $shopIds; + + /** + * @param int $id + * @param array $localizedNames array of names indexed by language id + * @param DecimalNumber $reduction + * @param bool $displayPriceTaxExcluded + * @param bool $showPrice + * @param array $shopIds + */ + public function __construct( + int $id, + array $localizedNames, + DecimalNumber $reduction, + bool $displayPriceTaxExcluded, + bool $showPrice, + array $shopIds + ) { + $this->id = $id; + $this->localizedNames = $localizedNames; + $this->reduction = $reduction; + $this->displayPriceTaxExcluded = $displayPriceTaxExcluded; + $this->showPrice = $showPrice; + $this->shopIds = $shopIds; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return string[] + */ + public function getLocalizedNames(): array + { + return $this->localizedNames; + } + + /** + * @return DecimalNumber + */ + public function getReduction(): DecimalNumber + { + return $this->reduction; + } + + /** + * @return bool + */ + public function displayPriceTaxExcluded(): bool + { + return $this->displayPriceTaxExcluded; + } + + /** + * @return bool + */ + public function showPrice(): bool + { + return $this->showPrice; + } + + /** + * @return array + */ + public function getShopIds(): array + { + return $this->shopIds; + } +} diff --git a/src/PrestaShopBundle/Resources/config/routing/admin/configure/shop_parameters/customer_groups.yml b/src/PrestaShopBundle/Resources/config/routing/admin/configure/shop_parameters/customer_groups.yml index b3d75c672bb8d..a847a16b8c0b5 100644 --- a/src/PrestaShopBundle/Resources/config/routing/admin/configure/shop_parameters/customer_groups.yml +++ b/src/PrestaShopBundle/Resources/config/routing/admin/configure/shop_parameters/customer_groups.yml @@ -4,7 +4,7 @@ admin_customer_groups_index: defaults: _controller: 'PrestaShopBundle\Controller\Admin\Configure\ShopParameters\CustomerGroupsController::indexAction' _legacy_controller: AdminGroups - # _legacy_link: AdminGroups + _legacy_link: AdminGroups admin_customer_groups_search: path: / diff --git a/src/PrestaShopBundle/Resources/config/services/adapter/group.yml b/src/PrestaShopBundle/Resources/config/services/adapter/group.yml index 25658fe7ac5b8..739be9702fe2a 100644 --- a/src/PrestaShopBundle/Resources/config/services/adapter/group.yml +++ b/src/PrestaShopBundle/Resources/config/services/adapter/group.yml @@ -17,3 +17,21 @@ services: class: 'PrestaShop\PrestaShop\Adapter\Feature\GroupFeature' arguments: - '@prestashop.adapter.legacy.configuration' + + PrestaShop\PrestaShop\Adapter\Customer\Group\CommandHandler\AddCustomerGroupHandler: + public: false + autowire: true + tags: + - name: tactician.handler + command: 'PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand' + + PrestaShop\PrestaShop\Adapter\Customer\Group\QueryHandler\GetCustomerGroupForEditingHandler: + public: false + autowire: true + tags: + - name: tactician.handler + command: 'PrestaShop\PrestaShop\Core\Domain\Customer\Group\Query\GetCustomerGroupForEditing' + + PrestaShop\PrestaShop\Adapter\Customer\Group\Validate\CustomerGroupValidator: + public: false + autowire: true diff --git a/tests/Integration/Behaviour/Features/Context/Domain/CustomerGroupFeatureContext.php b/tests/Integration/Behaviour/Features/Context/Domain/CustomerGroupFeatureContext.php new file mode 100644 index 0000000000000..5d32b766dd88b --- /dev/null +++ b/tests/Integration/Behaviour/Features/Context/Domain/CustomerGroupFeatureContext.php @@ -0,0 +1,104 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace Tests\Integration\Behaviour\Features\Context\Domain; + +use Behat\Gherkin\Node\TableNode; +use Exception; +use PHPUnit\Framework\Assert; +use PrestaShop\Decimal\DecimalNumber; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Query\GetCustomerGroupForEditing; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryResult\EditableCustomerGroup; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\ValueObject\GroupId; + +class CustomerGroupFeatureContext extends AbstractDomainFeatureContext +{ + /** + * @When /^I create a Customer Group "(.+)" with the following details:$/ + * + * @param string $customerGroupReference + * @param TableNode $tableNode + * + * @throws Exception + */ + public function createCustomerUsingCommand(string $customerGroupReference, TableNode $tableNode) + { + $data = $this->localizeByRows($tableNode); + + $command = new AddCustomerGroupCommand( + $data['name'], + new DecimalNumber($data['reduction']), + (bool) $data['displayPriceTaxExcluded'], + (bool) $data['showPrice'], + $this->referencesToIds($data['shopIds']) + ); + + /** @var GroupId $id */ + $id = $this->getCommandBus()->handle($command); + $this->getSharedStorage()->set($customerGroupReference, $id->getValue()); + } + + /** + * @When /^I query Customer Group "(.+)" I should get a Customer Group with properties:$/ + */ + public function assertQueryCustomerProperties($customerGroupReference, EditableCustomerGroup $expectedGroup) + { + Assert::assertEquals($this->getCustomerGroupForEditing($customerGroupReference), $expectedGroup); + } + + /** + * @Transform table:customer group,value + * + * @param TableNode $tableNode + * + * @return EditableCustomerGroup + */ + public function transformEditableCustomerGroup(TableNode $tableNode): EditableCustomerGroup + { + $data = $this->localizeByRows($tableNode); + + return new EditableCustomerGroup( + (int) $data['id'], + $data['name'], + new DecimalNumber($data['reduction']), + (bool) $data['displayPriceTaxExcluded'], + (bool) $data['showPrice'], + $this->referencesToIds($data['shopIds']) + ); + } + + /** + * @param string $customerGroupReference + * + * @return EditableCustomerGroup + */ + private function getCustomerGroupForEditing(string $customerGroupReference): EditableCustomerGroup + { + return $this->getQueryBus()->handle(new GetCustomerGroupForEditing($this->getSharedStorage()->get($customerGroupReference))); + } +} diff --git a/tests/Integration/Behaviour/Features/Scenario/CustomerGroup/customer_group_management.feature b/tests/Integration/Behaviour/Features/Scenario/CustomerGroup/customer_group_management.feature new file mode 100644 index 0000000000000..60dbd9904021b --- /dev/null +++ b/tests/Integration/Behaviour/Features/Scenario/CustomerGroup/customer_group_management.feature @@ -0,0 +1,38 @@ +# ./vendor/bin/behat -c tests/Integration/Behaviour/behat.yml -s customer_group --tags customer-group-management +@customer-group-management +@restore-all-tables-before-feature +@restore-shops-after-feature +@restore-languages-after-feature +@clear-cache-after-feature +Feature: CustomerGroup Management + PrestaShop allows BO users to manage customer groups in the Customers > Customers > Groups page + As a BO user + I must be able to create customer groups + + Background: + Given groups feature is activated + And I enable multishop feature + And language "fr" with locale "fr-FR" exists + And shop "shop1" with name "test_shop" exists + And shop group "default_shop_group" with name "Default" exists + And I add a shop "shop2" with name "test_shop2" and color "blue" for the group "default_shop_group" + And I add a shop "shop3" with name "test_shop3" and color "red" for the group "default_shop_group" + + Scenario: Create a simple customer group + When I create a Customer Group "CustomerGroup1" with the following details: + | name[en-US] | Name EN | + | name[fr-FR] | Name FR | + | reduction | 1.23 | + | displayPriceTaxExcluded | true | + | showPrice | true | + | shopIds | shop1,shop2,shop3 | + # See CustomerGroupFeatureContext::transformEditableCustomerGroup + When I query Customer Group "CustomerGroup1" I should get a Customer Group with properties: + | customer group | value | + | id | 4 | + | name[en-US] | Name EN | + | name[fr-FR] | Name FR | + | reduction | 1.23 | + | displayPriceTaxExcluded | true | + | showPrice | true | + | shopIds | shop1,shop2,shop3 | diff --git a/tests/Integration/Behaviour/behat.yml b/tests/Integration/Behaviour/behat.yml index 28a6bc7e1ce7d..f8849a6a2eadc 100644 --- a/tests/Integration/Behaviour/behat.yml +++ b/tests/Integration/Behaviour/behat.yml @@ -54,6 +54,18 @@ default: - Tests\Integration\Behaviour\Features\Context\Domain\CustomerFeatureContext - Tests\Integration\Behaviour\Features\Context\RiskFeatureContext - Tests\Integration\Behaviour\Features\Transform\StringToBoolTransformContext + customer_group: + paths: + - "%paths.base%/Features/Scenario/CustomerGroup" + contexts: + - Tests\Integration\Behaviour\Features\Context\CommonFeatureContext + - Tests\Integration\Behaviour\Features\Context\ShopFeatureContext + - Tests\Integration\Behaviour\Features\Context\Configuration\CommonConfigurationFeatureContext + - Tests\Integration\Behaviour\Features\Context\CustomerFeatureContext + - Tests\Integration\Behaviour\Features\Context\Domain\CustomerGroupFeatureContext + - Tests\Integration\Behaviour\Features\Context\LanguageFeatureContext + - Tests\Integration\Behaviour\Features\Transform\LocalizedArrayTransformContext + - Tests\Integration\Behaviour\Features\Transform\StringToBoolTransformContext module: paths: - "%paths.base%/Features/Scenario/Module" diff --git a/tests/Unit/Core/Domain/Customer/Group/Command/AddCustomerGroupCommandTest.php b/tests/Unit/Core/Domain/Customer/Group/Command/AddCustomerGroupCommandTest.php new file mode 100644 index 0000000000000..eefd9c5fee483 --- /dev/null +++ b/tests/Unit/Core/Domain/Customer/Group/Command/AddCustomerGroupCommandTest.php @@ -0,0 +1,102 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ +declare(strict_types=1); + +namespace Tests\Unit\Core\Domain\Customer\Group\Command; + +use Generator; +use PHPUnit\Framework\TestCase; +use PrestaShop\Decimal\DecimalNumber; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand; +use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupConstraintException; + +class AddCustomerGroupCommandTest extends TestCase +{ + /** + * @dataProvider getValidValues + * + * @param DecimalNumber $reductionPercent + */ + public function testItWorksWhenProvidingValidReduction(DecimalNumber $reductionPercent): void + { + new AddCustomerGroupCommand( + ['toto', 'tata'], + $reductionPercent, + false, + true, + [1] + ); + $this->assertTrue(true); + } + + /** + * @dataProvider getInvalidValues + * + * @param DecimalNumber $reductionPercent + */ + public function testItThrowsExceptionWhenProvidingInvalidReduction(DecimalNumber $reductionPercent): void + { + $this->expectException(GroupConstraintException::class); + + new AddCustomerGroupCommand( + ['toto', 'tata'], + $reductionPercent, + false, + true, + [1] + ); + } + + /** + * @return Generator + */ + public function getValidValues(): Generator + { + yield [new DecimalNumber('-0.00')]; + yield [new DecimalNumber('-0')]; + yield [new DecimalNumber('0')]; + yield [new DecimalNumber('0.001')]; + yield [new DecimalNumber('0.01')]; + yield [new DecimalNumber('1.23')]; + yield [new DecimalNumber('12.34')]; + yield [new DecimalNumber('99.99')]; + yield [new DecimalNumber('99.999')]; + yield [new DecimalNumber('100')]; + yield [new DecimalNumber('100.0')]; + yield [new DecimalNumber('100.00')]; + } + + /** + * @return Generator + */ + public function getInvalidValues(): Generator + { + yield [new DecimalNumber('-0.001')]; + yield [new DecimalNumber('-0.01')]; + yield [new DecimalNumber('100.01')]; + yield [new DecimalNumber('100.001')]; + } +}