Skip to content

Commit b9762ed

Browse files
Merge remote-tracking branch 'origin/MC-32996' into 2.4-develop-pr34
2 parents 06a6bbb + 3af121c commit b9762ed

File tree

13 files changed

+922
-319
lines changed

13 files changed

+922
-319
lines changed
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: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,39 @@
66
*/
77
namespace Magento\Catalog\Model\Product\Attribute;
88

9+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
10+
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
11+
use Magento\Catalog\Api\ProductAttributeOptionUpdateInterface;
12+
use Magento\Eav\Api\AttributeOptionManagementInterface;
13+
use Magento\Eav\Api\AttributeOptionUpdateInterface;
14+
use Magento\Eav\Api\Data\AttributeOptionInterface;
915
use Magento\Framework\Exception\InputException;
1016

1117
/**
1218
* Option management model for product attribute.
1319
*/
14-
class OptionManagement implements \Magento\Catalog\Api\ProductAttributeOptionManagementInterface
20+
class OptionManagement implements ProductAttributeOptionManagementInterface, ProductAttributeOptionUpdateInterface
1521
{
1622
/**
17-
* @var \Magento\Eav\Api\AttributeOptionManagementInterface
23+
* @var AttributeOptionManagementInterface
1824
*/
1925
protected $eavOptionManagement;
2026

2127
/**
22-
* @param \Magento\Eav\Api\AttributeOptionManagementInterface $eavOptionManagement
28+
* @var AttributeOptionUpdateInterface
29+
*/
30+
private $eavOptionUpdate;
31+
32+
/**
33+
* @param AttributeOptionManagementInterface $eavOptionManagement
34+
* @param AttributeOptionUpdateInterface $eavOptionUpdate
2335
*/
2436
public function __construct(
25-
\Magento\Eav\Api\AttributeOptionManagementInterface $eavOptionManagement
37+
AttributeOptionManagementInterface $eavOptionManagement,
38+
AttributeOptionUpdateInterface $eavOptionUpdate
2639
) {
2740
$this->eavOptionManagement = $eavOptionManagement;
41+
$this->eavOptionUpdate = $eavOptionUpdate;
2842
}
2943

3044
/**
@@ -33,7 +47,7 @@ public function __construct(
3347
public function getItems($attributeCode)
3448
{
3549
return $this->eavOptionManagement->getItems(
36-
\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
50+
ProductAttributeInterface::ENTITY_TYPE_CODE,
3751
$attributeCode
3852
);
3953
}
@@ -44,8 +58,21 @@ public function getItems($attributeCode)
4458
public function add($attributeCode, $option)
4559
{
4660
return $this->eavOptionManagement->add(
47-
\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
61+
ProductAttributeInterface::ENTITY_TYPE_CODE,
62+
$attributeCode,
63+
$option
64+
);
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public function update(string $attributeCode, int $optionId, AttributeOptionInterface $option): bool
71+
{
72+
return $this->eavOptionUpdate->update(
73+
ProductAttributeInterface::ENTITY_TYPE_CODE,
4874
$attributeCode,
75+
$optionId,
4976
$option
5077
);
5178
}
@@ -60,7 +87,7 @@ public function delete($attributeCode, $optionId)
6087
}
6188

6289
return $this->eavOptionManagement->delete(
63-
\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
90+
ProductAttributeInterface::ENTITY_TYPE_CODE,
6491
$attributeCode,
6592
$optionId
6693
);

app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/OptionManagementTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1111
use Magento\Catalog\Model\Product\Attribute\OptionManagement;
1212
use Magento\Eav\Api\AttributeOptionManagementInterface;
13+
use Magento\Eav\Api\AttributeOptionUpdateInterface;
1314
use Magento\Eav\Api\Data\AttributeOptionInterface;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516
use PHPUnit\Framework\TestCase;
1617

18+
/**
19+
* Class to test management of attribute options
20+
*/
1721
class OptionManagementTest extends TestCase
1822
{
1923
/**
@@ -22,18 +26,28 @@ class OptionManagementTest extends TestCase
2226
protected $model;
2327

2428
/**
25-
* @var MockObject
29+
* @var AttributeOptionManagementInterface|MockObject
2630
*/
2731
protected $eavOptionManagementMock;
2832

33+
/**
34+
* @var AttributeOptionUpdateInterface|MockObject
35+
*/
36+
private $eavOptionUpdateMock;
37+
2938
protected function setUp(): void
3039
{
3140
$this->eavOptionManagementMock = $this->getMockForAbstractClass(AttributeOptionManagementInterface::class);
41+
$this->eavOptionUpdateMock = $this->getMockForAbstractClass(AttributeOptionUpdateInterface::class);
3242
$this->model = new OptionManagement(
33-
$this->eavOptionManagementMock
43+
$this->eavOptionManagementMock,
44+
$this->eavOptionUpdateMock
3445
);
3546
}
3647

48+
/**
49+
* Test to Retrieve list of attribute options
50+
*/
3751
public function testGetItems()
3852
{
3953
$attributeCode = 10;
@@ -44,6 +58,9 @@ public function testGetItems()
4458
$this->assertEquals([], $this->model->getItems($attributeCode));
4559
}
4660

61+
/**
62+
* Test to Add option to attribute
63+
*/
4764
public function testAdd()
4865
{
4966
$attributeCode = 42;
@@ -56,6 +73,9 @@ public function testAdd()
5673
$this->assertTrue($this->model->add($attributeCode, $optionMock));
5774
}
5875

76+
/**
77+
* Test to delete attribute option
78+
*/
5979
public function testDelete()
6080
{
6181
$attributeCode = 'atrCde';
@@ -68,6 +88,9 @@ public function testDelete()
6888
$this->assertTrue($this->model->delete($attributeCode, $optionId));
6989
}
7090

91+
/**
92+
* Test to delete attribute option with invalid option id
93+
*/
7194
public function testDeleteWithInvalidOption()
7295
{
7396
$this->expectException('Magento\Framework\Exception\InputException');
@@ -77,4 +100,24 @@ public function testDeleteWithInvalidOption()
77100
$this->eavOptionManagementMock->expects($this->never())->method('delete');
78101
$this->model->delete($attributeCode, $optionId);
79102
}
103+
104+
/**
105+
* Test to update attribute option
106+
*/
107+
public function testUpdate()
108+
{
109+
$attributeCode = 'atrCde';
110+
$optionId = 10;
111+
$optionMock = $this->getMockForAbstractClass(AttributeOptionInterface::class);
112+
113+
$this->eavOptionUpdateMock->expects($this->once())
114+
->method('update')
115+
->with(
116+
ProductAttributeInterface::ENTITY_TYPE_CODE,
117+
$attributeCode,
118+
$optionId,
119+
$optionMock
120+
)->willReturn(true);
121+
$this->assertTrue($this->model->update($attributeCode, $optionId, $optionMock));
122+
}
80123
}

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@
183183
<resource ref="Magento_Catalog::attributes_attributes" />
184184
</resources>
185185
</route>
186+
<route url="/V1/products/attributes/:attributeCode/options/:optionId" method="PUT">
187+
<service class="Magento\Catalog\Api\ProductAttributeOptionUpdateInterface" method="update" />
188+
<resources>
189+
<resource ref="Magento_Catalog::attributes_attributes" />
190+
</resources>
191+
</route>
186192
<route url="/V1/products/attributes/:attributeCode/options/:optionId" method="DELETE">
187193
<service class="Magento\Catalog\Api\ProductAttributeOptionManagementInterface" method="delete" />
188194
<resources>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ interface AttributeOptionManagementInterface
1515
/**
1616
* Add option to attribute
1717
*
18-
* @param string $attributeCode
1918
* @param int $entityType
19+
* @param string $attributeCode
2020
* @param \Magento\Eav\Api\Data\AttributeOptionInterface $option
2121
* @throws \Magento\Framework\Exception\StateException
2222
* @throws \Magento\Framework\Exception\InputException
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+
}

0 commit comments

Comments
 (0)