Skip to content

Commit f8be412

Browse files
committed
Merge remote-tracking branch 'troll/MAGETWO-56487' into PRS
2 parents f59f172 + 81f34a8 commit f8be412

File tree

6 files changed

+121
-32
lines changed

6 files changed

+121
-32
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Validate.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,13 @@ public function execute()
104104
$multipleOption = null == $multipleOption ? 'select' : $multipleOption;
105105

106106
if (isset($this->multipleAttributeList[$multipleOption]) && !(null == ($multipleOption))) {
107+
$options = $this->getRequest()->getParam($this->multipleAttributeList[$multipleOption]);
107108
$this->checkUniqueOption(
108109
$response,
109-
$this->getRequest()->getParam($this->multipleAttributeList[$multipleOption])
110+
$options
110111
);
112+
$valueOptions = (isset($options['value']) && is_array($options['value'])) ? $options['value'] : [];
113+
$this->checkEmptyOption($response, $valueOptions);
111114
}
112115

113116
return $this->resultJsonFactory->create()->setJsonData($response->toJson());
@@ -155,13 +158,30 @@ private function setMessageToResponse($response, $messages)
155158
private function checkUniqueOption(DataObject $response, array $options = null)
156159
{
157160
if (is_array($options)
158-
&& !empty($options['value'])
159-
&& !empty($options['delete'])
161+
&& isset($options['value'])
162+
&& isset($options['delete'])
160163
&& !$this->isUniqueAdminValues($options['value'], $options['delete'])
161164
) {
162165
$this->setMessageToResponse($response, [__("The value of Admin must be unique.")]);
163166
$response->setError(true);
164167
}
165168
return $this;
166169
}
170+
171+
/**
172+
* Check that admin does not try to create option with empty admin scope option.
173+
*
174+
* @param DataObject $response
175+
* @param array $optionsForCheck
176+
* @return void
177+
*/
178+
private function checkEmptyOption(DataObject $response, array $optionsForCheck = null)
179+
{
180+
foreach ($optionsForCheck as $optionValues) {
181+
if (isset($optionValues[0]) && $optionValues[0] == '') {
182+
$this->setMessageToResponse($response, [__("The value of Admin scope can't be empty.")]);
183+
$response->setError(true);
184+
}
185+
}
186+
}
167187
}

app/code/Magento/Catalog/Model/Product/Attribute/DataProvider.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
*/
66
namespace Magento\Catalog\Model\Product\Attribute;
77

8-
use Magento\Catalog\Model\Category;
98
use Magento\Framework\Stdlib\ArrayManager;
109
use Magento\Store\Api\StoreRepositoryInterface;
1110
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
1211
use Magento\Eav\Model\Entity\Attribute as EavAttribute;
13-
use Magento\Ui\Component\Form;
1412
use Magento\Ui\Component\Form\Element\Input;
1513
use Magento\Ui\Component\Form\Field;
1614
use Magento\Ui\Component\Form\Element\DataType\Text;
15+
use Magento\Store\Model\Store;
1716

1817
/**
1918
* Data provider for the form of adding new product attribute.
@@ -149,40 +148,36 @@ private function customizeOptions($meta)
149148
$sortOrder = 1;
150149
foreach ($this->storeRepository->getList() as $store) {
151150
$storeId = $store->getId();
152-
151+
$storeLabelConfiguration = [
152+
'dataType' => 'text',
153+
'formElement' => 'input',
154+
'component' => 'Magento_Catalog/js/form/element/input',
155+
'template' => 'Magento_Catalog/form/element/input',
156+
'prefixName' => 'option.value',
157+
'prefixElementName' => 'option_',
158+
'suffixName' => (string)$storeId,
159+
'label' => $store->getName(),
160+
'sortOrder' => $sortOrder,
161+
'componentType' => Field::NAME,
162+
];
163+
// JS code can't understand 'required-entry' => false|null, we have to avoid even empty property.
164+
if ($store->getCode() == Store::ADMIN_CODE) {
165+
$storeLabelConfiguration['validation'] = [
166+
'required-entry' => true,
167+
];
168+
}
153169
$meta['attribute_options_select_container']['children']['attribute_options_select']['children']
154170
['record']['children']['value_option_' . $storeId] = $this->arrayManager->set(
155171
'arguments/data/config',
156172
[],
157-
[
158-
'dataType' => 'text',
159-
'formElement' => 'input',
160-
'component' => 'Magento_Catalog/js/form/element/input',
161-
'template' => 'Magento_Catalog/form/element/input',
162-
'prefixName' => 'option.value',
163-
'prefixElementName' => 'option_',
164-
'suffixName' => (string)$storeId,
165-
'label' => $store->getName(),
166-
'sortOrder' => $sortOrder,
167-
'componentType' => Field::NAME,
168-
]
173+
$storeLabelConfiguration
169174
);
175+
170176
$meta['attribute_options_multiselect_container']['children']['attribute_options_multiselect']['children']
171177
['record']['children']['value_option_' . $storeId] = $this->arrayManager->set(
172178
'arguments/data/config',
173179
[],
174-
[
175-
'dataType' => 'text',
176-
'formElement' => 'input',
177-
'component' => 'Magento_Catalog/js/form/element/input',
178-
'template' => 'Magento_Catalog/form/element/input',
179-
'prefixName' => 'option.value',
180-
'prefixElementName' => 'option_',
181-
'suffixName' => (string)$storeId,
182-
'label' => $store->getName(),
183-
'sortOrder' => $sortOrder,
184-
'componentType' => Field::NAME,
185-
]
180+
$storeLabelConfiguration
186181
);
187182
++$sortOrder;
188183
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Attribute/ValidateTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,77 @@ public function provideUniqueData()
251251
],
252252
];
253253
}
254+
255+
/**
256+
* Check that empty admin scope labels will trigger error.
257+
*
258+
* @dataProvider provideEmptyOption
259+
* @param array $options
260+
* @throws \Magento\Framework\Exception\NotFoundException
261+
*/
262+
public function testEmptyOption(array $options, $result)
263+
{
264+
$this->requestMock->expects($this->any())
265+
->method('getParam')
266+
->willReturnMap([
267+
['frontend_label', null, null],
268+
['frontend_input', 'select', 'multipleselect'],
269+
['attribute_code', null, "test_attribute_code"],
270+
['new_attribute_set_name', null, 'test_attribute_set_name'],
271+
['option', null, $options],
272+
['message_key', Validate::DEFAULT_MESSAGE_KEY, 'message'],
273+
]);
274+
275+
$this->objectManagerMock->expects($this->once())
276+
->method('create')
277+
->willReturn($this->attributeMock);
278+
279+
$this->attributeMock->expects($this->once())
280+
->method('loadByCode')
281+
->willReturnSelf();
282+
283+
$this->resultJsonFactoryMock->expects($this->once())
284+
->method('create')
285+
->willReturn($this->resultJson);
286+
287+
$this->resultJson->expects($this->once())
288+
->method('setJsonData')
289+
->willReturnArgument(0);
290+
291+
$response = $this->getModel()->execute();
292+
$responseObject = json_decode($response);
293+
$this->assertEquals($responseObject, $result);
294+
}
295+
296+
/**
297+
* Dataprovider for testEmptyOption.
298+
*
299+
* @return array
300+
*/
301+
public function provideEmptyOption()
302+
{
303+
return [
304+
'empty admin scope options' => [
305+
[
306+
'value' => [
307+
"option_0" => [''],
308+
],
309+
],
310+
(object) [
311+
'error' => true,
312+
'message' => 'The value of Admin scope can\'t be empty.',
313+
]
314+
],
315+
'not empty admin scope options' => [
316+
[
317+
'value' => [
318+
"option_0" => ['asdads'],
319+
],
320+
],
321+
(object) [
322+
'error' => false,
323+
]
324+
]
325+
];
326+
}
254327
}

app/code/Magento/Catalog/etc/adminhtml/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<arguments>
5252
<argument name="multipleAttributeList" xsi:type="array">
5353
<item name="select" xsi:type="string">option</item>
54+
<item name="multiselect" xsi:type="string">option</item>
5455
</argument>
5556
</arguments>
5657
</type>

app/code/Magento/Catalog/view/adminhtml/web/template/form/element/input.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
id: uid,
1818
disabled: disabled
1919
}"/>
20+
<label class="admin__field-error" if="error" attr="for: uid" text="error"/>

app/code/Magento/Ui/view/base/web/templates/dynamic-rows/templates/default.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
<table class="admin__dynamic-rows admin__control-table" data-role="grid" attr="{'data-index': index}">
2323
<thead if="element.columnsHeader">
2424
<tr>
25-
<th if="dndConfig.enabled"/>
25+
<th if="dndConfig.enabled" />
2626
<th repeat="foreach: labels, item: '$label'"
27-
text="$label().label"
2827
css="setClasses($label())"
2928
visible="$label().visible"
3029
disable="$label().disabled">

0 commit comments

Comments
 (0)