Skip to content

Commit 5da55b4

Browse files
AC-2719 Adding tier_price to product comparison will crash the page
1 parent 5c3867e commit 5da55b4

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

app/code/Magento/Catalog/Block/Product/Compare/ListCompare.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ class ListCompare extends \Magento\Catalog\Block\Product\AbstractProduct
4040
protected $_useLinkForAsLowAs = false;
4141

4242
/**
43-
* Customer id
44-
*
4543
* @var null|int
4644
*/
4745
protected $_customerId = null;
@@ -52,22 +50,16 @@ class ListCompare extends \Magento\Catalog\Block\Product\AbstractProduct
5250
protected $httpContext;
5351

5452
/**
55-
* Customer visitor
56-
*
5753
* @var \Magento\Customer\Model\Visitor
5854
*/
5955
protected $_customerVisitor;
6056

6157
/**
62-
* Catalog product visibility
63-
*
6458
* @var \Magento\Catalog\Model\Product\Visibility
6559
*/
6660
protected $_catalogProductVisibility;
6761

6862
/**
69-
* Item collection factory
70-
*
7163
* @var \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory
7264
*/
7365
protected $_itemCollectionFactory;
@@ -205,6 +197,9 @@ public function getProductAttributeValue($product, $attribute)
205197
} else {
206198
$value = $product->getData($attribute->getAttributeCode());
207199
}
200+
if (is_array($value)) {
201+
return __('N/A');
202+
}
208203
return (string)$value == '' ? __('No') : $value;
209204
}
210205

app/code/Magento/Catalog/Test/Unit/Block/Product/Compare/ListCompareTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Catalog\Block\Product\Compare\ListCompare;
1111
use Magento\Catalog\Block\Product\Context;
1212
use Magento\Catalog\Model\Product;
13+
use Magento\Eav\Model\Entity\Attribute\AttributeInterface;
14+
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
1315
use Magento\Framework\Pricing\Render;
1416
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1517
use Magento\Framework\View\Layout;
@@ -50,6 +52,46 @@ protected function tearDown(): void
5052
$this->block = null;
5153
}
5254

55+
/**
56+
* @dataProvider attributeDataProvider
57+
*/
58+
public function testProductAttributeValue($attributeData, $expectedResult)
59+
{
60+
$attribute = $this->getMockBuilder(AttributeInterface::class)
61+
->addMethods(['getAttributeCode', 'getSourceModel', 'getFrontendInput', 'getFrontend'])
62+
->getMockForAbstractClass();
63+
$frontEndModel = $this->createPartialMock(AbstractFrontend::class, ['getValue']);
64+
$productMock = $this->createPartialMock(Product::class, ['getId', 'getData', 'hasData']);
65+
$productMock->expects($this->any())
66+
->method('hasData')
67+
->with($attributeData['attribute_code'])
68+
->willReturn(true);
69+
$productMock->expects($this->any())
70+
->method('getData')
71+
->with($attributeData['attribute_code'])
72+
->willReturn($attributeData['attribute_value']);
73+
$attribute->expects($this->any())
74+
->method('getAttributeCode')
75+
->willReturn($attributeData['attribute_code']);
76+
$attribute->expects($this->any())
77+
->method('getSourceModel')
78+
->willReturn($attributeData['source_model']);
79+
$attribute->expects($this->any())
80+
->method('getFrontendInput')
81+
->willReturn($attributeData['frontend_input']);
82+
$frontEndModel->expects($this->any())
83+
->method('getValue')
84+
->with($productMock)
85+
->willReturn($attributeData['attribute_value']);
86+
$attribute->expects($this->any())
87+
->method('getFrontend')
88+
->willReturn($frontEndModel);
89+
$this->assertEquals(
90+
$expectedResult,
91+
$this->block->getProductAttributeValue($productMock, $attribute)
92+
);
93+
}
94+
5395
public function testGetProductPrice()
5496
{
5597
//Data
@@ -84,4 +126,31 @@ public function testGetProductPrice()
84126

85127
$this->assertEquals($expectedResult, $this->block->getProductPrice($product, '-compare-list-top'));
86128
}
129+
130+
/**
131+
* @return array
132+
*/
133+
public function attributeDataProvider(): array
134+
{
135+
return [
136+
[
137+
'attributeData' => [
138+
'attribute_code' => 'tier_price',
139+
'source_model' => null,
140+
'frontend_input' => 'text',
141+
'attribute_value' => []
142+
],
143+
'expectedResult' => __('N/A')
144+
],
145+
[
146+
'attributeData' => [
147+
'attribute_code' => 'special_price',
148+
'source_model' => null,
149+
'frontend_input' => 'decimal',
150+
'attribute_value' => 50.00
151+
],
152+
'expectedResult' => '50.00'
153+
]
154+
];
155+
}
87156
}

0 commit comments

Comments
 (0)