Skip to content

Commit 8f9157a

Browse files
glo23503glo60612
authored andcommitted
ACP2E-3374: In Backend, default store values for product attributes (instead of expected admin values)
1 parent 8193536 commit 8f9157a

File tree

3 files changed

+169
-26
lines changed

3 files changed

+169
-26
lines changed

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Source/TableTest.php

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

@@ -423,6 +423,17 @@ public static function getAllOptionsDataProvider()
423423
['value' => '16', 'label' => 'black'],
424424
['value' => '17', 'label' => 'white']
425425
]
426+
],
427+
[
428+
true,
429+
true,
430+
[['value' => '16', 'label' => 'default sv black'], ['value' => '17', 'label' => 'default sv white']],
431+
[['value' => '16', 'label' => 'black'], ['value' => '17', 'label' => 'white']],
432+
[
433+
['label' => ' ', 'value' => ''],
434+
['value' => '16', 'label' => 'black'],
435+
['value' => '17', 'label' => 'white']
436+
]
426437
]
427438
];
428439
}

app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
*/
66

77
namespace Magento\Rule\Model\Condition\Product;
@@ -243,7 +243,7 @@ protected function _prepareValueOptions()
243243
} else {
244244
$addEmptyOption = true;
245245
}
246-
$selectOptions = $attributeObject->getSource()->getAllOptions($addEmptyOption);
246+
$selectOptions = $attributeObject->getSource()->getAllOptions($addEmptyOption, true);
247247
}
248248
}
249249

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

Lines changed: 153 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
*/
66
declare(strict_types=1);
77

@@ -11,12 +11,20 @@
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;
1415
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
16+
use Magento\Eav\Model\Entity\Attribute\Source\Table;
1517
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;
1621
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
1722
use Magento\Framework\DataObject;
1823
use Magento\Framework\Model\AbstractModel;
24+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1925
use Magento\Rule\Model\Condition\Product\AbstractProduct;
26+
use Magento\Store\Api\Data\StoreInterface;
27+
use Magento\Store\Model\StoreManagerInterface;
2028
use PHPUnit\Framework\MockObject\MockObject;
2129
use 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

Comments
 (0)