Skip to content

Commit fb6faf5

Browse files
Merge pull request #424 from magento-troll/MAGETWO-58265
Fixed issues: - MAGETWO-58265: Fix Varnish X-Header
2 parents c4cec78 + 76ecca8 commit fb6faf5

File tree

19 files changed

+673
-65
lines changed

19 files changed

+673
-65
lines changed

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\CacheInvalidate\Observer;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\Event\ObserverInterface;
910

1011
class InvalidateVarnishObserver implements ObserverInterface
@@ -21,6 +22,13 @@ class InvalidateVarnishObserver implements ObserverInterface
2122
*/
2223
protected $purgeCache;
2324

25+
/**
26+
* Invalidation tags resolver
27+
*
28+
* @var \Magento\Framework\App\Cache\Tag\Resolver
29+
*/
30+
private $tagResolver;
31+
2432
/**
2533
* @param \Magento\PageCache\Model\Config $config
2634
* @param \Magento\CacheInvalidate\Model\PurgeCache $purgeCache
@@ -42,18 +50,35 @@ public function __construct(
4250
*/
4351
public function execute(\Magento\Framework\Event\Observer $observer)
4452
{
53+
$object = $observer->getEvent()->getObject();
54+
if (!is_object($object)) {
55+
return;
56+
}
4557
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-
}
58+
$bareTags = $this->getTagResolver()->getTags($object);
59+
60+
$tags = [];
61+
$pattern = "((^|,)%s(,|$))";
62+
foreach ($bareTags as $tag) {
63+
$tags[] = sprintf($pattern, $tag);
64+
}
65+
if (!empty($tags)) {
66+
$this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
5667
}
68+
69+
}
70+
}
71+
72+
/**
73+
* @deprecated
74+
* @return \Magento\Framework\App\Cache\Tag\Resolver
75+
*/
76+
private function getTagResolver()
77+
{
78+
if ($this->tagResolver === null) {
79+
$this->tagResolver = \Magento\Framework\App\ObjectManager::getInstance()
80+
->get(\Magento\Framework\App\Cache\Tag\Resolver::class);
5781
}
82+
return $this->tagResolver;
5883
}
5984
}

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

Lines changed: 14 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::class,
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::class,
4455
['getEvent'],
@@ -65,10 +76,12 @@ public function testInvalidateVarnish()
6576
)->will(
6677
$this->returnValue(\Magento\PageCache\Model\Config::VARNISH)
6778
);
79+
6880
$eventMock = $this->getMock(\Magento\Framework\Event::class, ['getObject'], [], '', false);
6981
$eventMock->expects($this->once())->method('getObject')->will($this->returnValue($this->observerObject));
7082
$this->observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
71-
$this->observerObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags));
83+
$this->tagResolver->expects($this->once())->method('getTags')->with($this->observerObject)
84+
->will($this->returnValue($tags));
7285
$this->purgeCache->expects($this->once())->method('sendPurgeRequest')->with($pattern);
7386

7487
$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
@@ -61,9 +61,6 @@
6161
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
6262
<plugin name="configurableProductSaveOptions" sortOrder="10" type="Magento\ConfigurableProduct\Model\Plugin\ProductRepositorySave"/>
6363
</type>
64-
<type name="Magento\Catalog\Model\Product">
65-
<plugin name="configurable_identity" type="Magento\ConfigurableProduct\Plugin\Model\Product" />
66-
</type>
6764
<type name="Magento\Catalog\Model\Product\Type">
6865
<plugin name="configurable_output" type="Magento\ConfigurableProduct\Model\Product\Type\Plugin" />
6966
</type>
@@ -160,4 +157,11 @@
160157
</argument>
161158
</arguments>
162159
</type>
160+
<type name="Magento\Framework\App\Cache\Tag\Strategy\Factory">
161+
<arguments>
162+
<argument name="customStrategies" xsi:type="array">
163+
<item name="Magento\Catalog\Api\Data\ProductInterface" xsi:type="object">\Magento\ConfigurableProduct\Model\Product\Cache\Tag\Configurable</item>
164+
</argument>
165+
</arguments>
166+
</type>
163167
</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
}

0 commit comments

Comments
 (0)