Skip to content

Commit 4b3c9cc

Browse files
committed
MAGETWO-59240: There is no WHERE claus for retrieving Media Gallery Data
2 parents 8e273f1 + 4db67e4 commit 4b3c9cc

File tree

2 files changed

+100
-9
lines changed

2 files changed

+100
-9
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,12 +2182,17 @@ public function addMediaGalleryData()
21822182

21832183
$mediaGalleries = [];
21842184
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
2185+
$items = $this->getItems();
2186+
2187+
$select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) {
2188+
return $item->getId();
2189+
}, $items));
21852190

21862191
foreach ($this->getConnection()->fetchAll($select) as $row) {
21872192
$mediaGalleries[$row[$linkField]][] = $row;
21882193
}
21892194

2190-
foreach ($this->getItems() as $item) {
2195+
foreach ($items as $item) {
21912196
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
21922197
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
21932198
}

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
3030
*/
3131
protected $collection;
3232

33+
/**
34+
* @var \PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $galleryResourceMock;
37+
38+
/**
39+
* @var \PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $entityMock;
42+
43+
/**
44+
* @var \PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $metadataPoolMock;
47+
48+
/**
49+
* @var \PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
private $galleryReadHandlerMock;
52+
3353
/**
3454
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
3555
*/
@@ -100,24 +120,49 @@ protected function setUp()
100120
->disableOriginalConstructor()
101121
->getMock();
102122

103-
$entityMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
123+
$this->entityMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
104124
->disableOriginalConstructor()
105125
->getMock();
106126

127+
$this->galleryResourceMock = $this->getMockBuilder(
128+
\Magento\Catalog\Model\ResourceModel\Product\Gallery::class
129+
)->disableOriginalConstructor()->getMock();
130+
131+
$this->metadataPoolMock = $this->getMockBuilder(
132+
\Magento\Framework\EntityManager\MetadataPool::class
133+
)->disableOriginalConstructor()->getMock();
134+
135+
$this->galleryReadHandlerMock = $this->getMockBuilder(
136+
\Magento\Catalog\Model\Product\Gallery\ReadHandler::class
137+
)->disableOriginalConstructor()->getMock();
138+
107139
$storeManager->expects($this->any())->method('getId')->willReturn(1);
108140
$storeManager->expects($this->any())->method('getStore')->willReturnSelf();
109141
$universalFactory->expects($this->exactly(1))->method('create')->willReturnOnConsecutiveCalls(
110-
$entityMock
142+
$this->entityMock
111143
);
112-
$entityMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
113-
$entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([]);
114-
$entityMock->expects($this->any())->method('getTable')->willReturnArgument(0);
144+
$this->entityMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
145+
$this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([]);
146+
$this->entityMock->expects($this->any())->method('getTable')->willReturnArgument(0);
115147
$this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock);
116148
$helper = new ObjectManager($this);
117149

118150
$this->prepareObjectManager([
119-
[\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class,
151+
[
152+
\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class,
120153
$this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class)
154+
],
155+
[
156+
\Magento\Catalog\Model\ResourceModel\Product\Gallery::class,
157+
$this->galleryResourceMock
158+
],
159+
[
160+
\Magento\Framework\EntityManager\MetadataPool::class,
161+
$this->metadataPoolMock
162+
],
163+
[
164+
\Magento\Catalog\Model\Product\Gallery\ReadHandler::class,
165+
$this->galleryReadHandlerMock
121166
]
122167
]);
123168
$this->collection = $helper->getObject(
@@ -150,8 +195,8 @@ protected function setUp()
150195

151196
public function testAddProductCategoriesFilter()
152197
{
153-
$condition = ['in' => [1,2]];
154-
$values = [1,2];
198+
$condition = ['in' => [1, 2]];
199+
$values = [1, 2];
155200
$conditionType = 'nin';
156201
$preparedSql = "category_id IN(1,2)";
157202
$tableName = "catalog_category_product";
@@ -174,6 +219,47 @@ public function testAddProductCategoriesFilter()
174219
$this->collection->addCategoriesFilter([$conditionType => $values]);
175220
}
176221

222+
public function testAddMediaGalleryData()
223+
{
224+
$attributeId = 42;
225+
$itemId = 4242;
226+
$linkField = 'entity_id';
227+
$mediaGalleriesMock = [[$linkField => $itemId]];
228+
$itemMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
229+
->disableOriginalConstructor()
230+
->getMock();
231+
$attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
232+
->disableOriginalConstructor()
233+
->getMock();
234+
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
235+
->disableOriginalConstructor()
236+
->getMock();
237+
$metadataMock = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
238+
->disableOriginalConstructor()
239+
->getMock();
240+
$this->collection->addItem($itemMock);
241+
$reflection = new \ReflectionClass(get_class($this->collection));
242+
$reflectionProperty = $reflection->getProperty('_isCollectionLoaded');
243+
$reflectionProperty->setAccessible(true);
244+
$reflectionProperty->setValue($this->collection, true);
245+
246+
$this->galleryResourceMock->expects($this->once())->method('createBatchBaseSelect')->willReturn($selectMock);
247+
$attributeMock->expects($this->once())->method('getAttributeId')->willReturn($attributeId);
248+
$this->entityMock->expects($this->once())->method('getAttribute')->willReturn($attributeMock);
249+
$itemMock->expects($this->atLeastOnce())->method('getId')->willReturn($itemId);
250+
$selectMock->expects($this->once())->method('where')->with('entity.' . $linkField . ' IN (?)', [$itemId]);
251+
$this->metadataPoolMock->expects($this->once())->method('getMetadata')->willReturn($metadataMock);
252+
$metadataMock->expects($this->once())->method('getLinkField')->willReturn($linkField);
253+
254+
$this->connectionMock->expects($this->once())->method('fetchAll')->with($selectMock)->willReturn(
255+
[['entity_id' => $itemId]]
256+
);
257+
$this->galleryReadHandlerMock->expects($this->once())->method('addMediaDataToProduct')
258+
->with($itemMock, $mediaGalleriesMock);
259+
260+
$this->assertSame($this->collection, $this->collection->addMediaGalleryData());
261+
}
262+
177263
/**
178264
* @param $map
179265
*/

0 commit comments

Comments
 (0)