diff --git a/features/shop/promotion/applying_promotion_coupon/applying_promotion_coupon_with_per_customer_usage_limit.feature b/features/shop/promotion/applying_promotion_coupon/applying_promotion_coupon_with_per_customer_usage_limit.feature index fd7b8ca213..11b669f07f 100644 --- a/features/shop/promotion/applying_promotion_coupon/applying_promotion_coupon_with_per_customer_usage_limit.feature +++ b/features/shop/promotion/applying_promotion_coupon/applying_promotion_coupon_with_per_customer_usage_limit.feature @@ -73,3 +73,59 @@ Feature: Applying promotion coupon with per customer usage limit When I check the details of my cart Then my cart total should be "$100.00" And there should be no discount applied + + @api @ui + Scenario: Orders placed before track usage was re-enabled do not count toward per customer usage limit + Given this coupon can be used once per customer + And I placed an order "#00000022" + And I bought a single "PHP T-Shirt" using "SANTA2016" coupon + And I chose "Free" shipping method to "United States" with "Cash on Delivery" payment + And this coupon has had its track usage re-enabled + And I added product "PHP T-Shirt" to the cart + And I applied the coupon with code "SANTA2016" + When I check the details of my cart + Then my cart total should be "$90.00" + And my discount should be "-$10.00" + + @api @ui + Scenario: Enabling track usage does not count orders placed before tracking was enabled + Given this coupon has track usage disabled + And this coupon can be used once per customer + And I placed an order "#00000022" + And I bought a single "PHP T-Shirt" using "SANTA2016" coupon + And I chose "Free" shipping method to "United States" with "Cash on Delivery" payment + And this coupon has had its track usage re-enabled + And I added product "PHP T-Shirt" to the cart + And I applied the coupon with code "SANTA2016" + When I check the details of my cart + Then my cart total should be "$90.00" + And my discount should be "-$10.00" + + @api @ui + Scenario: Removing per customer usage limit allows customer to use the coupon after the limit was reached + Given this coupon can be used once per customer + And I placed an order "#00000022" + And I bought a single "PHP T-Shirt" using "SANTA2016" coupon + And I chose "Free" shipping method to "United States" with "Cash on Delivery" payment + And this coupon has no per customer usage limit + And I added product "PHP T-Shirt" to the cart + And I applied the coupon with code "SANTA2016" + When I check the details of my cart + Then my cart total should be "$90.00" + And my discount should be "-$10.00" + + @api @ui + Scenario: Orders placed after track usage was re-enabled still count toward per customer usage limit + Given this coupon can be used once per customer + And I placed an order "#00000022" + And I bought a single "PHP T-Shirt" using "SANTA2016" coupon + And I chose "Free" shipping method to "United States" with "Cash on Delivery" payment + And this coupon has had its track usage re-enabled + And I placed an order "#00000023" + And I bought a single "PHP T-Shirt" using "SANTA2016" coupon + And I chose "Free" shipping method to "United States" with "Cash on Delivery" payment + And I added product "PHP T-Shirt" to the cart + And I applied the coupon with code "SANTA2016" + When I check the details of my cart + Then my cart total should be "$100.00" + And there should be no discount applied diff --git a/src/Sylius/Behat/Context/Setup/PromotionContext.php b/src/Sylius/Behat/Context/Setup/PromotionContext.php index 06778c147d..e9dd3b8ec1 100644 --- a/src/Sylius/Behat/Context/Setup/PromotionContext.php +++ b/src/Sylius/Behat/Context/Setup/PromotionContext.php @@ -23,9 +23,11 @@ use Sylius\Component\Core\Factory\PromotionActionFactoryInterface; use Sylius\Component\Core\Factory\PromotionRuleFactoryInterface; use Sylius\Component\Core\Formatter\StringInflector; use Sylius\Component\Core\Model\ChannelInterface; +use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\PromotionCouponInterface; use Sylius\Component\Core\Model\PromotionInterface; +use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Core\Promotion\Checker\Rule\ContainsProductRuleChecker; use Sylius\Component\Core\Promotion\Checker\Rule\CustomerGroupRuleChecker; @@ -58,6 +60,7 @@ final readonly class PromotionContext implements Context private ObjectManager $objectManager, private ExampleFactoryInterface $promotionExampleFactory, private MessageBusInterface $commandBus, + private OrderRepositoryInterface $orderRepository, ) { } @@ -308,6 +311,21 @@ final readonly class PromotionContext implements Context $this->objectManager->flush(); } + #[Given('/^(this coupon) has had its track usage re-enabled$/')] + public function thisCouponHasHadItsTrackUsageReEnabled(PromotionCouponInterface $coupon): void + { + /** @var OrderInterface[] $orders */ + $orders = $this->orderRepository->findBy(['promotionCoupon' => $coupon]); + foreach ($orders as $order) { + $order->setCheckoutCompletedAt(new \DateTimeImmutable('-1 day')); + } + + $coupon->setTrackUsage(false); + $coupon->setTrackUsage(true); + + $this->objectManager->flush(); + } + #[Given('/^(this coupon) has already reached its usage limit$/')] public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon): void { diff --git a/src/Sylius/Behat/Resources/config/services/contexts/setup.php b/src/Sylius/Behat/Resources/config/services/contexts/setup.php index 5bcde0a3b4..7533536cf5 100644 --- a/src/Sylius/Behat/Resources/config/services/contexts/setup.php +++ b/src/Sylius/Behat/Resources/config/services/contexts/setup.php @@ -362,6 +362,7 @@ return static function (ContainerConfigurator $container) { service('doctrine.orm.entity_manager'), service('sylius.fixture.example_factory.promotion'), service('sylius.command_bus'), + service('sylius.repository.order'), ]) ;