Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Promotion] Add validation of coupon usage limit per customer #13745

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion features/promotion/managing_coupons/coupon_validation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Feature: Coupon validation
In order to avoid making mistakes when managing a coupon
As an Administrator
I want to be prevented from adding it without specifying required fields
I want to be prevented from adding incorrect coupons

Background:
Given the store operates on a single channel in "United States"
Expand Down Expand Up @@ -31,3 +31,14 @@ Feature: Coupon validation
And I try to add it
Then I should be notified that coupon usage limit must be at least one
And there should be 0 coupon related to this promotion

@ui
Scenario: Trying to add a new coupon with per customer usage limit below one
When I want to create a new coupon for this promotion
And I specify its code as "SANTA2016"
And I limit its usage to 30 times
And I limit its per customer usage to -1 times
And I make it valid until "26.03.2017"
And I try to add it
Then I should be notified that coupon usage limit per customer must be at least one
And there should be 0 coupon related to this promotion
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function iSpecifyItsAmountAs(?int $amount = null): void
}

/**
* @When I limit its per customer usage to :limit times
* @When /^I limit its per customer usage to ([^"]+) times$/
*/
public function iLimitItsPerCustomerUsageLimitTo(int $limit)
{
Expand Down Expand Up @@ -398,6 +398,17 @@ public function iShouldBeNotifiedThatCouponUsageLimitMustBeAtLeast()
Assert::same($currentPage->getValidationMessage('usage_limit'), 'Coupon usage limit must be at least 1.');
}

/**
* @Then I should be notified that coupon usage limit per customer must be at least one
*/
public function iShouldBeNotifiedThatCouponUsageLimitPerCustomerMustBeAtLeast(): void
{
/** @var CreatePageInterface|UpdatePageInterface $currentPage */
$currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);

Assert::same($currentPage->getValidationMessage('per_customer_usage_limit'), 'Coupon usage limit per customer must be at least 1.');
}

/**
* @Then /^(this coupon) should no longer exist in the coupon registry$/
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Page/Admin/PromotionCoupon/CreatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function getDefinedElements(): array
'code' => '#sylius_promotion_coupon_code',
'expires_at' => '#sylius_promotion_coupon_expiresAt',
'usage_limit' => '#sylius_promotion_coupon_usageLimit',
'usage_limit_per_customer' => '#sylius_promotion_coupon_usageLimit',
'per_customer_usage_limit' => '#sylius_promotion_coupon_perCustomerUsageLimit',
]);
}
}
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Page/Admin/PromotionCoupon/UpdatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function getDefinedElements(): array
'code' => '#sylius_promotion_coupon_code',
'expires_at' => '#sylius_promotion_coupon_expiresAt',
'usage_limit' => '#sylius_promotion_coupon_usageLimit',
'usage_limit_per_customer' => '#sylius_promotion_coupon_usageLimit',
'per_customer_usage_limit' => '#sylius_promotion_coupon_perCustomerUsageLimit',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Component\Core\Model\PromotionCoupon">
<property name="perCustomerUsageLimit">
<constraint name="Range">
<option name="min">1</option>
<option name="minMessage">sylius.promotion_coupon.per_customer_usage_limit.min</option>
<option name="groups">sylius</option>
</constraint>
</property>
</class>
</constraint-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ sylius:
invalid: Depth must be a number.
channel_pricing:
all_defined: 'You must define price for every channel.'
promotion_coupon:
per_customer_usage_limit:
min: Coupon usage limit per customer must be at least {{ limit }}.
product_translation:
short_description:
max: Short description must not be longer then {{ limit }} characters.
Expand Down