Skip to content

Commit 5df7a50

Browse files
committed
Merge branch 'ACP2E-3432' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-11-19-2024
2 parents 7939bd0 + 126c383 commit 5df7a50

File tree

4 files changed

+237
-12
lines changed

4 files changed

+237
-12
lines changed

app/code/Magento/SalesRule/Model/ResourceModel/Coupon.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
namespace Magento\SalesRule\Model\ResourceModel;
78

89
use Magento\Framework\Model\AbstractModel;
@@ -12,8 +13,6 @@
1213

1314
/**
1415
* SalesRule Resource Coupon
15-
*
16-
* @author Magento Core Team <core@magentocommerce.com>
1716
*/
1817
class Coupon extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements
1918
\Magento\SalesRule\Model\Spi\CouponResourceInterface
@@ -143,11 +142,11 @@ public function updateSpecificCoupons(\Magento\SalesRule\Model\Rule $rule)
143142
}
144143

145144
$updateArray = [];
146-
if ($rule->dataHasChangedFor('uses_per_coupon')) {
145+
if ($rule->dataHasChangedFor('uses_per_coupon') || $rule->dataHasChangedFor('coupon_type')) {
147146
$updateArray['usage_limit'] = $rule->getUsesPerCoupon();
148147
}
149148

150-
if ($rule->dataHasChangedFor('uses_per_customer')) {
149+
if ($rule->dataHasChangedFor('uses_per_customer') || $rule->dataHasChangedFor('coupon_type')) {
151150
$updateArray['usage_per_customer'] = $rule->getUsesPerCustomer();
152151
}
153152

app/code/Magento/SalesRule/Model/ResourceModel/Rule.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
namespace Magento\SalesRule\Model\ResourceModel;
78

89
use Magento\Framework\App\ObjectManager;
@@ -187,7 +188,10 @@ protected function _afterSave(AbstractModel $object)
187188
}
188189

189190
// Update auto geterated specific coupons if exists
190-
if ($object->getUseAutoGeneration() && $object->hasDataChanges()) {
191+
if (($object->getUseAutoGeneration()
192+
|| ((int) $object->getCouponType()) === \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO
193+
) && $object->hasDataChanges()
194+
) {
191195
$this->_resourceCoupon->updateSpecificCoupons($object);
192196
}
193197
return parent::_afterSave($object);
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesRule\Test\Unit\Model\ResourceModel;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\Model\ResourceModel\Db\Context;
13+
use Magento\Framework\Stdlib\DateTime;
14+
use Magento\SalesRule\Model\ResourceModel\Coupon;
15+
use Magento\SalesRule\Model\Rule;
16+
use PHPUnit\Framework\MockObject\Exception;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class CouponTest extends TestCase
21+
{
22+
/**
23+
* @var Context|MockObject
24+
*/
25+
private $contextMock;
26+
27+
/**
28+
* @var DateTime\DateTime|MockObject
29+
*/
30+
private $dateMock;
31+
32+
/**
33+
* @var DateTime|MockObject
34+
*/
35+
private $datetimeMock;
36+
37+
/**
38+
* @var ResourceConnection|MockObject
39+
*/
40+
private $resourcesMock;
41+
42+
/**
43+
* @var AdapterInterface|MockObject
44+
*/
45+
private $connectionMock;
46+
/**
47+
* @var Coupon
48+
*/
49+
private $model;
50+
51+
protected function setUp(): void
52+
{
53+
parent::setUp();
54+
$this->contextMock = $this->createMock(Context::class);
55+
$this->dateMock = $this->createMock(DateTime\DateTime::class);
56+
$this->datetimeMock = $this->createMock(DateTime::class);
57+
$this->resourcesMock = $this->createMock(ResourceConnection::class);
58+
$this->connectionMock = $this->createMock(AdapterInterface::class);
59+
$this->contextMock->method('getResources')
60+
->willReturn($this->resourcesMock);
61+
$this->resourcesMock->expects($this->any())
62+
->method('getConnection')
63+
->willReturn($this->connectionMock);
64+
$this->model = new Coupon(
65+
$this->contextMock,
66+
$this->dateMock,
67+
$this->datetimeMock
68+
);
69+
}
70+
71+
/**
72+
* @dataProvider updateSpecificCouponsDataProvider
73+
* @param array $origData
74+
* @param array $data
75+
* @param array|null $update
76+
* @return void
77+
* @throws Exception
78+
*/
79+
public function testUpdateSpecificCoupons(array $origData, array $data, ?array $update = null): void
80+
{
81+
/** @var Rule|MockObject $abstractModel */
82+
$ruleMock = $this->getMockBuilder(Rule::class)
83+
->disableOriginalConstructor()
84+
->onlyMethods(['save'])
85+
->getMock();
86+
87+
$ruleMock->setData($origData);
88+
$ruleMock->setOrigData();
89+
$ruleMock->addData($data);
90+
91+
$this->resourcesMock->expects($this->any())
92+
->method('getTableName')
93+
->willReturnArgument(0);
94+
95+
if ($update) {
96+
$this->connectionMock->expects($this->once())
97+
->method('update')
98+
->with(
99+
'salesrule_coupon',
100+
$update,
101+
['rule_id = ?' => $data['id']]
102+
);
103+
} else {
104+
$this->connectionMock->expects($this->never())
105+
->method('update');
106+
}
107+
$this->model->updateSpecificCoupons($ruleMock);
108+
}
109+
110+
/**
111+
* @return array
112+
*/
113+
public static function updateSpecificCouponsDataProvider(): array
114+
{
115+
return [
116+
[
117+
['uses_per_coupon' => 1],
118+
['uses_per_coupon' => 0],
119+
null
120+
],
121+
[
122+
['uses_per_customer' => 1],
123+
['uses_per_customer' => 0],
124+
null
125+
],
126+
[
127+
['coupon_type' => Rule::COUPON_TYPE_SPECIFIC, 'uses_per_coupon' => 1, 'uses_per_customer' => 1],
128+
['coupon_type' => Rule::COUPON_TYPE_AUTO, 'uses_per_coupon' => 0, 'uses_per_customer' => 0],
129+
null
130+
],
131+
[
132+
['uses_per_coupon' => 1],
133+
['id' => 1, 'uses_per_coupon' => 0],
134+
['usage_limit' => 0],
135+
],
136+
[
137+
['uses_per_customer' => 1],
138+
['id' => 1, 'uses_per_customer' => 0],
139+
['usage_per_customer' => 0],
140+
],
141+
[
142+
['coupon_type' => Rule::COUPON_TYPE_SPECIFIC, 'uses_per_coupon' => 1, 'uses_per_customer' => 1],
143+
['id' => 1, 'coupon_type' => Rule::COUPON_TYPE_AUTO, 'uses_per_coupon' => 1, 'uses_per_customer' => 1],
144+
['usage_limit' => 1, 'usage_per_customer' => 1],
145+
],
146+
[
147+
['uses_per_coupon' => 1, 'uses_per_customer' => 1],
148+
['id' => 1, 'uses_per_coupon' => 0, 'uses_per_customer' => 0],
149+
['usage_limit' => 0, 'usage_per_customer' => 0],
150+
],
151+
[
152+
['uses_per_coupon' => 1, 'uses_per_customer' => 1],
153+
['id' => 1, 'uses_per_coupon' => 0, 'uses_per_customer' => 1],
154+
['usage_limit' => 0],
155+
],
156+
[
157+
['uses_per_coupon' => 1, 'uses_per_customer' => 1],
158+
['id' => 1, 'uses_per_coupon' => 1, 'uses_per_customer' => 0],
159+
['usage_per_customer' => 0],
160+
],
161+
];
162+
}
163+
}

app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
declare(strict_types=1);
78

89
namespace Magento\SalesRule\Test\Unit\Model\ResourceModel;
@@ -86,6 +87,11 @@ class RuleTest extends TestCase
8687
*/
8788
private $metadataPoolMock;
8889

90+
/**
91+
* @var \Magento\SalesRule\Model\ResourceModel\Coupon|MockObject
92+
*/
93+
private $resourceCoupon;
94+
8995
protected function setUp(): void
9096
{
9197
$this->objectManager = new ObjectManager($this);
@@ -169,6 +175,8 @@ protected function setUp(): void
169175
->disableOriginalConstructor()
170176
->getMock();
171177

178+
$this->resourceCoupon = $this->createMock(\Magento\SalesRule\Model\ResourceModel\Coupon::class);
179+
172180
$this->model = $this->objectManager->getObject(
173181
Rule::class,
174182
[
@@ -177,7 +185,8 @@ protected function setUp(): void
177185
'entityManager' => $this->entityManager,
178186
'associatedEntityMapInstance' => $associatedEntitiesMap,
179187
'serializer' => $serializerMock,
180-
'metadataPool' => $this->metadataPoolMock
188+
'metadataPool' => $this->metadataPoolMock,
189+
'resourceCoupon' => $this->resourceCoupon
181190
]
182191
);
183192
}
@@ -245,6 +254,56 @@ public function testSaveStoreLabels()
245254
$this->model->saveStoreLabels(1, ['test']);
246255
}
247256

257+
/**
258+
* @dataProvider afterSaveShouldUpdateExistingCouponsDataProvider
259+
* @param array $data
260+
* @param bool $update
261+
* @return void
262+
* @throws \PHPUnit\Framework\MockObject\Exception
263+
*/
264+
public function testAfterSaveShouldUpdateExistingCoupons(array $data, bool $update = true): void
265+
{
266+
/** @var AbstractModel|MockObject $abstractModel */
267+
$ruleMock = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
268+
->disableOriginalConstructor()
269+
->onlyMethods(['getConditions', 'getActions'])
270+
->getMock();
271+
$conditions = $this->createMock(\Magento\Rule\Model\Condition\Combine::class);
272+
$actions = $this->createMock(\Magento\Rule\Model\Action\Collection::class);
273+
$ruleMock->method('getConditions')->willReturn($conditions);
274+
$ruleMock->method('getActions')->willReturn($actions);
275+
$ruleMock->addData($data);
276+
$this->resourceCoupon->expects($update ? $this->once() : $this->never())
277+
->method('updateSpecificCoupons')
278+
->with($ruleMock);
279+
$this->model->afterSave($ruleMock);
280+
}
281+
282+
/**
283+
* @return array
284+
*/
285+
public static function afterSaveShouldUpdateExistingCouponsDataProvider(): array
286+
{
287+
return [
288+
[
289+
['use_auto_generation' => 0, 'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON],
290+
false
291+
],
292+
[
293+
['use_auto_generation' => 0, 'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC],
294+
false
295+
],
296+
[
297+
['use_auto_generation' => 1, 'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC],
298+
true
299+
],
300+
[
301+
['use_auto_generation' => 0, 'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO],
302+
true
303+
]
304+
];
305+
}
306+
248307
/**
249308
* @return array
250309
*/

0 commit comments

Comments
 (0)