From b0c73803961d4d703b33e0a8d506a98b68e58f06 Mon Sep 17 00:00:00 2001 From: Rafael Kassner Date: Mon, 29 May 2017 13:08:55 +0000 Subject: [PATCH] Fix for #5897: getIdentities relies on uninitialized collection --- .../Block/Product/ProductList/Related.php | 7 ++ .../Block/Product/ProductList/Upsell.php | 7 ++ .../Block/Product/ProductList/RelatedTest.php | 58 ++++++++++---- .../Block/Product/ProductList/UpsellTest.php | 79 +++++++++++++++++++ 4 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/UpsellTest.php diff --git a/app/code/Magento/Catalog/Block/Product/ProductList/Related.php b/app/code/Magento/Catalog/Block/Product/ProductList/Related.php index fc55bf4120d5b..346570b955249 100644 --- a/app/code/Magento/Catalog/Block/Product/ProductList/Related.php +++ b/app/code/Magento/Catalog/Block/Product/ProductList/Related.php @@ -116,6 +116,13 @@ protected function _beforeToHtml() */ public function getItems() { + /** + * getIdentities() depends on _itemCollection populated, but it can be empty if the block is hidden + * @see https://github.com/magento/magento2/issues/5897 + */ + if (is_null($this->_itemCollection)) { + $this->_prepareData(); + } return $this->_itemCollection; } diff --git a/app/code/Magento/Catalog/Block/Product/ProductList/Upsell.php b/app/code/Magento/Catalog/Block/Product/ProductList/Upsell.php index 0eaf4bfcc3548..ccf6a41cb95f5 100644 --- a/app/code/Magento/Catalog/Block/Product/ProductList/Upsell.php +++ b/app/code/Magento/Catalog/Block/Product/ProductList/Upsell.php @@ -135,6 +135,13 @@ protected function _beforeToHtml() */ public function getItemCollection() { + /** + * getIdentities() depends on _itemCollection populated, but it can be empty if the block is hidden + * @see https://github.com/magento/magento2/issues/5897 + */ + if (is_null($this->_itemCollection)) { + $this->_prepareData(); + } return $this->_itemCollection; } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/RelatedTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/RelatedTest.php index 60153537abb93..f5778fe96d426 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/RelatedTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/RelatedTest.php @@ -12,7 +12,22 @@ */ class RelatedTest extends \PHPUnit_Framework_TestCase { - public function testAll() + /** + * @var \Magento\Catalog\Block\Product\ProductList\Related + */ + protected $block; + + /** + * @var \Magento\Catalog\Api\Data\ProductInterface + */ + protected $product; + + /** + * @var \Magento\Catalog\Api\Data\ProductInterface + */ + protected $relatedProduct; + + protected function setUp() { /** @var $objectManager \Magento\TestFramework\ObjectManager */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); @@ -21,35 +36,50 @@ public function testAll() /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); - $product = $productRepository->get('simple'); - $productWithCross = $productRepository->get('simple_with_cross'); - $objectManager->get(\Magento\Framework\Registry::class)->register('product', $productWithCross); + $this->relatedProduct = $productRepository->get('simple'); + $this->product = $productRepository->get('simple_with_cross'); + $objectManager->get(\Magento\Framework\Registry::class)->register('product', $this->product); - /** @var $block \Magento\Catalog\Block\Product\ProductList\Related */ - $block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class) + $this->block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class) ->createBlock(\Magento\Catalog\Block\Product\ProductList\Related::class); - $block->setLayout($objectManager->get(\Magento\Framework\View\LayoutInterface::class)); - $block->setTemplate('Magento_Catalog::product/list/items.phtml'); - $block->setType('related'); - $block->addChild('addto', \Magento\Catalog\Block\Product\ProductList\Item\Container::class); - $block->getChildBlock( + $this->block->setLayout($objectManager->get(\Magento\Framework\View\LayoutInterface::class)); + $this->block->setTemplate('Magento_Catalog::product/list/items.phtml'); + $this->block->setType('related'); + $this->block->addChild('addto', \Magento\Catalog\Block\Product\ProductList\Item\Container::class); + $this->block->getChildBlock( 'addto' )->addChild( 'compare', \Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare::class, ['template' => 'Magento_Catalog::product/list/addto/compare.phtml'] ); + } - $html = $block->toHtml(); + /** + * @magentoAppIsolation enabled + */ + public function testAll() + { + $html = $this->block->toHtml(); $this->assertNotEmpty($html); $this->assertContains('Simple Related Product', $html); /* name */ - $this->assertContains('"product":"' . $product->getId() .'"', $html); + $this->assertContains('"product":"' . $this->relatedProduct->getId() . '"', $html); /* part of url */ $this->assertInstanceOf( \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class, - $block->getItems() + $this->block->getItems() ); } + + /** + * @magentoAppIsolation enabled + */ + public function testGetIdentities() + { + $expectedTags = ['cat_p_' . $this->relatedProduct->getId(), 'cat_p']; + $tags = $this->block->getIdentities(); + $this->assertEquals($expectedTags, $tags); + } } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/UpsellTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/UpsellTest.php new file mode 100644 index 0000000000000..8739faf0323ab --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ProductList/UpsellTest.php @@ -0,0 +1,79 @@ +loadArea(\Magento\Framework\App\Area::AREA_FRONTEND); + + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ + $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + + $this->upsellProduct = $productRepository->get('simple'); + $this->product = $productRepository->get('simple_with_upsell'); + $objectManager->get(\Magento\Framework\Registry::class)->register('product', $this->product); + + $this->block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class) + ->createBlock(\Magento\Catalog\Block\Product\ProductList\Upsell::class); + + $this->block->setLayout($objectManager->get(\Magento\Framework\View\LayoutInterface::class)); + $this->block->setTemplate('Magento_Catalog::product/list/items.phtml'); + $this->block->setType('upsell'); + $this->block->addChild('addto', \Magento\Catalog\Block\Product\ProductList\Item\Container::class); + $this->block->getChildBlock( + 'addto' + )->addChild( + 'compare', + \Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare::class, + ['template' => 'Magento_Catalog::product/list/addto/compare.phtml'] + ); + } + + /** + * @magentoAppIsolation enabled + */ + public function testAll() + { + $html = $this->block->toHtml(); + $this->assertNotEmpty($html); + $this->assertContains('Simple Up Sell', $html); + $this->assertCount(1, $this->block->getItems()); + } + + /** + * @magentoAppIsolation enabled + */ + public function testGetIdentities() + { + $expectedTags = ['cat_p_' . $this->upsellProduct->getId(), 'cat_p']; + $tags = $this->block->getIdentities(); + $this->assertEquals($expectedTags, $tags); + } +}