Skip to content

Commit 8655554

Browse files
committed
magento#12817: Coupon code with canceled order.
1 parent a7f6412 commit 8655554

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\SalesRule\Observer;
8+
9+
use Magento\Framework\DataObject;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Sales\Model\Order;
12+
use Magento\SalesRule\Model\Coupon;
13+
use Magento\SalesRule\Model\ResourceModel\Coupon\Usage;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
/**
17+
* Test decreasing coupon usages after order cancellation.
18+
*
19+
* @magentoDbIsolation enabled
20+
* @magentoAppIsolation enabled
21+
*/
22+
class SalesOrderAfterCancelObserverTest extends \PHPUnit\Framework\TestCase
23+
{
24+
/**
25+
* @var ObjectManagerInterface
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var Coupon
31+
*/
32+
private $coupon;
33+
34+
/**
35+
* @var Usage
36+
*/
37+
private $usage;
38+
39+
/**
40+
* @var DataObject
41+
*/
42+
private $couponUsage;
43+
44+
/**
45+
* @var Order
46+
*/
47+
private $order;
48+
49+
/**
50+
* Test increasing coupon usages after after order placing and decreasing after order cancellation.
51+
*
52+
* @magentoDataFixture Magento/Customer/_files/customer.php
53+
* @magentoDataFixture Magento/SalesRule/_files/coupons_limited_order.php
54+
*/
55+
public function testOrderCancellation()
56+
{
57+
$customerId = 1;
58+
$couponCode = 'one_usage';
59+
$orderId = '100000001';
60+
61+
$this->coupon->loadByCode($couponCode);
62+
$this->order->loadByIncrementId($orderId);
63+
64+
// Make sure coupon usages value is incremented then order is placed.
65+
$this->order->place();
66+
$this->usage->loadByCustomerCoupon($this->couponUsage, $customerId, $this->coupon->getId());
67+
$this->coupon->loadByCode($couponCode);
68+
69+
self::assertEquals(
70+
1,
71+
$this->coupon->getTimesUsed()
72+
);
73+
self::assertEquals(
74+
1,
75+
$this->couponUsage->getTimesUsed()
76+
);
77+
78+
// Make sure order coupon usages value is decremented then order is cancelled.
79+
$this->order->cancel();
80+
$this->usage->loadByCustomerCoupon($this->couponUsage, $customerId, $this->coupon->getId());
81+
$this->coupon->loadByCode($couponCode);
82+
83+
self::assertEquals(
84+
0,
85+
$this->coupon->getTimesUsed()
86+
);
87+
self::assertEquals(
88+
0,
89+
$this->couponUsage->getTimesUsed()
90+
);
91+
}
92+
93+
protected function setUp()
94+
{
95+
$this->objectManager = Bootstrap::getObjectManager();
96+
$this->coupon = $this->objectManager->get(Coupon::class);
97+
$this->usage = $this->objectManager->get(Usage::class);
98+
$this->couponUsage = $this->objectManager->get(DataObject::class);
99+
$this->order = $this->objectManager->get(Order::class);
100+
}
101+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Sales\Model\Order;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
require_once 'coupons_limited.php';
11+
require __DIR__ . '/../../../Magento/Sales/_files/order.php';
12+
13+
$collection = Bootstrap::getObjectManager()->create(
14+
\Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
15+
);
16+
$items = array_values($collection->getItems());
17+
18+
/** @var Order $order */
19+
$order = Bootstrap::getObjectManager()->create(Order::class);
20+
21+
$order->loadByIncrementId('100000001')
22+
->setCouponCode('one_usage')
23+
->setAppliedRuleIds("{$items[0]->getId()}")
24+
->setCreatedAt('2014-10-25 10:10:10')
25+
->setCustomerId(1)
26+
->save();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require __DIR__ . '/../../../Magento/Sales/_files/order_rollback.php';
8+
require_once 'coupons_limited_rollback.php';

0 commit comments

Comments
 (0)