Skip to content

Commit eb5422a

Browse files
committed
MAGETWO-50123: Unable to assign blank value to attribute #3545 #4910 #5485 #5225
- modifying logic and unit test according to resolved logic agreed, only new products should select default value
1 parent 10ac7a5 commit eb5422a

File tree

3 files changed

+40
-53
lines changed
  • app/code/Magento
    • Catalog
      • Test/Unit/Ui/DataProvider/Product/Form/Modifier
      • Ui/DataProvider/Product/Form/Modifier
    • Ui/view/base/web/js/form/element

3 files changed

+40
-53
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/EavTest.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ class EavTest extends AbstractModifierTest
182182
*/
183183
protected $eventManagerMock;
184184

185-
186185
/**
187186
* @var ObjectManager
188187
*/
@@ -193,6 +192,9 @@ class EavTest extends AbstractModifierTest
193192
*/
194193
protected $eav;
195194

195+
/**
196+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
197+
*/
196198
protected function setUp()
197199
{
198200
parent::setUp();
@@ -466,13 +468,14 @@ public function testSetupAttributeMetaDefaultAttribute($productId, $productRequi
466468
$this->productMock->expects($this->any())
467469
->method('getId')
468470
->willReturn($productId);
471+
469472
$this->productAttributeMock->expects($this->any())
470473
->method('getIsRequired')
471474
->willReturn($productRequired);
472475

473476
$this->productAttributeMock->expects($this->any())
474477
->method('getDefaultValue')
475-
->willReturn($productRequired? 'required_value' : null);
478+
->willReturn('required_value');
476479

477480
$this->productAttributeMock->expects($this->any())
478481
->method('getAttributeCode')
@@ -529,14 +532,14 @@ public function setupAttributeMetaDataProvider()
529532
'default_null_prod_not_new_and_required' => [
530533
'productId' => 1,
531534
'productRequired' => true,
532-
'attrValue' => null,
535+
'attrValue' => 'val',
533536
'expected' => [
534537
'dataType' => null,
535538
'formElement' => null,
536539
'visible' => null,
537540
'required' => true,
538541
'notice' => null,
539-
'default' => 'required_value',
542+
'default' => null,
540543
'label' => null,
541544
'code' => 'code',
542545
'source' => 'product-details',
@@ -545,15 +548,15 @@ public function setupAttributeMetaDataProvider()
545548
'sortOrder' => 0
546549
],
547550
],
548-
'default_null_prod_not_new_and_required_with_value' => [
551+
'default_null_prod_not_new_and_not_required' => [
549552
'productId' => 1,
550-
'productRequired' => true,
553+
'productRequired' => false,
551554
'attrValue' => 'val',
552555
'expected' => [
553556
'dataType' => null,
554557
'formElement' => null,
555558
'visible' => null,
556-
'required' => true,
559+
'required' => false,
557560
'notice' => null,
558561
'default' => null,
559562
'label' => null,
@@ -562,10 +565,10 @@ public function setupAttributeMetaDataProvider()
562565
'scopeLabel' => '',
563566
'globalScope' => false,
564567
'sortOrder' => 0
568+
],
565569
],
566-
],
567-
'default_null_prod_not_new_and_not_required_no_value' => [
568-
'productId' => 1,
570+
'default_null_prod_new_and_not_required' => [
571+
'productId' => null,
569572
'productRequired' => false,
570573
'attrValue' => null,
571574
'expected' => [
@@ -574,7 +577,7 @@ public function setupAttributeMetaDataProvider()
574577
'visible' => null,
575578
'required' => false,
576579
'notice' => null,
577-
'default' => null,
580+
'default' => 'required_value',
578581
'label' => null,
579582
'code' => 'code',
580583
'source' => 'product-details',
@@ -583,15 +586,15 @@ public function setupAttributeMetaDataProvider()
583586
'sortOrder' => 0
584587
],
585588
],
586-
'default_null_prod_NEW_no_value' => [
589+
'default_null_prod_new_and_required' => [
587590
'productId' => null,
588-
'productRequired' => true,
591+
'productRequired' => false,
589592
'attrValue' => null,
590593
'expected' => [
591594
'dataType' => null,
592595
'formElement' => null,
593596
'visible' => null,
594-
'required' => true,
597+
'required' => false,
595598
'notice' => null,
596599
'default' => 'required_value',
597600
'label' => null,
@@ -601,7 +604,7 @@ public function setupAttributeMetaDataProvider()
601604
'globalScope' => false,
602605
'sortOrder' => 0
603606
],
604-
],
607+
]
605608
];
606609
}
607610
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -527,41 +527,11 @@ private function getPreviousSetAttributes()
527527
*
528528
* @return bool
529529
*/
530-
private function isProductNew()
530+
private function isProductExists()
531531
{
532532
return (bool) $this->locator->getProduct()->getId();
533533
}
534534

535-
/**
536-
* Check is product has some value for attribute
537-
*
538-
* @param ProductAttributeInterface $attribute
539-
* @return bool
540-
*/
541-
private function isProductHasValueForAttribute(ProductAttributeInterface $attribute)
542-
{
543-
/** @var \Magento\Framework\Api\AttributeInterface $attributeCode */
544-
$attributeCode = $this->locator->getProduct()->getCustomAttribute($attribute->getAttributeCode());
545-
return (bool)($attributeCode !== null) && $attributeCode->getValue();
546-
}
547-
548-
/**
549-
* Check should we display default values for attribute or not
550-
*
551-
* @param ProductAttributeInterface $attribute
552-
* @return bool
553-
*/
554-
private function isShowDefaultValue(ProductAttributeInterface $attribute)
555-
{
556-
if (!$this->isProductNew()) {
557-
return true;
558-
} elseif ($attribute->getIsRequired() && !$this->isProductHasValueForAttribute($attribute)) {
559-
return true;
560-
}
561-
562-
return false;
563-
}
564-
565535
/**
566536
* Initial meta setup
567537
*
@@ -584,7 +554,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
584554
'visible' => $attribute->getIsVisible(),
585555
'required' => $attribute->getIsRequired(),
586556
'notice' => $attribute->getNote(),
587-
'default' => $this->isShowDefaultValue($attribute) ? $attribute->getDefaultValue() : null,
557+
'default' => (!$this->isProductExists()) ? $attribute->getDefaultValue() : null,
588558
'label' => $attribute->getDefaultFrontendLabel(),
589559
'code' => $attribute->getAttributeCode(),
590560
'source' => $groupCode,

app/code/Magento/Ui/view/base/web/js/form/element/select.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ define([
4444
if (_.isUndefined(caption)) {
4545
caption = node.label;
4646
}
47-
} else {
48-
return node;
4947
}
48+
49+
return node;
5050
});
5151

5252
return {
@@ -163,10 +163,6 @@ define([
163163
this.observe('options')
164164
.setOptions(this.options());
165165

166-
if (_.isUndefined(this.value()) && !this.default) {
167-
this.clear();
168-
}
169-
170166
return this;
171167
},
172168

@@ -290,6 +286,11 @@ define([
290286
return preview;
291287
},
292288

289+
/**
290+
*
291+
* @param {Number} value
292+
* @returns {Object} Chainable
293+
*/
293294
getOption: function (value) {
294295
return this.indexedOptions[value];
295296
},
@@ -305,6 +306,19 @@ define([
305306
this.value(value);
306307

307308
return this;
309+
},
310+
311+
/**
312+
* Initializes observable properties of instance
313+
*
314+
* @returns {Object} Chainable.
315+
*/
316+
setInitialValue: function () {
317+
if (_.isUndefined(this.value()) && !this.default) {
318+
this.clear();
319+
}
320+
321+
return this._super();
308322
}
309323
});
310324
});

0 commit comments

Comments
 (0)