Skip to content

Commit 68cc3a0

Browse files
ENGCOM-5926: Added description discount for each summary(cart, order) #24595
- Merge Pull Request #24595 from MaxSouza/magento2:fix-3594 - Merged commits: 1. 23382ed 2. 217ab79 3. 83a84a3 4. 7ec7d65 5. 42edfaf 6. fb5c663 7. 7eed116 8. afd489b 9. c1d4190
2 parents 50a085d + c1d4190 commit 68cc3a0

File tree

2 files changed

+122
-45
lines changed

2 files changed

+122
-45
lines changed

app/code/Magento/SalesRule/Model/RulesApplier.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
namespace Magento\SalesRule\Model;
77

88
use Magento\Quote\Model\Quote\Address;
9+
use Magento\Quote\Model\Quote\Item\AbstractItem;
910
use Magento\SalesRule\Model\Quote\ChildrenValidationLocator;
1011
use Magento\Framework\App\ObjectManager;
12+
use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
1113
use Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory;
1214

1315
/**
1416
* Class RulesApplier
17+
*
1518
* @package Magento\SalesRule\Model\Validator
1619
*/
1720
class RulesApplier
@@ -54,14 +57,14 @@ public function __construct(
5457
$this->validatorUtility = $utility;
5558
$this->_eventManager = $eventManager;
5659
$this->childrenValidationLocator = $childrenValidationLocator
57-
?: ObjectManager::getInstance()->get(ChildrenValidationLocator::class);
60+
?: ObjectManager::getInstance()->get(ChildrenValidationLocator::class);
5861
}
5962

6063
/**
6164
* Apply rules to current order item
6265
*
63-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
64-
* @param \Magento\SalesRule\Model\ResourceModel\Rule\Collection $rules
66+
* @param AbstractItem $item
67+
* @param Collection $rules
6568
* @param bool $skipValidation
6669
* @param mixed $couponCode
6770
* @return array
@@ -71,15 +74,15 @@ public function applyRules($item, $rules, $skipValidation, $couponCode)
7174
{
7275
$address = $item->getAddress();
7376
$appliedRuleIds = [];
74-
/* @var $rule \Magento\SalesRule\Model\Rule */
77+
/* @var $rule Rule */
7578
foreach ($rules as $rule) {
7679
if (!$this->validatorUtility->canProcessRule($rule, $address)) {
7780
continue;
7881
}
7982

8083
if (!$skipValidation && !$rule->getActions()->validate($item)) {
8184
if (!$this->childrenValidationLocator->isChildrenValidationRequired($item)) {
82-
continue;
85+
continue;
8386
}
8487
$childItems = $item->getChildren();
8588
$isContinue = true;
@@ -110,7 +113,7 @@ public function applyRules($item, $rules, $skipValidation, $couponCode)
110113
* Add rule discount description label to address object
111114
*
112115
* @param Address $address
113-
* @param \Magento\SalesRule\Model\Rule $rule
116+
* @param Rule $rule
114117
* @return $this
115118
*/
116119
public function addDiscountDescription($address, $rule)
@@ -123,6 +126,10 @@ public function addDiscountDescription($address, $rule)
123126
} else {
124127
if (strlen($address->getCouponCode())) {
125128
$label = $address->getCouponCode();
129+
130+
if ($rule->getDescription()) {
131+
$label = $rule->getDescription();
132+
}
126133
}
127134
}
128135

@@ -136,8 +143,10 @@ public function addDiscountDescription($address, $rule)
136143
}
137144

138145
/**
139-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
140-
* @param \Magento\SalesRule\Model\Rule $rule
146+
* Apply Rule
147+
*
148+
* @param AbstractItem $item
149+
* @param Rule $rule
141150
* @param \Magento\Quote\Model\Quote\Address $address
142151
* @param mixed $couponCode
143152
* @return $this
@@ -154,8 +163,10 @@ protected function applyRule($item, $rule, $address, $couponCode)
154163
}
155164

156165
/**
157-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
158-
* @param \Magento\SalesRule\Model\Rule $rule
166+
* Get discount Data
167+
*
168+
* @param AbstractItem $item
169+
* @param Rule $rule
159170
* @return \Magento\SalesRule\Model\Rule\Action\Discount\Data
160171
*/
161172
protected function getDiscountData($item, $rule)
@@ -180,8 +191,10 @@ protected function getDiscountData($item, $rule)
180191
}
181192

182193
/**
194+
* Set discount
195+
*
183196
* @param \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData
184-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
197+
* @param AbstractItem $item
185198
* @return $this
186199
*/
187200
protected function setDiscountData($discountData, $item)
@@ -198,7 +211,7 @@ protected function setDiscountData($discountData, $item)
198211
* Set coupon code to address if $rule contains validated coupon
199212
*
200213
* @param Address $address
201-
* @param \Magento\SalesRule\Model\Rule $rule
214+
* @param Rule $rule
202215
* @param mixed $couponCode
203216
* @return $this
204217
*/
@@ -208,7 +221,7 @@ public function maintainAddressCouponCode($address, $rule, $couponCode)
208221
Rule is a part of rules collection, which includes only rules with 'No Coupon' type or with validated coupon.
209222
As a result, if rule uses coupon code(s) ('Specific' or 'Auto' Coupon Type), it always contains validated coupon
210223
*/
211-
if ($rule->getCouponType() != \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON) {
224+
if ($rule->getCouponType() != Rule::COUPON_TYPE_NO_COUPON) {
212225
$address->setCouponCode($couponCode);
213226
}
214227

@@ -219,15 +232,15 @@ public function maintainAddressCouponCode($address, $rule, $couponCode)
219232
* Fire event to allow overwriting of discount amounts
220233
*
221234
* @param \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData
222-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
223-
* @param \Magento\SalesRule\Model\Rule $rule
235+
* @param AbstractItem $item
236+
* @param Rule $rule
224237
* @param float $qty
225238
* @return $this
226239
*/
227240
protected function eventFix(
228241
\Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData,
229-
\Magento\Quote\Model\Quote\Item\AbstractItem $item,
230-
\Magento\SalesRule\Model\Rule $rule,
242+
AbstractItem $item,
243+
Rule $rule,
231244
$qty
232245
) {
233246
$quote = $item->getQuote();
@@ -249,11 +262,13 @@ protected function eventFix(
249262
}
250263

251264
/**
252-
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
265+
* Set Applied Rule Ids
266+
*
267+
* @param AbstractItem $item
253268
* @param int[] $appliedRuleIds
254269
* @return $this
255270
*/
256-
public function setAppliedRuleIds(\Magento\Quote\Model\Quote\Item\AbstractItem $item, array $appliedRuleIds)
271+
public function setAppliedRuleIds(AbstractItem $item, array $appliedRuleIds)
257272
{
258273
$address = $item->getAddress();
259274
$quote = $item->getQuote();

app/code/Magento/SalesRule/Test/Unit/Model/RulesApplierTest.php

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,67 @@
66

77
namespace Magento\SalesRule\Test\Unit\Model;
88

9+
use Magento\Framework\Event\Manager;
10+
use Magento\Quote\Model\Quote;
11+
use Magento\Quote\Model\Quote\Address;
12+
use Magento\Quote\Model\Quote\Item;
13+
use Magento\Quote\Model\Quote\Item\AbstractItem;
14+
use Magento\Rule\Model\Action\Collection;
15+
use Magento\SalesRule\Model\Quote\ChildrenValidationLocator;
16+
use Magento\SalesRule\Model\Rule;
17+
use Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory;
18+
use Magento\SalesRule\Model\Rule\Action\Discount\Data;
19+
use Magento\SalesRule\Model\Rule\Action\Discount\DiscountInterface;
20+
use Magento\SalesRule\Model\RulesApplier;
21+
use Magento\SalesRule\Model\Utility;
22+
use PHPUnit\Framework\TestCase;
23+
use PHPUnit_Framework_MockObject_MockObject;
24+
925
/**
1026
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1127
*/
12-
class RulesApplierTest extends \PHPUnit\Framework\TestCase
28+
class RulesApplierTest extends TestCase
1329
{
1430
/**
15-
* @var \Magento\SalesRule\Model\RulesApplier
31+
* @var RulesApplier
1632
*/
1733
protected $rulesApplier;
1834

1935
/**
20-
* @var \Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory|\PHPUnit_Framework_MockObject_MockObject
36+
* @var CalculatorFactory|PHPUnit_Framework_MockObject_MockObject
2137
*/
2238
protected $calculatorFactory;
2339

2440
/**
25-
* @var \Magento\Framework\Event\Manager|\PHPUnit_Framework_MockObject_MockObject
41+
* @var Manager|PHPUnit_Framework_MockObject_MockObject
2642
*/
2743
protected $eventManager;
2844

2945
/**
30-
* @var \Magento\SalesRule\Model\Utility|\PHPUnit_Framework_MockObject_MockObject
46+
* @var Utility|PHPUnit_Framework_MockObject_MockObject
3147
*/
3248
protected $validatorUtility;
3349

3450
/**
35-
* @var \Magento\SalesRule\Model\Quote\ChildrenValidationLocator|\PHPUnit_Framework_MockObject_MockObject
51+
* @var ChildrenValidationLocator|PHPUnit_Framework_MockObject_MockObject
3652
*/
3753
protected $childrenValidationLocator;
3854

3955
protected function setUp()
4056
{
4157
$this->calculatorFactory = $this->createMock(
42-
\Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory::class
58+
CalculatorFactory::class
4359
);
44-
$this->eventManager = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
60+
$this->eventManager = $this->createPartialMock(Manager::class, ['dispatch']);
4561
$this->validatorUtility = $this->createPartialMock(
46-
\Magento\SalesRule\Model\Utility::class,
62+
Utility::class,
4763
['canProcessRule', 'minFix', 'deltaRoundingFix', 'getItemQty']
4864
);
4965
$this->childrenValidationLocator = $this->createPartialMock(
50-
\Magento\SalesRule\Model\Quote\ChildrenValidationLocator::class,
66+
ChildrenValidationLocator::class,
5167
['isChildrenValidationRequired']
5268
);
53-
$this->rulesApplier = new \Magento\SalesRule\Model\RulesApplier(
69+
$this->rulesApplier = new RulesApplier(
5470
$this->calculatorFactory,
5571
$this->eventManager,
5672
$this->validatorUtility,
@@ -75,19 +91,21 @@ public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed($isChildren,
7591
$appliedRuleIds = [$ruleId => $ruleId];
7692

7793
/**
78-
* @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleWithStopFurtherProcessing
94+
* @var Rule|PHPUnit_Framework_MockObject_MockObject $ruleWithStopFurtherProcessing
7995
*/
8096
$ruleWithStopFurtherProcessing = $this->createPartialMock(
81-
\Magento\SalesRule\Model\Rule::class,
97+
Rule::class,
8298
['getStoreLabel', 'getCouponType', 'getRuleId', '__wakeup', 'getActions']
8399
);
84-
/** @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleThatShouldNotBeRun */
100+
/**
101+
* @var Rule|PHPUnit_Framework_MockObject_MockObject $ruleThatShouldNotBeRun
102+
*/
85103
$ruleThatShouldNotBeRun = $this->createPartialMock(
86-
\Magento\SalesRule\Model\Rule::class,
104+
Rule::class,
87105
['getStopRulesProcessing', '__wakeup']
88106
);
89107

90-
$actionMock = $this->createPartialMock(\Magento\Rule\Model\Action\Collection::class, ['validate']);
108+
$actionMock = $this->createPartialMock(Collection::class, ['validate']);
91109

92110
$ruleWithStopFurtherProcessing->setName('ruleWithStopFurtherProcessing');
93111
$ruleThatShouldNotBeRun->setName('ruleThatShouldNotBeRun');
@@ -140,6 +158,40 @@ public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed($isChildren,
140158
$this->assertEquals($appliedRuleIds, $result);
141159
}
142160

161+
public function testAddCouponDescriptionWithRuleDescriptionIsUsed()
162+
{
163+
$ruleId = 1;
164+
$ruleDescription = 'Rule description';
165+
166+
/**
167+
* @var Rule|PHPUnit_Framework_MockObject_MockObject $rule
168+
*/
169+
$rule = $this->createPartialMock(
170+
Rule::class,
171+
['getStoreLabel', 'getCouponType', 'getRuleId', '__wakeup', 'getActions']
172+
);
173+
174+
$rule->setDescription($ruleDescription);
175+
176+
/**
177+
* @var Address|PHPUnit_Framework_MockObject_MockObject $address
178+
*/
179+
$address = $this->createPartialMock(
180+
Address::class,
181+
[
182+
'getQuote',
183+
'setCouponCode',
184+
'setAppliedRuleIds',
185+
'__wakeup'
186+
]
187+
);
188+
$description = $address->getDiscountDescriptionArray();
189+
$description[$ruleId] = $rule->getDescription();
190+
$address->setDiscountDescriptionArray($description[$ruleId]);
191+
192+
$this->assertEquals($address->getDiscountDescriptionArray(), $description[$ruleId]);
193+
}
194+
143195
/**
144196
* @return array
145197
*/
@@ -152,28 +204,38 @@ public function dataProviderChildren()
152204
}
153205

154206
/**
155-
* @return \Magento\Quote\Model\Quote\Item\AbstractItem|\PHPUnit_Framework_MockObject_MockObject
207+
* @return AbstractItem|PHPUnit_Framework_MockObject_MockObject
156208
*/
157209
protected function getPreparedItem()
158210
{
159-
/** @var \Magento\Quote\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject $address */
160-
$address = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
211+
/**
212+
* @var Address|PHPUnit_Framework_MockObject_MockObject $address
213+
*/
214+
$address = $this->createPartialMock(
215+
Address::class,
216+
[
161217
'getQuote',
162218
'setCouponCode',
163219
'setAppliedRuleIds',
164220
'__wakeup'
165-
]);
166-
/** @var \Magento\Quote\Model\Quote\Item\AbstractItem|\PHPUnit_Framework_MockObject_MockObject $item */
167-
$item = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, [
221+
]
222+
);
223+
/**
224+
* @var AbstractItem|PHPUnit_Framework_MockObject_MockObject $item
225+
*/
226+
$item = $this->createPartialMock(
227+
Item::class,
228+
[
168229
'setDiscountAmount',
169230
'setBaseDiscountAmount',
170231
'setDiscountPercent',
171232
'getAddress',
172233
'setAppliedRuleIds',
173234
'__wakeup',
174235
'getChildren'
175-
]);
176-
$quote = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getStore', '__wakeUp']);
236+
]
237+
);
238+
$quote = $this->createPartialMock(Quote::class, ['getStore', '__wakeUp']);
177239
$item->expects($this->any())->method('getAddress')->will($this->returnValue($address));
178240
$address->expects($this->any())
179241
->method('getQuote')
@@ -190,10 +252,10 @@ protected function applyRule($item, $rule)
190252
{
191253
$qty = 2;
192254
$discountCalc = $this->createPartialMock(
193-
\Magento\SalesRule\Model\Rule\Action\Discount\DiscountInterface::class,
255+
DiscountInterface::class,
194256
['fixQuantity', 'calculate']
195257
);
196-
$discountData = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Action\Discount\Data::class)
258+
$discountData = $this->getMockBuilder(Data::class)
197259
->setConstructorArgs(
198260
[
199261
'amount' => 30,

0 commit comments

Comments
 (0)