Skip to content

Commit 28f2ec5

Browse files
MAGETWO-71832: Prevent unset Boolean product attributes from displaying in product page #10619
2 parents 98ec420 + bd1b089 commit 28f2ec5

File tree

2 files changed

+158
-5
lines changed

2 files changed

+158
-5
lines changed

app/code/Magento/Catalog/Block/Product/View/Attributes.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,13 @@ public function getAdditionalData(array $excludeAttr = [])
8383
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
8484
$value = $attribute->getFrontend()->getValue($product);
8585

86-
if (!$product->hasData($attribute->getAttributeCode())) {
87-
$value = __('N/A');
88-
} elseif ((string)$value == '') {
89-
$value = __('No');
86+
if ($value instanceof Phrase) {
87+
$value = (string)$value;
9088
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
9189
$value = $this->priceCurrency->convertAndFormat($value);
9290
}
9391

94-
if ($value instanceof Phrase || (is_string($value) && strlen($value))) {
92+
if (is_string($value) && strlen($value)) {
9593
$data[$attribute->getAttributeCode()] = [
9694
'label' => __($attribute->getStoreLabel()),
9795
'value' => $value,
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Block\Product\View;
8+
9+
use \PHPUnit\Framework\TestCase;
10+
use \Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
12+
use \Magento\Catalog\Model\Product;
13+
use \Magento\Framework\View\Element\Template\Context;
14+
use \Magento\Framework\Registry;
15+
use \Magento\Framework\Pricing\PriceCurrencyInterface;
16+
use \Magento\Catalog\Block\Product\View\Attributes as AttributesBlock;
17+
18+
/**
19+
* Test class for \Magento\Catalog\Block\Product\View\Attributes
20+
*
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
*/
23+
class AttributesTest extends TestCase
24+
{
25+
/**
26+
* @var \Magento\Framework\Phrase
27+
*/
28+
private $phrase;
29+
30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Eav\Model\Entity\Attribute\AbstractAttribute
32+
*/
33+
private $attribute;
34+
35+
/**
36+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
37+
*/
38+
private $frontendAttribute;
39+
40+
/**
41+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product
42+
*/
43+
private $product;
44+
45+
/**
46+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\Template\Context
47+
*/
48+
private $context;
49+
50+
/**
51+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Registry
52+
*/
53+
private $registry;
54+
55+
/**
56+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Pricing\PriceCurrencyInterface
57+
*/
58+
private $priceCurrencyInterface;
59+
60+
/**
61+
* @var \Magento\Catalog\Block\Product\View\Attributes
62+
*/
63+
private $attributesBlock;
64+
65+
protected function setUp()
66+
{
67+
$this->attribute = $this
68+
->getMockBuilder(AbstractAttribute::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->attribute
72+
->expects($this->any())
73+
->method('getIsVisibleOnFront')
74+
->willReturn(true);
75+
$this->attribute
76+
->expects($this->any())
77+
->method('getAttributeCode')
78+
->willReturn('phrase');
79+
$this->frontendAttribute = $this
80+
->getMockBuilder(AbstractFrontend::class)
81+
->disableOriginalConstructor()
82+
->getMock();
83+
$this->attribute
84+
->expects($this->any())
85+
->method('getFrontendInput')
86+
->willReturn('phrase');
87+
$this->attribute
88+
->expects($this->any())
89+
->method('getFrontend')
90+
->willReturn($this->frontendAttribute);
91+
$this->product = $this
92+
->getMockBuilder(Product::class)
93+
->disableOriginalConstructor()
94+
->getMock();
95+
$this->product
96+
->expects($this->any())
97+
->method('getAttributes')
98+
->willReturn([$this->attribute]);
99+
$this->product
100+
->expects($this->any())
101+
->method('hasData')
102+
->willReturn(true);
103+
$this->context = $this
104+
->getMockBuilder(Context::class)
105+
->disableOriginalConstructor()
106+
->getMock();
107+
$this->registry = $this
108+
->getMockBuilder(Registry::class)
109+
->disableOriginalConstructor()
110+
->getMock();
111+
$this->registry
112+
->expects($this->any())
113+
->method('registry')
114+
->willReturn($this->product);
115+
$this->priceCurrencyInterface = $this
116+
->getMockBuilder(PriceCurrencyInterface::class)
117+
->disableOriginalConstructor()
118+
->getMock();
119+
$this->attributesBlock = new AttributesBlock(
120+
$this->context,
121+
$this->registry,
122+
$this->priceCurrencyInterface
123+
);
124+
}
125+
126+
/**
127+
* @return void
128+
*/
129+
public function testGetAttributeNoValue()
130+
{
131+
$this->phrase = '';
132+
$this->frontendAttribute
133+
->expects($this->any())
134+
->method('getValue')
135+
->willReturn($this->phrase);
136+
$attributes = $this->attributesBlock->getAdditionalData();
137+
$this->assertTrue(empty($attributes['phrase']));
138+
}
139+
140+
/**
141+
* @return void
142+
*/
143+
public function testGetAttributeHasValue()
144+
{
145+
$this->phrase = __('Yes');
146+
$this->frontendAttribute
147+
->expects($this->any())
148+
->method('getValue')
149+
->willReturn($this->phrase);
150+
$attributes = $this->attributesBlock->getAdditionalData();
151+
$this->assertNotTrue(empty($attributes['phrase']));
152+
$this->assertNotTrue(empty($attributes['phrase']['value']));
153+
$this->assertEquals('Yes', $attributes['phrase']['value']);
154+
}
155+
}

0 commit comments

Comments
 (0)