diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/SetRepository.php b/app/code/Magento/Catalog/Model/Product/Attribute/SetRepository.php index 14774103b8cd2..73a8a1e25957f 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/SetRepository.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/SetRepository.php @@ -6,8 +6,6 @@ */ namespace Magento\Catalog\Model\Product\Attribute; -use Magento\Framework\Exception\InputException; - class SetRepository implements \Magento\Catalog\Api\AttributeSetRepositoryInterface { /** @@ -53,7 +51,7 @@ public function __construct( */ public function save(\Magento\Eav\Api\Data\AttributeSetInterface $attributeSet) { - $this->validate($attributeSet); + $this->validateBeforeSave($attributeSet); return $this->attributeSetRepository->save($attributeSet); } @@ -127,4 +125,29 @@ protected function validate(\Magento\Eav\Api\Data\AttributeSetInterface $attribu ); } } + + /** + * Validate attribute set entity type id. + * + * @param \Magento\Eav\Api\Data\AttributeSetInterface $attributeSet + * @return void + * @throws \Magento\Framework\Exception\StateException + * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws \Magento\Framework\Exception\LocalizedException + */ + private function validateBeforeSave(\Magento\Eav\Api\Data\AttributeSetInterface $attributeSet) + { + $productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId(); + $result = $attributeSet->getEntityTypeId() === $productEntityId; + if (!$result && $attributeSet->getAttributeSetId()) { + $existingAttributeSet = $this->attributeSetRepository->get($attributeSet->getAttributeSetId()); + $attributeSet->setEntityTypeId($existingAttributeSet->getEntityTypeId()); + $result = $existingAttributeSet->getEntityTypeId() === $productEntityId; + } + if (!$result) { + throw new \Magento\Framework\Exception\StateException( + __('Provided Attribute set non product Attribute set.') + ); + } + } } diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/AttributeSetRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/AttributeSetRepositoryTest.php index 4f917a9c9961a..a879756e32cb6 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/AttributeSetRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/AttributeSetRepositoryTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Catalog\Api; +use Magento\Eav\Api\Data\AttributeSetInterface; use Magento\TestFramework\TestCase\WebapiAbstract; class AttributeSetRepositoryTest extends WebapiAbstract @@ -71,38 +72,39 @@ public function testSave() { $attributeSetName = 'empty_attribute_set'; $attributeSet = $this->getAttributeSetByName($attributeSetName); - $serviceInfo = [ - 'rest' => [ - 'resourcePath' => '/V1/products/attribute-sets/' . $attributeSet->getId(), - 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, - ], - 'soap' => [ - 'service' => 'catalogAttributeSetRepositoryV1', - 'serviceVersion' => 'V1', - 'operation' => 'catalogAttributeSetRepositoryV1Save', + $updatedSortOrder = $attributeSet->getSortOrder() + 200; + $arguments = [ + 'attributeSet' => [ + 'attribute_set_id' => $attributeSet->getId(), + // name is the same, because it is used by fixture rollback script + 'attribute_set_name' => $attributeSet->getAttributeSetName(), + 'entity_type_id' => $attributeSet->getEntityTypeId(), + 'sort_order' => $updatedSortOrder, ], ]; + $result = $this->save($attributeSet, $arguments); + $this->assertAttributeSetData($result, $attributeSetName, $updatedSortOrder); + } + /** + * Test update attribute set without specified optional "entity_type_id" param. + * + * @magentoApiDataFixture Magento/Eav/_files/empty_attribute_set.php + */ + public function testUpdateWithoutEntityType() + { + $attributeSetName = 'empty_attribute_set'; + $attributeSet = $this->getAttributeSetByName('empty_attribute_set'); $updatedSortOrder = $attributeSet->getSortOrder() + 200; - $arguments = [ 'attributeSet' => [ 'attribute_set_id' => $attributeSet->getId(), - // name is the same, because it is used by fixture rollback script 'attribute_set_name' => $attributeSet->getAttributeSetName(), - 'entity_type_id' => $attributeSet->getEntityTypeId(), 'sort_order' => $updatedSortOrder, ], ]; - $result = $this->_webApiCall($serviceInfo, $arguments); - $this->assertNotNull($result); - // Reload attribute set data - $attributeSet = $this->getAttributeSetByName($attributeSetName); - $this->assertEquals($attributeSet->getAttributeSetId(), $result['attribute_set_id']); - $this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']); - $this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']); - $this->assertEquals($updatedSortOrder, $result['sort_order']); - $this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']); + $result = $this->save($attributeSet, $arguments); + $this->assertAttributeSetData($result, $attributeSetName, $updatedSortOrder); } /** @@ -215,4 +217,50 @@ protected function getAttributeSetByName($attributeSetName) } return $attributeSet; } + + /** + * Save given attribute set with specified arguments. + * + * @param AttributeSetInterface $attributeSet + * @param array $arguments + * @return array + */ + private function save(AttributeSetInterface $attributeSet, array $arguments) + { + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/products/attribute-sets/' . $attributeSet->getAttributeSetId(), + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT, + ], + 'soap' => [ + 'service' => 'catalogAttributeSetRepositoryV1', + 'serviceVersion' => 'V1', + 'operation' => 'catalogAttributeSetRepositoryV1Save', + ], + ]; + + return $this->_webApiCall($serviceInfo, $arguments); + } + + /** + * Check attribute set data. + * + * @param string $attributeSetName + * @param array $result + * @param int $updatedSortOrder + */ + private function assertAttributeSetData( + array $result, + string $attributeSetName, + int $updatedSortOrder + ) { + $this->assertNotNull($result); + // Reload attribute set data + $attributeSet = $this->getAttributeSetByName($attributeSetName); + $this->assertEquals($attributeSet->getAttributeSetId(), $result['attribute_set_id']); + $this->assertEquals($attributeSet->getAttributeSetName(), $result['attribute_set_name']); + $this->assertEquals($attributeSet->getEntityTypeId(), $result['entity_type_id']); + $this->assertEquals($updatedSortOrder, $result['sort_order']); + $this->assertEquals($attributeSet->getSortOrder(), $result['sort_order']); + } }