Skip to content

Added description discount for each summary(cart, order) #24595

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

Merged
merged 10 commits into from
Sep 26, 2019
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
50 changes: 28 additions & 22 deletions app/code/Magento/SalesRule/Model/RulesApplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
namespace Magento\SalesRule\Model;

use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\SalesRule\Model\Quote\ChildrenValidationLocator;
use Magento\Framework\App\ObjectManager;
use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
use Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory;
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;

/**
* Rules Applier Model
* Class RulesApplier
*
* @package Magento\SalesRule\Model\Validator
*/
Expand Down Expand Up @@ -70,8 +72,8 @@ public function __construct(
/**
* Apply rules to current order item
*
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param \Magento\SalesRule\Model\ResourceModel\Rule\Collection $rules
* @param AbstractItem $item
* @param Collection $rules
* @param bool $skipValidation
* @param mixed $couponCode
* @return array
Expand All @@ -81,15 +83,15 @@ public function applyRules($item, $rules, $skipValidation, $couponCode)
{
$address = $item->getAddress();
$appliedRuleIds = [];
/* @var $rule \Magento\SalesRule\Model\Rule */
/* @var $rule Rule */
foreach ($rules as $rule) {
if (!$this->validatorUtility->canProcessRule($rule, $address)) {
continue;
}

if (!$skipValidation && !$rule->getActions()->validate($item)) {
if (!$this->childrenValidationLocator->isChildrenValidationRequired($item)) {
continue;
continue;
}
$childItems = $item->getChildren();
$isContinue = true;
Expand Down Expand Up @@ -120,7 +122,7 @@ public function applyRules($item, $rules, $skipValidation, $couponCode)
* Add rule discount description label to address object
*
* @param Address $address
* @param \Magento\SalesRule\Model\Rule $rule
* @param Rule $rule
* @return $this
*/
public function addDiscountDescription($address, $rule)
Expand All @@ -133,6 +135,10 @@ public function addDiscountDescription($address, $rule)
} else {
if (strlen($address->getCouponCode())) {
$label = $address->getCouponCode();

if ($rule->getDescription()) {
$label = $rule->getDescription();
}
}
}

Expand All @@ -146,10 +152,10 @@ public function addDiscountDescription($address, $rule)
}

/**
* Apply rule
* Apply Rule
*
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param \Magento\SalesRule\Model\Rule $rule
* @param AbstractItem $item
* @param Rule $rule
* @param \Magento\Quote\Model\Quote\Address $address
* @param mixed $couponCode
* @return $this
Expand All @@ -166,10 +172,10 @@ protected function applyRule($item, $rule, $address, $couponCode)
}

/**
* Get Discount data
* Get discount Data
*
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param \Magento\SalesRule\Model\Rule $rule
* @param AbstractItem $item
* @param Rule $rule
* @return \Magento\SalesRule\Model\Rule\Action\Discount\Data
*/
protected function getDiscountData($item, $rule)
Expand Down Expand Up @@ -222,7 +228,7 @@ private function setDiscountBreakdown($discountData, $item, $rule)
* Set Discount data
*
* @param \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param AbstractItem $item
* @return $this
*/
protected function setDiscountData($discountData, $item)
Expand All @@ -239,7 +245,7 @@ protected function setDiscountData($discountData, $item)
* Set coupon code to address if $rule contains validated coupon
*
* @param Address $address
* @param \Magento\SalesRule\Model\Rule $rule
* @param Rule $rule
* @param mixed $couponCode
* @return $this
*/
Expand All @@ -249,7 +255,7 @@ public function maintainAddressCouponCode($address, $rule, $couponCode)
Rule is a part of rules collection, which includes only rules with 'No Coupon' type or with validated coupon.
As a result, if rule uses coupon code(s) ('Specific' or 'Auto' Coupon Type), it always contains validated coupon
*/
if ($rule->getCouponType() != \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON) {
if ($rule->getCouponType() != Rule::COUPON_TYPE_NO_COUPON) {
$address->setCouponCode($couponCode);
}

Expand All @@ -260,15 +266,15 @@ public function maintainAddressCouponCode($address, $rule, $couponCode)
* Fire event to allow overwriting of discount amounts
*
* @param \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param \Magento\SalesRule\Model\Rule $rule
* @param AbstractItem $item
* @param Rule $rule
* @param float $qty
* @return $this
*/
protected function eventFix(
\Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData,
\Magento\Quote\Model\Quote\Item\AbstractItem $item,
\Magento\SalesRule\Model\Rule $rule,
AbstractItem $item,
Rule $rule,
$qty
) {
$quote = $item->getQuote();
Expand All @@ -290,13 +296,13 @@ protected function eventFix(
}

/**
* Set Applied Rule ids
* Set Applied Rule Ids
*
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param AbstractItem $item
* @param int[] $appliedRuleIds
* @return $this
*/
public function setAppliedRuleIds(\Magento\Quote\Model\Quote\Item\AbstractItem $item, array $appliedRuleIds)
public function setAppliedRuleIds(AbstractItem $item, array $appliedRuleIds)
{
$address = $item->getAddress();
$quote = $item->getQuote();
Expand Down
100 changes: 78 additions & 22 deletions app/code/Magento/SalesRule/Test/Unit/Model/RulesApplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,34 @@

namespace Magento\SalesRule\Test\Unit\Model;

use Magento\Framework\Event\Manager;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Item;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\Rule\Model\Action\Collection;
use Magento\SalesRule\Model\Quote\ChildrenValidationLocator;
use Magento\SalesRule\Model\Rule;
use Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory;
use Magento\SalesRule\Model\Rule\Action\Discount\Data;
use Magento\SalesRule\Model\Rule\Action\Discount\DiscountInterface;
use Magento\SalesRule\Model\RulesApplier;
use Magento\SalesRule\Model\Utility;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class RulesApplierTest extends \PHPUnit\Framework\TestCase
class RulesApplierTest extends TestCase
{
/**
* @var \Magento\SalesRule\Model\RulesApplier
* @var RulesApplier
*/
protected $rulesApplier;

/**
* @var \Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory|\PHPUnit_Framework_MockObject_MockObject
* @var CalculatorFactory|PHPUnit_Framework_MockObject_MockObject
*/
protected $calculatorFactory;

Expand All @@ -27,39 +43,39 @@ class RulesApplierTest extends \PHPUnit\Framework\TestCase
protected $discountFactory;

/**
* @var \Magento\Framework\Event\Manager|\PHPUnit_Framework_MockObject_MockObject
* @var Manager|PHPUnit_Framework_MockObject_MockObject
*/
protected $eventManager;

/**
* @var \Magento\SalesRule\Model\Utility|\PHPUnit_Framework_MockObject_MockObject
* @var Utility|PHPUnit_Framework_MockObject_MockObject
*/
protected $validatorUtility;

/**
* @var \Magento\SalesRule\Model\Quote\ChildrenValidationLocator|\PHPUnit_Framework_MockObject_MockObject
* @var ChildrenValidationLocator|PHPUnit_Framework_MockObject_MockObject
*/
protected $childrenValidationLocator;

protected function setUp()
{
$this->calculatorFactory = $this->createMock(
\Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory::class
CalculatorFactory::class
);
$this->discountFactory = $this->createPartialMock(
\Magento\SalesRule\Model\Rule\Action\Discount\DataFactory::class,
['create']
);
$this->eventManager = $this->createPartialMock(\Magento\Framework\Event\Manager::class, ['dispatch']);
$this->validatorUtility = $this->createPartialMock(
\Magento\SalesRule\Model\Utility::class,
Utility::class,
['canProcessRule', 'minFix', 'deltaRoundingFix', 'getItemQty']
);
$this->childrenValidationLocator = $this->createPartialMock(
\Magento\SalesRule\Model\Quote\ChildrenValidationLocator::class,
ChildrenValidationLocator::class,
['isChildrenValidationRequired']
);
$this->rulesApplier = new \Magento\SalesRule\Model\RulesApplier(
$this->rulesApplier = new RulesApplier(
$this->calculatorFactory,
$this->eventManager,
$this->validatorUtility,
Expand Down Expand Up @@ -98,19 +114,21 @@ public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed($isChildren,
->with($this->anything())
->will($this->returnValue($discountData));
/**
* @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleWithStopFurtherProcessing
* @var Rule|PHPUnit_Framework_MockObject_MockObject $ruleWithStopFurtherProcessing
*/
$ruleWithStopFurtherProcessing = $this->createPartialMock(
\Magento\SalesRule\Model\Rule::class,
Rule::class,
['getStoreLabel', 'getCouponType', 'getRuleId', '__wakeup', 'getActions']
);
/** @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject $ruleThatShouldNotBeRun */
/**
* @var Rule|PHPUnit_Framework_MockObject_MockObject $ruleThatShouldNotBeRun
*/
$ruleThatShouldNotBeRun = $this->createPartialMock(
\Magento\SalesRule\Model\Rule::class,
Rule::class,
['getStopRulesProcessing', '__wakeup']
);

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

$ruleWithStopFurtherProcessing->setName('ruleWithStopFurtherProcessing');
$ruleThatShouldNotBeRun->setName('ruleThatShouldNotBeRun');
Expand Down Expand Up @@ -163,6 +181,40 @@ public function testApplyRulesWhenRuleWithStopRulesProcessingIsUsed($isChildren,
$this->assertEquals($appliedRuleIds, $result);
}

public function testAddCouponDescriptionWithRuleDescriptionIsUsed()
{
$ruleId = 1;
$ruleDescription = 'Rule description';

/**
* @var Rule|PHPUnit_Framework_MockObject_MockObject $rule
*/
$rule = $this->createPartialMock(
Rule::class,
['getStoreLabel', 'getCouponType', 'getRuleId', '__wakeup', 'getActions']
);

$rule->setDescription($ruleDescription);

/**
* @var Address|PHPUnit_Framework_MockObject_MockObject $address
*/
$address = $this->createPartialMock(
Address::class,
[
'getQuote',
'setCouponCode',
'setAppliedRuleIds',
'__wakeup'
]
);
$description = $address->getDiscountDescriptionArray();
$description[$ruleId] = $rule->getDescription();
$address->setDiscountDescriptionArray($description[$ruleId]);

$this->assertEquals($address->getDiscountDescriptionArray(), $description[$ruleId]);
}

/**
* @return array
*/
Expand All @@ -175,23 +227,27 @@ public function dataProviderChildren()
}

/**
* @return \Magento\Quote\Model\Quote\Item\AbstractItem|\PHPUnit_Framework_MockObject_MockObject
* @return AbstractItem|PHPUnit_Framework_MockObject_MockObject
*/
protected function getPreparedItem()
{
/** @var \Magento\Quote\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject $address */
/**
* @var Address|PHPUnit_Framework_MockObject_MockObject $address
*/
$address = $this->createPartialMock(
\Magento\Quote\Model\Quote\Address::class,
Address::class,
[
'getQuote',
'setCouponCode',
'setAppliedRuleIds',
'__wakeup'
]
);
/** @var \Magento\Quote\Model\Quote\Item\AbstractItem|\PHPUnit_Framework_MockObject_MockObject $item */
/**
* @var AbstractItem|PHPUnit_Framework_MockObject_MockObject $item
*/
$item = $this->createPartialMock(
\Magento\Quote\Model\Quote\Item::class,
Item::class,
[
'setDiscountAmount',
'setBaseDiscountAmount',
Expand Down Expand Up @@ -228,10 +284,10 @@ protected function applyRule($item, $rule)
{
$qty = 2;
$discountCalc = $this->createPartialMock(
\Magento\SalesRule\Model\Rule\Action\Discount\DiscountInterface::class,
DiscountInterface::class,
['fixQuantity', 'calculate']
);
$discountData = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Action\Discount\Data::class)
$discountData = $this->getMockBuilder(Data::class)
->setConstructorArgs(
[
'amount' => 30,
Expand Down