diff --git a/app/code/Magento/Catalog/Block/Product/ListProduct.php b/app/code/Magento/Catalog/Block/Product/ListProduct.php index b181a5392905b..e6dcceb43956b 100644 --- a/app/code/Magento/Catalog/Block/Product/ListProduct.php +++ b/app/code/Magento/Catalog/Block/Product/ListProduct.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\Catalog\Block\Product; @@ -141,14 +142,7 @@ public function getLayer() */ public function getLoadedProductCollection() { - $collection = $this->_getProductCollection(); - - $categoryId = $this->getLayer()->getCurrentCategory()->getId(); - foreach ($collection as $product) { - $product->setData('category_id', $categoryId); - } - - return $collection; + return $this->_getProductCollection(); } /** @@ -205,6 +199,14 @@ protected function _beforeToHtml() $collection->load(); } + $categoryId = $this->getLayer()->getCurrentCategory()->getId(); + + if ($categoryId) { + foreach ($collection as $product) { + $product->setData('category_id', $categoryId); + } + } + return parent::_beforeToHtml(); } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php index 0e625dccd85dc..5aa0f24998863 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ListTest.php @@ -3,8 +3,13 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Catalog\Block\Product; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\MockObject; + /** * Test class for \Magento\Catalog\Block\Product\List. * @@ -12,13 +17,18 @@ * @magentoAppArea frontend * @magentoDbIsolation disabled */ -class ListTest extends \PHPUnit\Framework\TestCase +class ListTest extends TestCase { /** * @var \Magento\Catalog\Block\Product\ListProduct */ protected $_block; + /** + * @var \Magento\Catalog\Model\ResourceModel\Product\Collection|MockObject + */ + private $collectionProductMock; + protected function setUp(): void { \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class) @@ -28,6 +38,8 @@ protected function setUp(): void )->createBlock( \Magento\Catalog\Block\Product\ListProduct::class ); + + $this->collectionProductMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product\Collection::class); } public function testGetLayer() @@ -42,6 +54,7 @@ public function testGetLoadedProductCollection() $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $collection); /* Check that root category was defined for Layer as current */ $this->assertEquals(2, $this->_block->getLayer()->getCurrentCategory()->getId()); + $this->collectionProductMock->expects($this->never())->method('load'); } /**