Skip to content

Commit 90a988f

Browse files
mazhalaiIvan Gavryshko
authored andcommitted
Merge remote-tracking branch 'ogresce/MAGETWO-33240-cron-fpc' into develop
2 parents bbbbf9d + 2a08259 commit 90a988f

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageCache\Model\Observer;
7+
8+
/**
9+
* An observer to invalidate full page cache when the content given is changed
10+
*/
11+
class InvalidateCacheIfChanged
12+
{
13+
/**
14+
* @var \Magento\Framework\App\Cache\TypeListInterface
15+
*/
16+
protected $typeList;
17+
18+
/**
19+
* Application config object
20+
*
21+
* @var \Magento\PageCache\Model\Config
22+
*/
23+
protected $config;
24+
25+
/**
26+
* @param \Magento\PageCache\Model\Config $config
27+
* @param \Magento\Framework\App\Cache\TypeListInterface $typeList
28+
*/
29+
public function __construct(
30+
\Magento\PageCache\Model\Config $config,
31+
\Magento\Framework\App\Cache\TypeListInterface $typeList
32+
) {
33+
$this->config = $config;
34+
$this->typeList = $typeList;
35+
}
36+
37+
/**
38+
* Invalidate full page cache if content is changed
39+
*
40+
* @param \Magento\Framework\Event\Observer $observer
41+
* @return void
42+
*/
43+
public function execute(\Magento\Framework\Event\Observer $observer)
44+
{
45+
if ($this->config->isEnabled()) {
46+
$object = $observer->getEvent()->getObject();
47+
if ($object instanceof \Magento\Framework\Object\IdentityInterface) {
48+
if ($object->getIdentities()) {
49+
$this->typeList->invalidate('full_page');
50+
}
51+
}
52+
}
53+
}
54+
}

app/code/Magento/PageCache/etc/events.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<observer name="invalidate_builtin" instance="Magento\PageCache\Model\Observer\FlushCacheByTags" method="execute" />
1414
</event>
1515
<event name="clean_cache_after_reindex">
16-
<observer name="reindex_cache_flush" instance="Magento\PageCache\Model\Observer\InvalidateCache" method="execute" />
16+
<observer name="reindex_cache_flush" instance="Magento\PageCache\Model\Observer\InvalidateCacheIfChanged" method="execute" />
1717
</event>
1818
<event name="adminhtml_cache_flush_system">
1919
<observer name="flush_system_pagecache" instance="Magento\PageCache\Model\Observer\FlushAllCache" method="execute" />
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\PageCache\Model\Observer;
8+
9+
class InvalidateCacheIfChangedTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/** @var \Magento\PageCache\Model\Observer\InvalidateCacheIfChanged */
12+
protected $model;
13+
14+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Config */
15+
protected $configMock;
16+
17+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\TypeListInterface */
18+
protected $typeListMock;
19+
20+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\Observer */
21+
protected $observerMock;
22+
23+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Object\IdentityInterface */
24+
protected $objectMock;
25+
26+
/**
27+
* Set up all mocks and data for test
28+
*/
29+
public function setUp()
30+
{
31+
$this->configMock = $this->getMock(
32+
'Magento\PageCache\Model\Config',
33+
['getType', 'isEnabled'],
34+
[],
35+
'',
36+
false
37+
);
38+
$this->typeListMock = $this->getMock('Magento\Framework\App\Cache\TypeList', [], [], '', false);
39+
40+
$this->model = new \Magento\PageCache\Model\Observer\InvalidateCacheIfChanged(
41+
$this->configMock,
42+
$this->typeListMock
43+
);
44+
45+
$this->observerMock = $this->getMock('Magento\Framework\Event\Observer', [], [], '', false);
46+
$eventMock = $this->getMock('Magento\Framework\Event', ['getObject'], [], '', false);
47+
$this->objectMock = $this->getMockForAbstractClass('Magento\Framework\Object\IdentityInterface', [], '', false);
48+
$eventMock->expects($this->any())->method('getObject')->willReturn($this->objectMock);
49+
$this->observerMock->expects($this->any())->method('getEvent')->willReturn($eventMock);
50+
}
51+
52+
/**
53+
* @dataProvider invalidateCacheDataProvider
54+
* @param bool $cacheState
55+
*/
56+
public function testExecuteChanged($cacheState)
57+
{
58+
$this->configMock->expects($this->once())->method('isEnabled')->will($this->returnValue($cacheState));
59+
60+
if ($cacheState) {
61+
$this->typeListMock->expects($this->once())->method('invalidate')->with($this->equalTo('full_page'));
62+
$this->objectMock->expects($this->once())->method('getIdentities')->will($this->returnValue(['tag_1']));
63+
}
64+
$this->model->execute($this->observerMock);
65+
}
66+
67+
/**
68+
* @dataProvider invalidateCacheDataProvider
69+
* @param bool $cacheState
70+
*/
71+
public function testExecuteNoChanged($cacheState)
72+
{
73+
$this->configMock->expects($this->once())->method('isEnabled')->will($this->returnValue($cacheState));
74+
$this->typeListMock->expects($this->never())->method('invalidate');
75+
76+
if ($cacheState) {
77+
$this->objectMock->expects($this->once())->method('getIdentities')->will($this->returnValue([]));
78+
}
79+
$this->model->execute($this->observerMock);
80+
}
81+
82+
public function invalidateCacheDataProvider()
83+
{
84+
return [[true], [false]];
85+
}
86+
}

0 commit comments

Comments
 (0)