-
Notifications
You must be signed in to change notification settings - Fork 9.4k
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does… #16342
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
magento-engcom-team
merged 18 commits into
magento:2.2-develop
from
novikor:#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does-not-work-for-configurable-products
Nov 21, 2018
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
90b6803
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor 2717cb1
Removed empty line due to failing static test
phoenix128 d2a0de8
Removed dependency to configurable product
phoenix128 fae98c0
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor 9a35b45
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor 887ee4a
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor 0c9aa81
Revert "#14020-Cart-Sales-Rule-with-negated-condition-over-special-pr…
novikor 4e68337
Merge branch '2.2-develop' into #14020-Cart-Sales-Rule-with-negated-c…
novikor 5b95b22
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor 618f408
Moved adjustments to plugin.
novikor 7c8482b
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor e7130bf
Merge remote-tracking branch 'upstream/2.2-develop' into #14020-Cart-…
novikor 5a7e78a
Merge remote-tracking branch 'origin/#14020-Cart-Sales-Rule-with-nega…
novikor 8d417ac
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor 90ff989
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor c52e0e8
Covered case when simple product is being validated
novikor 5c3154b
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does…
novikor 51adb9d
Merge remote-tracking branch 'upstream/2.2-develop' into #14020-Cart-…
novikor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
app/code/Magento/ConfigurableProduct/Plugin/SalesRule/Model/Rule/Condition/Product.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition; | ||
|
||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable; | ||
|
||
/** | ||
* Class Product | ||
* | ||
* @package Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition | ||
*/ | ||
class Product | ||
{ | ||
/** | ||
* @param \Magento\SalesRule\Model\Rule\Condition\Product $subject | ||
* @param \Magento\Framework\Model\AbstractModel $model | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, remove extra spaces |
||
*/ | ||
public function beforeValidate( | ||
\Magento\SalesRule\Model\Rule\Condition\Product $subject, | ||
\Magento\Framework\Model\AbstractModel $model | ||
) { | ||
$model->setProduct( | ||
$this->getProductToValidate($subject, $model) | ||
); | ||
} | ||
|
||
/** | ||
* @param \Magento\SalesRule\Model\Rule\Condition\Product $subject | ||
* @param \Magento\Framework\Model\AbstractModel $model | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, remove extra spaces |
||
* | ||
* @return \Magento\Catalog\Api\Data\ProductInterface|\Magento\Catalog\Model\Product | ||
*/ | ||
private function getProductToValidate( | ||
\Magento\SalesRule\Model\Rule\Condition\Product $subject, | ||
\Magento\Framework\Model\AbstractModel $model | ||
) { | ||
/** @var \Magento\Catalog\Model\Product $product */ | ||
$product = $model->getProduct(); | ||
|
||
$attrCode = $subject->getAttribute(); | ||
|
||
/* Check for attributes which are not available for configurable products */ | ||
if ($product->getTypeId() == Configurable::TYPE_CODE && !$product->hasData($attrCode)) { | ||
/** @var \Magento\Catalog\Model\AbstractModel $childProduct */ | ||
$childProduct = current($model->getChildren())->getProduct(); | ||
if ($childProduct->hasData($attrCode)) { | ||
$product = $childProduct; | ||
} | ||
} | ||
|
||
return $product; | ||
} | ||
} |
230 changes: 230 additions & 0 deletions
230
...gento/ConfigurableProduct/Test/Unit/Plugin/SalesRule/Model/Rule/Condition/ProductTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\ConfigurableProduct\Test\Unit\Plugin\SalesRule\Model\Rule\Condition; | ||
|
||
use Magento\Backend\Helper\Data; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\Product\Type; | ||
use Magento\Catalog\Model\ProductFactory; | ||
use Magento\Catalog\Model\ResourceModel\Product; | ||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable; | ||
use Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition\Product as ValidatorPlugin; | ||
use Magento\Directory\Model\CurrencyFactory; | ||
use Magento\Eav\Model\Config; | ||
use Magento\Eav\Model\Entity\AbstractEntity; | ||
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection; | ||
use Magento\Framework\App\ScopeResolverInterface; | ||
use Magento\Framework\Locale\Format; | ||
use Magento\Framework\Locale\FormatInterface; | ||
use Magento\Framework\Locale\ResolverInterface; | ||
use Magento\Quote\Model\Quote\Item\AbstractItem; | ||
use Magento\Rule\Model\Condition\Context; | ||
use Magento\SalesRule\Model\Rule\Condition\Product as SalesRuleProduct; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
* @SuppressWarnings(PHPMD.LongVariable) | ||
*/ | ||
class ProductTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var SalesRuleProduct | ||
*/ | ||
private $validator; | ||
|
||
/** | ||
* @var \Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition\Product | ||
*/ | ||
private $validatorPlugin; | ||
|
||
public function setUp() | ||
{ | ||
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); | ||
$this->validator = $this->createValidator(); | ||
$this->validatorPlugin = $this->objectManager->getObject(ValidatorPlugin::class); | ||
} | ||
|
||
/** | ||
* @return \Magento\SalesRule\Model\Rule\Condition\Product | ||
*/ | ||
private function createValidator(): SalesRuleProduct | ||
{ | ||
/** @var Context|\PHPUnit_Framework_MockObject_MockObject $contextMock */ | ||
$contextMock = $this->getMockBuilder(Context::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
/** @var Data|\PHPUnit_Framework_MockObject_MockObject $backendHelperMock */ | ||
$backendHelperMock = $this->getMockBuilder(Data::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
/** @var Config|\PHPUnit_Framework_MockObject_MockObject $configMock */ | ||
$configMock = $this->getMockBuilder(Config::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
/** @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject $productFactoryMock */ | ||
$productFactoryMock = $this->getMockBuilder(ProductFactory::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
/** @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject $productRepositoryMock */ | ||
$productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class) | ||
->getMockForAbstractClass(); | ||
$attributeLoaderInterfaceMock = $this->getMockBuilder(AbstractEntity::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getAttributesByCode']) | ||
->getMock(); | ||
$attributeLoaderInterfaceMock | ||
->expects($this->any()) | ||
->method('getAttributesByCode') | ||
->willReturn([]); | ||
/** @var Product|\PHPUnit_Framework_MockObject_MockObject $productMock */ | ||
$productMock = $this->getMockBuilder(Product::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['loadAllAttributes', 'getConnection', 'getTable']) | ||
->getMock(); | ||
$productMock->expects($this->any()) | ||
->method('loadAllAttributes') | ||
->willReturn($attributeLoaderInterfaceMock); | ||
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */ | ||
$collectionMock = $this->getMockBuilder(Collection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
/** @var FormatInterface|\PHPUnit_Framework_MockObject_MockObject $formatMock */ | ||
$formatMock = new Format( | ||
$this->getMockBuilder(ScopeResolverInterface::class)->disableOriginalConstructor()->getMock(), | ||
$this->getMockBuilder(ResolverInterface::class)->disableOriginalConstructor()->getMock(), | ||
$this->getMockBuilder(CurrencyFactory::class)->disableOriginalConstructor()->getMock() | ||
); | ||
|
||
return new SalesRuleProduct( | ||
$contextMock, | ||
$backendHelperMock, | ||
$configMock, | ||
$productFactoryMock, | ||
$productRepositoryMock, | ||
$productMock, | ||
$collectionMock, | ||
$formatMock | ||
); | ||
} | ||
|
||
public function testChildIsUsedForValidation() | ||
{ | ||
$configurableProductMock = $this->createProductMock(); | ||
$configurableProductMock | ||
->expects($this->any()) | ||
->method('getTypeId') | ||
->willReturn(Configurable::TYPE_CODE); | ||
$configurableProductMock | ||
->expects($this->any()) | ||
->method('hasData') | ||
->with($this->equalTo('special_price')) | ||
->willReturn(false); | ||
|
||
/* @var AbstractItem|\PHPUnit_Framework_MockObject_MockObject $item */ | ||
$item = $this->getMockBuilder(AbstractItem::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['setProduct', 'getProduct', 'getChildren']) | ||
->getMockForAbstractClass(); | ||
$item->expects($this->any()) | ||
->method('getProduct') | ||
->willReturn($configurableProductMock); | ||
|
||
$simpleProductMock = $this->createProductMock(); | ||
$simpleProductMock | ||
->expects($this->any()) | ||
->method('getTypeId') | ||
->willReturn(Type::TYPE_SIMPLE); | ||
$simpleProductMock | ||
->expects($this->any()) | ||
->method('hasData') | ||
->with($this->equalTo('special_price')) | ||
->willReturn(true); | ||
|
||
$childItem = $this->getMockBuilder(AbstractItem::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getProduct']) | ||
->getMockForAbstractClass(); | ||
$childItem->expects($this->any()) | ||
->method('getProduct') | ||
->willReturn($simpleProductMock); | ||
|
||
$item->expects($this->any()) | ||
->method('getChildren') | ||
->willReturn([$childItem]); | ||
$item->expects($this->once()) | ||
->method('setProduct') | ||
->with($this->identicalTo($simpleProductMock)); | ||
|
||
$this->validator->setAttribute('special_price'); | ||
|
||
$this->validatorPlugin->beforeValidate($this->validator, $item); | ||
} | ||
|
||
/** | ||
* @return Product|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private function createProductMock(): \PHPUnit_Framework_MockObject_MockObject | ||
{ | ||
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) | ||
->disableOriginalConstructor() | ||
->setMethods([ | ||
'getAttribute', | ||
'getId', | ||
'setQuoteItemQty', | ||
'setQuoteItemPrice', | ||
'getTypeId', | ||
'hasData', | ||
]) | ||
->getMock(); | ||
$productMock | ||
->expects($this->any()) | ||
->method('setQuoteItemQty') | ||
->willReturnSelf(); | ||
$productMock | ||
->expects($this->any()) | ||
->method('setQuoteItemPrice') | ||
->willReturnSelf(); | ||
|
||
return $productMock; | ||
} | ||
|
||
public function testChildIsNotUsedForValidation() | ||
{ | ||
$simpleProductMock = $this->createProductMock(); | ||
$simpleProductMock | ||
->expects($this->any()) | ||
->method('getTypeId') | ||
->willReturn(Type::TYPE_SIMPLE); | ||
$simpleProductMock | ||
->expects($this->any()) | ||
->method('hasData') | ||
->with($this->equalTo('special_price')) | ||
->willReturn(true); | ||
|
||
/* @var AbstractItem|\PHPUnit_Framework_MockObject_MockObject $item */ | ||
$item = $this->getMockBuilder(AbstractItem::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['setProduct', 'getProduct']) | ||
->getMockForAbstractClass(); | ||
$item->expects($this->any()) | ||
->method('getProduct') | ||
->willReturn($simpleProductMock); | ||
|
||
$item->expects($this->once()) | ||
->method('setProduct') | ||
->with($this->identicalTo($simpleProductMock)); | ||
|
||
$this->validator->setAttribute('special_price'); | ||
|
||
$this->validatorPlugin->beforeValidate($this->validator, $item); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
* Product rule condition data model | ||
* | ||
* @author Magento Core Team <[email protected]> | ||
* | ||
* @method string getAttribute() | ||
*/ | ||
class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct | ||
{ | ||
|
@@ -31,7 +33,9 @@ protected function _addSpecialAttributes(array &$attributes) | |
* Validate Product Rule Condition | ||
* | ||
* @param \Magento\Framework\Model\AbstractModel $model | ||
* | ||
* @return bool | ||
* @throws \Magento\Framework\Exception\NoSuchEntityException | ||
*/ | ||
public function validate(\Magento\Framework\Model\AbstractModel $model) | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, cover new code at least by the unit test. It would be great if scenario will be covered with integration test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit tests coverage have been implemented.
@sidolov, please check.