Skip to content

Commit

Permalink
Merge pull request #3826 from magento-tsg-csl3/2.2-develop-pr23
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.2 (pr23)
  • Loading branch information
viktym authored Mar 1, 2019
2 parents 6ddfc58 + 0f0bbe8 commit 9f04388
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 100 deletions.
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 as 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'
);
}
}
22 changes: 7 additions & 15 deletions app/code/Magento/SalesRule/Model/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ protected function _getRules(Address $address = null)
}

/**
* Address id getter.
*
* @param Address $address
* @return string
*/
Expand Down Expand Up @@ -329,21 +331,7 @@ public function processShippingAmount(Address $address)
$baseDiscountAmount = $rule->getDiscountAmount();
break;
case \Magento\SalesRule\Model\Rule::CART_FIXED_ACTION:
$cartRules = $address->getCartFixedRules();
if (!isset($cartRules[$rule->getId()])) {
$cartRules[$rule->getId()] = $rule->getDiscountAmount();
}
if ($cartRules[$rule->getId()] > 0) {
$quoteAmount = $this->priceCurrency->convert($cartRules[$rule->getId()], $quote->getStore());
$discountAmount = min($shippingAmount - $address->getShippingDiscountAmount(), $quoteAmount);
$baseDiscountAmount = min(
$baseShippingAmount - $address->getBaseShippingDiscountAmount(),
$cartRules[$rule->getId()]
);
$cartRules[$rule->getId()] -= $baseDiscountAmount;
}

$address->setCartFixedRules($cartRules);
// Shouldn't be proceed according to MAGETWO-96403
break;
}

Expand Down Expand Up @@ -521,6 +509,8 @@ public function sortItemsByPriority($items, Address $address = null)
}

/**
* Rule total items getter.
*
* @param int $key
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
Expand All @@ -535,6 +525,8 @@ public function getRuleItemTotalsInfo($key)
}

/**
* Decrease rule items count.
*
* @param int $key
* @return $this
*/
Expand Down
Loading

0 comments on commit 9f04388

Please sign in to comment.