Skip to content

Commit 1fb35ef

Browse files
author
Volodymyr Klymenko
committed
MAGETWO-21754: Public Cache Invalidation works for Product Pages if Do Mass Actions
1 parent 0ca6009 commit 1fb35ef

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Plugin\PageCache\Product;
8+
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Indexer\Model\CacheContext;
11+
use Magento\Framework\Event\ManagerInterface as EventManager;
12+
13+
class Action
14+
{
15+
/**
16+
* @var CacheContext
17+
*/
18+
protected $cacheContext;
19+
20+
/**
21+
* @var EventManager
22+
*/
23+
protected $eventManager;
24+
25+
/**
26+
* @param CacheContext $cacheContext
27+
* @param EventManager $eventManager
28+
*/
29+
public function __construct(
30+
CacheContext $cacheContext,
31+
EventManager $eventManager
32+
) {
33+
$this->cacheContext = $cacheContext;
34+
$this->eventManager = $eventManager;
35+
}
36+
37+
/**
38+
* @param Product\Action $subject
39+
* @param callable $proceed
40+
* @param $productIds
41+
* @param $attrData
42+
* @param $storeId
43+
* @return Product\Action
44+
*
45+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
46+
*/
47+
public function aroundUpdateAttributes(
48+
Product\Action $subject,
49+
\Closure $proceed,
50+
$productIds,
51+
$attrData,
52+
$storeId
53+
) {
54+
$returnValue = $proceed($productIds, $attrData, $storeId);
55+
56+
$this->cacheContext->registerEntities(Product::CACHE_TAG, $productIds);
57+
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
58+
59+
return $returnValue;
60+
}
61+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Model\Plugin\PageCache\Product;
7+
8+
use Magento\Catalog\Model\Product;
9+
10+
class ActionTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testAroundUpdateAttributes()
13+
{
14+
$productIds = [1, 2, 3];
15+
$attrData = [];
16+
$storeId = 1;
17+
18+
$productActionMock = $this->getMock('Magento\Catalog\Model\Product\Action', [], [], '', false);
19+
20+
$cacheContextMock = $this->getMock('Magento\Indexer\Model\CacheContext', [], [], '', false);
21+
$cacheContextMock->expects($this->once())
22+
->method('registerEntities')
23+
->with(Product::CACHE_TAG, $productIds);
24+
25+
26+
$eventManagerMock = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
27+
$eventManagerMock->expects($this->once())
28+
->method('dispatch')
29+
->with('clean_cache_by_tags', ['object' => $cacheContextMock]);
30+
31+
$model = new \Magento\Catalog\Model\Plugin\PageCache\Product\Action($cacheContextMock, $eventManagerMock);
32+
33+
$closureMock = function () use ($productActionMock) {
34+
return $productActionMock;
35+
};
36+
37+
$model->aroundUpdateAttributes($productActionMock, $closureMock, $productIds, $attrData, $storeId);
38+
}
39+
}

app/code/Magento/Catalog/etc/adminhtml/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,7 @@
7676
</argument>
7777
</arguments>
7878
</type>
79+
<type name="Magento\Catalog\Model\Product\Action">
80+
<plugin name="invalidate_pagecache_after_update_product_attributes" type="Magento\Catalog\Model\Plugin\PageCache\Product\Action" />
81+
</type>
7982
</config>

0 commit comments

Comments
 (0)