Skip to content

Commit

Permalink
Merge pull request #3835 from magento-tsg-csl3/2.3-develop-pr18
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.3 (pr18)
  • Loading branch information
viktym authored Mar 4, 2019
2 parents ef58edb + d9dfcca commit 7d0efc5
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 24 deletions.
156 changes: 156 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Ui/Component/ColumnFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\Ui\Component;

use PHPUnit\Framework\TestCase;
use Magento\Catalog\Ui\Component\ColumnFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\ColumnInterface;
use Magento\Ui\Component\Filters\FilterModifier;

/**
* ColumnFactory test.
*/
class ColumnFactoryTest extends TestCase
{
/**
* @var ColumnFactory
*/
private $columnFactory;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var ProductAttributeInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $attribute;

/**
* @var ContextInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $context;

/**
* @var UiComponentFactory|\PHPUnit\Framework\MockObject\MockObject
*/
private $uiComponentFactory;

/**
* @var ColumnInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $column;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = new ObjectManager($this);

$this->attribute = $this->getMockBuilder(ProductAttributeInterface::class)
->setMethods(['usesSource'])
->getMockForAbstractClass();
$this->context = $this->createMock(ContextInterface::class);
$this->uiComponentFactory = $this->createMock(UiComponentFactory::class);
$this->column = $this->getMockForAbstractClass(ColumnInterface::class);
$this->uiComponentFactory->method('create')
->willReturn($this->column);

$this->columnFactory = $this->objectManager->getObject(ColumnFactory::class, [
'componentFactory' => $this->uiComponentFactory
]);
}

/**
* Tests the create method will return correct object.
*
* @return void
*/
public function testCreatedObject(): void
{
$this->context->method('getRequestParam')
->with(FilterModifier::FILTER_MODIFIER, [])
->willReturn([]);

$object = $this->columnFactory->create($this->attribute, $this->context);
$this->assertEquals(
$this->column,
$object,
'Object must be the same which the ui component factory creates.'
);
}

/**
* Tests create method with not filterable in grid attribute.
*
* @param array $filterModifiers
* @param null|string $filter
*
* @return void
* @dataProvider filterModifiersProvider
*/
public function testCreateWithNotFilterableInGridAttribute(array $filterModifiers, ?string $filter): void
{
$componentFactoryArgument = [
'data' => [
'config' => [
'label' => __(null),
'dataType' => 'text',
'add_field' => true,
'visible' => null,
'filter' => $filter,
'component' => 'Magento_Ui/js/grid/columns/column',
],
],
'context' => $this->context,
];

$this->context->method('getRequestParam')
->with(FilterModifier::FILTER_MODIFIER, [])
->willReturn($filterModifiers);
$this->attribute->method('getIsFilterableInGrid')
->willReturn(false);
$this->attribute->method('getAttributeCode')
->willReturn('color');

$this->uiComponentFactory->expects($this->once())
->method('create')
->with($this->anything(), $this->anything(), $componentFactoryArgument);

$this->columnFactory->create($this->attribute, $this->context);
}

/**
* Filter modifiers data provider.
*
* @return array
*/
public function filterModifiersProvider(): array
{
return [
'without' => [
'filter_modifiers' => [],
'filter' => null,
],
'with' => [
'filter_modifiers' => [
'color' => [
'condition_type' => 'notnull',
],
],
'filter' => 'text',
],
];
}
}
6 changes: 5 additions & 1 deletion app/code/Magento/Catalog/Ui/Component/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Catalog\Ui\Component;

use Magento\Ui\Component\Filters\FilterModifier;

/**
* Column Factory
*
Expand Down Expand Up @@ -60,13 +62,15 @@ public function __construct(\Magento\Framework\View\Element\UiComponentFactory $
*/
public function create($attribute, $context, array $config = [])
{
$filterModifiers = $context->getRequestParam(FilterModifier::FILTER_MODIFIER, []);

$columnName = $attribute->getAttributeCode();
$config = array_merge([
'label' => __($attribute->getDefaultFrontendLabel()),
'dataType' => $this->getDataType($attribute),
'add_field' => true,
'visible' => $attribute->getIsVisibleInGrid(),
'filter' => ($attribute->getIsFilterableInGrid())
'filter' => ($attribute->getIsFilterableInGrid() || array_key_exists($columnName, $filterModifiers))
? $this->getFilterType($attribute->getFrontendInput())
: null,
], $config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
*/
namespace Magento\ConfigurableProduct\Model\Product\Type\Configurable;

use Magento\Catalog\Model\Product;

/**
* Class Price for configurable product
*/
class Price extends \Magento\Catalog\Model\Product\Type\Price
{
/**
* Get product final price
*
* @param float $qty
* @param \Magento\Catalog\Model\Product $product
* @return float
* @inheritdoc
*/
public function getFinalPrice($qty, $product)
{
if ($qty === null && $product->getCalculatedFinalPrice() !== null) {
return $product->getCalculatedFinalPrice();
}
if ($product->getCustomOption('simple_product') && $product->getCustomOption('simple_product')->getProduct()) {
$finalPrice = parent::getFinalPrice($qty, $product->getCustomOption('simple_product')->getProduct());
/** @var Product $simpleProduct */
$simpleProduct = $product->getCustomOption('simple_product')->getProduct();
$simpleProduct->setCustomerGroupId($product->getCustomerGroupId());
$finalPrice = parent::getFinalPrice($qty, $simpleProduct);
} else {
$priceInfo = $product->getPriceInfo();
$finalPrice = $priceInfo->getPrice('final_price')->getAmount()->getValue();
Expand All @@ -35,7 +39,7 @@ public function getFinalPrice($qty, $product)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getPrice($product)
{
Expand All @@ -48,6 +52,7 @@ public function getPrice($product)
}
}
}

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,77 @@

namespace Magento\ConfigurableProduct\Test\Unit\Model\Product\Type\Configurable;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Configuration\Item\Option;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price as ConfigurablePrice;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Pricing\Amount\AmountInterface;
use Magento\Framework\Pricing\Price\PriceInterface;
use Magento\Framework\Pricing\PriceInfo\Base as PriceInfoBase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit\Framework\MockObject\MockObject;

class PriceTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price */
/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;

/**
* @var ConfigurablePrice
*/
protected $model;

/** @var ObjectManagerHelper */
protected $objectManagerHelper;
/**
* @var ManagerInterface|MockObject
*/
private $eventManagerMock;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);

$this->eventManagerMock = $this->createPartialMock(
ManagerInterface::class,
['dispatch']
);
$this->model = $this->objectManagerHelper->getObject(
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Price::class
ConfigurablePrice::class,
['eventManager' => $this->eventManagerMock]
);
}

public function testGetFinalPrice()
{
$finalPrice = 10;
$qty = 1;
$configurableProduct = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->setMethods(['getCustomOption', 'getPriceInfo', 'setFinalPrice', '__wakeUp'])
->getMock();
$customOption = $this->getMockBuilder(\Magento\Catalog\Model\Product\Configuration\Item\Option::class)

/** @var Product|MockObject $configurableProduct */
$configurableProduct = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->setMethods(['getProduct'])
->setMethods(['getCustomOption', 'getPriceInfo', 'setFinalPrice'])
->getMock();
$priceInfo = $this->getMockBuilder(\Magento\Framework\Pricing\PriceInfo\Base::class)
/** @var PriceInfoBase|MockObject $priceInfo */
$priceInfo = $this->getMockBuilder(PriceInfoBase::class)
->disableOriginalConstructor()
->setMethods(['getPrice'])
->getMock();
$price = $this->getMockBuilder(\Magento\Framework\Pricing\Price\PriceInterface::class)
/** @var PriceInterface|MockObject $price */
$price = $this->getMockBuilder(PriceInterface::class)
->disableOriginalConstructor()
->getMock();
$amount = $this->getMockBuilder(\Magento\Framework\Pricing\Amount\AmountInterface::class)
/** @var AmountInterface|MockObject $amount */
$amount = $this->getMockBuilder(AmountInterface::class)
->disableOriginalConstructor()
->getMock();

$configurableProduct->expects($this->any())
->method('getCustomOption')
->willReturnMap([['simple_product', false], ['option_ids', false]]);
$customOption->expects($this->never())->method('getProduct');
$configurableProduct->expects($this->once())->method('getPriceInfo')->willReturn($priceInfo);
$priceInfo->expects($this->once())->method('getPrice')->with('final_price')->willReturn($price);
$price->expects($this->once())->method('getAmount')->willReturn($amount);
Expand All @@ -60,4 +85,60 @@ public function testGetFinalPrice()

$this->assertEquals($finalPrice, $this->model->getFinalPrice($qty, $configurableProduct));
}

public function testGetFinalPriceWithSimpleProduct()
{
$finalPrice = 10;
$qty = 1;
$customerGroupId = 1;

/** @var Product|MockObject $configurableProduct */
$configurableProduct = $this->createPartialMock(
Product::class,
['getCustomOption', 'setFinalPrice', 'getCustomerGroupId']
);
/** @var Option|MockObject $customOption */
$customOption = $this->createPartialMock(
Option::class,
['getProduct']
);
/** @var Product|MockObject $simpleProduct */
$simpleProduct = $this->createPartialMock(
Product::class,
['setCustomerGroupId', 'setFinalPrice', 'getPrice', 'getTierPrice', 'getData', 'getCustomOption']
);

$configurableProduct->method('getCustomOption')
->willReturnMap([
['simple_product', $customOption],
['option_ids', false]
]);
$configurableProduct->method('getCustomerGroupId')->willReturn($customerGroupId);
$configurableProduct->expects($this->atLeastOnce())
->method('setFinalPrice')
->with($finalPrice)
->willReturnSelf();
$customOption->method('getProduct')->willReturn($simpleProduct);
$simpleProduct->expects($this->atLeastOnce())
->method('setCustomerGroupId')
->with($customerGroupId)
->willReturnSelf();
$simpleProduct->method('getPrice')->willReturn($finalPrice);
$simpleProduct->method('getTierPrice')->with($qty)->willReturn($finalPrice);
$simpleProduct->expects($this->atLeastOnce())
->method('setFinalPrice')
->with($finalPrice)
->willReturnSelf();
$simpleProduct->method('getData')->with('final_price')->willReturn($finalPrice);
$simpleProduct->method('getCustomOption')->with('option_ids')->willReturn(false);
$this->eventManagerMock->expects($this->once())
->method('dispatch')
->with('catalog_product_get_final_price', ['product' => $simpleProduct, 'qty' => $qty]);

$this->assertEquals(
$finalPrice,
$this->model->getFinalPrice($qty, $configurableProduct),
'The final price calculation is wrong'
);
}
}
Loading

0 comments on commit 7d0efc5

Please sign in to comment.