Skip to content

Commit f903902

Browse files
ENGCOM-4467: Add MAGE_RUN_TYPE+MAGE_RUN_CODE to FPC identifier #18624
2 parents 5dd4c01 + 7e1e332 commit f903902

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

app/code/Magento/PageCache/Model/App/CacheIdentifierPlugin.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
namespace Magento\PageCache\Model\App;
88

9+
use Magento\Store\Model\StoreManager;
10+
911
/**
1012
* Class CachePlugin
13+
*
1114
* Should add design exceptions o identifier for built-in cache
1215
*/
1316
class CacheIdentifierPlugin
@@ -40,10 +43,23 @@ public function __construct(
4043
public function afterGetValue(\Magento\Framework\App\PageCache\Identifier $identifier, $result)
4144
{
4245
if ($this->config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->config->isEnabled()) {
46+
$identifierPrefix = '';
47+
4348
$ruleDesignException = $this->designExceptions->getThemeByRequest($this->request);
4449
if ($ruleDesignException !== false) {
45-
return $ruleDesignException . $result;
50+
$identifierPrefix .= 'DESIGN' . '=' . $ruleDesignException . '|';
51+
}
52+
53+
if ($runType = $this->request->getServerValue(StoreManager::PARAM_RUN_TYPE)) {
54+
$identifierPrefix .= StoreManager::PARAM_RUN_TYPE . '=' . $runType . '|';
4655
}
56+
57+
if ($runCode = $this->request->getServerValue(StoreManager::PARAM_RUN_CODE)) {
58+
$identifierPrefix .= StoreManager::PARAM_RUN_CODE . '=' . $runCode . '|';
59+
}
60+
61+
return $identifierPrefix . $result;
62+
4763
}
4864
return $result;
4965
}

app/code/Magento/PageCache/Test/Unit/App/CacheIdentifierPluginTest.php

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
namespace Magento\PageCache\Test\Unit\App;
77

88
use Magento\PageCache\Model\Config;
9+
use Magento\Store\Model\StoreManager;
910

1011
/**
1112
* Class CacheIdentifierPluginTest
13+
*
1214
* Test for plugin to identifier to work with design exceptions
1315
*/
1416
class CacheIdentifierPluginTest extends \PHPUnit\Framework\TestCase
@@ -56,7 +58,7 @@ protected function setUp()
5658
}
5759

5860
/**
59-
* Test of adding design exceptions to the kay of cache hash
61+
* Test of adding design exceptions + run code to the key of cache hash
6062
*
6163
* @param string $cacheType
6264
* @param bool $isPageCacheEnabled
@@ -93,9 +95,83 @@ public function afterGetValueDataProvider()
9395
'Varnish + PageCache enabled' => [Config::VARNISH, true, null, false, false],
9496
'Built-in + PageCache disabled' => [Config::BUILT_IN, false, null, false, false],
9597
'Built-in + PageCache enabled' => [Config::BUILT_IN, true, null, false, false],
96-
'Built-in, PageCache enabled, no user-agent exceptions' =>
97-
[Config::BUILT_IN, true, 'aa123aa', false, 'aa123aa'],
98-
'Built-in, PageCache enabled, with design exception' => [Config::BUILT_IN, true, 'aa123aa', '7', '7aa123aa']
98+
'Built-in, PageCache enabled, no user-agent exceptions' => [Config::BUILT_IN,
99+
true,
100+
'aa123aa',
101+
false,
102+
'aa123aa'
103+
],
104+
'Built-in, PageCache enabled, with design exception' => [Config::BUILT_IN,
105+
true,
106+
'aa123aa',
107+
'7',
108+
'DESIGN=7|aa123aa'
109+
]
99110
];
100111
}
112+
113+
/**
114+
* Tests that different stores cause different identifiers
115+
* (property based testing approach)
116+
*/
117+
public function testAfterGetValueRunParamsCauseDifferentIdentifiers()
118+
{
119+
$identifierMock = $this->createMock(\Magento\Framework\App\PageCache\Identifier::class);
120+
121+
$this->pageCacheConfigMock->expects($this->any())
122+
->method('getType')
123+
->willReturn(Config::BUILT_IN);
124+
$this->pageCacheConfigMock->expects($this->any())
125+
->method('isEnabled')
126+
->willReturn(true);
127+
128+
$defaultRequestMock = clone $this->requestMock;
129+
$defaultRequestMock->expects($this->any())
130+
->method('getServerValue')
131+
->willReturnCallback(
132+
function ($param) {
133+
if ($param == StoreManager::PARAM_RUN_TYPE) {
134+
return 'store';
135+
}
136+
if ($param == StoreManager::PARAM_RUN_CODE) {
137+
return 'default';
138+
}
139+
}
140+
);
141+
142+
$nullSha1 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
143+
144+
$defaultPlugin = new \Magento\PageCache\Model\App\CacheIdentifierPlugin(
145+
$this->designExceptionsMock,
146+
$defaultRequestMock,
147+
$this->pageCacheConfigMock
148+
);
149+
150+
$defaultStoreResult = $defaultPlugin->afterGetValue($identifierMock, $nullSha1);
151+
152+
$otherRequestMock = clone $this->requestMock;
153+
$otherRequestMock->expects($this->any())
154+
->method('getServerValue')
155+
->willReturnCallback(
156+
function ($param) {
157+
if ($param == StoreManager::PARAM_RUN_TYPE) {
158+
return 'store';
159+
}
160+
if ($param == StoreManager::PARAM_RUN_CODE) {
161+
return 'klingon';
162+
}
163+
}
164+
);
165+
166+
$otherPlugin = new \Magento\PageCache\Model\App\CacheIdentifierPlugin(
167+
$this->designExceptionsMock,
168+
$otherRequestMock,
169+
$this->pageCacheConfigMock
170+
);
171+
$otherStoreResult = $otherPlugin->afterGetValue($identifierMock, $nullSha1);
172+
173+
$this->assertNotEquals($nullSha1, $defaultStoreResult);
174+
$this->assertNotEquals($nullSha1, $otherStoreResult);
175+
$this->assertNotEquals($defaultStoreResult, $otherStoreResult);
176+
}
101177
}

lib/internal/Magento/Framework/App/PageCache/Identifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function getValue()
5656
$this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
5757
?: $this->context->getVaryString()
5858
];
59+
5960
return sha1($this->serializer->serialize($data));
6061
}
6162
}

0 commit comments

Comments
 (0)