Skip to content

Commit 24a49b9

Browse files
author
Stanislav Idolov
authored
ENGCOM-1402: Rest API attribute set updating not working as expected #1233
2 parents 0b54cdc + 24bba3e commit 24a49b9

File tree

2 files changed

+95
-24
lines changed

2 files changed

+95
-24
lines changed

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

+26-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
*/
77
namespace Magento\Catalog\Model\Product\Attribute;
88

9-
use Magento\Framework\Exception\InputException;
10-
119
class SetRepository implements \Magento\Catalog\Api\AttributeSetRepositoryInterface
1210
{
1311
/**
@@ -53,7 +51,7 @@ public function __construct(
5351
*/
5452
public function save(\Magento\Eav\Api\Data\AttributeSetInterface $attributeSet)
5553
{
56-
$this->validate($attributeSet);
54+
$this->validateBeforeSave($attributeSet);
5755
return $this->attributeSetRepository->save($attributeSet);
5856
}
5957

@@ -127,4 +125,29 @@ protected function validate(\Magento\Eav\Api\Data\AttributeSetInterface $attribu
127125
);
128126
}
129127
}
128+
129+
/**
130+
* Validate attribute set entity type id.
131+
*
132+
* @param \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet
133+
* @return void
134+
* @throws \Magento\Framework\Exception\StateException
135+
* @throws \Magento\Framework\Exception\NoSuchEntityException
136+
* @throws \Magento\Framework\Exception\LocalizedException
137+
*/
138+
private function validateBeforeSave(\Magento\Eav\Api\Data\AttributeSetInterface $attributeSet)
139+
{
140+
$productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
141+
$result = $attributeSet->getEntityTypeId() === $productEntityId;
142+
if (!$result && $attributeSet->getAttributeSetId()) {
143+
$existingAttributeSet = $this->attributeSetRepository->get($attributeSet->getAttributeSetId());
144+
$attributeSet->setEntityTypeId($existingAttributeSet->getEntityTypeId());
145+
$result = $existingAttributeSet->getEntityTypeId() === $productEntityId;
146+
}
147+
if (!$result) {
148+
throw new \Magento\Framework\Exception\StateException(
149+
__('Provided Attribute set non product Attribute set.')
150+
);
151+
}
152+
}
130153
}

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

+69-21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Catalog\Api;
77

8+
use Magento\Eav\Api\Data\AttributeSetInterface;
89
use Magento\TestFramework\TestCase\WebapiAbstract;
910

1011
class AttributeSetRepositoryTest extends WebapiAbstract
@@ -71,38 +72,39 @@ public function testSave()
7172
{
7273
$attributeSetName = 'empty_attribute_set';
7374
$attributeSet = $this->getAttributeSetByName($attributeSetName);
74-
$serviceInfo = [
75-
'rest' => [
76-
'resourcePath' => '/V1/products/attribute-sets/' . $attributeSet->getId(),
77-
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
78-
],
79-
'soap' => [
80-
'service' => 'catalogAttributeSetRepositoryV1',
81-
'serviceVersion' => 'V1',
82-
'operation' => 'catalogAttributeSetRepositoryV1Save',
75+
$updatedSortOrder = $attributeSet->getSortOrder() + 200;
76+
$arguments = [
77+
'attributeSet' => [
78+
'attribute_set_id' => $attributeSet->getId(),
79+
// name is the same, because it is used by fixture rollback script
80+
'attribute_set_name' => $attributeSet->getAttributeSetName(),
81+
'entity_type_id' => $attributeSet->getEntityTypeId(),
82+
'sort_order' => $updatedSortOrder,
8383
],
8484
];
85+
$result = $this->save($attributeSet, $arguments);
86+
$this->assertAttributeSetData($result, $attributeSetName, $updatedSortOrder);
87+
}
8588

89+
/**
90+
* Test update attribute set without specified optional "entity_type_id" param.
91+
*
92+
* @magentoApiDataFixture Magento/Eav/_files/empty_attribute_set.php
93+
*/
94+
public function testUpdateWithoutEntityType()
95+
{
96+
$attributeSetName = 'empty_attribute_set';
97+
$attributeSet = $this->getAttributeSetByName('empty_attribute_set');
8698
$updatedSortOrder = $attributeSet->getSortOrder() + 200;
87-
8899
$arguments = [
89100
'attributeSet' => [
90101
'attribute_set_id' => $attributeSet->getId(),
91-
// name is the same, because it is used by fixture rollback script
92102
'attribute_set_name' => $attributeSet->getAttributeSetName(),
93-
'entity_type_id' => $attributeSet->getEntityTypeId(),
94103
'sort_order' => $updatedSortOrder,
95104
],
96105
];
97-
$result = $this->_webApiCall($serviceInfo, $arguments);
98-
$this->assertNotNull($result);
99-
// Reload attribute set data
100-
$attributeSet = $this->getAttributeSetByName($attributeSetName);
101-
$this->assertEquals($attributeSet->getAttributeSetId(), $result['attribute_set_id']);
102-
$this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
103-
$this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
104-
$this->assertEquals($updatedSortOrder, $result['sort_order']);
105-
$this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
106+
$result = $this->save($attributeSet, $arguments);
107+
$this->assertAttributeSetData($result, $attributeSetName, $updatedSortOrder);
106108
}
107109

108110
/**
@@ -215,4 +217,50 @@ protected function getAttributeSetByName($attributeSetName)
215217
}
216218
return $attributeSet;
217219
}
220+
221+
/**
222+
* Save given attribute set with specified arguments.
223+
*
224+
* @param AttributeSetInterface $attributeSet
225+
* @param array $arguments
226+
* @return array
227+
*/
228+
private function save(AttributeSetInterface $attributeSet, array $arguments)
229+
{
230+
$serviceInfo = [
231+
'rest' => [
232+
'resourcePath' => '/V1/products/attribute-sets/' . $attributeSet->getAttributeSetId(),
233+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
234+
],
235+
'soap' => [
236+
'service' => 'catalogAttributeSetRepositoryV1',
237+
'serviceVersion' => 'V1',
238+
'operation' => 'catalogAttributeSetRepositoryV1Save',
239+
],
240+
];
241+
242+
return $this->_webApiCall($serviceInfo, $arguments);
243+
}
244+
245+
/**
246+
* Check attribute set data.
247+
*
248+
* @param string $attributeSetName
249+
* @param array $result
250+
* @param int $updatedSortOrder
251+
*/
252+
private function assertAttributeSetData(
253+
array $result,
254+
string $attributeSetName,
255+
int $updatedSortOrder
256+
) {
257+
$this->assertNotNull($result);
258+
// Reload attribute set data
259+
$attributeSet = $this->getAttributeSetByName($attributeSetName);
260+
$this->assertEquals($attributeSet->getAttributeSetId(), $result['attribute_set_id']);
261+
$this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']);
262+
$this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']);
263+
$this->assertEquals($updatedSortOrder, $result['sort_order']);
264+
$this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']);
265+
}
218266
}

0 commit comments

Comments
 (0)