Skip to content

Commit db987b2

Browse files
committed
Merge branch 'MAGETWO-54283' into pr_branch
2 parents 792e1f3 + 762556f commit db987b2

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

app/code/Magento/CatalogWidget/Block/Product/ProductsList.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
namespace Magento\CatalogWidget\Block\Product;
1010

11+
use Magento\Framework\DataObject\IdentityInterface;
12+
use Magento\Widget\Block\BlockInterface;
13+
1114
/**
1215
* Catalog Products List widget block
1316
* Class ProductsList
1417
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1518
*/
16-
class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements \Magento\Widget\Block\BlockInterface
19+
class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements BlockInterface, IdentityInterface
1720
{
1821
/**
1922
* Default value for products count that will be shown
@@ -78,6 +81,9 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
7881
*/
7982
protected $conditionsHelper;
8083

84+
/** @var array */
85+
private $widgetIdentities;
86+
8187
/**
8288
* @param \Magento\Catalog\Block\Product\Context $context
8389
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
@@ -327,7 +333,19 @@ public function getPagerHtml()
327333
*/
328334
public function getIdentities()
329335
{
330-
return [\Magento\Catalog\Model\Product::CACHE_TAG];
336+
if ($this->widgetIdentities === null) {
337+
$collection = $this->productCollectionFactory->create();
338+
$conditions = $this->getConditions();
339+
$conditions->collectValidatedAttributes($collection);
340+
$this->sqlBuilder->attachConditionToCollection($collection, $conditions);
341+
$this->widgetIdentities = [];
342+
foreach ($collection as $product) {
343+
$this->widgetIdentities[] = \Magento\Catalog\Model\Product::CACHE_TAG . '_' . $product->getId();
344+
}
345+
$this->widgetIdentities = $this->widgetIdentities ?: [\Magento\Catalog\Model\Product::CACHE_TAG];
346+
}
347+
348+
return $this->widgetIdentities;
331349
}
332350

333351
/**

app/code/Magento/CatalogWidget/Test/Unit/Block/Product/ProductsListTest.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,10 @@ public function testCreateCollection($pagerEnable, $productsCount, $productsPerP
261261
$this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
262262
$this->productsList->setData('conditions_encoded', 'some_serialized_conditions');
263263

264-
$conditions = $this->getMockBuilder('\Magento\Rule\Model\Condition\Combine')
265-
->setMethods(['collectValidatedAttributes'])
266-
->disableOriginalConstructor()
267-
->getMock();
268-
$conditions->expects($this->once())->method('collectValidatedAttributes')
269-
->with($collection)
270-
->willReturnSelf();
271-
272264
$this->builder->expects($this->once())->method('attachConditionToCollection')
273-
->with($collection, $conditions)
265+
->with($collection, $this->getConditionsForCollection($collection))
274266
->willReturnSelf();
275267

276-
$this->rule->expects($this->once())->method('loadPost')->willReturnSelf();
277-
$this->rule->expects($this->once())->method('getConditions')->willReturn($conditions);
278-
279268
if ($productsPerPage) {
280269
$this->productsList->setData('products_per_page', $productsPerPage);
281270
} else {
@@ -333,7 +322,46 @@ public function testShowPager()
333322

334323
public function testGetIdentities()
335324
{
336-
$this->assertEquals([\Magento\Catalog\Model\Product::CACHE_TAG], $this->productsList->getIdentities());
325+
$collection = $this->getMockBuilder('\Magento\Catalog\Model\ResourceModel\Product\Collection')
326+
->setMethods([
327+
'addAttributeToSelect',
328+
'getIterator',
329+
])->disableOriginalConstructor()
330+
->getMock();
331+
332+
$this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
333+
$this->productsList->setData('conditions_encoded', 'some_serialized_conditions');
334+
335+
$this->builder->expects($this->once())->method('attachConditionToCollection')
336+
->with($collection, $this->getConditionsForCollection($collection))
337+
->willReturnSelf();
338+
$product = $this->getMock('Magento\Catalog\Api\ProductInterface', ['getId']);
339+
$product->expects($this->once())->method('getId')->willReturn('product_id');
340+
$collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$product]));
341+
342+
$this->assertEquals(
343+
[\Magento\Catalog\Model\Product::CACHE_TAG . '_' . 'product_id'],
344+
$this->productsList->getIdentities()
345+
);
346+
}
347+
348+
/**
349+
* @param $collection
350+
* @return \PHPUnit_Framework_MockObject_MockObject
351+
*/
352+
private function getConditionsForCollection($collection)
353+
{
354+
$conditions = $this->getMockBuilder('\Magento\Rule\Model\Condition\Combine')
355+
->setMethods(['collectValidatedAttributes'])
356+
->disableOriginalConstructor()
357+
->getMock();
358+
$conditions->expects($this->once())->method('collectValidatedAttributes')
359+
->with($collection)
360+
->willReturnSelf();
361+
362+
$this->rule->expects($this->once())->method('loadPost')->willReturnSelf();
363+
$this->rule->expects($this->once())->method('getConditions')->willReturn($conditions);
364+
return $conditions;
337365
}
338366

339367
public function testGetTitle()

0 commit comments

Comments
 (0)