diff --git a/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php b/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php index f363ffe70e80e..599406f455281 100644 --- a/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php +++ b/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php @@ -130,9 +130,17 @@ protected function setProperOptionsArray(Attribute $attribute) $swatchesArray = $attribute->getData('swatchtext'); } if ($canReplace == true) { - $attribute->setData('option', $optionsArray); - $attribute->setData('default', $defaultValue); - $attribute->setData('swatch', $swatchesArray); + if (!empty($optionsArray)) { + $attribute->setData('option', $optionsArray); + } + if (!empty($defaultValue)) { + $attribute->setData('default', $defaultValue); + } else { + $attribute->setData('default', [0 => $attribute->getDefaultValue()]); + } + if (!empty($swatchesArray)) { + $attribute->setData('swatch', $swatchesArray); + } } } diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductSwatchAttributeOptionManagementInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductSwatchAttributeOptionManagementInterfaceTest.php new file mode 100644 index 0000000000000..63e5282c22104 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductSwatchAttributeOptionManagementInterfaceTest.php @@ -0,0 +1,105 @@ + [ + 'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'add', + ], + ]; + + $response = $this->_webApiCall( + $serviceInfo, + [ + 'attributeCode' => $testAttributeCode, + 'option' => $optionData, + ] + ); + + $this->assertTrue($response); + $updatedData = $this->getAttributeOptions($testAttributeCode); + $lastOption = array_pop($updatedData); + $this->assertEquals( + $optionData[AttributeOptionInterface::STORE_LABELS][0][AttributeOptionLabelInterface::LABEL], + $lastOption['label'] + ); + } + + /** + * @return array + */ + public function addDataProvider() + { + $optionPayload = [ + AttributeOptionInterface::LABEL => 'new color', + AttributeOptionInterface::SORT_ORDER => 100, + AttributeOptionInterface::IS_DEFAULT => true, + AttributeOptionInterface::STORE_LABELS => [ + [ + AttributeOptionLabelInterface::LABEL => 'DE label', + AttributeOptionLabelInterface::STORE_ID => 1, + ], + ], + AttributeOptionInterface::VALUE => '' + ]; + + return [ + 'option_without_value_node' => [ + $optionPayload + ], + 'option_with_value_node_that_starts_with_text' => [ + array_merge($optionPayload, [AttributeOptionInterface::VALUE => 'some_text']) + ], + 'option_with_value_node_that_starts_with_a_number' => [ + array_merge($optionPayload, [AttributeOptionInterface::VALUE => '123_some_text']) + ], + + ]; + } + + /** + * @param $testAttributeCode + * @return array|bool|float|int|string + */ + private function getAttributeOptions($testAttributeCode) + { + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, + ], + 'soap' => [ + 'service' => self::SERVICE_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_NAME . 'getItems', + ], + ]; + return $this->_webApiCall($serviceInfo, ['attributeCode' => $testAttributeCode]); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Swatches/Model/SwatchAttributeOptionAddTest.php b/dev/tests/integration/testsuite/Magento/Swatches/Model/SwatchAttributeOptionAddTest.php new file mode 100644 index 0000000000000..84ba587f5e784 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Swatches/Model/SwatchAttributeOptionAddTest.php @@ -0,0 +1,82 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + /** + * @magentoAppArea adminhtml + * @magentoDbIsolation enabled + * @magentoDataFixture Magento/Swatches/_files/swatch_attribute.php + */ + public function testSwatchOptionAdd() + { + /** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */ + $attribute = $this->objectManager + ->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class) + ->load('color_swatch', 'attribute_code'); + $optionsPerAttribute = 3; + + $data['options']['option'] = array_reduce( + range(10, $optionsPerAttribute), + function ($values, $index) use ($optionsPerAttribute) { + $values[] = [ + 'label' => 'option ' . $index, + 'value' => 'option_' . $index + ]; + return $values; + }, + [] + ); + + /** @var AttributeOptionInterface[] $options */ + $options = []; + foreach ($data['options']['option'] as $optionData) { + $options[] = $this->objectManager + ->get(AttributeOptionInterfaceFactory::class) + ->create(['data' => $optionData]); + } + + /** @var ProductAttributeOptionManagementInterface $optionManagement */ + $optionManagement = $this->objectManager->get(ProductAttributeOptionManagementInterface::class); + foreach ($options as $option) { + $optionManagement->add( + $attribute->getAttributeCode(), + $option + ); + } + + $items = $optionManagement->getItems($attribute->getAttributeCode()); + array_walk( + $items, + function (&$item) { + /** @var AttributeOptionInterface $item */ + $item = $item->getLabel(); + } + ); + foreach ($options as $option) { + $this->assertTrue(in_array($option->getLabel(), $items)); + } + } +}