Skip to content

Commit 7296626

Browse files
authored
MAGETWO-66189: [GitHub][PR] Bugfix for _getProductCollection on a product page #7598
2 parents c2a9d42 + fddceb6 commit 7296626

File tree

2 files changed

+94
-11
lines changed

2 files changed

+94
-11
lines changed

app/code/Magento/Catalog/Block/Product/ListProduct.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function __construct(
8484
}
8585

8686
/**
87-
* Retrieve loaded category collection
87+
* Retrieve loaded product collection
8888
*
8989
* The goal of this method is to choose whether the existing collection should be returned
9090
* or a new one should be initialized.
@@ -357,7 +357,7 @@ private function initializeProductCollection()
357357
// if the product is associated with any category
358358
if ($categories->count()) {
359359
// show products from this category
360-
$this->setCategoryId(current($categories->getIterator()));
360+
$this->setCategoryId(current($categories->getIterator())->getId());
361361
}
362362
}
363363

app/code/Magento/Catalog/Test/Unit/Block/Product/ListProductTest.php

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ class ListProductTest extends \PHPUnit_Framework_TestCase
4949
* @var \Magento\Framework\Url\Helper\Data | \PHPUnit_Framework_MockObject_MockObject
5050
*/
5151
protected $urlHelperMock;
52+
53+
/**
54+
* @var \Magento\Catalog\Model\ResourceModel\Category | \PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
protected $catCollectionMock;
57+
58+
/**
59+
* @var \Magento\Catalog\Model\ResourceModel\Product | \PHPUnit_Framework_MockObject_MockObject
60+
*/
61+
protected $prodCollectionMock;
62+
63+
/**
64+
* @var \Magento\Framework\View\LayoutInterface | \PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
protected $layoutMock;
67+
68+
/**
69+
* @var \Magento\Catalog\Block\Product\ProductList\Toolbar | \PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
protected $toolbarMock;
5272

5373
protected function setUp()
5474
{
@@ -92,6 +112,34 @@ protected function setUp()
92112
'',
93113
false
94114
);
115+
$this->catCollectionMock = $this->getMock(
116+
\Magento\Catalog\Model\ResourceModel\Category\Collection::class,
117+
[],
118+
[],
119+
'',
120+
false
121+
);
122+
$this->prodCollectionMock = $this->getMock(
123+
\Magento\Catalog\Model\ResourceModel\Product\Collection::class,
124+
[],
125+
[],
126+
'',
127+
false
128+
);
129+
$this->layoutMock = $this->getMock(
130+
\Magento\Framework\View\LayoutInterface::class,
131+
[],
132+
[],
133+
'',
134+
false
135+
);
136+
$this->toolbarMock = $this->getMock(
137+
\Magento\Catalog\Block\Product\ProductList\Toolbar::class,
138+
[],
139+
[],
140+
'',
141+
false
142+
);
95143

96144
$this->urlHelperMock = $this->getMockBuilder(\Magento\Framework\Url\Helper\Data::class)
97145
->disableOriginalConstructor()->getMock();
@@ -105,6 +153,8 @@ protected function setUp()
105153
'urlHelper' => $this->urlHelperMock,
106154
]
107155
);
156+
$this->block->setToolbarBlockName('mock');
157+
$this->block->setLayout($this->layoutMock);
108158
}
109159

110160
protected function tearDown()
@@ -121,26 +171,59 @@ public function testGetIdentities()
121171
->method('getIdentities')
122172
->will($this->returnValue([$productTag]));
123173

124-
$itemsCollection = new \ReflectionProperty(
125-
\Magento\Catalog\Block\Product\ListProduct::class,
126-
'_productCollection'
127-
);
128-
$itemsCollection->setAccessible(true);
129-
$itemsCollection->setValue($this->block, [$this->productMock]);
174+
$this->productMock->expects($this->once())
175+
->method('getCategoryCollection')
176+
->will($this->returnValue($this->catCollectionMock));
177+
178+
$this->catCollectionMock->expects($this->once())
179+
->method('load')
180+
->will($this->returnValue($this->catCollectionMock));
181+
182+
$this->catCollectionMock->expects($this->once())
183+
->method('setPage')
184+
->will($this->returnValue($this->catCollectionMock));
185+
186+
$this->catCollectionMock->expects($this->once())
187+
->method('count')
188+
->will($this->returnValue(1));
189+
190+
$this->registryMock->expects($this->any())
191+
->method('registry')
192+
->will($this->returnValue($this->productMock));
130193

131194
$currentCategory = $this->getMock(\Magento\Catalog\Model\Category::class, [], [], '', false);
132-
$currentCategory->expects($this->once())
195+
$currentCategory->expects($this->any())
133196
->method('getId')
134197
->will($this->returnValue('1'));
135198

136-
$this->layerMock->expects($this->once())
199+
$this->catCollectionMock->expects($this->once())
200+
->method('getIterator')
201+
->will($this->returnValue([$currentCategory]));
202+
203+
$this->prodCollectionMock->expects($this->any())
204+
->method('getIterator')
205+
->will($this->returnValue(new \ArrayIterator([$this->productMock])));
206+
207+
$this->layerMock->expects($this->any())
137208
->method('getCurrentCategory')
138209
->will($this->returnValue($currentCategory));
139210

211+
$this->layerMock->expects($this->once())
212+
->method('getProductCollection')
213+
->will($this->returnValue($this->prodCollectionMock));
214+
215+
$this->layoutMock->expects($this->once())
216+
->method('getBlock')
217+
->will($this->returnValue($this->toolbarMock));
218+
140219
$this->assertEquals(
141-
[$productTag, $categoryTag ],
220+
[$productTag, $categoryTag],
142221
$this->block->getIdentities()
143222
);
223+
$this->assertEquals(
224+
'1',
225+
$this->block->getCategoryId()
226+
);
144227
}
145228

146229
public function testGetAddToCartPostParams()

0 commit comments

Comments
 (0)