Skip to content

Commit 1d01b20

Browse files
committed
ACP2E-3374: In Backend, default store values for product attributes (instead of expected admin values)
1 parent bac659e commit 1d01b20

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

app/code/Magento/Rule/Test/Unit/Model/Condition/Product/AbstractProductTest.php

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
2222
use Magento\Framework\DataObject;
2323
use Magento\Framework\Model\AbstractModel;
24-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2524
use Magento\Rule\Model\Condition\Product\AbstractProduct;
2625
use Magento\Store\Api\Data\StoreInterface;
2726
use Magento\Store\Model\StoreManagerInterface;
@@ -68,17 +67,11 @@ class AbstractProductTest extends TestCase
6867
*/
6968
private $productCategoryListProperty;
7069

71-
/**
72-
* @var ObjectManager
73-
*/
74-
protected $objectManager;
75-
7670
/**
7771
* @inheritdoc
7872
*/
7973
protected function setUp(): void
8074
{
81-
$this->objectManager = new ObjectManager($this);
8275
$this->_condition = $this->getMockForAbstractClass(
8376
AbstractProduct::class,
8477
[],
@@ -351,6 +344,8 @@ public function testGetMappedSqlField()
351344
* @param bool $expectedAttrObjSourceAllOptionsParam
352345
* @param array $expectedValueSelectOptions
353346
* @param array $expectedValueOption
347+
* @param array|null $options,
348+
* @param array|null $optionsDefault
354349
* @dataProvider prepareValueOptionsDataProvider
355350
* @SuppressWarnings(PHPMD.NPathComplexity)
356351
*/
@@ -361,40 +356,32 @@ public function testPrepareValueOptions(
361356
$attrSetCollectionOptionsArray,
362357
$expectedAttrObjSourceAllOptionsParam,
363358
$expectedValueSelectOptions,
364-
$expectedValueOption
359+
$expectedValueOption,
360+
$options = null,
361+
$optionsDefault = null
365362
) {
366363
foreach ($setData as $key => $value) {
367364
$this->_condition->setData($key, $value);
368365
}
369366

370-
if ($attributeObjectFrontendInput == 'select') {
367+
$attributeObjectMock = $this->getMockBuilder(Attribute::class)
368+
->disableOriginalConstructor()
369+
->getMock();
370+
if ($attributeObjectFrontendInput == 'select' || $attributeObjectFrontendInput == 'multiselect') {
371371
$attrObjectSourceMock = $this->verifySelectAllOptions(
372372
$attrObjectSourceAllOptionsValue,
373-
$expectedAttrObjSourceAllOptionsParam
373+
$expectedAttrObjSourceAllOptionsParam,
374+
$options,
375+
$optionsDefault
374376
);
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);
377+
$attributeObjectMock->method('getSource')->willReturn($attrObjectSourceMock);
385378
}
386379

387-
$attributeObjectMock = $this->getMockBuilder(Attribute::class)
388-
->addMethods(['getAllOptions'])
389-
->onlyMethods(['usesSource', 'getFrontendInput', 'getSource'])
390-
->disableOriginalConstructor()
391-
->getMock();
392380
$attributeObjectMock->method('usesSource')->willReturn(true);
393381
$attributeObjectMock
394382
->expects((null === $attributeObjectFrontendInput) ? $this->never() : $this->once())
395383
->method('getFrontendInput')
396384
->willReturn($attributeObjectFrontendInput);
397-
$attributeObjectMock->method('getSource')->willReturn($attrObjectSourceMock);
398385

399386
$entityTypeMock = $this->getMockBuilder(Type::class)
400387
->onlyMethods(['getId'])
@@ -456,7 +443,9 @@ public function testPrepareValueOptions(
456443
*/
457444
private function verifySelectAllOptions(
458445
array $attrObjectSourceAllOptionsValue,
459-
bool $expectedAttrObjSourceAllOptionsParam
446+
bool $expectedAttrObjSourceAllOptionsParam,
447+
array $options,
448+
array $optionsDefault
460449
): Table {
461450
$collectionFactory = $this->getMockBuilder(CollectionFactory::class)
462451
->addMethods(
@@ -485,35 +474,23 @@ private function verifySelectAllOptions(
485474
]
486475
)
487476
->disableOriginalConstructor()
488-
->getMockForAbstractClass();
489-
490-
$attrObjectSourceMock = $this->objectManager->getObject(
491-
Table::class,
492-
[
493-
'attrOptionCollectionFactory' => $collectionFactory,
494-
'attrOptionFactory' => $attrOptionFactory
495-
]
496-
);
497-
$attrObjectSourceMock->setAttribute($abstractAttributeMock);
477+
->getMock();
498478

499-
$storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
500-
$storeMock = $this->getMockForAbstractClass(StoreInterface::class);
479+
$storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMock();
480+
$storeMock = $this->getMockBuilder(StoreInterface::class)->getMock();
501481

502-
$this->objectManager->setBackwardCompatibleProperty(
503-
$attrObjectSourceMock,
504-
'storeManager',
505-
$storeManagerMock
506-
);
482+
$attrObjectSourceMock = new Table($collectionFactory, $attrOptionFactory, $storeManagerMock);
483+
$attrObjectSourceMock->setAttribute($abstractAttributeMock);
507484

508485
$storeId = '1';
509486
$attributeId = '42';
510487

511-
$abstractAttributeMock->expects($this->any())->method('getStoreId')->willReturn(null);
488+
$abstractAttributeMock->expects($this->atLeastOnce())->method('getStoreId')->willReturn(null);
512489

513-
$storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
514-
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
490+
$storeManagerMock->expects($this->atLeastOnce())->method('getStore')->willReturn($storeMock);
491+
$storeMock->expects($this->atLeastOnce())->method('getId')->willReturn($storeId);
515492

516-
$abstractAttributeMock->expects($this->any())->method('getId')->willReturn($attributeId);
493+
$abstractAttributeMock->expects($this->atLeastOnce())->method('getId')->willReturn($attributeId);
517494

518495
$collectionFactory->expects($this->once())
519496
->method('create')
@@ -532,8 +509,6 @@ private function verifySelectAllOptions(
532509
$collectionFactory->expects($this->once())
533510
->method('load')
534511
->willReturn($attributeOptionCollectionMock);
535-
$options = [['value' => '16', 'label' => 'default sv black'], ['value' => '17', 'label' => 'default sv white']];
536-
$optionsDefault = [['value' => '16', 'label' => 'black'], ['value' => '17', 'label' => 'white']];
537512
$attributeOptionCollectionMock->expects($this->any())
538513
->method('toOptionArray')
539514
->willReturnMap(
@@ -640,6 +615,14 @@ public static function prepareValueOptionsDataProvider()
640615
'value7' => 'Label for value 7',
641616
'value8' => 'Label for value 8',
642617
],
618+
[
619+
['value' => 'value7', 'label' => 'Label for default sv value 7'],
620+
['value' => 'value8', 'label' => 'Label for default sv value 8'],
621+
],
622+
[
623+
['value' => 'value7', 'label' => 'Label for value 7'],
624+
['value' => 'value8', 'label' => 'Label for value 8'],
625+
]
643626
],
644627
[
645628
[],
@@ -657,6 +640,14 @@ public static function prepareValueOptionsDataProvider()
657640
[
658641
'valueA' => 'Label for value A',
659642
],
643+
[
644+
['value' => 'valueA', 'label' => 'Label for default sv value A'],
645+
['value' => ['array value'], 'label' => 'Label for default sv value B']
646+
],
647+
[
648+
['value' => 'valueA', 'label' => 'Label for value A'],
649+
['value' => ['array value'], 'label' => 'Label for value B']
650+
],
660651
],
661652
[
662653
[],
@@ -678,6 +669,14 @@ public static function prepareValueOptionsDataProvider()
678669
'16' => 'black',
679670
'17' => 'white'
680671
],
672+
[
673+
['value' => '16', 'label' => 'default sv black'],
674+
['value' => '17', 'label' => 'default sv white']
675+
],
676+
[
677+
['value' => '16', 'label' => 'black'],
678+
['value' => '17', 'label' => 'white']
679+
],
681680
]
682681
];
683682
}

0 commit comments

Comments
 (0)