Skip to content

Add MAGE_RUN_TYPE+MAGE_RUN_CODE to FPC identifier #18624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/code/Magento/PageCache/Model/App/CacheIdentifierPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

namespace Magento\PageCache\Model\App;

use Magento\Store\Model\StoreManager;

/**
* Class CachePlugin
*
* Should add design exceptions o identifier for built-in cache
*/
class CacheIdentifierPlugin
Expand Down Expand Up @@ -40,10 +43,23 @@ public function __construct(
public function afterGetValue(\Magento\Framework\App\PageCache\Identifier $identifier, $result)
{
if ($this->config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->config->isEnabled()) {
$identifierPrefix = '';

$ruleDesignException = $this->designExceptions->getThemeByRequest($this->request);
if ($ruleDesignException !== false) {
return $ruleDesignException . $result;
$identifierPrefix .= 'DESIGN' . '=' . $ruleDesignException . '|';
}

if ($runType = $this->request->getServerValue(StoreManager::PARAM_RUN_TYPE)) {
$identifierPrefix .= StoreManager::PARAM_RUN_TYPE . '=' . $runType . '|';
}

if ($runCode = $this->request->getServerValue(StoreManager::PARAM_RUN_CODE)) {
$identifierPrefix .= StoreManager::PARAM_RUN_CODE . '=' . $runCode . '|';
}

return $identifierPrefix . $result;

}
return $result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
namespace Magento\PageCache\Test\Unit\App;

use Magento\PageCache\Model\Config;
use Magento\Store\Model\StoreManager;

/**
* Class CacheIdentifierPluginTest
*
* Test for plugin to identifier to work with design exceptions
*/
class CacheIdentifierPluginTest extends \PHPUnit\Framework\TestCase
Expand Down Expand Up @@ -56,7 +58,7 @@ protected function setUp()
}

/**
* Test of adding design exceptions to the kay of cache hash
* Test of adding design exceptions + run code to the key of cache hash
*
* @param string $cacheType
* @param bool $isPageCacheEnabled
Expand Down Expand Up @@ -93,9 +95,83 @@ public function afterGetValueDataProvider()
'Varnish + PageCache enabled' => [Config::VARNISH, true, null, false, false],
'Built-in + PageCache disabled' => [Config::BUILT_IN, false, null, false, false],
'Built-in + PageCache enabled' => [Config::BUILT_IN, true, null, false, false],
'Built-in, PageCache enabled, no user-agent exceptions' =>
[Config::BUILT_IN, true, 'aa123aa', false, 'aa123aa'],
'Built-in, PageCache enabled, with design exception' => [Config::BUILT_IN, true, 'aa123aa', '7', '7aa123aa']
'Built-in, PageCache enabled, no user-agent exceptions' => [Config::BUILT_IN,
true,
'aa123aa',
false,
'aa123aa'
],
'Built-in, PageCache enabled, with design exception' => [Config::BUILT_IN,
true,
'aa123aa',
'7',
'DESIGN=7|aa123aa'
]
];
}

/**
* Tests that different stores cause different identifiers
* (property based testing approach)
*/
public function testAfterGetValueRunParamsCauseDifferentIdentifiers()
{
$identifierMock = $this->createMock(\Magento\Framework\App\PageCache\Identifier::class);

$this->pageCacheConfigMock->expects($this->any())
->method('getType')
->willReturn(Config::BUILT_IN);
$this->pageCacheConfigMock->expects($this->any())
->method('isEnabled')
->willReturn(true);

$defaultRequestMock = clone $this->requestMock;
$defaultRequestMock->expects($this->any())
->method('getServerValue')
->willReturnCallback(
function ($param) {
if ($param == StoreManager::PARAM_RUN_TYPE) {
return 'store';
}
if ($param == StoreManager::PARAM_RUN_CODE) {
return 'default';
}
}
);

$nullSha1 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';

$defaultPlugin = new \Magento\PageCache\Model\App\CacheIdentifierPlugin(
$this->designExceptionsMock,
$defaultRequestMock,
$this->pageCacheConfigMock
);

$defaultStoreResult = $defaultPlugin->afterGetValue($identifierMock, $nullSha1);

$otherRequestMock = clone $this->requestMock;
$otherRequestMock->expects($this->any())
->method('getServerValue')
->willReturnCallback(
function ($param) {
if ($param == StoreManager::PARAM_RUN_TYPE) {
return 'store';
}
if ($param == StoreManager::PARAM_RUN_CODE) {
return 'klingon';
}
}
);

$otherPlugin = new \Magento\PageCache\Model\App\CacheIdentifierPlugin(
$this->designExceptionsMock,
$otherRequestMock,
$this->pageCacheConfigMock
);
$otherStoreResult = $otherPlugin->afterGetValue($identifierMock, $nullSha1);

$this->assertNotEquals($nullSha1, $defaultStoreResult);
$this->assertNotEquals($nullSha1, $otherStoreResult);
$this->assertNotEquals($defaultStoreResult, $otherStoreResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function getValue()
$this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING)
?: $this->context->getVaryString()
];

return sha1($this->serializer->serialize($data));
}
}