1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2011 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
11
11
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
12
12
use Magento \Catalog \Model \ResourceModel \Product ;
13
13
use Magento \Eav \Model \Config ;
14
+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
14
15
use Magento \Eav \Model \Entity \Attribute \Source \AbstractSource ;
16
+ use Magento \Eav \Model \Entity \Attribute \Source \Table ;
15
17
use Magento \Eav \Model \Entity \Type ;
18
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Option \Collection as AttributeOptionCollection ;
19
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Option \CollectionFactory ;
20
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \OptionFactory ;
16
21
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \Collection ;
17
22
use Magento \Framework \DataObject ;
18
23
use Magento \Framework \Model \AbstractModel ;
24
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
19
25
use Magento \Rule \Model \Condition \Product \AbstractProduct ;
26
+ use Magento \Store \Api \Data \StoreInterface ;
27
+ use Magento \Store \Model \StoreManagerInterface ;
20
28
use PHPUnit \Framework \MockObject \MockObject ;
21
29
use PHPUnit \Framework \TestCase ;
22
30
@@ -60,11 +68,17 @@ class AbstractProductTest extends TestCase
60
68
*/
61
69
private $ productCategoryListProperty ;
62
70
71
+ /**
72
+ * @var ObjectManager
73
+ */
74
+ protected $ objectManager ;
75
+
63
76
/**
64
77
* @inheritdoc
65
78
*/
66
79
protected function setUp (): void
67
80
{
81
+ $ this ->objectManager = new ObjectManager ($ this );
68
82
$ this ->_condition = $ this ->getMockForAbstractClass (
69
83
AbstractProduct::class,
70
84
[],
@@ -353,15 +367,22 @@ public function testPrepareValueOptions(
353
367
$ this ->_condition ->setData ($ key , $ value );
354
368
}
355
369
356
- $ attrObjectSourceMock = $ this ->getMockBuilder (AbstractSource::class)
357
- ->onlyMethods (['getAllOptions ' ])
358
- ->disableOriginalConstructor ()
359
- ->getMock ();
360
- $ attrObjectSourceMock
361
- ->expects ((null === $ expectedAttrObjSourceAllOptionsParam ) ? $ this ->never () : $ this ->once ())
362
- ->method ('getAllOptions ' )
363
- ->with ($ expectedAttrObjSourceAllOptionsParam )
364
- ->willReturn ($ attrObjectSourceAllOptionsValue );
370
+ if ($ attributeObjectFrontendInput == 'select ' ) {
371
+ $ attrObjectSourceMock = $ this ->verifySelectAllOptions (
372
+ $ attrObjectSourceAllOptionsValue ,
373
+ $ expectedAttrObjSourceAllOptionsParam
374
+ );
375
+ } else {
376
+ $ attrObjectSourceMock = $ this ->getMockBuilder (AbstractSource::class)
377
+ ->onlyMethods (['getAllOptions ' ])
378
+ ->disableOriginalConstructor ()
379
+ ->getMock ();
380
+ $ attrObjectSourceMock
381
+ ->expects ((null === $ expectedAttrObjSourceAllOptionsParam ) ? $ this ->never () : $ this ->once ())
382
+ ->method ('getAllOptions ' )
383
+ ->with ($ expectedAttrObjSourceAllOptionsParam )
384
+ ->willReturn ($ attrObjectSourceAllOptionsValue );
385
+ }
365
386
366
387
$ attributeObjectMock = $ this ->getMockBuilder (Attribute::class)
367
388
->addMethods (['getAllOptions ' ])
@@ -426,6 +447,117 @@ public function testPrepareValueOptions(
426
447
$ this ->assertEquals ($ expectedValueOption , $ this ->_condition ->getData ('value_option ' ));
427
448
}
428
449
450
+ /**
451
+ * Test to verify all select value options
452
+ *
453
+ * @param array $attrObjectSourceAllOptionsValue
454
+ * @param bool $expectedAttrObjSourceAllOptionsParam
455
+ * @return Table
456
+ */
457
+ private function verifySelectAllOptions (
458
+ array $ attrObjectSourceAllOptionsValue ,
459
+ bool $ expectedAttrObjSourceAllOptionsParam
460
+ ): Table {
461
+ $ collectionFactory = $ this ->getMockBuilder (CollectionFactory::class)
462
+ ->addMethods (
463
+ [
464
+ 'setPositionOrder ' ,
465
+ 'setAttributeFilter ' ,
466
+ 'addFieldToFilter ' ,
467
+ 'setStoreFilter ' ,
468
+ 'load ' ,
469
+ 'toOptionArray '
470
+ ]
471
+ )
472
+ ->onlyMethods (['create ' ])
473
+ ->disableOriginalConstructor ()
474
+ ->getMock ();
475
+
476
+ $ attributeOptionCollectionMock = $ this ->getMockBuilder (AttributeOptionCollection::class)
477
+ ->onlyMethods (['toOptionArray ' ])
478
+ ->disableOriginalConstructor ()
479
+ ->getMock ();
480
+
481
+ $ attrOptionFactory = $ this ->createPartialMock (
482
+ OptionFactory::class,
483
+ ['create ' ]
484
+ );
485
+
486
+ $ abstractAttributeMock = $ this ->getMockBuilder (AbstractAttribute::class)
487
+ ->addMethods (['getStoreId ' ])
488
+ ->onlyMethods (
489
+ [
490
+ 'getFrontend ' , 'getAttributeCode ' , '__wakeup ' ,
491
+ 'getId ' , 'getIsRequired ' , 'getEntity ' , 'getBackend '
492
+ ]
493
+ )
494
+ ->disableOriginalConstructor ()
495
+ ->getMockForAbstractClass ();
496
+
497
+ $ attrObjectSourceMock = $ this ->objectManager ->getObject (
498
+ Table::class,
499
+ [
500
+ 'attrOptionCollectionFactory ' => $ collectionFactory ,
501
+ 'attrOptionFactory ' => $ attrOptionFactory
502
+ ]
503
+ );
504
+ $ attrObjectSourceMock ->setAttribute ($ abstractAttributeMock );
505
+
506
+ $ storeManagerMock = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
507
+ $ storeMock = $ this ->getMockForAbstractClass (StoreInterface::class);
508
+
509
+ $ this ->objectManager ->setBackwardCompatibleProperty (
510
+ $ attrObjectSourceMock ,
511
+ 'storeManager ' ,
512
+ $ storeManagerMock
513
+ );
514
+
515
+ $ storeId = '1 ' ;
516
+ $ attributeId = '42 ' ;
517
+
518
+ $ abstractAttributeMock ->expects ($ this ->any ())->method ('getStoreId ' )->willReturn (null );
519
+
520
+ $ storeManagerMock ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ storeMock );
521
+ $ storeMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ storeId );
522
+
523
+ $ abstractAttributeMock ->expects ($ this ->any ())->method ('getId ' )->willReturn ($ attributeId );
524
+
525
+ $ collectionFactory ->expects ($ this ->once ())
526
+ ->method ('create ' )
527
+ ->willReturnSelf ();
528
+ $ collectionFactory ->expects ($ this ->once ())
529
+ ->method ('setPositionOrder ' )
530
+ ->willReturnSelf ();
531
+ $ collectionFactory ->expects ($ this ->once ())
532
+ ->method ('setAttributeFilter ' )
533
+ ->with ($ attributeId )
534
+ ->willReturnSelf ();
535
+ $ collectionFactory ->expects ($ this ->once ())
536
+ ->method ('setStoreFilter ' )
537
+ ->with ($ storeId )
538
+ ->willReturnSelf ();
539
+ $ collectionFactory ->expects ($ this ->once ())
540
+ ->method ('load ' )
541
+ ->willReturn ($ attributeOptionCollectionMock );
542
+ $ options = [
543
+ ['value ' => '16 ' , 'label ' => 'default sv black ' ],
544
+ ['value ' => '17 ' , 'label ' => 'default sv white ' ]
545
+ ];
546
+ $ optionsDefault = [['value ' => '16 ' , 'label ' => 'black ' ], ['value ' => '17 ' , 'label ' => 'white ' ]];
547
+ $ attributeOptionCollectionMock ->expects ($ this ->any ())
548
+ ->method ('toOptionArray ' )
549
+ ->willReturnMap (
550
+ [
551
+ ['value ' , $ options ],
552
+ ['default_value ' , $ optionsDefault ]
553
+ ]
554
+ );
555
+
556
+ $ allOptionsValue = $ attrObjectSourceMock ->getAllOptions ($ expectedAttrObjSourceAllOptionsParam , true );
557
+ $ this ->assertEquals ($ attrObjectSourceAllOptionsValue , $ allOptionsValue );
558
+ return $ attrObjectSourceMock ;
559
+ }
560
+
429
561
/**
430
562
* Data provider for prepare value options
431
563
*
@@ -439,7 +571,7 @@ public static function prepareValueOptionsDataProvider()
439
571
[
440
572
'value_select_options ' => ['key ' => 'value ' ],
441
573
'value_option ' => ['k ' => 'v ' ],
442
- ], null , null , null , null , ['key ' => 'value ' ], ['k ' => 'v ' ],
574
+ ], null , null , null , null , ['key ' => 'value ' ], ['k ' => 'v ' ]
443
575
],
444
576
[
445
577
['attribute ' => 'attribute_set_id ' ],
@@ -540,21 +672,21 @@ public static function prepareValueOptionsDataProvider()
540
672
[],
541
673
'select ' ,
542
674
[
543
- ['value ' => 'value7 ' , 'label ' => 'Label for value 7 ' ],
544
- ['value ' => 'value8 ' , 'label ' => 'Label for value 8 ' ],
545
- ['value ' => 'default ' , 'label ' => 'Default Option ' ]
675
+ ['label ' => ' ' , 'value ' => '' ],
676
+ ['value ' => '16 ' , 'label ' => 'black ' ],
677
+ ['value ' => '17 ' , 'label ' => 'white ' ]
546
678
],
547
679
null ,
548
680
true ,
549
681
[
550
- ['value ' => 'value7 ' , 'label ' => 'Label for value 7 ' ],
551
- ['value ' => 'value8 ' , 'label ' => 'Label for value 8 ' ],
552
- ['value ' => 'default ' , 'label ' => 'Default Option ' ]
682
+ ['label ' => ' ' , 'value ' => '' ],
683
+ ['value ' => '16 ' , 'label ' => 'black ' ],
684
+ ['value ' => '17 ' , 'label ' => 'white ' ]
553
685
],
554
686
[
555
- 'value7 ' => 'Label for value 7 ' ,
556
- 'value8 ' => 'Label for value 8 ' ,
557
- 'default ' => 'Default Option '
687
+ '' => ' ' ,
688
+ '16 ' => 'black ' ,
689
+ '17 ' => 'white '
558
690
],
559
691
]
560
692
];
0 commit comments