diff --git a/app/code/Magento/Catalog/Block/Product/View/Attributes.php b/app/code/Magento/Catalog/Block/Product/View/Attributes.php index 52039e23e0335..88416786dee7a 100644 --- a/app/code/Magento/Catalog/Block/Product/View/Attributes.php +++ b/app/code/Magento/Catalog/Block/Product/View/Attributes.php @@ -84,13 +84,15 @@ public function getAdditionalData(array $excludeAttr = []) if (!$product->hasData($attribute->getAttributeCode())) { $value = __('N/A'); + } elseif ($value instanceof Phrase) { + $value = (string)$value; } elseif ((string)$value == '') { $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, diff --git a/app/code/Magento/Catalog/Test/Unit/Block/Product/View/AttributesTest.php b/app/code/Magento/Catalog/Test/Unit/Block/Product/View/AttributesTest.php new file mode 100644 index 0000000000000..902cd80f542e7 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Block/Product/View/AttributesTest.php @@ -0,0 +1,176 @@ +phrase = new Phrase(__('')); + + $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']); + } +}