11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2011 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
1111use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
1212use Magento \Catalog \Model \ResourceModel \Product ;
1313use Magento \Eav \Model \Config ;
14+ use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
1415use Magento \Eav \Model \Entity \Attribute \Source \AbstractSource ;
16+ use Magento \Eav \Model \Entity \Attribute \Source \Table ;
1517use 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 ;
1621use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \Collection ;
1722use Magento \Framework \DataObject ;
1823use Magento \Framework \Model \AbstractModel ;
24+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1925use Magento \Rule \Model \Condition \Product \AbstractProduct ;
26+ use Magento \Store \Api \Data \StoreInterface ;
27+ use Magento \Store \Model \StoreManagerInterface ;
2028use PHPUnit \Framework \MockObject \MockObject ;
2129use PHPUnit \Framework \TestCase ;
2230
@@ -60,11 +68,17 @@ class AbstractProductTest extends TestCase
6068 */
6169 private $ productCategoryListProperty ;
6270
71+ /**
72+ * @var ObjectManager
73+ */
74+ protected $ objectManager ;
75+
6376 /**
6477 * @inheritdoc
6578 */
6679 protected function setUp (): void
6780 {
81+ $ this ->objectManager = new ObjectManager ($ this );
6882 $ this ->_condition = $ this ->getMockForAbstractClass (
6983 AbstractProduct::class,
7084 [],
@@ -353,15 +367,22 @@ public function testPrepareValueOptions(
353367 $ this ->_condition ->setData ($ key , $ value );
354368 }
355369
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+ }
365386
366387 $ attributeObjectMock = $ this ->getMockBuilder (Attribute::class)
367388 ->addMethods (['getAllOptions ' ])
@@ -426,6 +447,117 @@ public function testPrepareValueOptions(
426447 $ this ->assertEquals ($ expectedValueOption , $ this ->_condition ->getData ('value_option ' ));
427448 }
428449
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+
429561 /**
430562 * Data provider for prepare value options
431563 *
@@ -439,7 +571,7 @@ public static function prepareValueOptionsDataProvider()
439571 [
440572 'value_select_options ' => ['key ' => 'value ' ],
441573 'value_option ' => ['k ' => 'v ' ],
442- ], null , null , null , null , ['key ' => 'value ' ], ['k ' => 'v ' ],
574+ ], null , null , null , null , ['key ' => 'value ' ], ['k ' => 'v ' ]
443575 ],
444576 [
445577 ['attribute ' => 'attribute_set_id ' ],
@@ -540,21 +672,21 @@ public static function prepareValueOptionsDataProvider()
540672 [],
541673 'select ' ,
542674 [
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 ' ]
546678 ],
547679 null ,
548680 true ,
549681 [
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 ' ]
553685 ],
554686 [
555- 'value7 ' => 'Label for value 7 ' ,
556- 'value8 ' => 'Label for value 8 ' ,
557- 'default ' => 'Default Option '
687+ '' => ' ' ,
688+ '16 ' => 'black ' ,
689+ '17 ' => 'white '
558690 ],
559691 ]
560692 ];
0 commit comments