Skip to content

Commit 10ac7a5

Browse files
committed
MAGETWO-50123: Unable to assign blank value to attribute #3545 #4910 #5485
- adding unit test
1 parent 2808b89 commit 10ac7a5

File tree

2 files changed

+207
-1
lines changed
  • app/code/Magento/Catalog
    • Test/Unit/Ui/DataProvider/Product/Form/Modifier
    • Ui/DataProvider/Product/Form/Modifier

2 files changed

+207
-1
lines changed

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

Lines changed: 206 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav;
1010
use Magento\Eav\Model\Config;
1111
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\EntityManager\EventManager;
1213
use Magento\Store\Model\StoreManagerInterface;
1314
use Magento\Store\Api\Data\StoreInterface;
1415
use Magento\Ui\DataProvider\EavValidationRules;
@@ -27,11 +28,15 @@
2728
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
2829
use Magento\Framework\Api\SearchResultsInterface;
2930
use Magento\Catalog\Api\Data\ProductAttributeInterface;
31+
use Magento\Framework\Api\AttributeInterface;
3032
use Magento\Eav\Api\Data\AttributeGroupInterface;
3133
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
3234
use Magento\Framework\Currency;
3335
use Magento\Framework\Locale\Currency as CurrencyLocale;
3436
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
37+
use Magento\Framework\Stdlib\ArrayManager;
38+
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory as EavAttributeFactory;
39+
use Magento\Framework\Event\ManagerInterface;
3540

3641
/**
3742
* Class EavTest
@@ -157,6 +162,27 @@ class EavTest extends AbstractModifierTest
157162
*/
158163
protected $currencyLocaleMock;
159164

165+
/**
166+
* @var ProductAttributeInterface|\PHPUnit_Framework_MockObject_MockObject
167+
*/
168+
protected $productAttributeMock;
169+
170+
/**
171+
* @var ArrayManager|\PHPUnit_Framework_MockObject_MockObject
172+
*/
173+
protected $arrayManagerMock;
174+
175+
/**
176+
* @var EavAttributeFactory|\PHPUnit_Framework_MockObject_MockObject
177+
*/
178+
protected $eavAttributeFactoryMock;
179+
180+
/**
181+
* @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
182+
*/
183+
protected $eventManagerMock;
184+
185+
160186
/**
161187
* @var ObjectManager
162188
*/
@@ -228,10 +254,24 @@ protected function setUp()
228254
$this->searchResultsMock = $this->getMockBuilder(SearchResultsInterface::class)
229255
->getMockForAbstractClass();
230256
$this->eavAttributeMock = $this->getMockBuilder(Attribute::class)
231-
->setMethods(['getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])
257+
->setMethods(['load', 'getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])
258+
->disableOriginalConstructor()
259+
->getMock();
260+
$this->productAttributeMock = $this->getMockBuilder(ProductAttributeInterface::class)
261+
->getMock();
262+
$this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
263+
->getMock();
264+
$this->eavAttributeFactoryMock = $this->getMockBuilder(EavAttributeFactory::class)
265+
->disableOriginalConstructor()
266+
->setMethods(['create'])
267+
->getMock();
268+
$this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)
232269
->disableOriginalConstructor()
233270
->getMock();
234271

272+
$this->eavAttributeFactoryMock->expects($this->any())
273+
->method('create')
274+
->willReturn($this->eavAttributeMock);
235275
$this->groupCollectionFactoryMock->expects($this->any())
236276
->method('create')
237277
->willReturn($this->groupCollectionMock);
@@ -277,6 +317,9 @@ protected function setUp()
277317
->disableOriginalConstructor()
278318
->setMethods(['getCurrency'])
279319
->getMock();
320+
$this->eavAttributeMock->expects($this->any())
321+
->method('load')
322+
->willReturnSelf();
280323

281324
$this->eav =$this->getModel();
282325
$this->objectManager->setBackwardCompatibleProperty(
@@ -304,6 +347,9 @@ protected function createModel()
304347
'attributeGroupRepository' => $this->attributeGroupRepositoryMock,
305348
'sortOrderBuilder' => $this->sortOrderBuilderMock,
306349
'attributeRepository' => $this->attributeRepositoryMock,
350+
'arrayManager' => $this->arrayManagerMock,
351+
'eavAttributeFactory' => $this->eavAttributeFactoryMock,
352+
'_eventManager' => $this->eventManagerMock
307353
]);
308354
}
309355

@@ -399,4 +445,163 @@ public function testModifyData()
399445

400446
$this->assertEquals($sourceData, $this->eav->modifyData([]));
401447
}
448+
449+
/**
450+
* @param int $productId
451+
* @param bool $productRequired
452+
* @param string $attrValue
453+
* @param array $expected
454+
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier::isProductNew
455+
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier::isProductHasValueForAttribute
456+
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier::isShowDefaultValue
457+
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier::setupAttributeMeta
458+
* @dataProvider setupAttributeMetaDataProvider
459+
*/
460+
public function testSetupAttributeMetaDefaultAttribute($productId, $productRequired, $attrValue, $expected)
461+
{
462+
$configPath = 'arguments/data/config';
463+
$groupCode = 'product-details';
464+
$sortOrder = '0';
465+
466+
$this->productMock->expects($this->any())
467+
->method('getId')
468+
->willReturn($productId);
469+
$this->productAttributeMock->expects($this->any())
470+
->method('getIsRequired')
471+
->willReturn($productRequired);
472+
473+
$this->productAttributeMock->expects($this->any())
474+
->method('getDefaultValue')
475+
->willReturn($productRequired? 'required_value' : null);
476+
477+
$this->productAttributeMock->expects($this->any())
478+
->method('getAttributeCode')
479+
->willReturn('code');
480+
481+
$this->productAttributeMock->expects($this->any())
482+
->method('getValue')
483+
->willReturn('value');
484+
485+
$attributeMock = $this->getMockBuilder(AttributeInterface::class)
486+
->disableOriginalConstructor()
487+
->getMock();
488+
489+
$attributeMock->expects($this->any())
490+
->method('getValue')
491+
->willReturn($attrValue);
492+
493+
$this->productMock->expects($this->any())
494+
->method('getCustomAttribute')
495+
->willReturn($attributeMock);
496+
497+
$this->arrayManagerMock->expects($this->any())
498+
->method('set')
499+
->with(
500+
$configPath,
501+
[],
502+
$expected
503+
)
504+
->willReturn($expected);
505+
506+
$this->arrayManagerMock->expects($this->any())
507+
->method('merge')
508+
->willReturn($expected);
509+
510+
$this->arrayManagerMock->expects($this->any())
511+
->method('get')
512+
->willReturn([]);
513+
514+
$this->arrayManagerMock->expects($this->any())
515+
->method('exists');
516+
517+
$this->assertEquals(
518+
$expected,
519+
$this->eav->setupAttributeMeta($this->productAttributeMock, $groupCode, $sortOrder)
520+
);
521+
}
522+
523+
/**
524+
* @return array
525+
*/
526+
public function setupAttributeMetaDataProvider()
527+
{
528+
return [
529+
'default_null_prod_not_new_and_required' => [
530+
'productId' => 1,
531+
'productRequired' => true,
532+
'attrValue' => null,
533+
'expected' => [
534+
'dataType' => null,
535+
'formElement' => null,
536+
'visible' => null,
537+
'required' => true,
538+
'notice' => null,
539+
'default' => 'required_value',
540+
'label' => null,
541+
'code' => 'code',
542+
'source' => 'product-details',
543+
'scopeLabel' => '',
544+
'globalScope' => false,
545+
'sortOrder' => 0
546+
],
547+
],
548+
'default_null_prod_not_new_and_required_with_value' => [
549+
'productId' => 1,
550+
'productRequired' => true,
551+
'attrValue' => 'val',
552+
'expected' => [
553+
'dataType' => null,
554+
'formElement' => null,
555+
'visible' => null,
556+
'required' => true,
557+
'notice' => null,
558+
'default' => null,
559+
'label' => null,
560+
'code' => 'code',
561+
'source' => 'product-details',
562+
'scopeLabel' => '',
563+
'globalScope' => false,
564+
'sortOrder' => 0
565+
],
566+
],
567+
'default_null_prod_not_new_and_not_required_no_value' => [
568+
'productId' => 1,
569+
'productRequired' => false,
570+
'attrValue' => null,
571+
'expected' => [
572+
'dataType' => null,
573+
'formElement' => null,
574+
'visible' => null,
575+
'required' => false,
576+
'notice' => null,
577+
'default' => null,
578+
'label' => null,
579+
'code' => 'code',
580+
'source' => 'product-details',
581+
'scopeLabel' => '',
582+
'globalScope' => false,
583+
'sortOrder' => 0
584+
],
585+
],
586+
'default_null_prod_NEW_no_value' => [
587+
'productId' => null,
588+
'productRequired' => true,
589+
'attrValue' => null,
590+
'expected' => [
591+
'dataType' => null,
592+
'formElement' => null,
593+
'visible' => null,
594+
'required' => true,
595+
'notice' => null,
596+
'default' => 'required_value',
597+
'label' => null,
598+
'code' => 'code',
599+
'source' => 'product-details',
600+
'scopeLabel' => '',
601+
'globalScope' => false,
602+
'sortOrder' => 0
603+
],
604+
],
605+
];
606+
}
402607
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ private function isShowDefaultValue(ProductAttributeInterface $attribute)
571571
* @return array
572572
* @throws \Magento\Framework\Exception\LocalizedException
573573
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
574+
* @SuppressWarnings(PHPMD.NPathComplexity)
574575
* @api
575576
*/
576577
public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupCode, $sortOrder)

0 commit comments

Comments
 (0)