Skip to content

Commit

Permalink
Move assign category ids to beforeHtml instead inside load collection
Browse files Browse the repository at this point in the history
update
  • Loading branch information
mrtuvn committed Mar 7, 2021
1 parent c364929 commit c0d7665
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
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());
$this->collectionProductMock->expects($this->never())->method('load');
}

/**
Expand Down

0 comments on commit c0d7665

Please sign in to comment.