Skip to content

Commit 1ac5355

Browse files
committed
MAGETWO-54491: Match products by rule in admin not working
1 parent 3926be4 commit 1ac5355

File tree

2 files changed

+196
-58
lines changed
  • app/code/Magento/Eav

2 files changed

+196
-58
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Eav\Model\Entity\Attribute\Source;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
811
class Table extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
912
{
1013
/**
@@ -24,6 +27,11 @@ class Table extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
2427
*/
2528
protected $_attrOptionFactory;
2629

30+
/**
31+
* @var StoreManagerInterface
32+
*/
33+
private $storeManager;
34+
2735
/**
2836
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
2937
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
@@ -47,31 +55,51 @@ public function __construct(
4755
public function getAllOptions($withEmpty = true, $defaultValues = false)
4856
{
4957
$storeId = $this->getAttribute()->getStoreId();
58+
if ($storeId === null) {
59+
$storeId = $this->getStoreManager()->getStore()->getId();
60+
}
5061
if (!is_array($this->_options)) {
5162
$this->_options = [];
5263
}
5364
if (!is_array($this->_optionsDefault)) {
5465
$this->_optionsDefault = [];
5566
}
56-
if (!isset($this->_options[$storeId])) {
67+
$attributeId = $this->getAttribute()->getId();
68+
if (!isset($this->_options[$storeId][$attributeId])) {
5769
$collection = $this->_attrOptionCollectionFactory->create()->setPositionOrder(
5870
'asc'
5971
)->setAttributeFilter(
60-
$this->getAttribute()->getId()
72+
$attributeId
6173
)->setStoreFilter(
62-
$this->getAttribute()->getStoreId()
74+
$storeId
6375
)->load();
64-
$this->_options[$storeId] = $collection->toOptionArray();
65-
$this->_optionsDefault[$storeId] = $collection->toOptionArray('default_value');
76+
$this->_options[$storeId][$attributeId] = $collection->toOptionArray();
77+
$this->_optionsDefault[$storeId][$attributeId] = $collection->toOptionArray('default_value');
6678
}
67-
$options = $defaultValues ? $this->_optionsDefault[$storeId] : $this->_options[$storeId];
79+
$options = $defaultValues
80+
? $this->_optionsDefault[$storeId][$attributeId]
81+
: $this->_options[$storeId][$attributeId];
6882
if ($withEmpty) {
6983
$options = $this->addEmptyOption($options);
7084
}
7185

7286
return $options;
7387
}
7488

89+
/**
90+
* Get StoreManager dependency
91+
*
92+
* @return StoreManagerInterface
93+
* @deprecated
94+
*/
95+
private function getStoreManager()
96+
{
97+
if ($this->storeManager === null) {
98+
$this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
99+
}
100+
return $this->storeManager;
101+
}
102+
75103
/**
76104
* Retrieve Option values array by ids
77105
*

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

Lines changed: 162 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Source;
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
10+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection as AttributeOptionCollection;
914

1015
/**
1116
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -28,6 +33,31 @@ class TableTest extends \PHPUnit_Framework_TestCase
2833
*/
2934
private $attrOptionFactory;
3035

36+
/**
37+
* @var AbstractSource | \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $sourceMock;
40+
41+
/**
42+
* @var AbstractAttribute | \PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $abstractAttributeMock;
45+
46+
/**
47+
* @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $storeManagerMock;
50+
51+
/**
52+
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
private $storeMock;
55+
56+
/**
57+
* @var AttributeOptionCollection|\PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
private $attributeOptionCollectionMock;
60+
3161
protected function setUp()
3262
{
3363
$objectManager = new ObjectManager($this);
@@ -48,20 +78,49 @@ protected function setUp()
4878
false
4979
);
5080

81+
$this->attributeOptionCollectionMock = $this->getMockBuilder(AttributeOptionCollection::class)
82+
->setMethods(['toOptionArray'])
83+
->disableOriginalConstructor()
84+
->getMock();
85+
5186
$this->attrOptionFactory = $this->getMockBuilder(
5287
\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory::class
5388
)
5489
->setMethods(['create'])
5590
->disableOriginalConstructor()
5691
->getMockForAbstractClass();
5792

93+
$this->sourceMock = $this->getMockBuilder(AbstractSource::class)
94+
->disableOriginalConstructor()
95+
->getMockForAbstractClass();
96+
97+
$this->abstractAttributeMock = $this->getMockBuilder(AbstractAttribute::class)
98+
->setMethods(
99+
[
100+
'getFrontend', 'getAttributeCode', '__wakeup', 'getStoreId',
101+
'getId', 'getIsRequired', 'getEntity', 'getBackend'
102+
]
103+
)
104+
->disableOriginalConstructor()
105+
->getMockForAbstractClass();
106+
58107
$this->model = $objectManager->getObject(
59108
\Magento\Eav\Model\Entity\Attribute\Source\Table::class,
60109
[
61110
'attrOptionCollectionFactory' => $this->collectionFactory,
62111
'attrOptionFactory' => $this->attrOptionFactory
63112
]
64113
);
114+
$this->model->setAttribute($this->abstractAttributeMock);
115+
116+
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
117+
$this->storeMock = $this->getMockForAbstractClass(StoreInterface::class);
118+
119+
$objectManager->setBackwardCompatibleProperty(
120+
$this->model,
121+
'storeManager',
122+
$this->storeManagerMock
123+
);
65124
}
66125

67126
public function testGetFlatColumns()
@@ -74,25 +133,8 @@ public function testGetFlatColumns()
74133
false
75134
);
76135

77-
$abstractAttributeMock = $this->getMock(
78-
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
79-
['getFrontend', 'getAttributeCode', '__wakeup'],
80-
[],
81-
'',
82-
false
83-
);
84-
85-
$abstractAttributeMock->expects(
86-
$this->any()
87-
)->method(
88-
'getFrontend'
89-
)->will(
90-
$this->returnValue($abstractFrontendMock)
91-
);
92-
93-
$abstractAttributeMock->expects($this->any())->method('getAttributeCode')->will($this->returnValue('code'));
94-
95-
$this->model->setAttribute($abstractAttributeMock);
136+
$this->abstractAttributeMock->expects($this->any())->method('getFrontend')->willReturn(($abstractFrontendMock));
137+
$this->abstractAttributeMock->expects($this->any())->method('getAttributeCode')->willReturn('code');
96138

97139
$flatColumns = $this->model->getFlatColumns();
98140

@@ -121,25 +163,16 @@ public function testGetSpecificOptions($optionIds, $withEmpty)
121163
$storeId = 5;
122164
$options = [['label' => 'The label', 'value' => 'A value']];
123165

124-
$attribute = $this->getMock(
125-
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
126-
['getId', 'getStoreId', 'getIsRequired', '__wakeup'],
127-
[],
128-
'',
129-
false
130-
);
131-
$attribute->expects($this->once())
166+
$this->abstractAttributeMock->expects($this->once())
132167
->method('getId')
133168
->willReturn($attributeId);
134-
$attribute->expects($this->once())
169+
$this->abstractAttributeMock->expects($this->once())
135170
->method('getStoreId')
136171
->willReturn($storeId);
137-
$attribute->expects($this->any())
172+
$this->abstractAttributeMock->expects($this->any())
138173
->method('getIsRequired')
139174
->willReturn(false);
140175

141-
$this->model->setAttribute($attribute);
142-
143176
$this->collectionFactory->expects($this->once())
144177
->method('create')
145178
->willReturnSelf();
@@ -191,22 +224,14 @@ public function testGetOptionText($optionsIds, $value, $options, $expectedResult
191224
{
192225
$attributeId = 1;
193226
$storeId = 5;
194-
$attribute = $this->getMock(
195-
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
196-
['getId', 'getStoreId', '__wakeup'],
197-
[],
198-
'',
199-
false
200-
);
201-
$attribute->expects($this->once())
227+
228+
$this->abstractAttributeMock->expects($this->once())
202229
->method('getId')
203230
->willReturn($attributeId);
204-
$attribute->expects($this->once())
231+
$this->abstractAttributeMock->expects($this->once())
205232
->method('getStoreId')
206233
->willReturn($storeId);
207234

208-
$this->model->setAttribute($attribute);
209-
210235
$this->collectionFactory->expects($this->once())
211236
->method('create')
212237
->willReturnSelf();
@@ -257,16 +282,13 @@ public function testAddValueSortToCollection()
257282
->setMethods([ 'getSelect', 'getStoreId'])
258283
->disableOriginalConstructor()
259284
->getMockForAbstractClass();
260-
$attribute = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
261-
->setMethods(['getAttributeCode', 'getEntity', 'getBackend', 'getId'])
262-
->disableOriginalConstructor()
263-
->getMockForAbstractClass();
264-
$attribute->expects($this->any())->method('getAttributeCode')->willReturn($attributeCode);
285+
286+
$this->abstractAttributeMock->expects($this->any())->method('getAttributeCode')->willReturn($attributeCode);
265287
$entity = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
266288
->setMethods(['getLinkField'])
267289
->disableOriginalConstructor()
268290
->getMockForAbstractClass();
269-
$attribute->expects($this->once())->method('getEntity')->willReturn($entity);
291+
$this->abstractAttributeMock->expects($this->once())->method('getEntity')->willReturn($entity);
270292
$entity->expects($this->once())->method('getLinkField')->willReturn('entity_id');
271293
$select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
272294
->setMethods(['joinLeft', 'getConnection', 'order'])
@@ -278,9 +300,9 @@ public function testAddValueSortToCollection()
278300
->setMethods(['getTable'])
279301
->disableOriginalConstructor()
280302
->getMockForAbstractClass();
281-
$attribute->expects($this->any())->method('getBackend')->willReturn($backend);
303+
$this->abstractAttributeMock->expects($this->any())->method('getBackend')->willReturn($backend);
282304
$backend->expects($this->any())->method('getTable')->willReturn('table_name');
283-
$attribute->expects($this->any())->method('getId')->willReturn(1);
305+
$this->abstractAttributeMock->expects($this->any())->method('getId')->willReturn(1);
284306
$collection->expects($this->once())->method('getStoreId')->willReturn(1);
285307
$connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
286308
->disableOriginalConstructor()
@@ -294,11 +316,99 @@ public function testAddValueSortToCollection()
294316
->disableOriginalConstructor()
295317
->getMock();
296318
$this->attrOptionFactory->expects($this->once())->method('create')->willReturn($attrOption);
297-
$attrOption->expects($this->once())->method('addOptionValueToCollection')->with($collection, $attribute, $expr)
319+
$attrOption->expects($this->once())->method('addOptionValueToCollection')
320+
->with($collection, $this->abstractAttributeMock, $expr)
298321
->willReturnSelf();
299322
$select->expects($this->once())->method('order')->with("{$attributeCode} {$dir}");
300323

301-
$this->model->setAttribute($attribute);
302324
$this->assertEquals($this->model, $this->model->addValueSortToCollection($collection, $dir));
303325
}
326+
327+
/**
328+
* @param bool $withEmpty
329+
* @param bool $defaultValues
330+
* @param array $options
331+
* @param array $optionsDefault
332+
* @param array $expectedResult
333+
* @dataProvider getAllOptionsDataProvider
334+
*/
335+
public function testGetAllOptions(
336+
$withEmpty,
337+
$defaultValues,
338+
array $options,
339+
array $optionsDefault,
340+
array $expectedResult
341+
) {
342+
$storeId = '1';
343+
$attributeId = '42';
344+
345+
$this->abstractAttributeMock->expects($this->once())->method('getStoreId')->willReturn(null);
346+
347+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
348+
$this->storeMock->expects($this->once())->method('getId')->willReturn($storeId);
349+
350+
$this->abstractAttributeMock->expects($this->once())->method('getId')->willReturn($attributeId);
351+
352+
$this->collectionFactory->expects($this->once())
353+
->method('create')
354+
->willReturnSelf();
355+
$this->collectionFactory->expects($this->once())
356+
->method('setPositionOrder')
357+
->willReturnSelf();
358+
$this->collectionFactory->expects($this->once())
359+
->method('setAttributeFilter')
360+
->with($attributeId)
361+
->willReturnSelf();
362+
$this->collectionFactory->expects($this->once())
363+
->method('setStoreFilter')
364+
->with($storeId)
365+
->willReturnSelf();
366+
$this->collectionFactory->expects($this->once())
367+
->method('load')
368+
->willReturn($this->attributeOptionCollectionMock);
369+
$this->attributeOptionCollectionMock->expects($this->any())
370+
->method('toOptionArray')
371+
->willReturnMap(
372+
[
373+
['value', $options],
374+
['default_value', $optionsDefault]
375+
]
376+
);
377+
378+
$this->assertEquals($expectedResult, $this->model->getAllOptions($withEmpty, $defaultValues));
379+
}
380+
381+
/**
382+
* @return array
383+
*/
384+
public function getAllOptionsDataProvider()
385+
{
386+
return [
387+
[
388+
false,
389+
false,
390+
[['value' => '16', 'label' => 'black'], ['value' => '17', 'label' => 'white']],
391+
[['value' => '16', 'label' => 'blck'], ['value' => '17', 'label' => 'wht']],
392+
[['value' => '16', 'label' => 'black'], ['value' => '17', 'label' => 'white']]
393+
],
394+
[
395+
false,
396+
true,
397+
[['value' => '16', 'label' => 'black'], ['value' => '17', 'label' => 'white']],
398+
[['value' => '16', 'label' => 'blck'], ['value' => '17', 'label' => 'wht']],
399+
[['value' => '16', 'label' => 'blck'], ['value' => '17', 'label' => 'wht']]
400+
],
401+
[
402+
true,
403+
false,
404+
[['value' => '16', 'label' => 'black'], ['value' => '17', 'label' => 'white']],
405+
[['value' => '16', 'label' => 'blck'], ['value' => '17', 'label' => 'wht']],
406+
[
407+
['label' => ' ', 'value' => ''],
408+
['value' => '16', 'label' => 'black'],
409+
['value' => '17', 'label' => 'white']
410+
]
411+
]
412+
];
413+
}
304414
}

0 commit comments

Comments
 (0)