Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent unset Boolean product attributes from displaying in product page #10619

Merged
merged 1 commit into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/code/Magento/Catalog/Block/Product/View/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ public function getAdditionalData(array $excludeAttr = [])

if (!$product->hasData($attribute->getAttributeCode())) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this if as this is dead code for a long time.

$value = __('N/A');
} elseif ($value instanceof Phrase) {
$value = (string)$value;
} elseif ((string)$value == '') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this elseif as converting an empty value to No is pretty weird logic. If this is a workaround for some bug it should be fixed in another way as this code only hides the real problem.

$value = __('No');
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = $this->priceCurrency->convertAndFormat($value);
}

if ($value instanceof Phrase || (is_string($value) && strlen($value))) {
if (is_string($value) && strlen($value)) {
$data[$attribute->getAttributeCode()] = [
'label' => __($attribute->getStoreLabel()),
'value' => $value,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Test\Unit\Block\Product\View;

use \PHPUnit\Framework\TestCase;
use \Magento\Framework\Phrase;
use \Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
use \Magento\Catalog\Model\Product;
use \Magento\Framework\View\Element\Template\Context;
use \Magento\Framework\Registry;
use \Magento\Framework\Pricing\PriceCurrencyInterface;
use \Magento\Catalog\Block\Product\View\Attributes as AttributesBlock;

/**
* Test class for \Magento\Catalog\Block\Product\View\Attributes
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AttributesTest extends TestCase
{
/**
* @var \Magento\Framework\Phrase
*/
private $phrase;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Eav\Model\Entity\Attribute\AbstractAttribute
*/
private $attribute;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
*/
private $frontendAttribute;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product
*/
private $product;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\Template\Context
*/
private $context;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Registry
*/
private $registry;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Pricing\PriceCurrencyInterface
*/
private $priceCurrencyInterface;

/**
* @var \Magento\Catalog\Block\Product\View\Attributes
*/
private $attributesBlock;

protected function setUp()
{
$this->phrase = new Phrase(__(''));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function __ returns a Phrase instance. Use new Phrase('') or __(''). Result will be the same. While your code is working it lead to static tests failure:

Magento\Test\Integrity\Phrase\ArgumentsTest::testArguments
2 missed phrases were discovered: 

Missed Phrase: File: /opt/bamboo/bamboo-agent-home/xml-data/build-dir/M2-AT2646-INSE/magento2ce/app/code/Magento/Catalog/Test/Unit/Block/Product/View/AttributesTest.php 
Line: 67


Missed Phrase: File: /opt/bamboo/bamboo-agent-home/xml-data/build-dir/M2-AT2646-INSE/magento2ce/app/code/Magento/Catalog/Test/Unit/Block/Product/View/AttributesTest.php 
Line: 146
Failed asserting that an array is empty.

/opt/bamboo/bamboo-agent-home/xml-data/build-dir/M2-AT2646-INSE/magento2ce/dev/tests/static/testsuite/Magento/Test/Integrity/Phrase/ArgumentsTest.php:66


$this->attribute = $this
->getMockBuilder(AbstractAttribute::class)
->disableOriginalConstructor()
->getMock();

$this->attribute
->expects($this->any())
->method('getIsVisibleOnFront')
->willReturn(true);

$this->attribute
->expects($this->any())
->method('getAttributeCode')
->willReturn('phrase');

$this->frontendAttribute = $this
->getMockBuilder(AbstractFrontend::class)
->disableOriginalConstructor()
->getMock();

$this->attribute
->expects($this->any())
->method('getFrontendInput')
->willReturn('phrase');

$this->attribute
->expects($this->any())
->method('getFrontend')
->willReturn($this->frontendAttribute);

$this->product = $this
->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();

$this->product
->expects($this->any())
->method('getAttributes')
->willReturn([$this->attribute]);

$this->product
->expects($this->any())
->method('hasData')
->willReturn(true);

$this->context = $this
->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();

$this->registry = $this
->getMockBuilder(Registry::class)
->disableOriginalConstructor()
->getMock();

$this->registry
->expects($this->any())
->method('registry')
->willReturn($this->product);

$this->priceCurrencyInterface = $this
->getMockBuilder(PriceCurrencyInterface::class)
->disableOriginalConstructor()
->getMock();

$this->attributesBlock = new AttributesBlock(
$this->context,
$this->registry,
$this->priceCurrencyInterface
);
}

/**
* @return void
*/
public function testGetAttributesBooleanNoValue()
{
$this->phrase = new Phrase(__(''));

$this->frontendAttribute
->expects($this->any())
->method('getValue')
->willReturn($this->phrase);

$attributes = $this->attributesBlock->getAdditionalData();

$this->assertTrue(empty($attributes['phrase']));
}

/**
* @return void
*/
public function testGetAttributesBooleanHasValue()
{
$this->phrase = new Phrase(__('Yes'));

$this->frontendAttribute
->expects($this->any())
->method('getValue')
->willReturn($this->phrase);

$attributes = $this->attributesBlock->getAdditionalData();

$this->assertNotTrue(empty($attributes['phrase']));
$this->assertNotTrue(empty($attributes['phrase']['value']));
$this->assertEquals('Yes', $attributes['phrase']['value']);
}
}