Skip to content

Commit c2a2450

Browse files
committed
#9961: Unused product attributes display with value N/A or NO on storefront.
1 parent 13a933a commit c2a2450

File tree

2 files changed

+160
-1
lines changed

2 files changed

+160
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ public function getAdditionalData(array $excludeAttr = [])
8585

8686
if (!$product->hasData($attribute->getAttributeCode())) {
8787
$value = __('N/A');
88+
} elseif ($value instanceof Phrase) {
89+
$value = (string)$value;
8890
} elseif ((string)$value == '') {
8991
$value = __('No');
9092
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
9193
$value = $this->priceCurrency->convertAndFormat($value);
9294
}
9395

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

0 commit comments

Comments
 (0)