diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php index e99f4395953ad..ecee1c6ab6361 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php @@ -60,8 +60,8 @@ public function add($entityType, $attributeCode, $option) { $attribute = $this->loadAttribute($entityType, (string)$attributeCode); - $label = trim($option->getLabel() ?: ''); - if (empty($label)) { + $label = trim((string)$option->getLabel()); + if ($label === '') { throw new InputException(__('The attribute option label is empty. Enter the value and try again.')); } @@ -93,8 +93,8 @@ public function update( if (empty($optionId)) { throw new InputException(__('The option id is empty. Enter the value and try again.')); } - $label = trim($option->getLabel() ?: ''); - if (empty($label)) { + $label = trim((string)$option->getLabel()); + if ($label === '') { throw new InputException(__('The attribute option label is empty. Enter the value and try again.')); } if ($attribute->getSource()->getOptionText($optionId) === false) { diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php index b96b1e26696cd..7b554a19fc281 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php @@ -7,6 +7,7 @@ namespace Magento\Eav\Test\Unit\Model\Entity\Attribute; +use Magento\Catalog\Model\Product; use Magento\Eav\Api\Data\AttributeOptionInterface as EavAttributeOptionInterface; use Magento\Eav\Api\Data\AttributeOptionLabelInterface as EavAttributeOptionLabelInterface; use Magento\Eav\Model\AttributeRepository; @@ -18,7 +19,7 @@ use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Exception\StateException; -use PHPUnit\Framework\MockObject\MockObject as MockObject; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -59,13 +60,15 @@ protected function setUp(): void /** * Test to add attribute option + * + * @param string $label + * @dataProvider optionLabelDataProvider */ - public function testAdd() + public function testAdd(string $label): void { $entityType = 42; $storeId = 4; $attributeCode = 'atrCde'; - $label = 'optionLabel'; $storeLabel = 'labelLabel'; $sortOder = 'optionSortOrder'; $option = [ @@ -121,6 +124,17 @@ public function testAdd() ); } + /** + * @return array + */ + public function optionLabelDataProvider(): array + { + return [ + ['optionLabel'], + ['0'] + ]; + } + /** * Test to add attribute option with empty attribute code */ @@ -216,6 +230,75 @@ public function testAddWithCannotSaveException() $this->model->add($entityType, $attributeCode, $optionMock); } + /** + * Test to update attribute option + * + * @param string $label + * @dataProvider optionLabelDataProvider + */ + public function testUpdate(string $label): void + { + $entityType = Product::ENTITY; + $storeId = 4; + $attributeCode = 'atrCde'; + $storeLabel = 'labelLabel'; + $sortOder = 'optionSortOrder'; + $optionId = 10; + $option = [ + 'value' => [ + $optionId => [ + 0 => $label, + $storeId => $storeLabel, + ], + ], + 'order' => [ + $optionId => $sortOder, + ] + ]; + + $optionMock = $this->getAttributeOption(); + $labelMock = $this->getAttributeOptionLabel(); + /** @var SourceInterface|MockObject $sourceMock */ + $sourceMock = $this->createMock(EavAttributeSource::class); + + $sourceMock->expects($this->once()) + ->method('getOptionText') + ->with($optionId) + ->willReturn($label); + + $sourceMock->expects($this->once()) + ->method('getOptionId') + ->with($label) + ->willReturn($optionId); + + /** @var EavAbstractAttribute|MockObject $attributeMock */ + $attributeMock = $this->getMockBuilder(EavAbstractAttribute::class) + ->disableOriginalConstructor() + ->addMethods(['setOption']) + ->onlyMethods(['usesSource', 'getSource']) + ->getMock(); + $attributeMock->method('usesSource')->willReturn(true); + $attributeMock->expects($this->once())->method('setOption')->with($option); + $attributeMock->method('getSource')->willReturn($sourceMock); + + $this->attributeRepositoryMock->expects($this->once()) + ->method('get') + ->with($entityType, $attributeCode) + ->willReturn($attributeMock); + $optionMock->method('getLabel')->willReturn($label); + $optionMock->method('getSortOrder')->willReturn($sortOder); + $optionMock->method('getIsDefault')->willReturn(true); + $optionMock->method('getStoreLabels')->willReturn([$labelMock]); + $labelMock->method('getStoreId')->willReturn($storeId); + $labelMock->method('getLabel')->willReturn($storeLabel); + $this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock); + + $this->assertEquals( + true, + $this->model->update($entityType, $attributeCode, $optionId, $optionMock) + ); + } + /** * Test to delete attribute option */