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

Fix for #5897: getIdentities relies on uninitialized collection #9777

Merged
merged 1 commit into from
Jun 1, 2017
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
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);
}
}