Skip to content

Commit 17e0f2b

Browse files
author
Volodymyr Klymenko
authored
Merge pull request #717 from magento-tsg/2.1-prs2
[TSG] Backporting for 2.1 (prs2)
2 parents a971cc7 + b073e02 commit 17e0f2b

File tree

19 files changed

+534
-109
lines changed

19 files changed

+534
-109
lines changed

app/code/Magento/Catalog/Pricing/Render/FinalPriceBox.php

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\Catalog\Pricing\Render;
88

99
use Magento\Catalog\Pricing\Price;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Module\Manager;
1012
use Magento\Framework\Pricing\Render;
1113
use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox;
1214
use Magento\Msrp\Pricing\Price\MsrpPrice;
@@ -29,6 +31,9 @@ class FinalPriceBox extends BasePriceBox
2931
*/
3032
private $salableResolver;
3133

34+
/** @var Manager */
35+
private $moduleManager;
36+
3237
/**
3338
* @param Context $context
3439
* @param SaleableInterface $saleableItem
@@ -55,23 +60,15 @@ public function __construct(
5560
*/
5661
protected function _toHtml()
5762
{
58-
if (!$this->salableResolver->isSalable($this->getSaleableItem())) {
63+
// Check catalog permissions
64+
if ($this->getSaleableItem()->getCanShowPrice() === false) {
5965
return '';
6066
}
6167

6268
$result = parent::_toHtml();
6369

64-
try {
65-
/** @var MsrpPrice $msrpPriceType */
66-
$msrpPriceType = $this->getSaleableItem()->getPriceInfo()->getPrice('msrp_price');
67-
} catch (\InvalidArgumentException $e) {
68-
$this->_logger->critical($e);
69-
return $this->wrapResult($result);
70-
}
71-
7270
//Renders MSRP in case it is enabled
73-
$product = $this->getSaleableItem();
74-
if ($msrpPriceType->canApplyMsrp($product) && $msrpPriceType->isMinimalPriceLessMsrp($product)) {
71+
if ($this->isMsrpPriceApplicable()) {
7572
/** @var BasePriceBox $msrpBlock */
7673
$msrpBlock = $this->rendererPool->createPriceRender(
7774
MsrpPrice::PRICE_CODE,
@@ -87,6 +84,36 @@ protected function _toHtml()
8784
return $this->wrapResult($result);
8885
}
8986

87+
/**
88+
* Check is MSRP applicable for the current product.
89+
*
90+
* @return bool
91+
*/
92+
private function isMsrpPriceApplicable()
93+
{
94+
$moduleManager = $this->getModuleManager();
95+
96+
if (!$moduleManager->isEnabled('Magento_Msrp') || !$moduleManager->isOutputEnabled('Magento_Msrp') ) {
97+
return false;
98+
}
99+
100+
try {
101+
/** @var MsrpPrice $msrpPriceType */
102+
$msrpPriceType = $this->getSaleableItem()->getPriceInfo()->getPrice('msrp_price');
103+
} catch (\InvalidArgumentException $e) {
104+
$this->_logger->critical($e);
105+
return false;
106+
}
107+
108+
if ($msrpPriceType === null) {
109+
return false;
110+
}
111+
112+
$product = $this->getSaleableItem();
113+
114+
return $msrpPriceType->canApplyMsrp($product) && $msrpPriceType->isMinimalPriceLessMsrp($product);
115+
}
116+
90117
/**
91118
* Wrap with standard required container
92119
*
@@ -171,4 +198,16 @@ public function getCacheKeyInfo()
171198
$cacheKeys['display_minimal_price'] = $this->getDisplayMinimalPrice();
172199
return $cacheKeys;
173200
}
201+
202+
/**
203+
* @deprecated
204+
* @return Manager
205+
*/
206+
private function getModuleManager()
207+
{
208+
if ($this->moduleManager === null) {
209+
$this->moduleManager = ObjectManager::getInstance()->get(Manager::class);
210+
}
211+
return $this->moduleManager;
212+
}
174213
}

app/code/Magento/Catalog/Test/Unit/Pricing/Render/FinalPriceBoxTest.php

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\Catalog\Test\Unit\Pricing\Render;
88

99
use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
10+
use Magento\Framework\Module\Manager;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1012

1113
/**
1214
* Class FinalPriceBoxTest
@@ -63,11 +65,17 @@ class FinalPriceBoxTest extends \PHPUnit_Framework_TestCase
6365
*/
6466
private $salableResolverMock;
6567

68+
/** @var ObjectManager */
69+
private $objectManager;
70+
71+
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
72+
private $moduleManager;
73+
6674
protected function setUp()
6775
{
6876
$this->product = $this->getMock(
6977
\Magento\Catalog\Model\Product::class,
70-
['getPriceInfo', '__wakeup', 'getCanShowPrice', 'isSalable'],
78+
['getPriceInfo', '__wakeup', 'getCanShowPrice'],
7179
[],
7280
'',
7381
false
@@ -138,21 +146,22 @@ protected function setUp()
138146
->method('getUrlBuilder')
139147
->will($this->returnValue($urlBuilder));
140148

141-
$this->rendererPool = $this->getMockBuilder('Magento\Framework\Pricing\Render\RendererPool')
149+
$this->rendererPool = $this->getMockBuilder(\Magento\Framework\Pricing\Render\RendererPool::class)
142150
->disableOriginalConstructor()
143151
->getMock();
144152

145-
$this->price = $this->getMock('Magento\Framework\Pricing\Price\PriceInterface');
153+
$this->price = $this->getMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
146154
$this->price->expects($this->any())
147155
->method('getPriceCode')
148156
->will($this->returnValue(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE));
149157

150-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
158+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
159+
151160
$this->salableResolverMock = $this->getMockBuilder(SalableResolverInterface::class)
152161
->disableOriginalConstructor()
153162
->getMockForAbstractClass();
154163

155-
$this->object = $objectManager->getObject(
164+
$this->object = $this->objectManager->getObject(
156165
'Magento\Catalog\Pricing\Render\FinalPriceBox',
157166
[
158167
'context' => $context,
@@ -163,11 +172,33 @@ protected function setUp()
163172
'salableResolver' => $this->salableResolverMock
164173
]
165174
);
175+
176+
$this->moduleManager = $this->getMockBuilder(Manager::class)
177+
->setMethods(['isEnabled', 'isOutputEnabled'])
178+
->disableOriginalConstructor()
179+
->getMock();
180+
181+
$this->objectManager->setBackwardCompatibleProperty(
182+
$this->object,
183+
'moduleManager',
184+
$this->moduleManager
185+
);
166186
}
167187

168188
public function testRenderMsrpDisabled()
169189
{
170-
$priceType = $this->getMock('Magento\Msrp\Pricing\Price\MsrpPrice', [], [], '', false);
190+
$priceType = $this->getMock(\Magento\Msrp\Pricing\Price\MsrpPrice::class, [], [], '', false);
191+
192+
$this->moduleManager->expects(self::once())
193+
->method('isEnabled')
194+
->with('Magento_Msrp')
195+
->willReturn(true);
196+
197+
$this->moduleManager->expects(self::once())
198+
->method('isOutputEnabled')
199+
->with('Magento_Msrp')
200+
->willReturn(true);
201+
171202
$this->priceInfo->expects($this->once())
172203
->method('getPrice')
173204
->with($this->equalTo('msrp_price'))
@@ -178,8 +209,6 @@ public function testRenderMsrpDisabled()
178209
->with($this->equalTo($this->product))
179210
->will($this->returnValue(false));
180211

181-
$this->salableResolverMock->expects($this->once())->method('isSalable')->with($this->product)->willReturn(true);
182-
183212
$result = $this->object->toHtml();
184213

185214
//assert price wrapper
@@ -188,21 +217,21 @@ public function testRenderMsrpDisabled()
188217
$this->assertRegExp('/[final_price]/', $result);
189218
}
190219

191-
public function testNotSalableItem()
220+
public function testRenderMsrpEnabled()
192221
{
193-
$this->salableResolverMock
194-
->expects($this->once())
195-
->method('isSalable')
196-
->with($this->product)
197-
->willReturn(false);
198-
$result = $this->object->toHtml();
222+
$priceType = $this->getMock(\Magento\Msrp\Pricing\Price\MsrpPrice::class, [], [], '', false);
223+
224+
$this->moduleManager->expects(self::once())
225+
->method('isEnabled')
226+
->with('Magento_Msrp')
227+
->willReturn(true);
228+
229+
$this->moduleManager->expects(self::once())
230+
->method('isOutputEnabled')
231+
->with('Magento_Msrp')
232+
->willReturn(true);
199233

200-
$this->assertEmpty($result);
201-
}
202234

203-
public function testRenderMsrpEnabled()
204-
{
205-
$priceType = $this->getMock('Magento\Msrp\Pricing\Price\MsrpPrice', [], [], '', false);
206235
$this->priceInfo->expects($this->once())
207236
->method('getPrice')
208237
->with($this->equalTo('msrp_price'))
@@ -234,8 +263,6 @@ public function testRenderMsrpEnabled()
234263
->with('msrp_price', $this->product, $arguments)
235264
->will($this->returnValue($priceBoxRender));
236265

237-
$this->salableResolverMock->expects($this->once())->method('isSalable')->with($this->product)->willReturn(true);
238-
239266
$result = $this->object->toHtml();
240267

241268
//assert price wrapper
@@ -247,6 +274,16 @@ public function testRenderMsrpEnabled()
247274

248275
public function testRenderMsrpNotRegisteredException()
249276
{
277+
$this->moduleManager->expects(self::once())
278+
->method('isEnabled')
279+
->with('Magento_Msrp')
280+
->willReturn(true);
281+
282+
$this->moduleManager->expects(self::once())
283+
->method('isOutputEnabled')
284+
->with('Magento_Msrp')
285+
->willReturn(true);
286+
250287
$this->logger->expects($this->once())
251288
->method('critical');
252289

@@ -255,8 +292,6 @@ public function testRenderMsrpNotRegisteredException()
255292
->with($this->equalTo('msrp_price'))
256293
->will($this->throwException(new \InvalidArgumentException()));
257294

258-
$this->salableResolverMock->expects($this->once())->method('isSalable')->with($this->product)->willReturn(true);
259-
260295
$result = $this->object->toHtml();
261296

262297
//assert price wrapper
@@ -406,4 +441,34 @@ public function testGetCacheKeyInfo()
406441
{
407442
$this->assertArrayHasKey('display_minimal_price', $this->object->getCacheKeyInfo());
408443
}
444+
445+
public function testRenderMsrpModuleDisabled()
446+
{
447+
$this->moduleManager->expects(self::exactly(2))
448+
->method('isEnabled')
449+
->with('Magento_Msrp')
450+
->will($this->onConsecutiveCalls(false, true));
451+
452+
$this->priceInfo->expects($this->never())
453+
->method('getPrice');
454+
455+
$result = $this->object->toHtml();
456+
457+
//assert price wrapper
458+
$this->assertStringStartsWith('<div', $result);
459+
//assert css_selector
460+
$this->assertRegExp('/[final_price]/', $result);
461+
462+
$this->moduleManager->expects(self::once())
463+
->method('isOutputEnabled')
464+
->with('Magento_Msrp')
465+
->willReturn(false);
466+
467+
$result = $this->object->toHtml();
468+
469+
//assert price wrapper
470+
$this->assertStringStartsWith('<div', $result);
471+
//assert css_selector
472+
$this->assertRegExp('/[final_price]/', $result);
473+
}
409474
}

0 commit comments

Comments
 (0)