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,26 @@ 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
+
160
185
/**
161
186
* @var ObjectManager
162
187
*/
@@ -167,6 +192,9 @@ class EavTest extends AbstractModifierTest
167
192
*/
168
193
protected $ eav ;
169
194
195
+ /**
196
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
197
+ */
170
198
protected function setUp ()
171
199
{
172
200
parent ::setUp ();
@@ -228,10 +256,24 @@ protected function setUp()
228
256
$ this ->searchResultsMock = $ this ->getMockBuilder (SearchResultsInterface::class)
229
257
->getMockForAbstractClass ();
230
258
$ this ->eavAttributeMock = $ this ->getMockBuilder (Attribute::class)
231
- ->setMethods (['getAttributeGroupCode ' , 'getApplyTo ' , 'getFrontendInput ' , 'getAttributeCode ' ])
259
+ ->setMethods (['load ' , 'getAttributeGroupCode ' , 'getApplyTo ' , 'getFrontendInput ' , 'getAttributeCode ' ])
260
+ ->disableOriginalConstructor ()
261
+ ->getMock ();
262
+ $ this ->productAttributeMock = $ this ->getMockBuilder (ProductAttributeInterface::class)
263
+ ->getMock ();
264
+ $ this ->arrayManagerMock = $ this ->getMockBuilder (ArrayManager::class)
265
+ ->getMock ();
266
+ $ this ->eavAttributeFactoryMock = $ this ->getMockBuilder (EavAttributeFactory::class)
267
+ ->disableOriginalConstructor ()
268
+ ->setMethods (['create ' ])
269
+ ->getMock ();
270
+ $ this ->eventManagerMock = $ this ->getMockBuilder (ManagerInterface::class)
232
271
->disableOriginalConstructor ()
233
272
->getMock ();
234
273
274
+ $ this ->eavAttributeFactoryMock ->expects ($ this ->any ())
275
+ ->method ('create ' )
276
+ ->willReturn ($ this ->eavAttributeMock );
235
277
$ this ->groupCollectionFactoryMock ->expects ($ this ->any ())
236
278
->method ('create ' )
237
279
->willReturn ($ this ->groupCollectionMock );
@@ -277,6 +319,9 @@ protected function setUp()
277
319
->disableOriginalConstructor ()
278
320
->setMethods (['getCurrency ' ])
279
321
->getMock ();
322
+ $ this ->eavAttributeMock ->expects ($ this ->any ())
323
+ ->method ('load ' )
324
+ ->willReturnSelf ();
280
325
281
326
$ this ->eav =$ this ->getModel ();
282
327
$ this ->objectManager ->setBackwardCompatibleProperty (
@@ -304,6 +349,9 @@ protected function createModel()
304
349
'attributeGroupRepository ' => $ this ->attributeGroupRepositoryMock ,
305
350
'sortOrderBuilder ' => $ this ->sortOrderBuilderMock ,
306
351
'attributeRepository ' => $ this ->attributeRepositoryMock ,
352
+ 'arrayManager ' => $ this ->arrayManagerMock ,
353
+ 'eavAttributeFactory ' => $ this ->eavAttributeFactoryMock ,
354
+ '_eventManager ' => $ this ->eventManagerMock
307
355
]);
308
356
}
309
357
@@ -399,4 +447,162 @@ public function testModifyData()
399
447
400
448
$ this ->assertEquals ($ sourceData , $ this ->eav ->modifyData ([]));
401
449
}
450
+
451
+ /**
452
+ * @param int $productId
453
+ * @param bool $productRequired
454
+ * @param string $attrValue
455
+ * @param array $expected
456
+ * @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::isProductExists
457
+ * @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::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
+
470
+ $ this ->productAttributeMock ->expects ($ this ->any ())
471
+ ->method ('getIsRequired ' )
472
+ ->willReturn ($ productRequired );
473
+
474
+ $ this ->productAttributeMock ->expects ($ this ->any ())
475
+ ->method ('getDefaultValue ' )
476
+ ->willReturn ('required_value ' );
477
+
478
+ $ this ->productAttributeMock ->expects ($ this ->any ())
479
+ ->method ('getAttributeCode ' )
480
+ ->willReturn ('code ' );
481
+
482
+ $ this ->productAttributeMock ->expects ($ this ->any ())
483
+ ->method ('getValue ' )
484
+ ->willReturn ('value ' );
485
+
486
+ $ attributeMock = $ this ->getMockBuilder (AttributeInterface::class)
487
+ ->disableOriginalConstructor ()
488
+ ->getMock ();
489
+
490
+ $ attributeMock ->expects ($ this ->any ())
491
+ ->method ('getValue ' )
492
+ ->willReturn ($ attrValue );
493
+
494
+ $ this ->productMock ->expects ($ this ->any ())
495
+ ->method ('getCustomAttribute ' )
496
+ ->willReturn ($ attributeMock );
497
+
498
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
499
+ ->method ('set ' )
500
+ ->with (
501
+ $ configPath ,
502
+ [],
503
+ $ expected
504
+ )
505
+ ->willReturn ($ expected );
506
+
507
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
508
+ ->method ('merge ' )
509
+ ->willReturn ($ expected );
510
+
511
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
512
+ ->method ('get ' )
513
+ ->willReturn ([]);
514
+
515
+ $ this ->arrayManagerMock ->expects ($ this ->any ())
516
+ ->method ('exists ' );
517
+
518
+ $ this ->assertEquals (
519
+ $ expected ,
520
+ $ this ->eav ->setupAttributeMeta ($ this ->productAttributeMock , $ groupCode , $ sortOrder )
521
+ );
522
+ }
523
+
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