Skip to content

Commit 30861ff

Browse files
author
okarpenko
committed
Merge branch 'MAGETWO-45073' into BUGS
2 parents 5181fdd + f503c5a commit 30861ff

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
99
use Magento\Swatches\Model\Swatch;
10+
use Magento\Framework\Exception\InputException;
1011

1112
/**
1213
* Plugin model for Catalog Resource Attribute
@@ -72,13 +73,29 @@ public function __construct(
7273
*/
7374
public function beforeSave(Attribute $attribute)
7475
{
75-
if ($this->swatchHelper->isSwatchAttribute($attribute)) {
76+
if ($this->swatchHelper->isSwatchAttribute($attribute) && $this->validateOptions($attribute)) {
7677
$this->setProperOptionsArray($attribute);
7778
$this->swatchHelper->assembleAdditionalDataEavAttribute($attribute);
7879
}
7980
$this->convertSwatchToDropdown($attribute);
8081
}
8182

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+
8299
/**
83100
* Swatch save operations
84101
*

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ 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+
5760
public function setUp()
5861
{
5962
$this->attribute = $this->getMock('\Magento\Catalog\Model\ResourceModel\Eav\Attribute', [], [], '', false);
@@ -87,6 +90,13 @@ public function setUp()
8790
'swatchHelper' => $this->swatchHelper,
8891
]
8992
);
93+
$this->source = $this->getMockForAbstractClass(
94+
'Magento\Eav\Model\Entity\Attribute\Source\AbstractSource',
95+
[],
96+
'',
97+
false
98+
);
99+
90100

91101
$this->optionIds = [
92102
'value' => ['option 89' => 'test 1', 'option 114' => 'test 2', 'option 170' => 'test 3'],
@@ -117,6 +127,19 @@ public function testBeforeSaveVisualSwatch()
117127
['swatch', self::ATTRIBUTE_SWATCH_VALUE]
118128
);
119129

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+
120143
$this->swatchHelper->expects($this->once())->method('assembleAdditionalDataEavAttribute')
121144
->with($this->attribute);
122145
$this->swatchHelper->expects($this->once())->method('isVisualSwatch')
@@ -153,6 +176,19 @@ public function testBeforeSaveTextSwatch()
153176
['swatch', self::ATTRIBUTE_SWATCH_VALUE]
154177
);
155178

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+
156192
$this->swatchHelper->expects($this->once())->method('assembleAdditionalDataEavAttribute')
157193
->with($this->attribute);
158194
$this->swatchHelper->expects($this->once())->method('isVisualSwatch')
@@ -168,6 +204,27 @@ public function testBeforeSaveTextSwatch()
168204
$this->eavAttribute->beforeSave($this->attribute);
169205
}
170206

207+
/**
208+
* @expectedException \Magento\Framework\Exception\InputException
209+
* @expectedExceptionMessage Admin is a required field in the each row
210+
*/
211+
public function testBeforeSaveWithFailedValidation()
212+
{
213+
$this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
214+
->with($this->attribute)
215+
->willReturn(true);
216+
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([]);
224+
225+
$this->eavAttribute->beforeSave($this->attribute);
226+
}
227+
171228
public function testBeforeSaveNotSwatch()
172229
{
173230
$additionalData = [

0 commit comments

Comments
 (0)