Skip to content

Commit

Permalink
ENGCOM-9343: Fix adding an option with label '0' #34578
Browse files Browse the repository at this point in the history
  • Loading branch information
sidolov authored Jan 31, 2022
2 parents 4fcc78c + f017789 commit a629abe
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.'));
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit a629abe

Please sign in to comment.