Skip to content

Commit

Permalink
MAGETWO-69540: Fix for #5897: getIdentities relies on uninitialized c…
Browse files Browse the repository at this point in the history
…ollection #9777
  • Loading branch information
ishakhsuvarov authored May 31, 2017
2 parents 8ed6be6 + 56441ae commit 0b48e93
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/Catalog/Block/Product/ProductList/Upsell.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Block\Product\ProductList;

/**
* Test class for \Magento\Catalog\Block\Product\List\Upsell.
*
* @magentoDataFixture Magento/Catalog/_files/products_upsell.php
*/
class UpsellTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Catalog\Block\Product\ProductList\Upsell
*/
protected $block;

/**
* @var \Magento\Catalog\Api\Data\ProductInterface
*/
protected $product;

/**
* @var \Magento\Catalog\Api\Data\ProductInterface
*/
protected $upsellProduct;

protected function setUp()
{
/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
\Magento\TestFramework\Helper\Bootstrap::getInstance()->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);
}
}

0 comments on commit 0b48e93

Please sign in to comment.