Skip to content

Commit 9274371

Browse files
committed
MC-32996: Product Attribute Option Label update Magento 2.3.4 REST API
1 parent 34f6307 commit 9274371

File tree

11 files changed

+334
-180
lines changed

11 files changed

+334
-180
lines changed

app/code/Magento/Catalog/Api/ProductAttributeOptionManagementInterface.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@ public function getItems($attributeCode);
3333
*/
3434
public function add($attributeCode, $option);
3535

36-
/**
37-
* Update attribute option
38-
*
39-
* @param string $attributeCode
40-
* @param int $optionId
41-
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
42-
* @return bool
43-
* @throws \Magento\Framework\Exception\StateException
44-
* @throws \Magento\Framework\Exception\NoSuchEntityException
45-
* @throws \Magento\Framework\Exception\InputException
46-
*/
47-
public function update($attributeCode, $optionId, $option);
48-
4936
/**
5037
* Delete option from attribute
5138
*
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Api;
9+
10+
/**
11+
* Interface to update product attribute option
12+
*
13+
* @api
14+
*/
15+
interface ProductAttributeOptionUpdateInterface
16+
{
17+
/**
18+
* Update attribute option
19+
*
20+
* @param string $attributeCode
21+
* @param int $optionId
22+
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
23+
* @return bool
24+
* @throws \Magento\Framework\Exception\StateException
25+
* @throws \Magento\Framework\Exception\NoSuchEntityException
26+
* @throws \Magento\Framework\Exception\InputException
27+
*/
28+
public function update(
29+
string $attributeCode,
30+
int $optionId,
31+
\Magento\Eav\Api\Data\AttributeOptionInterface $option
32+
): bool;
33+
}

app/code/Magento/Catalog/Model/Product/Attribute/OptionManagement.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,37 @@
88

99
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1010
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
11+
use Magento\Catalog\Api\ProductAttributeOptionUpdateInterface;
1112
use Magento\Eav\Api\AttributeOptionManagementInterface;
13+
use Magento\Eav\Api\AttributeOptionUpdateInterface;
14+
use Magento\Eav\Api\Data\AttributeOptionInterface;
1215
use Magento\Framework\Exception\InputException;
1316

1417
/**
1518
* Option management model for product attribute.
1619
*/
17-
class OptionManagement implements ProductAttributeOptionManagementInterface
20+
class OptionManagement implements ProductAttributeOptionManagementInterface, ProductAttributeOptionUpdateInterface
1821
{
1922
/**
2023
* @var AttributeOptionManagementInterface
2124
*/
2225
protected $eavOptionManagement;
2326

27+
/**
28+
* @var AttributeOptionUpdateInterface
29+
*/
30+
private $eavOptionUpdate;
31+
2432
/**
2533
* @param AttributeOptionManagementInterface $eavOptionManagement
34+
* @param AttributeOptionUpdateInterface $eavOptionUpdate
2635
*/
2736
public function __construct(
28-
AttributeOptionManagementInterface $eavOptionManagement
37+
AttributeOptionManagementInterface $eavOptionManagement,
38+
AttributeOptionUpdateInterface $eavOptionUpdate
2939
) {
3040
$this->eavOptionManagement = $eavOptionManagement;
41+
$this->eavOptionUpdate = $eavOptionUpdate;
3142
}
3243

3344
/**
@@ -56,9 +67,9 @@ public function add($attributeCode, $option)
5667
/**
5768
* @inheritdoc
5869
*/
59-
public function update($attributeCode, $optionId, $option)
70+
public function update(string $attributeCode, int $optionId, AttributeOptionInterface $option): bool
6071
{
61-
return $this->eavOptionManagement->update(
72+
return $this->eavOptionUpdate->update(
6273
ProductAttributeInterface::ENTITY_TYPE_CODE,
6374
$attributeCode,
6475
$optionId,

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<preference for="Magento\Catalog\Api\Data\ProductAttributeTypeInterface" type="Magento\Catalog\Model\Product\Attribute\Type" />
3636
<preference for="Magento\Catalog\Api\ProductAttributeGroupRepositoryInterface" type="Magento\Catalog\Model\ProductAttributeGroupRepository" />
3737
<preference for="Magento\Catalog\Api\ProductAttributeOptionManagementInterface" type="Magento\Catalog\Model\Product\Attribute\OptionManagement" />
38+
<preference for="Magento\Catalog\Api\ProductAttributeOptionUpdateInterface" type="Magento\Catalog\Model\Product\Attribute\OptionManagement" />
3839
<preference for="Magento\Catalog\Api\ProductLinkRepositoryInterface" type="Magento\Catalog\Model\ProductLink\Repository" />
3940
<preference for="Magento\Catalog\Api\Data\ProductAttributeSearchResultsInterface" type="Magento\Catalog\Model\ProductAttributeSearchResults" />
4041
<preference for="Magento\Catalog\Api\Data\CategoryAttributeSearchResultsInterface" type="Magento\Catalog\Model\CategoryAttributeSearchResults" />

app/code/Magento/Catalog/etc/webapi.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
</resources>
185185
</route>
186186
<route url="/V1/products/attributes/:attributeCode/options/:optionId" method="PUT">
187-
<service class="Magento\Catalog\Api\ProductAttributeOptionManagementInterface" method="update" />
187+
<service class="Magento\Catalog\Api\ProductAttributeOptionUpdateInterface" method="update" />
188188
<resources>
189189
<resource ref="Magento_Catalog::attributes_attributes" />
190190
</resources>

app/code/Magento/Eav/Api/AttributeOptionManagementInterface.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ interface AttributeOptionManagementInterface
2424
*/
2525
public function add($entityType, $attributeCode, $option);
2626

27-
/**
28-
* Update attribute option
29-
*
30-
* @param string $entityType
31-
* @param string $attributeCode
32-
* @param int $optionId
33-
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
34-
* @return bool
35-
* @throws \Magento\Framework\Exception\StateException
36-
* @throws \Magento\Framework\Exception\InputException
37-
* @throws \Magento\Framework\Exception\NoSuchEntityException
38-
*/
39-
public function update($entityType, $attributeCode, $optionId, $option);
40-
4127
/**
4228
* Delete option from attribute
4329
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Eav\Api;
9+
10+
/**
11+
* Interface to update attribute option
12+
*
13+
* @api
14+
*/
15+
interface AttributeOptionUpdateInterface
16+
{
17+
/**
18+
* Update attribute option
19+
*
20+
* @param string $entityType
21+
* @param string $attributeCode
22+
* @param int $optionId
23+
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
24+
* @return bool
25+
* @throws \Magento\Framework\Exception\StateException
26+
* @throws \Magento\Framework\Exception\InputException
27+
* @throws \Magento\Framework\Exception\NoSuchEntityException
28+
*/
29+
public function update(
30+
string $entityType,
31+
string $attributeCode,
32+
int $optionId,
33+
\Magento\Eav\Api\Data\AttributeOptionInterface $option
34+
): bool;
35+
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Eav\Model\Entity\Attribute;
99

1010
use Magento\Eav\Api\AttributeOptionManagementInterface;
11+
use Magento\Eav\Api\AttributeOptionUpdateInterface;
1112
use Magento\Eav\Api\Data\AttributeInterface as EavAttributeInterface;
1213
use Magento\Eav\Api\Data\AttributeOptionInterface;
1314
use Magento\Eav\Model\AttributeRepository;
@@ -19,7 +20,7 @@
1920
/**
2021
* Eav Option Management
2122
*/
22-
class OptionManagement implements AttributeOptionManagementInterface
23+
class OptionManagement implements AttributeOptionManagementInterface, AttributeOptionUpdateInterface
2324
{
2425
/**
2526
* @var AttributeRepository
@@ -82,8 +83,12 @@ public function add($entityType, $attributeCode, $option)
8283
/**
8384
* @inheritdoc
8485
*/
85-
public function update($entityType, $attributeCode, $optionId, $option)
86-
{
86+
public function update(
87+
string $entityType,
88+
string $attributeCode,
89+
int $optionId,
90+
AttributeOptionInterface $option
91+
): bool {
8792
$attribute = $this->loadAttribute($entityType, (string)$attributeCode);
8893
if (empty($optionId)) {
8994
throw new InputException(__('The option id is empty. Enter the value and try again.'));

app/code/Magento/Eav/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<preference for="Magento\Eav\Api\Data\AttributeFrontendLabelInterface" type="Magento\Eav\Model\Entity\Attribute\FrontendLabel" />
2121
<preference for="Magento\Eav\Api\Data\AttributeOptionInterface" type="Magento\Eav\Model\Entity\Attribute\Option" />
2222
<preference for="Magento\Eav\Api\AttributeOptionManagementInterface" type="Magento\Eav\Model\Entity\Attribute\OptionManagement" />
23+
<preference for="Magento\Eav\Api\AttributeOptionUpdateInterface" type="Magento\Eav\Model\Entity\Attribute\OptionManagement" />
2324
<preference for="Magento\Eav\Api\Data\AttributeOptionLabelInterface" type="Magento\Eav\Model\Entity\Attribute\OptionLabel" />
2425
<preference for="Magento\Eav\Api\Data\AttributeValidationRuleInterface" type="Magento\Eav\Model\Entity\Attribute\ValidationRule" />
2526
<preference for="Magento\Eav\Api\Data\AttributeSearchResultsInterface" type="Magento\Eav\Model\AttributeSearchResults" />

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeOptionManagementInterfaceTest.php

Lines changed: 6 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,12 @@ public function testGetItems()
3636
],
3737
];
3838

39-
$serviceInfo = [
40-
'rest' => [
41-
'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options',
42-
'httpMethod' => Request::HTTP_METHOD_GET,
43-
],
44-
'soap' => [
45-
'service' => self::SERVICE_NAME,
46-
'serviceVersion' => self::SERVICE_VERSION,
47-
'operation' => self::SERVICE_NAME . 'getItems',
48-
],
49-
];
50-
51-
$response = $this->_webApiCall($serviceInfo, ['attributeCode' => $testAttributeCode]);
39+
$response = $this->webApiCallAttributeOptions(
40+
$testAttributeCode,
41+
Request::HTTP_METHOD_GET,
42+
'getItems',
43+
['attributeCode' => $testAttributeCode]
44+
);
5245

5346
$this->assertIsArray($response);
5447
$this->assertEquals($expectedOptions, $response);
@@ -123,138 +116,6 @@ public function addDataProvider(): array
123116
];
124117
}
125118

126-
/**
127-
* Test to update attribute option
128-
*
129-
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/select_attribute.php
130-
*/
131-
public function testUpdate()
132-
{
133-
$testAttributeCode = 'select_attribute';
134-
$optionData = [
135-
AttributeOptionInterface::LABEL => 'Fixture Option Changed',
136-
AttributeOptionInterface::VALUE => 'option_value',
137-
AttributeOptionInterface::STORE_LABELS => [
138-
[
139-
AttributeOptionLabelInterface::LABEL => 'Store Label Changed',
140-
AttributeOptionLabelInterface::STORE_ID => 1,
141-
],
142-
],
143-
];
144-
145-
$existOptionLabel = 'Fixture Option';
146-
$existAttributeOption = $this->getAttributeOption($testAttributeCode, $existOptionLabel, 'all');
147-
$optionId = $existAttributeOption['value'];
148-
149-
$response = $this->webApiCallAttributeOptions(
150-
$testAttributeCode,
151-
Request::HTTP_METHOD_PUT,
152-
'update',
153-
[
154-
'attributeCode' => $testAttributeCode,
155-
'optionId' => $optionId,
156-
'option' => $optionData,
157-
],
158-
$optionId
159-
);
160-
161-
$this->assertTrue($response);
162-
163-
/* Check update option labels by stores */
164-
$expectedStoreLabels = [
165-
'all' => $optionData[AttributeOptionLabelInterface::LABEL],
166-
'default' => $optionData[AttributeOptionInterface::STORE_LABELS][0][AttributeOptionLabelInterface::LABEL],
167-
];
168-
foreach ($expectedStoreLabels as $store => $label) {
169-
$this->assertNotNull($this->getAttributeOption($testAttributeCode, $label, $store));
170-
}
171-
}
172-
173-
/**
174-
* Test to update option with already exist exception
175-
*
176-
* Test to except case when the two options has a same label
177-
*
178-
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/select_attribute.php
179-
*/
180-
public function testUpdateWithAlreadyExistsException()
181-
{
182-
$this->expectExceptionMessage("Admin store attribute option label '%1' is already exists.");
183-
$testAttributeCode = 'select_attribute';
184-
185-
$newOptionData = [
186-
AttributeOptionInterface::LABEL => 'New Option',
187-
AttributeOptionInterface::VALUE => 'new_option_value',
188-
];
189-
$newOptionId = $this->webApiCallAttributeOptions(
190-
$testAttributeCode,
191-
Request::HTTP_METHOD_POST,
192-
'add',
193-
[
194-
'attributeCode' => $testAttributeCode,
195-
'option' => $newOptionData,
196-
]
197-
);
198-
199-
$editOptionData = [
200-
AttributeOptionInterface::LABEL => 'Fixture Option',
201-
AttributeOptionInterface::VALUE => $newOptionId,
202-
];
203-
$this->webApiCallAttributeOptions(
204-
$testAttributeCode,
205-
Request::HTTP_METHOD_PUT,
206-
'update',
207-
[
208-
'attributeCode' => $testAttributeCode,
209-
'optionId' => $newOptionId,
210-
'option' => $editOptionData,
211-
],
212-
$newOptionId
213-
);
214-
}
215-
216-
/**
217-
* Test to update option with not exist exception
218-
*
219-
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/select_attribute.php
220-
*/
221-
public function testUpdateWithNotExistsException()
222-
{
223-
$this->expectExceptionMessage("The '%1' attribute doesn't include an option id '%2'.");
224-
$testAttributeCode = 'select_attribute';
225-
226-
$newOptionData = [
227-
AttributeOptionInterface::LABEL => 'New Option',
228-
AttributeOptionInterface::VALUE => 'new_option_value'
229-
];
230-
$newOptionId = (int)$this->webApiCallAttributeOptions(
231-
$testAttributeCode,
232-
Request::HTTP_METHOD_POST,
233-
'add',
234-
[
235-
'attributeCode' => $testAttributeCode,
236-
'option' => $newOptionData,
237-
]
238-
);
239-
240-
$newOptionId++;
241-
$editOptionData = [
242-
AttributeOptionInterface::LABEL => 'New Option Changed',
243-
AttributeOptionInterface::VALUE => $newOptionId
244-
];
245-
$this->webApiCallAttributeOptions(
246-
$testAttributeCode,
247-
Request::HTTP_METHOD_PUT,
248-
'update',
249-
[
250-
'attributeCode' => $testAttributeCode,
251-
'optionId' => $newOptionId,
252-
'option' => $editOptionData,
253-
],
254-
$newOptionId
255-
);
256-
}
257-
258119
/**
259120
* Test to delete attribute option
260121
*

0 commit comments

Comments
 (0)