Skip to content

Commit 56441ae

Browse files
author
Oleksii Korshenko
committed
MAGETWO-69540: Fix for #5897: getIdentities relies on uninitialized collection #9777
- Merge Pull Request #9777 from kassner/magento2:patch-11
2 parents 83e7b3b + b0c7380 commit 56441ae

File tree

4 files changed

+137
-14
lines changed

4 files changed

+137
-14
lines changed

app/code/Magento/Catalog/Block/Product/ProductList/Related.php

+7
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ protected function _beforeToHtml()
116116
*/
117117
public function getItems()
118118
{
119+
/**
120+
* getIdentities() depends on _itemCollection populated, but it can be empty if the block is hidden
121+
* @see https://github.com/magento/magento2/issues/5897
122+
*/
123+
if (is_null($this->_itemCollection)) {
124+
$this->_prepareData();
125+
}
119126
return $this->_itemCollection;
120127
}
121128

app/code/Magento/Catalog/Block/Product/ProductList/Upsell.php

+7
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ protected function _beforeToHtml()
135135
*/
136136
public function getItemCollection()
137137
{
138+
/**
139+
* getIdentities() depends on _itemCollection populated, but it can be empty if the block is hidden
140+
* @see https://github.com/magento/magento2/issues/5897
141+
*/
142+
if (is_null($this->_itemCollection)) {
143+
$this->_prepareData();
144+
}
138145
return $this->_itemCollection;
139146
}
140147

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/RelatedTest.php

+44-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@
1212
*/
1313
class RelatedTest extends \PHPUnit_Framework_TestCase
1414
{
15-
public function testAll()
15+
/**
16+
* @var \Magento\Catalog\Block\Product\ProductList\Related
17+
*/
18+
protected $block;
19+
20+
/**
21+
* @var \Magento\Catalog\Api\Data\ProductInterface
22+
*/
23+
protected $product;
24+
25+
/**
26+
* @var \Magento\Catalog\Api\Data\ProductInterface
27+
*/
28+
protected $relatedProduct;
29+
30+
protected function setUp()
1631
{
1732
/** @var $objectManager \Magento\TestFramework\ObjectManager */
1833
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
@@ -21,35 +36,50 @@ public function testAll()
2136
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
2237
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
2338

24-
$product = $productRepository->get('simple');
25-
$productWithCross = $productRepository->get('simple_with_cross');
26-
$objectManager->get(\Magento\Framework\Registry::class)->register('product', $productWithCross);
39+
$this->relatedProduct = $productRepository->get('simple');
40+
$this->product = $productRepository->get('simple_with_cross');
41+
$objectManager->get(\Magento\Framework\Registry::class)->register('product', $this->product);
2742

28-
/** @var $block \Magento\Catalog\Block\Product\ProductList\Related */
29-
$block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class)
43+
$this->block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class)
3044
->createBlock(\Magento\Catalog\Block\Product\ProductList\Related::class);
3145

32-
$block->setLayout($objectManager->get(\Magento\Framework\View\LayoutInterface::class));
33-
$block->setTemplate('Magento_Catalog::product/list/items.phtml');
34-
$block->setType('related');
35-
$block->addChild('addto', \Magento\Catalog\Block\Product\ProductList\Item\Container::class);
36-
$block->getChildBlock(
46+
$this->block->setLayout($objectManager->get(\Magento\Framework\View\LayoutInterface::class));
47+
$this->block->setTemplate('Magento_Catalog::product/list/items.phtml');
48+
$this->block->setType('related');
49+
$this->block->addChild('addto', \Magento\Catalog\Block\Product\ProductList\Item\Container::class);
50+
$this->block->getChildBlock(
3751
'addto'
3852
)->addChild(
3953
'compare',
4054
\Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare::class,
4155
['template' => 'Magento_Catalog::product/list/addto/compare.phtml']
4256
);
57+
}
4358

44-
$html = $block->toHtml();
59+
/**
60+
* @magentoAppIsolation enabled
61+
*/
62+
public function testAll()
63+
{
64+
$html = $this->block->toHtml();
4565
$this->assertNotEmpty($html);
4666
$this->assertContains('Simple Related Product', $html);
4767
/* name */
48-
$this->assertContains('"product":"' . $product->getId() .'"', $html);
68+
$this->assertContains('"product":"' . $this->relatedProduct->getId() . '"', $html);
4969
/* part of url */
5070
$this->assertInstanceOf(
5171
\Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class,
52-
$block->getItems()
72+
$this->block->getItems()
5373
);
5474
}
75+
76+
/**
77+
* @magentoAppIsolation enabled
78+
*/
79+
public function testGetIdentities()
80+
{
81+
$expectedTags = ['cat_p_' . $this->relatedProduct->getId(), 'cat_p'];
82+
$tags = $this->block->getIdentities();
83+
$this->assertEquals($expectedTags, $tags);
84+
}
5585
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Block\Product\ProductList;
7+
8+
/**
9+
* Test class for \Magento\Catalog\Block\Product\List\Upsell.
10+
*
11+
* @magentoDataFixture Magento/Catalog/_files/products_upsell.php
12+
*/
13+
class UpsellTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var \Magento\Catalog\Block\Product\ProductList\Upsell
17+
*/
18+
protected $block;
19+
20+
/**
21+
* @var \Magento\Catalog\Api\Data\ProductInterface
22+
*/
23+
protected $product;
24+
25+
/**
26+
* @var \Magento\Catalog\Api\Data\ProductInterface
27+
*/
28+
protected $upsellProduct;
29+
30+
protected function setUp()
31+
{
32+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
33+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
34+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
35+
36+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
37+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
38+
39+
$this->upsellProduct = $productRepository->get('simple');
40+
$this->product = $productRepository->get('simple_with_upsell');
41+
$objectManager->get(\Magento\Framework\Registry::class)->register('product', $this->product);
42+
43+
$this->block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class)
44+
->createBlock(\Magento\Catalog\Block\Product\ProductList\Upsell::class);
45+
46+
$this->block->setLayout($objectManager->get(\Magento\Framework\View\LayoutInterface::class));
47+
$this->block->setTemplate('Magento_Catalog::product/list/items.phtml');
48+
$this->block->setType('upsell');
49+
$this->block->addChild('addto', \Magento\Catalog\Block\Product\ProductList\Item\Container::class);
50+
$this->block->getChildBlock(
51+
'addto'
52+
)->addChild(
53+
'compare',
54+
\Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare::class,
55+
['template' => 'Magento_Catalog::product/list/addto/compare.phtml']
56+
);
57+
}
58+
59+
/**
60+
* @magentoAppIsolation enabled
61+
*/
62+
public function testAll()
63+
{
64+
$html = $this->block->toHtml();
65+
$this->assertNotEmpty($html);
66+
$this->assertContains('Simple Up Sell', $html);
67+
$this->assertCount(1, $this->block->getItems());
68+
}
69+
70+
/**
71+
* @magentoAppIsolation enabled
72+
*/
73+
public function testGetIdentities()
74+
{
75+
$expectedTags = ['cat_p_' . $this->upsellProduct->getId(), 'cat_p'];
76+
$tags = $this->block->getIdentities();
77+
$this->assertEquals($expectedTags, $tags);
78+
}
79+
}

0 commit comments

Comments
 (0)