Skip to content

Commit 8db40e1

Browse files
author
Joan He
committed
MAGETWO-43857: [github] cache types don't properly invalidate all the time #1844
1 parent 503ab0b commit 8db40e1

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

app/code/Magento/Theme/Model/Design/Backend/Theme.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class Theme extends Value
1616
*/
1717
protected $_design = null;
1818

19+
/**
20+
* Path to config node with list of caches
21+
*
22+
* @var string
23+
*/
24+
const XML_PATH_INVALID_CACHES = 'design/invalid_caches';
25+
1926
/**
2027
* Initialize dependencies
2128
*
@@ -59,17 +66,22 @@ public function beforeSave()
5966
/**
6067
* {@inheritdoc}
6168
*
62-
* {@inheritdoc}. In addition, it cleans all Magento cache
69+
* {@inheritdoc}. In addition, it sets status 'invalidate' for blocks and other output caches
6370
*
6471
* @return $this
6572
*/
6673
public function afterSave()
6774
{
68-
parent::afterSave();
75+
$types = array_keys(
76+
$this->_config->getValue(
77+
self::XML_PATH_INVALID_CACHES,
78+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
79+
)
80+
);
6981
if ($this->isValueChanged()) {
70-
$this->_cacheManager->clean();
71-
$this->_eventManager->dispatch('adminhtml_cache_flush_system');
82+
$this->cacheTypeList->invalidate($types);
7283
}
73-
return $this;
84+
85+
return parent::afterSave();
7486
}
7587
}

app/code/Magento/Theme/Test/Unit/Model/Design/Backend/ThemeTest.php

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class ThemeTest extends \PHPUnit_Framework_TestCase
1818
protected $model;
1919

2020
/**
21-
* @var \Magento\Framework\Model\Context
21+
* @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
2222
*/
23-
protected $context;
23+
protected $contextMock;
2424

2525
/**
2626
* @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -32,50 +32,30 @@ class ThemeTest extends \PHPUnit_Framework_TestCase
3232
*/
3333
protected $cacheTypeListMock;
3434

35-
/**
36-
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
37-
*/
38-
protected $eventManagerMock;
39-
40-
/**
41-
* @var \Magento\Framework\App\CacheInterface | \PHPUnit_Framework_MockObject_MockObject
42-
*/
43-
protected $cacheManagerMock;
44-
4535
/**
4636
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
4737
*/
4838
protected $configMock;
4939

5040
protected function setUp()
5141
{
52-
$objectManager = new ObjectManager($this);
53-
$this->cacheManagerMock = $this->getMockBuilder('Magento\Framework\App\CacheInterface')
42+
$this->contextMock = $this->getMockBuilder('Magento\Framework\Model\Context')
5443
->disableOriginalConstructor()
5544
->getMock();
56-
$this->eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')
57-
->disableOriginalConstructor()
58-
->getMock();
59-
$this->context = $objectManager->getObject(
60-
'Magento\Framework\Model\Context',
61-
[
62-
'cacheManager' => $this->cacheManagerMock,
63-
'eventDispatcher' => $this->eventManagerMock,
64-
]
65-
);
66-
6745
$this->designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface')->getMock();
6846
$this->cacheTypeListMock = $this->getMockBuilder('Magento\Framework\App\Cache\TypeListInterface')
6947
->disableOriginalConstructor()
7048
->getMock();
49+
$this->contextMock->expects($this->once())
50+
->method('getEventDispatcher')
51+
->willReturn($this->getMockBuilder('Magento\Framework\Event\ManagerInterface')->getMock());
7152
$this->configMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')->getMock();
7253

73-
74-
$this->model = $objectManager->getObject(
54+
$this->model = (new ObjectManager($this))->getObject(
7555
'Magento\Theme\Model\Design\Backend\Theme',
7656
[
7757
'design' => $this->designMock,
78-
'context' => $this->context,
58+
'context' => $this->contextMock,
7959
'cacheTypeList' => $this->cacheTypeListMock,
8060
'config' => $this->configMock,
8161
]
@@ -106,16 +86,25 @@ public function testAfterSave($callNumber, $oldValue)
10686
{
10787
$this->cacheTypeListMock->expects($this->exactly($callNumber))
10888
->method('invalidate');
109-
$this->cacheManagerMock->expects($this->exactly($callNumber))
110-
->method('clean');
11189
$this->configMock->expects($this->any())
11290
->method('getValue')
113-
->willReturn($oldValue);
114-
if ($callNumber) {
115-
$this->eventManagerMock->expects($this->at(3))
116-
->method('dispatch')
117-
->with('adminhtml_cache_flush_system');
118-
}
91+
->willReturnMap(
92+
[
93+
[
94+
Theme::XML_PATH_INVALID_CACHES,
95+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
96+
null,
97+
['block_html' => 1, 'layout' => 1, 'translate' => 1]
98+
],
99+
[
100+
null,
101+
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
102+
null,
103+
$oldValue
104+
],
105+
106+
]
107+
);
119108
$this->model->setValue('some_value');
120109
$this->assertInstanceOf(get_class($this->model), $this->model->afterSave());
121110
}
@@ -124,7 +113,7 @@ public function afterSaveDataProvider()
124113
{
125114
return [
126115
[0, 'some_value'],
127-
[1, 'other_value'],
116+
[2, 'other_value'],
128117
];
129118
}
130119
}

app/code/Magento/Theme/etc/config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
99
<default>
1010
<design>
11+
<invalid_caches>
12+
<block_html />
13+
<layout />
14+
<translate />
15+
</invalid_caches>
1116
<head translate="default_description">
1217
<default_title>Magento Commerce</default_title>
1318
<default_description>Default Description</default_description>

0 commit comments

Comments
 (0)