Skip to content

Commit e540d16

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

File tree

1 file changed

+20
-131
lines changed

1 file changed

+20
-131
lines changed

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

Lines changed: 20 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright 2011 Adobe
4-
* All Rights Reserved.
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
77

@@ -11,18 +11,12 @@
1111
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
1212
use Magento\Catalog\Model\ResourceModel\Product;
1313
use Magento\Eav\Model\Config;
14-
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
15-
use Magento\Eav\Model\Entity\Attribute\Source\Table;
14+
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
1615
use Magento\Eav\Model\Entity\Type;
17-
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection as AttributeOptionCollection;
18-
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory;
19-
use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
2016
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
2117
use Magento\Framework\DataObject;
2218
use Magento\Framework\Model\AbstractModel;
2319
use Magento\Rule\Model\Condition\Product\AbstractProduct;
24-
use Magento\Store\Api\Data\StoreInterface;
25-
use Magento\Store\Model\StoreManagerInterface;
2620
use PHPUnit\Framework\MockObject\MockObject;
2721
use PHPUnit\Framework\TestCase;
2822

@@ -343,8 +337,6 @@ public function testGetMappedSqlField()
343337
* @param bool $expectedAttrObjSourceAllOptionsParam
344338
* @param array $expectedValueSelectOptions
345339
* @param array $expectedValueOption
346-
* @param array|null $options,
347-
* @param array|null $optionsDefault
348340
* @dataProvider prepareValueOptionsDataProvider
349341
* @SuppressWarnings(PHPMD.NPathComplexity)
350342
*/
@@ -355,32 +347,33 @@ public function testPrepareValueOptions(
355347
$attrSetCollectionOptionsArray,
356348
$expectedAttrObjSourceAllOptionsParam,
357349
$expectedValueSelectOptions,
358-
$expectedValueOption,
359-
$options = null,
360-
$optionsDefault = null
350+
$expectedValueOption
361351
) {
362352
foreach ($setData as $key => $value) {
363353
$this->_condition->setData($key, $value);
364354
}
365355

366-
$attributeObjectMock = $this->getMockBuilder(Attribute::class)
356+
$attrObjectSourceMock = $this->getMockBuilder(AbstractSource::class)
357+
->onlyMethods(['getAllOptions'])
367358
->disableOriginalConstructor()
368359
->getMock();
369-
if ($attributeObjectFrontendInput == 'select' || $attributeObjectFrontendInput == 'multiselect') {
370-
$attrObjectSourceMock = $this->verifySelectAllOptions(
371-
$attrObjectSourceAllOptionsValue,
372-
$expectedAttrObjSourceAllOptionsParam,
373-
$options,
374-
$optionsDefault
375-
);
376-
$attributeObjectMock->method('getSource')->willReturn($attrObjectSourceMock);
377-
}
360+
$attrObjectSourceMock
361+
->expects((null === $expectedAttrObjSourceAllOptionsParam) ? $this->never() : $this->once())
362+
->method('getAllOptions')
363+
->with($expectedAttrObjSourceAllOptionsParam, true)
364+
->willReturn($attrObjectSourceAllOptionsValue);
378365

366+
$attributeObjectMock = $this->getMockBuilder(Attribute::class)
367+
->addMethods(['getAllOptions'])
368+
->onlyMethods(['usesSource', 'getFrontendInput', 'getSource'])
369+
->disableOriginalConstructor()
370+
->getMock();
379371
$attributeObjectMock->method('usesSource')->willReturn(true);
380372
$attributeObjectMock
381373
->expects((null === $attributeObjectFrontendInput) ? $this->never() : $this->once())
382374
->method('getFrontendInput')
383375
->willReturn($attributeObjectFrontendInput);
376+
$attributeObjectMock->method('getSource')->willReturn($attrObjectSourceMock);
384377

385378
$entityTypeMock = $this->getMockBuilder(Type::class)
386379
->onlyMethods(['getId'])
@@ -433,86 +426,6 @@ public function testPrepareValueOptions(
433426
$this->assertEquals($expectedValueOption, $this->_condition->getData('value_option'));
434427
}
435428

436-
/**
437-
* Test to verify all select value options
438-
*
439-
* @param array $attrObjectSourceAllOptionsValue
440-
* @param bool $expectedAttrObjSourceAllOptionsParam
441-
* @return Table
442-
*/
443-
private function verifySelectAllOptions(
444-
array $attrObjectSourceAllOptionsValue,
445-
bool $expectedAttrObjSourceAllOptionsParam,
446-
array $options,
447-
array $optionsDefault
448-
): Table {
449-
$collectionFactory = $this->getMockBuilder(CollectionFactory::class)
450-
->onlyMethods(['create'])
451-
->disableOriginalConstructor()
452-
->getMock();
453-
454-
$attributeOptionCollectionMock = $this->getMockBuilder(AttributeOptionCollection::class)
455-
->onlyMethods([
456-
'setPositionOrder', 'setAttributeFilter', 'addFieldToFilter',
457-
'setStoreFilter', 'load', 'toOptionArray'
458-
])
459-
->disableOriginalConstructor()
460-
->getMock();
461-
462-
$storeId = '1';
463-
$storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMock();
464-
$storeMock = $this->getMockBuilder(StoreInterface::class)->getMock();
465-
$storeMock->expects($this->atLeastOnce())->method('getId')->willReturn($storeId);
466-
$storeManagerMock->expects($this->atLeastOnce())->method('getStore')->willReturn($storeMock);
467-
$attrOptionFactory = $this->createPartialMock(OptionFactory::class, ['create']);
468-
469-
$attributeId = '42';
470-
$abstractAttributeMock = $this->getMockBuilder(AbstractAttribute::class)
471-
->addMethods(['getStoreId'])
472-
->onlyMethods(
473-
[
474-
'getFrontend', 'getAttributeCode', '__wakeup',
475-
'getId', 'getIsRequired', 'getEntity', 'getBackend'
476-
]
477-
)
478-
->disableOriginalConstructor()
479-
->getMock();
480-
$abstractAttributeMock->expects($this->atLeastOnce())->method('getStoreId')->willReturn(null);
481-
$abstractAttributeMock->expects($this->atLeastOnce())->method('getId')->willReturn($attributeId);
482-
483-
$collectionFactory->expects($this->once())
484-
->method('create')
485-
->willReturn($attributeOptionCollectionMock);
486-
$attributeOptionCollectionMock->expects($this->once())
487-
->method('setPositionOrder')
488-
->willReturnSelf();
489-
$attributeOptionCollectionMock->expects($this->once())
490-
->method('setAttributeFilter')
491-
->with($attributeId)
492-
->willReturnSelf();
493-
$attributeOptionCollectionMock->expects($this->once())
494-
->method('setStoreFilter')
495-
->with($storeId)
496-
->willReturnSelf();
497-
$attributeOptionCollectionMock->expects($this->once())
498-
->method('load')
499-
->willReturnSelf();
500-
$attributeOptionCollectionMock->expects($this->any())
501-
->method('toOptionArray')
502-
->willReturnMap(
503-
[
504-
['value', $options],
505-
['default_value', $optionsDefault]
506-
]
507-
);
508-
509-
$attrObjectSourceMock = new Table($collectionFactory, $attrOptionFactory, $storeManagerMock);
510-
$attrObjectSourceMock->setAttribute($abstractAttributeMock);
511-
$allOptionsValue = $attrObjectSourceMock->getAllOptions($expectedAttrObjSourceAllOptionsParam, true);
512-
$this->assertEquals($attrObjectSourceAllOptionsValue, $allOptionsValue);
513-
return $attrObjectSourceMock;
514-
}
515-
516429
/**
517430
* Data provider for prepare value options
518431
*
@@ -605,14 +518,6 @@ public static function prepareValueOptionsDataProvider()
605518
'value7' => 'Label for value 7',
606519
'value8' => 'Label for value 8',
607520
],
608-
[
609-
['value' => 'value7', 'label' => 'Label for default sv value 7'],
610-
['value' => 'value8', 'label' => 'Label for default sv value 8'],
611-
],
612-
[
613-
['value' => 'value7', 'label' => 'Label for value 7'],
614-
['value' => 'value8', 'label' => 'Label for value 8'],
615-
]
616521
],
617522
[
618523
[],
@@ -630,43 +535,27 @@ public static function prepareValueOptionsDataProvider()
630535
[
631536
'valueA' => 'Label for value A',
632537
],
633-
[
634-
['value' => 'valueA', 'label' => 'Label for default sv value A'],
635-
['value' => ['array value'], 'label' => 'Label for default sv value B']
636-
],
637-
[
638-
['value' => 'valueA', 'label' => 'Label for value A'],
639-
['value' => ['array value'], 'label' => 'Label for value B']
640-
],
641538
],
642539
[
643540
[],
644541
'select',
645542
[
646-
['value' => '', 'label' => ' '],
647543
['value' => 'value7', 'label' => 'Label for value 7'],
648544
['value' => 'value8', 'label' => 'Label for value 8'],
545+
['value' => 'default', 'label' => 'Default Option']
649546
],
650547
null,
651548
true,
652549
[
653-
['value' => '', 'label' => ' '],
654550
['value' => 'value7', 'label' => 'Label for value 7'],
655551
['value' => 'value8', 'label' => 'Label for value 8'],
552+
['value' => 'default', 'label' => 'Default Option']
656553
],
657554
[
658-
'' => ' ',
659555
'value7' => 'Label for value 7',
660556
'value8' => 'Label for value 8',
557+
'default' => 'Default Option'
661558
],
662-
[
663-
['value' => 'value7', 'label' => 'Label for default sv value 7'],
664-
['value' => 'value8', 'label' => 'Label for default sv value 8'],
665-
],
666-
[
667-
['value' => 'value7', 'label' => 'Label for value 7'],
668-
['value' => 'value8', 'label' => 'Label for value 8'],
669-
]
670559
]
671560
];
672561
}

0 commit comments

Comments
 (0)