Skip to content

Commit 600e79a

Browse files
author
Yushkin, Dmytro
committed
Merge branch 'MAGETWO-45061' into MAGETWO-45269
2 parents 08bf8cb + f949b30 commit 600e79a

File tree

2 files changed

+145
-70
lines changed

2 files changed

+145
-70
lines changed

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

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,14 @@ public function __construct(
7373
*/
7474
public function beforeSave(Attribute $attribute)
7575
{
76-
if ($this->swatchHelper->isSwatchAttribute($attribute) && $this->validateOptions($attribute)) {
76+
if ($this->swatchHelper->isSwatchAttribute($attribute)) {
7777
$this->setProperOptionsArray($attribute);
78+
$this->validateOptions($attribute);
7879
$this->swatchHelper->assembleAdditionalDataEavAttribute($attribute);
7980
}
8081
$this->convertSwatchToDropdown($attribute);
8182
}
8283

83-
/**
84-
* Validate that attribute options exist
85-
*
86-
* @param Attribute $attribute
87-
* @return bool
88-
* @throws InputException
89-
*/
90-
protected function validateOptions(Attribute $attribute)
91-
{
92-
$attributeSavedOptions = $attribute->getSource()->getAllOptions(false);
93-
if (!count($attributeSavedOptions)) {
94-
throw new InputException(__('Admin is a required field in the each row'));
95-
}
96-
return true;
97-
}
98-
9984
/**
10085
* Swatch save operations
10186
*
@@ -395,10 +380,59 @@ protected function saveDefaultSwatchOptionValue(Attribute $attribute)
395380
if (!empty($defaultValue)) {
396381
/** @var \Magento\Swatches\Model\Swatch $swatch */
397382
$swatch = $this->swatchFactory->create();
398-
if (substr($defaultValue, 0, 6) == self::BASE_OPTION_TITLE) {
383+
// created and removed on frontend option not exists in dependency array
384+
if (
385+
substr($defaultValue, 0, 6) == self::BASE_OPTION_TITLE &&
386+
isset($this->dependencyArray[$defaultValue])
387+
) {
399388
$defaultValue = $this->dependencyArray[$defaultValue];
400389
}
401390
$swatch->getResource()->saveDefaultSwatchOption($attribute->getId(), $defaultValue);
402391
}
403392
}
393+
394+
/**
395+
* Validate that attribute options exist
396+
*
397+
* @param Attribute $attribute
398+
* @return bool
399+
* @throws InputException
400+
*/
401+
protected function validateOptions(Attribute $attribute)
402+
{
403+
$options = null;
404+
if ($this->swatchHelper->isVisualSwatch($attribute)) {
405+
$options = $attribute->getData('optionvisual');
406+
} elseif ($this->swatchHelper->isTextSwatch($attribute)) {
407+
$options = $attribute->getData('optiontext');
408+
}
409+
if ($options && !$this->isOptionsValid($options, $attribute)) {
410+
throw new InputException(__('Admin is a required field in the each row'));
411+
}
412+
return true;
413+
}
414+
415+
/**
416+
* Check if attribute options are valid
417+
*
418+
* @param array $options
419+
* @param Attribute $attribute
420+
* @return bool
421+
*/
422+
protected function isOptionsValid(array $options, Attribute $attribute)
423+
{
424+
if (!isset($options['value'])) {
425+
return false;
426+
}
427+
foreach ($options['value'] as $optionId => $option) {
428+
// do not validate options marked as deleted
429+
if ($this->isOptionForDelete($attribute, $optionId)) {
430+
continue;
431+
}
432+
if (empty($option[0])) {
433+
return false;
434+
}
435+
}
436+
return true;
437+
}
404438
}

app/code/Magento/Swatches/Test/Unit/Model/Plugin/EavAttributeTest.php

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ class EavAttributeTest extends \PHPUnit_Framework_TestCase
5454
/** @var array */
5555
private $dependencyArray = [];
5656

57-
/** @var \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource|\PHPUnit_Framework_MockObject_MockObject */
58-
protected $source;
59-
6057
public function setUp()
6158
{
6259
$this->attribute = $this->getMock('\Magento\Catalog\Model\ResourceModel\Eav\Attribute', [], [], '', false);
@@ -90,12 +87,6 @@ public function setUp()
9087
'swatchHelper' => $this->swatchHelper,
9188
]
9289
);
93-
$this->source = $this->getMockForAbstractClass(
94-
'Magento\Eav\Model\Entity\Attribute\Source\AbstractSource',
95-
[],
96-
'',
97-
false
98-
);
9990

10091

10192
$this->optionIds = [
@@ -108,16 +99,23 @@ public function setUp()
10899

109100
public function testBeforeSaveVisualSwatch()
110101
{
111-
$this->attribute->expects($this->exactly(4))->method('getData')->withConsecutive(
102+
$option = [
103+
'value' => [
104+
0 => 'option value',
105+
]
106+
];
107+
$this->attribute->expects($this->exactly(6))->method('getData')->withConsecutive(
112108
['defaultvisual'],
113109
['optionvisual'],
114110
['swatchvisual'],
115-
[Swatch::SWATCH_INPUT_TYPE_KEY]
111+
['optionvisual'],
112+
['option/delete/0']
116113
)->will($this->onConsecutiveCalls(
117114
self::ATTRIBUTE_DEFAULT_VALUE,
118115
self::ATTRIBUTE_OPTION_VALUE,
119116
self::ATTRIBUTE_SWATCH_VALUE,
120-
Swatch::SWATCH_INPUT_TYPE_VISUAL
117+
$option,
118+
false
121119
));
122120

123121
$this->attribute->expects($this->exactly(3))->method('setData')
@@ -127,22 +125,9 @@ public function testBeforeSaveVisualSwatch()
127125
['swatch', self::ATTRIBUTE_SWATCH_VALUE]
128126
);
129127

130-
$this->attribute->expects($this->once())
131-
->method('getSource')
132-
->willReturn($this->source);
133-
$this->source->expects($this->once())
134-
->method('getAllOptions')
135-
->with(false)
136-
->willReturn([
137-
[
138-
'value' => 'value',
139-
'label' => 'label'
140-
]
141-
]);
142-
143128
$this->swatchHelper->expects($this->once())->method('assembleAdditionalDataEavAttribute')
144129
->with($this->attribute);
145-
$this->swatchHelper->expects($this->once())->method('isVisualSwatch')
130+
$this->swatchHelper->expects($this->atLeastOnce())->method('isVisualSwatch')
146131
->with($this->attribute)
147132
->willReturn(true);
148133
$this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
@@ -155,17 +140,24 @@ public function testBeforeSaveVisualSwatch()
155140

156141
public function testBeforeSaveTextSwatch()
157142
{
158-
$this->attribute->expects($this->exactly(4))->method('getData')->withConsecutive(
143+
$option = [
144+
'value' => [
145+
0 => 'option value',
146+
]
147+
];
148+
$this->attribute->expects($this->exactly(6))->method('getData')->withConsecutive(
159149
['defaulttext'],
160150
['optiontext'],
161151
['swatchtext'],
162-
[Swatch::SWATCH_INPUT_TYPE_KEY]
152+
['optiontext'],
153+
['option/delete/0']
163154
)->will(
164155
$this->onConsecutiveCalls(
165156
self::ATTRIBUTE_DEFAULT_VALUE,
166157
self::ATTRIBUTE_OPTION_VALUE,
167158
self::ATTRIBUTE_SWATCH_VALUE,
168-
Swatch::SWATCH_INPUT_TYPE_TEXT
159+
$option,
160+
false
169161
)
170162
);
171163

@@ -176,25 +168,12 @@ public function testBeforeSaveTextSwatch()
176168
['swatch', self::ATTRIBUTE_SWATCH_VALUE]
177169
);
178170

179-
$this->attribute->expects($this->once())
180-
->method('getSource')
181-
->willReturn($this->source);
182-
$this->source->expects($this->once())
183-
->method('getAllOptions')
184-
->with(false)
185-
->willReturn([
186-
[
187-
'value' => 'value',
188-
'label' => 'label'
189-
]
190-
]);
191-
192171
$this->swatchHelper->expects($this->once())->method('assembleAdditionalDataEavAttribute')
193172
->with($this->attribute);
194-
$this->swatchHelper->expects($this->once())->method('isVisualSwatch')
173+
$this->swatchHelper->expects($this->atLeastOnce())->method('isVisualSwatch')
195174
->with($this->attribute)
196175
->willReturn(false);
197-
$this->swatchHelper->expects($this->once())->method('isTextSwatch')
176+
$this->swatchHelper->expects($this->atLeastOnce())->method('isTextSwatch')
198177
->with($this->attribute)
199178
->willReturn(true);
200179
$this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
@@ -210,21 +189,83 @@ public function testBeforeSaveTextSwatch()
210189
*/
211190
public function testBeforeSaveWithFailedValidation()
212191
{
213-
$this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
192+
$optionText = [
193+
'value' => [
194+
0 => '',
195+
]
196+
];
197+
$this->swatchHelper->expects($this->once())
198+
->method('isSwatchAttribute')
214199
->with($this->attribute)
215200
->willReturn(true);
216201

217-
$this->attribute->expects($this->once())
218-
->method('getSource')
219-
->willReturn($this->source);
220-
$this->source->expects($this->once())
221-
->method('getAllOptions')
222-
->with(false)
223-
->willReturn([]);
202+
$this->swatchHelper->expects($this->atLeastOnce())
203+
->method('isVisualSwatch')
204+
->willReturn(true);
205+
$this->attribute->expects($this->exactly(5))
206+
->method('getData')
207+
->withConsecutive(
208+
['defaultvisual'],
209+
['optionvisual'],
210+
['swatchvisual'],
211+
['optionvisual'],
212+
['option/delete/0']
213+
)
214+
->will(
215+
$this->onConsecutiveCalls(
216+
self::ATTRIBUTE_DEFAULT_VALUE,
217+
self::ATTRIBUTE_OPTION_VALUE,
218+
self::ATTRIBUTE_SWATCH_VALUE,
219+
$optionText,
220+
false
221+
)
222+
);
224223

225224
$this->eavAttribute->beforeSave($this->attribute);
226225
}
227226

227+
/**
228+
* @covers \Magento\Swatches\Model\Plugin\EavAttribute::beforeSave()
229+
*/
230+
public function testBeforeSaveWithDeletedOption()
231+
{
232+
$optionText = [
233+
'value' => [
234+
0 => '',
235+
]
236+
];
237+
238+
$this->swatchHelper->expects($this->once())
239+
->method('isSwatchAttribute')
240+
->with($this->attribute)
241+
->willReturn(true);
242+
243+
$this->swatchHelper->expects($this->atLeastOnce())
244+
->method('isVisualSwatch')
245+
->willReturn(true);
246+
$this->attribute->expects($this->exactly(6))
247+
->method('getData')
248+
->withConsecutive(
249+
['defaultvisual'],
250+
['optionvisual'],
251+
['swatchvisual'],
252+
['optionvisual'],
253+
['option/delete/0'],
254+
['swatch_input_type']
255+
)
256+
->will(
257+
$this->onConsecutiveCalls(
258+
self::ATTRIBUTE_DEFAULT_VALUE,
259+
self::ATTRIBUTE_OPTION_VALUE,
260+
self::ATTRIBUTE_SWATCH_VALUE,
261+
$optionText,
262+
true,
263+
false
264+
)
265+
);
266+
$this->eavAttribute->beforeSave($this->attribute);
267+
}
268+
228269
public function testBeforeSaveNotSwatch()
229270
{
230271
$additionalData = [

0 commit comments

Comments
 (0)