Skip to content

Commit 0eca759

Browse files
author
Oleksii Korshenko
authored
MAGETWO-83292: [Backport 2.3] Add swatch option: Prevent loosing data and default value if data is not populated via adminhtml #12044
2 parents ae4cff0 + cdd07f5 commit 0eca759

File tree

3 files changed

+198
-3
lines changed

3 files changed

+198
-3
lines changed

app/code/Magento/Swatches/Model/Plugin/EavAttribute.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,17 @@ protected function setProperOptionsArray(Attribute $attribute)
130130
$swatchesArray = $attribute->getData('swatchtext');
131131
}
132132
if ($canReplace == true) {
133-
$attribute->setData('option', $optionsArray);
134-
$attribute->setData('default', $defaultValue);
135-
$attribute->setData('swatch', $swatchesArray);
133+
if (!empty($optionsArray)) {
134+
$attribute->setData('option', $optionsArray);
135+
}
136+
if (!empty($defaultValue)) {
137+
$attribute->setData('default', $defaultValue);
138+
} else {
139+
$attribute->setData('default', [0 => $attribute->getDefaultValue()]);
140+
}
141+
if (!empty($swatchesArray)) {
142+
$attribute->setData('swatch', $swatchesArray);
143+
}
136144
}
137145
}
138146

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Api;
7+
8+
use Magento\Eav\Api\Data\AttributeOptionInterface;
9+
use Magento\Eav\Api\Data\AttributeOptionLabelInterface;
10+
use Magento\TestFramework\TestCase\WebapiAbstract;
11+
12+
class ProductSwatchAttributeOptionManagementInterfaceTest extends WebapiAbstract
13+
{
14+
const SERVICE_NAME = 'catalogProductAttributeOptionManagementV1';
15+
const SERVICE_VERSION = 'V1';
16+
const RESOURCE_PATH = '/V1/products/attributes';
17+
18+
/**
19+
* @magentoApiDataFixture Magento/Swatches/_files/swatch_attribute.php
20+
* @dataProvider addDataProvider
21+
*/
22+
public function testAdd($optionData)
23+
{
24+
$testAttributeCode = 'color_swatch';
25+
$serviceInfo = [
26+
'rest' => [
27+
'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options',
28+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
29+
],
30+
'soap' => [
31+
'service' => self::SERVICE_NAME,
32+
'serviceVersion' => self::SERVICE_VERSION,
33+
'operation' => self::SERVICE_NAME . 'add',
34+
],
35+
];
36+
37+
$response = $this->_webApiCall(
38+
$serviceInfo,
39+
[
40+
'attributeCode' => $testAttributeCode,
41+
'option' => $optionData,
42+
]
43+
);
44+
45+
$this->assertTrue($response);
46+
$updatedData = $this->getAttributeOptions($testAttributeCode);
47+
$lastOption = array_pop($updatedData);
48+
$this->assertEquals(
49+
$optionData[AttributeOptionInterface::STORE_LABELS][0][AttributeOptionLabelInterface::LABEL],
50+
$lastOption['label']
51+
);
52+
}
53+
54+
/**
55+
* @return array
56+
*/
57+
public function addDataProvider()
58+
{
59+
$optionPayload = [
60+
AttributeOptionInterface::LABEL => 'new color',
61+
AttributeOptionInterface::SORT_ORDER => 100,
62+
AttributeOptionInterface::IS_DEFAULT => true,
63+
AttributeOptionInterface::STORE_LABELS => [
64+
[
65+
AttributeOptionLabelInterface::LABEL => 'DE label',
66+
AttributeOptionLabelInterface::STORE_ID => 1,
67+
],
68+
],
69+
AttributeOptionInterface::VALUE => ''
70+
];
71+
72+
return [
73+
'option_without_value_node' => [
74+
$optionPayload
75+
],
76+
'option_with_value_node_that_starts_with_text' => [
77+
array_merge($optionPayload, [AttributeOptionInterface::VALUE => 'some_text'])
78+
],
79+
'option_with_value_node_that_starts_with_a_number' => [
80+
array_merge($optionPayload, [AttributeOptionInterface::VALUE => '123_some_text'])
81+
],
82+
83+
];
84+
}
85+
86+
/**
87+
* @param $testAttributeCode
88+
* @return array|bool|float|int|string
89+
*/
90+
private function getAttributeOptions($testAttributeCode)
91+
{
92+
$serviceInfo = [
93+
'rest' => [
94+
'resourcePath' => self::RESOURCE_PATH . '/' . $testAttributeCode . '/options',
95+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
96+
],
97+
'soap' => [
98+
'service' => self::SERVICE_NAME,
99+
'serviceVersion' => self::SERVICE_VERSION,
100+
'operation' => self::SERVICE_NAME . 'getItems',
101+
],
102+
];
103+
return $this->_webApiCall($serviceInfo, ['attributeCode' => $testAttributeCode]);
104+
}
105+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Model;
7+
8+
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
9+
use Magento\Eav\Api\Data\AttributeOptionInterface;
10+
use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
11+
12+
/**
13+
* Test add option of swatch attribute
14+
*
15+
*/
16+
class SwatchAttributeOptionAddTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var \Magento\Framework\ObjectManagerInterface
20+
*/
21+
private $objectManager;
22+
23+
protected function setUp()
24+
{
25+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
26+
}
27+
28+
/**
29+
* @magentoAppArea adminhtml
30+
* @magentoDbIsolation enabled
31+
* @magentoDataFixture Magento/Swatches/_files/swatch_attribute.php
32+
*/
33+
public function testSwatchOptionAdd()
34+
{
35+
/** @var \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute */
36+
$attribute = $this->objectManager
37+
->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
38+
->load('color_swatch', 'attribute_code');
39+
$optionsPerAttribute = 3;
40+
41+
$data['options']['option'] = array_reduce(
42+
range(10, $optionsPerAttribute),
43+
function ($values, $index) use ($optionsPerAttribute) {
44+
$values[] = [
45+
'label' => 'option ' . $index,
46+
'value' => 'option_' . $index
47+
];
48+
return $values;
49+
},
50+
[]
51+
);
52+
53+
/** @var AttributeOptionInterface[] $options */
54+
$options = [];
55+
foreach ($data['options']['option'] as $optionData) {
56+
$options[] = $this->objectManager
57+
->get(AttributeOptionInterfaceFactory::class)
58+
->create(['data' => $optionData]);
59+
}
60+
61+
/** @var ProductAttributeOptionManagementInterface $optionManagement */
62+
$optionManagement = $this->objectManager->get(ProductAttributeOptionManagementInterface::class);
63+
foreach ($options as $option) {
64+
$optionManagement->add(
65+
$attribute->getAttributeCode(),
66+
$option
67+
);
68+
}
69+
70+
$items = $optionManagement->getItems($attribute->getAttributeCode());
71+
array_walk(
72+
$items,
73+
function (&$item) {
74+
/** @var AttributeOptionInterface $item */
75+
$item = $item->getLabel();
76+
}
77+
);
78+
foreach ($options as $option) {
79+
$this->assertTrue(in_array($option->getLabel(), $items));
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)