5
5
*/
6
6
namespace Magento \Catalog \Test \Unit \Ui \DataProvider \Product \Form \Modifier ;
7
7
8
- use Magento \Catalog \Model \Product \Type ;
8
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
9
+ use Magento \Catalog \Api \ProductAttributeGroupRepositoryInterface ;
10
+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
11
+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
12
+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute as EavAttribute ;
13
+ use Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory as EavAttributeFactory ;
9
14
use Magento \Catalog \Ui \DataProvider \Product \Form \Modifier \Eav ;
15
+ use Magento \Eav \Api \Data \AttributeGroupInterface ;
10
16
use Magento \Eav \Model \Config ;
11
- use Magento \Framework \App \RequestInterface ;
12
- use Magento \Store \Model \StoreManagerInterface ;
13
- use Magento \Store \Api \Data \StoreInterface ;
14
- use Magento \Ui \DataProvider \EavValidationRules ;
15
- use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \Collection as GroupCollection ;
16
- use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \CollectionFactory as GroupCollectionFactory ;
17
17
use Magento \Eav \Model \Entity \Attribute \Group ;
18
- use Magento \Catalog \Model \ResourceModel \Eav \Attribute as EavAttribute ;
19
18
use Magento \Eav \Model \Entity \Type as EntityType ;
20
19
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Collection as AttributeCollection ;
21
- use Magento \Ui \DataProvider \Mapper \FormElement as FormElementMapper ;
22
- use Magento \Ui \DataProvider \Mapper \MetaProperties as MetaPropertiesMapper ;
23
- use Magento \Framework \Api \SearchCriteriaBuilder ;
24
- use Magento \Catalog \Api \ProductAttributeGroupRepositoryInterface ;
20
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \Collection as GroupCollection ;
21
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Group \CollectionFactory as GroupCollectionFactory ;
22
+ use Magento \Framework \Api \AttributeInterface ;
25
23
use Magento \Framework \Api \SearchCriteria ;
26
- use Magento \Framework \Api \SortOrderBuilder ;
27
- use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
24
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
28
25
use Magento \Framework \Api \SearchResultsInterface ;
29
- use Magento \Catalog \Api \Data \ProductAttributeInterface ;
30
- use Magento \Eav \Api \Data \AttributeGroupInterface ;
31
- use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
26
+ use Magento \Framework \Api \SortOrderBuilder ;
27
+ use Magento \Framework \App \RequestInterface ;
32
28
use Magento \Framework \Currency ;
29
+ use Magento \Framework \Event \ManagerInterface ;
33
30
use Magento \Framework \Locale \Currency as CurrencyLocale ;
34
- Use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
31
+ use Magento \Framework \Stdlib \ArrayManager ;
32
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
33
+ use Magento \Store \Api \Data \StoreInterface ;
34
+ use Magento \Store \Model \StoreManagerInterface ;
35
+ use Magento \Ui \DataProvider \EavValidationRules ;
36
+ use Magento \Ui \DataProvider \Mapper \FormElement as FormElementMapper ;
37
+ use Magento \Ui \DataProvider \Mapper \MetaProperties as MetaPropertiesMapper ;
35
38
36
39
/**
37
40
* Class EavTest
@@ -157,6 +160,26 @@ class EavTest extends AbstractModifierTest
157
160
*/
158
161
protected $ currencyLocaleMock ;
159
162
163
+ /**
164
+ * @var ProductAttributeInterface|\PHPUnit_Framework_MockObject_MockObject
165
+ */
166
+ private $ productAttributeMock ;
167
+
168
+ /**
169
+ * @var ArrayManager|\PHPUnit_Framework_MockObject_MockObject
170
+ */
171
+ protected $ arrayManagerMock ;
172
+
173
+ /**
174
+ * @var EavAttributeFactory|\PHPUnit_Framework_MockObject_MockObject
175
+ */
176
+ private $ eavAttributeFactoryMock ;
177
+
178
+ /**
179
+ * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
180
+ */
181
+ private $ eventManagerMock ;
182
+
160
183
/**
161
184
* @var ObjectManager
162
185
*/
@@ -167,6 +190,9 @@ class EavTest extends AbstractModifierTest
167
190
*/
168
191
protected $ eav ;
169
192
193
+ /**
194
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
195
+ */
170
196
protected function setUp ()
171
197
{
172
198
parent ::setUp ();
@@ -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,7 +317,10 @@ protected function setUp()
277
317
->disableOriginalConstructor ()
278
318
->setMethods (['getCurrency ' ])
279
319
->getMock ();
280
-
320
+ $ this ->eavAttributeMock ->expects ($ this ->any ())
321
+ ->method ('load ' )
322
+ ->willReturnSelf ();
323
+
281
324
$ this ->eav =$ this ->getModel ();
282
325
$ this ->objectManager ->setBackwardCompatibleProperty (
283
326
$ this ->eav ,
@@ -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,164 @@ 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\Eav::isProductExists
455
+ * @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::setupAttributeMeta
456
+ * @dataProvider setupAttributeMetaDataProvider
457
+ */
458
+ public function testSetupAttributeMetaDefaultAttribute ($ productId , $ productRequired , $ attrValue , $ expected )
459
+ {
460
+ $ configPath = 'arguments/data/config ' ;
461
+ $ groupCode = 'product-details ' ;
462
+ $ sortOrder = '0 ' ;
463
+
464
+ $ this ->productMock ->expects ($ this ->any ())
465
+ ->method ('getId ' )
466
+ ->willReturn ($ productId );
467
+
468
+ $ this ->productAttributeMock ->expects ($ this ->any ())
469
+ ->method ('getIsRequired ' )
470
+ ->willReturn ($ productRequired );
471
+
472
+ $ this ->productAttributeMock ->expects ($ this ->any ())
473
+ ->method ('getDefaultValue ' )
474
+ ->willReturn ('required_value ' );
475
+
476
+ $ this ->productAttributeMock ->expects ($ this ->any ())
477
+ ->method ('getAttributeCode ' )
478
+ ->willReturn ('code ' );
479
+
480
+ $ this ->productAttributeMock ->expects ($ this ->any ())
481
+ ->method ('getValue ' )
482
+ ->willReturn ('value ' );
483
+
484
+ $ attributeMock = $ this ->getMockBuilder (AttributeInterface::class)
485
+ ->disableOriginalConstructor ()
486
+ ->getMock ();
487
+
488
+ $ attributeMock ->expects ($ this ->any ())
489
+ ->method ('getValue ' )
490
+ ->willReturn ($ attrValue );
491
+
492
+ $ this ->productMock ->expects ($ this ->any ())
493
+ ->method ('getCustomAttribute ' )
494
+ ->willReturn ($ attributeMock );
495
+
496
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
497
+ ->method ('set ' )
498
+ ->with (
499
+ $ configPath ,
500
+ [],
501
+ $ expected
502
+ )
503
+ ->willReturn ($ expected );
504
+
505
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
506
+ ->method ('merge ' )
507
+ ->willReturn ($ expected );
508
+
509
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
510
+ ->method ('get ' )
511
+ ->willReturn ([]);
512
+
513
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
514
+ ->method ('exists ' );
515
+
516
+ $ this ->assertEquals (
517
+ $ expected ,
518
+ $ this ->eav ->setupAttributeMeta ($ this ->productAttributeMock , $ groupCode , $ sortOrder )
519
+ );
520
+ }
521
+
522
+ /**
523
+ * Setup attribute meta data provider.
524
+ *
525
+ * @return array
526
+ */
527
+ public function setupAttributeMetaDataProvider ()
528
+ {
529
+ return [
530
+ 'default_null_prod_not_new_and_required ' => [
531
+ 'productId ' => 1 ,
532
+ 'productRequired ' => true ,
533
+ 'attrValue ' => 'val ' ,
534
+ 'expected ' => [
535
+ 'dataType ' => null ,
536
+ 'formElement ' => null ,
537
+ 'visible ' => null ,
538
+ 'required ' => true ,
539
+ 'notice ' => null ,
540
+ 'default ' => null ,
541
+ 'label ' => null ,
542
+ 'code ' => 'code ' ,
543
+ 'source ' => 'product-details ' ,
544
+ 'scopeLabel ' => '' ,
545
+ 'globalScope ' => false ,
546
+ 'sortOrder ' => 0 ,
547
+ ],
548
+ ],
549
+ 'default_null_prod_not_new_and_not_required ' => [
550
+ 'productId ' => 1 ,
551
+ 'productRequired ' => false ,
552
+ 'attrValue ' => 'val ' ,
553
+ 'expected ' => [
554
+ 'dataType ' => null ,
555
+ 'formElement ' => null ,
556
+ 'visible ' => null ,
557
+ 'required ' => false ,
558
+ 'notice ' => null ,
559
+ 'default ' => null ,
560
+ 'label ' => null ,
561
+ 'code ' => 'code ' ,
562
+ 'source ' => 'product-details ' ,
563
+ 'scopeLabel ' => '' ,
564
+ 'globalScope ' => false ,
565
+ 'sortOrder ' => 0 ,
566
+ ],
567
+ ],
568
+ 'default_null_prod_new_and_not_required ' => [
569
+ 'productId ' => null ,
570
+ 'productRequired ' => false ,
571
+ 'attrValue ' => null ,
572
+ 'expected ' => [
573
+ 'dataType ' => null ,
574
+ 'formElement ' => null ,
575
+ 'visible ' => null ,
576
+ 'required ' => false ,
577
+ 'notice ' => null ,
578
+ 'default ' => 'required_value ' ,
579
+ 'label ' => null ,
580
+ 'code ' => 'code ' ,
581
+ 'source ' => 'product-details ' ,
582
+ 'scopeLabel ' => '' ,
583
+ 'globalScope ' => false ,
584
+ 'sortOrder ' => 0 ,
585
+ ],
586
+ ],
587
+ 'default_null_prod_new_and_required ' => [
588
+ 'productId ' => null ,
589
+ 'productRequired ' => false ,
590
+ 'attrValue ' => null ,
591
+ 'expected ' => [
592
+ 'dataType ' => null ,
593
+ 'formElement ' => null ,
594
+ 'visible ' => null ,
595
+ 'required ' => false ,
596
+ 'notice ' => null ,
597
+ 'default ' => 'required_value ' ,
598
+ 'label ' => null ,
599
+ 'code ' => 'code ' ,
600
+ 'source ' => 'product-details ' ,
601
+ 'scopeLabel ' => '' ,
602
+ 'globalScope ' => false ,
603
+ 'sortOrder ' => 0 ,
604
+ ],
605
+ ]
606
+ ];
607
+ }
402
608
}
0 commit comments