Skip to content

Commit 4577b30

Browse files
author
okarpenko
committed
MAGETWO-45073: PHP notice during to create Text Swatch product attribute
1 parent 22629ab commit 4577b30

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

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

Lines changed: 21 additions & 3 deletions
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
@@ -49,19 +50,27 @@ class EavAttribute
4950
*/
5051
protected $isSwatchExists;
5152

53+
/**
54+
* @var \Magento\Framework\Message\ManagerInterface
55+
*/
56+
protected $messageManager;
57+
5258
/**
5359
* @param \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory
5460
* @param \Magento\Swatches\Model\SwatchFactory $swatchFactory
5561
* @param \Magento\Swatches\Helper\Data $swatchHelper
62+
* @param \Magento\Framework\Message\ManagerInterface $messageManager
5663
*/
5764
public function __construct(
5865
\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory,
5966
\Magento\Swatches\Model\SwatchFactory $swatchFactory,
60-
\Magento\Swatches\Helper\Data $swatchHelper
67+
\Magento\Swatches\Helper\Data $swatchHelper,
68+
\Magento\Framework\Message\ManagerInterface $messageManager
6169
) {
6270
$this->swatchCollectionFactory = $collectionFactory;
6371
$this->swatchFactory = $swatchFactory;
6472
$this->swatchHelper = $swatchHelper;
73+
$this->messageManager = $messageManager;
6574
}
6675

6776
/**
@@ -72,13 +81,22 @@ public function __construct(
7281
*/
7382
public function beforeSave(Attribute $attribute)
7483
{
75-
if ($this->swatchHelper->isSwatchAttribute($attribute)) {
84+
if ($this->swatchHelper->isSwatchAttribute($attribute) && $this->validateOptions($attribute)) {
7685
$this->setProperOptionsArray($attribute);
7786
$this->swatchHelper->assembleAdditionalDataEavAttribute($attribute);
7887
}
7988
$this->convertSwatchToDropdown($attribute);
8089
}
8190

91+
protected function validateOptions(Attribute $attribute)
92+
{
93+
$attributeSavedOptions = $attribute->getSource()->getAllOptions(false);
94+
if (!count($attributeSavedOptions)) {
95+
throw new InputException(__('Admin is a required field in the each row'));
96+
}
97+
return true;
98+
}
99+
82100
/**
83101
* Swatch save operations
84102
*
@@ -154,7 +172,7 @@ protected function processSwatchOptions(Attribute $attribute)
154172

155173
if (!empty($optionsArray) && is_array($optionsArray)) {
156174
$optionsArray = $this->prepareOptionIds($optionsArray);
157-
$attributeSavedOptions = $attribute->getSource()->getAllOptions();
175+
$attributeSavedOptions = $attribute->getSource()->getAllOptions(false);
158176
$this->prepareOptionLinks($optionsArray, $attributeSavedOptions);
159177
}
160178

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)