Skip to content

Commit 28ce05f

Browse files
ENGCOM-8913: Move assign category ids to beforeToHtml instead inside load collection #32390
2 parents af3fbb6 + e17dcca commit 28ce05f

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Block\Product;
89

@@ -141,14 +142,7 @@ public function getLayer()
141142
*/
142143
public function getLoadedProductCollection()
143144
{
144-
$collection = $this->_getProductCollection();
145-
146-
$categoryId = $this->getLayer()->getCurrentCategory()->getId();
147-
foreach ($collection as $product) {
148-
$product->setData('category_id', $categoryId);
149-
}
150-
151-
return $collection;
145+
return $this->_getProductCollection();
152146
}
153147

154148
/**
@@ -205,6 +199,14 @@ protected function _beforeToHtml()
205199
$collection->load();
206200
}
207201

202+
$categoryId = $this->getLayer()->getCurrentCategory()->getId();
203+
204+
if ($categoryId) {
205+
foreach ($collection as $product) {
206+
$product->setData('category_id', $categoryId);
207+
}
208+
}
209+
208210
return parent::_beforeToHtml();
209211
}
210212

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,32 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Block\Product;
79

10+
use PHPUnit\Framework\TestCase;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
813
/**
914
* Test class for \Magento\Catalog\Block\Product\List.
1015
*
1116
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
1217
* @magentoAppArea frontend
1318
* @magentoDbIsolation disabled
1419
*/
15-
class ListTest extends \PHPUnit\Framework\TestCase
20+
class ListTest extends TestCase
1621
{
1722
/**
1823
* @var \Magento\Catalog\Block\Product\ListProduct
1924
*/
2025
protected $_block;
2126

27+
/**
28+
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection|MockObject
29+
*/
30+
private $collectionProductMock;
31+
2232
protected function setUp(): void
2333
{
2434
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
@@ -28,6 +38,8 @@ protected function setUp(): void
2838
)->createBlock(
2939
\Magento\Catalog\Block\Product\ListProduct::class
3040
);
41+
42+
$this->collectionProductMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
3143
}
3244

3345
public function testGetLayer()
@@ -42,6 +54,7 @@ public function testGetLoadedProductCollection()
4254
$this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $collection);
4355
/* Check that root category was defined for Layer as current */
4456
$this->assertEquals(2, $this->_block->getLayer()->getCurrentCategory()->getId());
57+
$this->collectionProductMock->expects($this->never())->method('load');
4558
}
4659

4760
/**

0 commit comments

Comments
 (0)