Skip to content

Commit d83c61c

Browse files
committed
MAGETWO-94819: Swatch validation breaks the whole attribute form
1 parent 8373cd1 commit d83c61c

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

app/code/Magento/Swatches/Test/Mftf/Test/AdminCreateVisualSwatchWithNonValidOptionsTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<group value="Swatches"/>
1818
</annotations>
1919
<before>
20-
<actionGroup ref="LoginActionGroup" stepKey="login"/>
20+
<actionGroup ref="LoginAsAdmin" stepKey="login"/>
2121
</before>
2222
<after>
2323
<!-- Remove attribute -->

dev/tests/acceptance/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ tests/functional.suite.yml
77
tests/functional/Magento/FunctionalTest/_generated
88
vendor/*
99
mftf.log
10+
/.credentials.example
1011
/utils/

dev/tests/functional/tests/app/Magento/Catalog/Test/Handler/CatalogProductAttribute/Curl.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,72 @@ public function persist(FixtureInterface $fixture = null)
134134
}
135135

136136
/**
137+
* Additional data handling.
138+
*
137139
* @param array $data
138140
* @return array
139141
*/
140142
protected function changeStructureOfTheData(array $data)
141143
{
144+
$serializedOptions = $this->getSerializeOptions($data, ['option']);
145+
if ($serializedOptions) {
146+
$data['serialized_options'] = $serializedOptions;
147+
unset($data['option']);
148+
}
149+
142150
return $data;
143151
}
152+
153+
/**
154+
* Provides serialized product attribute options.
155+
*
156+
* @param array $data
157+
* @param array $optionKeys
158+
* @return array
159+
*/
160+
protected function getSerializeOptions(array $data, array $optionKeys): string
161+
{
162+
$options = [];
163+
foreach ($optionKeys as $optionKey) {
164+
if (!empty($data[$optionKey])) {
165+
$options = array_merge(
166+
$options,
167+
$this->getEncodedOptions([$optionKey => $data[$optionKey]])
168+
);
169+
}
170+
}
171+
172+
return json_encode($options);
173+
}
174+
175+
/**
176+
* Provides encoded attribute values.
177+
*
178+
* @param array $data
179+
* @return array
180+
*/
181+
private function getEncodedOptions(array $data): array
182+
{
183+
$optionsData = [];
184+
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($data));
185+
foreach ($iterator as $value) {
186+
$depth = $iterator->getDepth();
187+
$option = '';
188+
189+
$level = 0;
190+
$option .= $iterator->getSubIterator($level)->key();
191+
$level++;
192+
193+
while ($level <= $depth) {
194+
$option .= '[' . $iterator->getSubIterator($level)->key() . ']';
195+
$level++;
196+
}
197+
198+
$option .= '=' . $value;
199+
200+
$optionsData[] = $option;
201+
}
202+
203+
return $optionsData;
204+
}
144205
}

dev/tests/functional/tests/app/Magento/Swatches/Test/Handler/SwatchProductAttribute/Curl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ public function __construct(DataInterface $configuration, EventManagerInterface
3838
*/
3939
protected function changeStructureOfTheData(array $data)
4040
{
41-
$data = parent::changeStructureOfTheData($data);
4241
$data['optiontext'] = $data['option'];
4342
$data['swatchtext'] = [
4443
'value' => $data['option']['value']
4544
];
45+
$data['serialized_options'] = $this->getSerializeOptions($data, ['optiontext', 'swatchtext']);
4646
unset($data['option']);
47+
$data = parent::changeStructureOfTheData($data);
48+
4749
return $data;
4850
}
4951
}

0 commit comments

Comments
 (0)