Skip to content

Commit 356dc37

Browse files
authored
Merge pull request #431 from magento-troll/MAGETWO-58362
[Troll] Bugfix P0 backport 2.1.3 - MAGETWO-58362 [Backport][Github][Cloud][Customer]Fix Varnish X-Header - for 2.1
2 parents e7cb393 + c4e0ce1 commit 356dc37

File tree

19 files changed

+669
-65
lines changed

19 files changed

+669
-65
lines changed

app/code/Magento/CacheInvalidate/Observer/InvalidateVarnishObserver.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class InvalidateVarnishObserver implements ObserverInterface
2121
*/
2222
protected $purgeCache;
2323

24+
/**
25+
* Invalidation tags resolver
26+
*
27+
* @var \Magento\Framework\App\Cache\Tag\Resolver
28+
*/
29+
private $tagResolver;
30+
2431
/**
2532
* @param \Magento\PageCache\Model\Config $config
2633
* @param \Magento\CacheInvalidate\Model\PurgeCache $purgeCache
@@ -42,18 +49,35 @@ public function __construct(
4249
*/
4350
public function execute(\Magento\Framework\Event\Observer $observer)
4451
{
52+
$object = $observer->getEvent()->getObject();
53+
if (!is_object($object)) {
54+
return;
55+
}
4556
if ($this->config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->config->isEnabled()) {
46-
$object = $observer->getEvent()->getObject();
47-
if ($object instanceof \Magento\Framework\DataObject\IdentityInterface) {
48-
$tags = [];
49-
$pattern = "((^|,)%s(,|$))";
50-
foreach ($object->getIdentities() as $tag) {
51-
$tags[] = sprintf($pattern, $tag);
52-
}
53-
if (!empty($tags)) {
54-
$this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
55-
}
57+
$bareTags = $this->getTagResolver()->getTags($object);
58+
59+
$tags = [];
60+
$pattern = "((^|,)%s(,|$))";
61+
foreach ($bareTags as $tag) {
62+
$tags[] = sprintf($pattern, $tag);
63+
}
64+
if (!empty($tags)) {
65+
$this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
5666
}
67+
68+
}
69+
}
70+
71+
/**
72+
* @deprecated
73+
* @return \Magento\Framework\App\Cache\Tag\Resolver
74+
*/
75+
private function getTagResolver()
76+
{
77+
if ($this->tagResolver === null) {
78+
$this->tagResolver = \Magento\Framework\App\ObjectManager::getInstance()
79+
->get(\Magento\Framework\App\Cache\Tag\Resolver::class);
5780
}
81+
return $this->tagResolver;
5882
}
5983
}

app/code/Magento/CacheInvalidate/Test/Unit/Observer/InvalidateVarnishObserverTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\CacheInvalidate\Test\Unit\Observer;
77

8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
810
class InvalidateVarnishObserverTest extends \PHPUnit_Framework_TestCase
911
{
1012
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Observer\InvalidateVarnishObserver */
@@ -22,11 +24,16 @@ class InvalidateVarnishObserverTest extends \PHPUnit_Framework_TestCase
2224
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\DataObject\ */
2325
protected $observerObject;
2426

27+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Cache\Tag\Resolver */
28+
private $tagResolver;
29+
2530
/**
2631
* Set up all mocks and data for test
2732
*/
2833
protected function setUp()
2934
{
35+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
36+
3037
$this->configMock = $this->getMock(
3138
'Magento\PageCache\Model\Config',
3239
['getType', 'isEnabled'],
@@ -39,6 +46,10 @@ protected function setUp()
3946
$this->configMock,
4047
$this->purgeCache
4148
);
49+
50+
$this->tagResolver = $this->getMock(\Magento\Framework\App\Cache\Tag\Resolver::class, [], [], '', false);
51+
$helper->setBackwardCompatibleProperty($this->model, 'tagResolver', $this->tagResolver);
52+
4253
$this->observerMock = $this->getMock(
4354
'Magento\Framework\Event\Observer',
4455
['getEvent'],
@@ -68,7 +79,7 @@ public function testInvalidateVarnish()
6879
$eventMock = $this->getMock('Magento\Framework\Event', ['getObject'], [], '', false);
6980
$eventMock->expects($this->once())->method('getObject')->will($this->returnValue($this->observerObject));
7081
$this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
71-
$this->observerObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags));
82+
$this->tagResolver->expects($this->once())->method('getTags')->will($this->returnValue($tags));
7283
$this->purgeCache->expects($this->once())->method('sendPurgeRequest')->with($pattern);
7384

7485
$this->model->execute($this->observerMock);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model\Product\Cache\Tag;
7+
8+
use Magento\Framework\App\Cache\Tag\StrategyInterface;
9+
10+
/**
11+
* Add parent invalidation tags
12+
*/
13+
class Configurable implements StrategyInterface
14+
{
15+
/**
16+
* Configurable product type resource
17+
*
18+
* @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable
19+
*/
20+
private $catalogProductTypeConfigurable;
21+
22+
/**
23+
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable
24+
*/
25+
public function __construct(
26+
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable
27+
) {
28+
$this->catalogProductTypeConfigurable = $catalogProductTypeConfigurable;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getTags($object)
35+
{
36+
if (!is_object($object)) {
37+
throw new \InvalidArgumentException('Provided argument is not an object');
38+
}
39+
40+
if (!($object instanceof \Magento\Catalog\Model\Product)) {
41+
throw new \InvalidArgumentException('Provided argument must be a product');
42+
}
43+
44+
$result = $object->getIdentities();
45+
46+
foreach ($this->catalogProductTypeConfigurable->getParentIdsByChild($object->getId()) as $parentId) {
47+
$result[] = \Magento\Catalog\Model\Product::CACHE_TAG . '_' . $parentId;
48+
}
49+
return $result;
50+
}
51+
}

app/code/Magento/ConfigurableProduct/Plugin/Model/Product.php

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ConfigurableProduct\Test\Unit\Model\Product\Cache\Tag;
8+
9+
use \Magento\ConfigurableProduct\Model\Product\Cache\Tag\Configurable;
10+
11+
class ConfigurableTest extends \PHPUnit_Framework_TestCase
12+
{
13+
14+
/**
15+
* @var \PHPUnit_Framework_MockObject_MockObject|Configurable
16+
*/
17+
private $typeResource;
18+
19+
/**
20+
* @var Configurable
21+
*/
22+
private $model;
23+
24+
protected function setUp()
25+
{
26+
$this->typeResource = $this->getMock(
27+
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable::class,
28+
[],
29+
[],
30+
'',
31+
false
32+
);
33+
34+
$this->model = new Configurable($this->typeResource);
35+
}
36+
37+
public function testGetWithScalar()
38+
{
39+
$this->setExpectedException(\InvalidArgumentException::class, 'Provided argument is not an object');
40+
$this->model->getTags('scalar');
41+
}
42+
43+
public function testGetTagsWithObject()
44+
{
45+
$this->setExpectedException(\InvalidArgumentException::class, 'Provided argument must be a product');
46+
$this->model->getTags(new \StdClass);
47+
}
48+
49+
public function testGetTagsWithVariation()
50+
{
51+
$product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
52+
53+
$identities = ['id1', 'id2'];
54+
55+
$product->expects($this->once())
56+
->method('getIdentities')
57+
->willReturn($identities);
58+
59+
$parentId = 4;
60+
$this->typeResource->expects($this->once())
61+
->method('getParentIdsByChild')
62+
->willReturn([$parentId]);
63+
64+
$expected = array_merge($identities, [\Magento\Catalog\Model\Product::CACHE_TAG . '_' . $parentId]);
65+
66+
$this->assertEquals($expected, $this->model->getTags($product));
67+
}
68+
}

app/code/Magento/ConfigurableProduct/etc/di.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@
6464
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
6565
<plugin name="configurableProductSaveOptions" type="\Magento\ConfigurableProduct\Model\Plugin\AroundProductRepositorySave"/>
6666
</type>
67-
<type name="Magento\Catalog\Model\Product">
68-
<plugin name="configurable_identity" type="Magento\ConfigurableProduct\Plugin\Model\Product" />
69-
</type>
7067
<type name="Magento\Catalog\Model\Product\Type">
7168
<plugin name="configurable_output" type="Magento\ConfigurableProduct\Model\Product\Type\Plugin" />
7269
</type>
@@ -163,4 +160,11 @@
163160
</argument>
164161
</arguments>
165162
</type>
163+
<type name="Magento\Framework\App\Cache\Tag\Strategy\Factory">
164+
<arguments>
165+
<argument name="customStrategies" xsi:type="array">
166+
<item name="Magento\Catalog\Api\Data\ProductInterface" xsi:type="object">\Magento\ConfigurableProduct\Model\Product\Cache\Tag\Configurable</item>
167+
</argument>
168+
</arguments>
169+
</type>
166170
</config>

app/code/Magento/PageCache/Observer/FlushCacheByTags.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class FlushCacheByTags implements ObserverInterface
3030
*/
3131
private $fullPageCache;
3232

33+
/**
34+
* Invalidation tags resolver
35+
*
36+
* @var \Magento\Framework\App\Cache\Tag\Resolver
37+
*/
38+
private $tagResolver;
39+
3340
/**
3441
* @param \Magento\PageCache\Model\Config $config
3542
* @param \Magento\Framework\App\PageCache\Cache $cache
@@ -51,18 +58,21 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5158
{
5259
if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) {
5360
$object = $observer->getEvent()->getObject();
54-
if ($object instanceof \Magento\Framework\DataObject\IdentityInterface) {
55-
$tags = $object->getIdentities();
56-
if (!empty($tags)) {
57-
$this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
58-
}
61+
if (!is_object($object)) {
62+
return;
63+
}
64+
$tags = $this->getTagResolver()->getTags($object);
65+
66+
if (!empty($tags)) {
67+
$this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
5968
}
6069
}
6170
}
6271

6372
/**
6473
* TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547
6574
*
75+
*
6676
* @return \Magento\PageCache\Model\Cache\Type
6777
*/
6878
private function getCache()
@@ -72,4 +82,17 @@ private function getCache()
7282
}
7383
return $this->fullPageCache;
7484
}
85+
86+
/**
87+
* @deprecated
88+
* @return \Magento\Framework\App\Cache\Tag\Resolver
89+
*/
90+
private function getTagResolver()
91+
{
92+
if ($this->tagResolver === null) {
93+
$this->tagResolver = \Magento\Framework\App\ObjectManager::getInstance()
94+
->get(\Magento\Framework\App\Cache\Tag\Resolver::class);
95+
}
96+
return $this->tagResolver;
97+
}
7598
}

app/code/Magento/PageCache/Test/Unit/Observer/FlushCacheByTagsTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ class FlushCacheByTagsTest extends \PHPUnit_Framework_TestCase
2323
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Cache\Type */
2424
private $fullPageCacheMock;
2525

26+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Cache\Tag\Resolver */
27+
private $tagResolver;
28+
2629
/**
2730
* Set up all mocks and data for test
2831
*/
2932
protected function setUp()
3033
{
34+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3135
$this->_configMock = $this->getMock(
3236
'Magento\PageCache\Model\Config',
3337
['getType', 'isEnabled'],
@@ -42,6 +46,10 @@ protected function setUp()
4246
$this->_configMock,
4347
$this->_cacheMock
4448
);
49+
50+
$this->tagResolver = $this->getMock('\Magento\Framework\App\Cache\Tag\Resolver', [], [], '', false);
51+
$helper->setBackwardCompatibleProperty($this->_model, 'tagResolver', $this->tagResolver);
52+
4553
$reflection = new \ReflectionClass('\Magento\PageCache\Observer\FlushCacheByTags');
4654
$reflectionProperty = $reflection->getProperty('fullPageCache');
4755
$reflectionProperty->setAccessible(true);
@@ -70,7 +78,7 @@ public function testExecute($cacheState)
7078
$this->_configMock->expects($this->once())
7179
->method('getType')
7280
->willReturn(\Magento\PageCache\Model\Config::BUILT_IN);
73-
$observedObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags));
81+
$this->tagResolver->expects($this->once())->method('getTags')->will($this->returnValue($tags));
7482

7583
$this->fullPageCacheMock->expects($this->once())
7684
->method('clean')
@@ -106,7 +114,7 @@ public function testExecuteWithEmptyTags()
106114
)->will(
107115
$this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)
108116
);
109-
$observedObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags));
117+
$this->tagResolver->expects($this->once())->method('getTags')->will($this->returnValue($tags));
110118

111119
$this->fullPageCacheMock->expects($this->never())->method('clean');
112120

0 commit comments

Comments
 (0)