|
9 | 9 | use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav;
|
10 | 10 | use Magento\Eav\Model\Config;
|
11 | 11 | use Magento\Framework\App\RequestInterface;
|
| 12 | +use Magento\Framework\EntityManager\EventManager; |
12 | 13 | use Magento\Store\Model\StoreManagerInterface;
|
13 | 14 | use Magento\Store\Api\Data\StoreInterface;
|
14 | 15 | use Magento\Ui\DataProvider\EavValidationRules;
|
|
27 | 28 | use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
28 | 29 | use Magento\Framework\Api\SearchResultsInterface;
|
29 | 30 | use Magento\Catalog\Api\Data\ProductAttributeInterface;
|
| 31 | +use Magento\Framework\Api\AttributeInterface; |
30 | 32 | use Magento\Eav\Api\Data\AttributeGroupInterface;
|
31 | 33 | use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
|
32 | 34 | use Magento\Framework\Currency;
|
33 | 35 | use Magento\Framework\Locale\Currency as CurrencyLocale;
|
34 | 36 | 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; |
35 | 40 |
|
36 | 41 | /**
|
37 | 42 | * Class EavTest
|
@@ -157,6 +162,27 @@ class EavTest extends AbstractModifierTest
|
157 | 162 | */
|
158 | 163 | protected $currencyLocaleMock;
|
159 | 164 |
|
| 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 | + |
160 | 186 | /**
|
161 | 187 | * @var ObjectManager
|
162 | 188 | */
|
@@ -228,10 +254,24 @@ protected function setUp()
|
228 | 254 | $this->searchResultsMock = $this->getMockBuilder(SearchResultsInterface::class)
|
229 | 255 | ->getMockForAbstractClass();
|
230 | 256 | $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) |
232 | 269 | ->disableOriginalConstructor()
|
233 | 270 | ->getMock();
|
234 | 271 |
|
| 272 | + $this->eavAttributeFactoryMock->expects($this->any()) |
| 273 | + ->method('create') |
| 274 | + ->willReturn($this->eavAttributeMock); |
235 | 275 | $this->groupCollectionFactoryMock->expects($this->any())
|
236 | 276 | ->method('create')
|
237 | 277 | ->willReturn($this->groupCollectionMock);
|
@@ -277,6 +317,9 @@ protected function setUp()
|
277 | 317 | ->disableOriginalConstructor()
|
278 | 318 | ->setMethods(['getCurrency'])
|
279 | 319 | ->getMock();
|
| 320 | + $this->eavAttributeMock->expects($this->any()) |
| 321 | + ->method('load') |
| 322 | + ->willReturnSelf(); |
280 | 323 |
|
281 | 324 | $this->eav =$this->getModel();
|
282 | 325 | $this->objectManager->setBackwardCompatibleProperty(
|
@@ -304,6 +347,9 @@ protected function createModel()
|
304 | 347 | 'attributeGroupRepository' => $this->attributeGroupRepositoryMock,
|
305 | 348 | 'sortOrderBuilder' => $this->sortOrderBuilderMock,
|
306 | 349 | 'attributeRepository' => $this->attributeRepositoryMock,
|
| 350 | + 'arrayManager' => $this->arrayManagerMock, |
| 351 | + 'eavAttributeFactory' => $this->eavAttributeFactoryMock, |
| 352 | + '_eventManager' => $this->eventManagerMock |
307 | 353 | ]);
|
308 | 354 | }
|
309 | 355 |
|
@@ -399,4 +445,163 @@ public function testModifyData()
|
399 | 445 |
|
400 | 446 | $this->assertEquals($sourceData, $this->eav->modifyData([]));
|
401 | 447 | }
|
| 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 | + } |
402 | 607 | }
|
0 commit comments