Skip to content
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

[Forwardport] Cart Rule with negated condition over special price for configurable products #19343

Merged
merged 4 commits into from
Jan 14, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
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
{
/**
* Prepare configurable product for validation.
*
* @param \Magento\SalesRule\Model\Rule\Condition\Product $subject
* @param \Magento\Framework\Model\AbstractModel $model
* @return array
*/
public function beforeValidate(
\Magento\SalesRule\Model\Rule\Condition\Product $subject,
\Magento\Framework\Model\AbstractModel $model
) {
$product = $this->getProductToValidate($subject, $model);
if ($model->getProduct() !== $product) {
// We need to replace product only for validation and keep original product for all other cases.
$clone = clone $model;
$clone->setProduct($product);
$model = $clone;
}

return [$model];
}

/**
* Select proper product for validation.
*
* @param \Magento\SalesRule\Model\Rule\Condition\Product $subject
* @param \Magento\Framework\Model\AbstractModel $model
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@
<argument name="sortBy" value="price"/>
<argument name="sort" value="desc"/>
</actionGroup>
<see selector="{{StorefrontCategoryMainSection.lineProductName('1')}}" userInput="$$createSimpleProduct2.name$$" stepKey="seeSimpleProductTwo2"/>
<see selector="{{StorefrontCategoryMainSection.lineProductName('2')}}" userInput="$$createSimpleProduct.name$$" stepKey="seeSimpleProduct2"/>
<see selector="{{StorefrontCategoryMainSection.lineProductName('3')}}" userInput="$$createConfigProduct.name$$" stepKey="seeConfigurableProduct2"/>
<see selector="{{StorefrontCategoryMainSection.lineProductName('1')}}" userInput="$$createConfigProduct.name$$" stepKey="seeConfigurableProduct2"/>
<see selector="{{StorefrontCategoryMainSection.lineProductName('2')}}" userInput="$$createSimpleProduct2.name$$" stepKey="seeSimpleProductTwo2"/>
<see selector="{{StorefrontCategoryMainSection.lineProductName('3')}}" userInput="$$createSimpleProduct.name$$" stepKey="seeSimpleProduct2"/>

<!-- Delete the rule -->
<amOnPage url="{{CatalogRulePage.url}}" stepKey="goToPriceRulePage"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?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($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);

$this->validator->setAttribute('special_price');

$this->validatorPlugin->beforeValidate($this->validator, $item);
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/ConfigurableProduct/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,7 @@
</argument>
</arguments>
</type>
<type name="Magento\SalesRule\Model\Rule\Condition\Product">
<plugin name="apply_rule_on_configurable_children" type="Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition\Product" />
</type>
</config>
Loading