Skip to content

Commit a629abe

Browse files
authored
ENGCOM-9343: Fix adding an option with label '0' #34578
2 parents 4fcc78c + f017789 commit a629abe

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public function add($entityType, $attributeCode, $option)
6060
{
6161
$attribute = $this->loadAttribute($entityType, (string)$attributeCode);
6262

63-
$label = trim($option->getLabel() ?: '');
64-
if (empty($label)) {
63+
$label = trim((string)$option->getLabel());
64+
if ($label === '') {
6565
throw new InputException(__('The attribute option label is empty. Enter the value and try again.'));
6666
}
6767

@@ -93,8 +93,8 @@ public function update(
9393
if (empty($optionId)) {
9494
throw new InputException(__('The option id is empty. Enter the value and try again.'));
9595
}
96-
$label = trim($option->getLabel() ?: '');
97-
if (empty($label)) {
96+
$label = trim((string)$option->getLabel());
97+
if ($label === '') {
9898
throw new InputException(__('The attribute option label is empty. Enter the value and try again.'));
9999
}
100100
if ($attribute->getSource()->getOptionText($optionId) === false) {

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/OptionManagementTest.php

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute;
99

10+
use Magento\Catalog\Model\Product;
1011
use Magento\Eav\Api\Data\AttributeOptionInterface as EavAttributeOptionInterface;
1112
use Magento\Eav\Api\Data\AttributeOptionLabelInterface as EavAttributeOptionLabelInterface;
1213
use Magento\Eav\Model\AttributeRepository;
@@ -18,7 +19,7 @@
1819
use Magento\Framework\Exception\InputException;
1920
use Magento\Framework\Exception\NoSuchEntityException;
2021
use Magento\Framework\Exception\StateException;
21-
use PHPUnit\Framework\MockObject\MockObject as MockObject;
22+
use PHPUnit\Framework\MockObject\MockObject;
2223
use PHPUnit\Framework\TestCase;
2324

2425
/**
@@ -59,13 +60,15 @@ protected function setUp(): void
5960

6061
/**
6162
* Test to add attribute option
63+
*
64+
* @param string $label
65+
* @dataProvider optionLabelDataProvider
6266
*/
63-
public function testAdd()
67+
public function testAdd(string $label): void
6468
{
6569
$entityType = 42;
6670
$storeId = 4;
6771
$attributeCode = 'atrCde';
68-
$label = 'optionLabel';
6972
$storeLabel = 'labelLabel';
7073
$sortOder = 'optionSortOrder';
7174
$option = [
@@ -121,6 +124,17 @@ public function testAdd()
121124
);
122125
}
123126

127+
/**
128+
* @return array
129+
*/
130+
public function optionLabelDataProvider(): array
131+
{
132+
return [
133+
['optionLabel'],
134+
['0']
135+
];
136+
}
137+
124138
/**
125139
* Test to add attribute option with empty attribute code
126140
*/
@@ -216,6 +230,75 @@ public function testAddWithCannotSaveException()
216230
$this->model->add($entityType, $attributeCode, $optionMock);
217231
}
218232

233+
/**
234+
* Test to update attribute option
235+
*
236+
* @param string $label
237+
* @dataProvider optionLabelDataProvider
238+
*/
239+
public function testUpdate(string $label): void
240+
{
241+
$entityType = Product::ENTITY;
242+
$storeId = 4;
243+
$attributeCode = 'atrCde';
244+
$storeLabel = 'labelLabel';
245+
$sortOder = 'optionSortOrder';
246+
$optionId = 10;
247+
$option = [
248+
'value' => [
249+
$optionId => [
250+
0 => $label,
251+
$storeId => $storeLabel,
252+
],
253+
],
254+
'order' => [
255+
$optionId => $sortOder,
256+
]
257+
];
258+
259+
$optionMock = $this->getAttributeOption();
260+
$labelMock = $this->getAttributeOptionLabel();
261+
/** @var SourceInterface|MockObject $sourceMock */
262+
$sourceMock = $this->createMock(EavAttributeSource::class);
263+
264+
$sourceMock->expects($this->once())
265+
->method('getOptionText')
266+
->with($optionId)
267+
->willReturn($label);
268+
269+
$sourceMock->expects($this->once())
270+
->method('getOptionId')
271+
->with($label)
272+
->willReturn($optionId);
273+
274+
/** @var EavAbstractAttribute|MockObject $attributeMock */
275+
$attributeMock = $this->getMockBuilder(EavAbstractAttribute::class)
276+
->disableOriginalConstructor()
277+
->addMethods(['setOption'])
278+
->onlyMethods(['usesSource', 'getSource'])
279+
->getMock();
280+
$attributeMock->method('usesSource')->willReturn(true);
281+
$attributeMock->expects($this->once())->method('setOption')->with($option);
282+
$attributeMock->method('getSource')->willReturn($sourceMock);
283+
284+
$this->attributeRepositoryMock->expects($this->once())
285+
->method('get')
286+
->with($entityType, $attributeCode)
287+
->willReturn($attributeMock);
288+
$optionMock->method('getLabel')->willReturn($label);
289+
$optionMock->method('getSortOrder')->willReturn($sortOder);
290+
$optionMock->method('getIsDefault')->willReturn(true);
291+
$optionMock->method('getStoreLabels')->willReturn([$labelMock]);
292+
$labelMock->method('getStoreId')->willReturn($storeId);
293+
$labelMock->method('getLabel')->willReturn($storeLabel);
294+
$this->resourceModelMock->expects($this->once())->method('save')->with($attributeMock);
295+
296+
$this->assertEquals(
297+
true,
298+
$this->model->update($entityType, $attributeCode, $optionId, $optionMock)
299+
);
300+
}
301+
219302
/**
220303
* Test to delete attribute option
221304
*/

0 commit comments

Comments
 (0)