Skip to content

Commit

Permalink
ENGCOM-4467: Add MAGE_RUN_TYPE+MAGE_RUN_CODE to FPC identifier magent…
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirZaets authored Aug 8, 2019
2 parents 5dd4c01 + 7e1e332 commit f903902
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
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));
}
}

0 comments on commit f903902

Please sign in to comment.