Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move assign category ids to beforeToHtml instead inside load collection #32390

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,32 @@
* 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.
*
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @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)
Expand All @@ -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()
Expand All @@ -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());
mrtuvn marked this conversation as resolved.
Show resolved Hide resolved
$this->collectionProductMock->expects($this->never())->method('load');
}
mrtuvn marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand Down