Skip to content

Commit 315dd68

Browse files
committed
Merge remote-tracking branch 'origin/AC-11819-v1' into spartans_pr_12062024
2 parents d4a7478 + 11e213b commit 315dd68

File tree

5 files changed

+218
-8
lines changed

5 files changed

+218
-8
lines changed

app/code/Magento/PageCache/Model/App/Request/Http/IdentifierForSave.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ class IdentifierForSave implements IdentifierInterface
2121
* @param Http $request
2222
* @param Context $context
2323
* @param Json $serializer
24+
* @param IdentifierStoreReader $identifierStoreReader
2425
*/
2526
public function __construct(
26-
private Http $request,
27-
private Context $context,
28-
private Json $serializer
27+
private Http $request,
28+
private Context $context,
29+
private Json $serializer,
30+
private IdentifierStoreReader $identifierStoreReader,
2931
) {
3032
}
3133

@@ -42,6 +44,8 @@ public function getValue()
4244
$this->context->getVaryString()
4345
];
4446

47+
$data = $this->identifierStoreReader->getPageTagsWithStoreCacheTags($data);
48+
4549
return sha1($this->serializer->serialize($data));
4650
}
4751
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageCache\Model\App\Request\Http;
9+
10+
use Magento\Store\Model\StoreManager;
11+
12+
class IdentifierStoreReader
13+
{
14+
/**
15+
* @var \Magento\Framework\View\DesignExceptions
16+
*/
17+
private $designExceptions;
18+
19+
/**
20+
* @var \Magento\Framework\App\RequestInterface
21+
*/
22+
private $request;
23+
24+
/**
25+
* @var \Magento\PageCache\Model\Config
26+
*/
27+
private $config;
28+
29+
/**
30+
* @param \Magento\Framework\View\DesignExceptions $designExceptions
31+
* @param \Magento\Framework\App\RequestInterface $request
32+
* @param \Magento\PageCache\Model\Config $config
33+
*/
34+
public function __construct(
35+
\Magento\Framework\View\DesignExceptions $designExceptions,
36+
\Magento\Framework\App\RequestInterface $request,
37+
\Magento\PageCache\Model\Config $config
38+
) {
39+
$this->designExceptions = $designExceptions;
40+
$this->request = $request;
41+
$this->config = $config;
42+
}
43+
44+
/**
45+
* Adds a theme key to identifier for a built-in cache if user-agent theme rule is actual
46+
*
47+
* @param array $data
48+
* @return array|null
49+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
50+
*/
51+
public function getPageTagsWithStoreCacheTags(array $data): ?array
52+
{
53+
if ($this->config->getType() === \Magento\PageCache\Model\Config::BUILT_IN && $this->config->isEnabled()) {
54+
$ruleDesignException = $this->designExceptions->getThemeByRequest($this->request);
55+
if ($ruleDesignException !== false) {
56+
$data['DESIGN'] = $ruleDesignException;
57+
}
58+
59+
if ($runType = $this->request->getServerValue(StoreManager::PARAM_RUN_TYPE)) {
60+
$data[StoreManager::PARAM_RUN_TYPE] = $runType;
61+
}
62+
63+
if ($runCode = $this->request->getServerValue(StoreManager::PARAM_RUN_CODE)) {
64+
$data[StoreManager::PARAM_RUN_CODE] = $runCode;
65+
}
66+
}
67+
68+
return $data;
69+
}
70+
}

app/code/Magento/PageCache/Test/Unit/Model/App/Request/Http/IdentifierForSaveTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\Request\Http as HttpRequest;
1212
use Magento\Framework\Serialize\Serializer\Json;
1313
use Magento\PageCache\Model\App\Request\Http\IdentifierForSave;
14+
use Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516
use PHPUnit\Framework\TestCase;
1617

@@ -40,6 +41,10 @@ class IdentifierForSaveTest extends TestCase
4041
* @var Json|MockObject
4142
*/
4243
private mixed $serializerMock;
44+
/**
45+
* @var IdentifierStoreReader|MockObject
46+
*/
47+
private $identifierStoreReader;
4348

4449
/**
4550
* @inheritdoc
@@ -64,10 +69,16 @@ function ($value) {
6469
}
6570
);
6671

72+
$this->identifierStoreReader = $this->getMockBuilder(IdentifierStoreReader::class)
73+
->onlyMethods(['getPageTagsWithStoreCacheTags'])
74+
->disableOriginalConstructor()
75+
->getMock();
76+
6777
$this->model = new IdentifierForSave(
6878
$this->requestMock,
6979
$this->contextMock,
70-
$this->serializerMock
80+
$this->serializerMock,
81+
$this->identifierStoreReader
7182
);
7283
parent::setUp();
7384
}
@@ -91,6 +102,12 @@ public function testGetValue(): void
91102
->method('getVaryString')
92103
->willReturn(self::VARY);
93104

105+
$this->identifierStoreReader->method('getPageTagsWithStoreCacheTags')->willReturnCallback(
106+
function ($value) {
107+
return $value;
108+
}
109+
);
110+
94111
$this->assertEquals(
95112
sha1(
96113
json_encode(
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageCache\Test\Unit\Model\App\Request\Http;
9+
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\View\DesignExceptions;
13+
use Magento\PageCache\Model\App\Request\Http\IdentifierStoreReader;
14+
use Magento\PageCache\Model\Config;
15+
use Magento\Store\Model\StoreManager;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class IdentifierStoreReaderTest extends TestCase
20+
{
21+
/**
22+
* @var DesignExceptions|MockObject
23+
*/
24+
private $designExceptionsMock;
25+
/**
26+
* @var RequestInterface|MockObject
27+
*/
28+
private MockObject|RequestInterface $requestMock;
29+
/**
30+
* @var Config|MockObject
31+
*/
32+
private $configMock;
33+
/**
34+
* @var IdentifierStoreReader
35+
*/
36+
private $model;
37+
38+
protected function setUp(): void
39+
{
40+
$this->designExceptionsMock = $this->createPartialMock(
41+
DesignExceptions::class,
42+
['getThemeByRequest']
43+
);
44+
45+
$this->requestMock = $this->createMock(Http::class);
46+
47+
$this->configMock = $this->getMockBuilder(Config::class)
48+
->onlyMethods(['getType', 'isEnabled'])
49+
//->addMethods(['getType', 'isEnabled'])
50+
->disableOriginalConstructor()
51+
->getMockForAbstractClass();
52+
53+
$this->model = new IdentifierStoreReader(
54+
$this->designExceptionsMock,
55+
$this->requestMock,
56+
$this->configMock
57+
);
58+
}
59+
60+
public function testGetPageTagsWithStoreCacheTagsWhenVarnishCacheIsEnabled()
61+
{
62+
$this->configMock->expects($this->any())
63+
->method('getType')
64+
->willReturn(\Magento\PageCache\Model\Config::VARNISH);
65+
66+
$this->requestMock->expects($this->never())->method('getServerValue');
67+
68+
$data = ['anything'];
69+
$this->model->getPageTagsWithStoreCacheTags($data);
70+
}
71+
72+
public function testGetPageTagsWithStoreCacheTagsWhenFPCIsDisabled()
73+
{
74+
$this->configMock->expects($this->any())
75+
->method('isEnabled')
76+
->willReturn(false);
77+
78+
$this->requestMock->expects($this->never())->method('getServerValue');
79+
80+
$data = ['anything'];
81+
$this->model->getPageTagsWithStoreCacheTags($data);
82+
}
83+
84+
public function testGetPageTagsWithStoreCacheTagsWhenStoreDataAreInContext()
85+
{
86+
$this->configMock->expects($this->any())
87+
->method('isEnabled')
88+
->willReturn(true);
89+
90+
$this->configMock->expects($this->any())
91+
->method('getType')
92+
->willReturn(\Magento\PageCache\Model\Config::BUILT_IN);
93+
94+
$defaultRequestMock = clone $this->requestMock;
95+
$defaultRequestMock->expects($this->any())
96+
->method('getServerValue')
97+
->willReturnCallback(
98+
function ($param) {
99+
if ($param == StoreManager::PARAM_RUN_TYPE) {
100+
return 'store';
101+
}
102+
if ($param == StoreManager::PARAM_RUN_CODE) {
103+
return 'default';
104+
}
105+
}
106+
);
107+
108+
$data = ['anything'];
109+
110+
$this->model = new IdentifierStoreReader(
111+
$this->designExceptionsMock,
112+
$defaultRequestMock,
113+
$this->configMock
114+
);
115+
$newData = $this->model->getPageTagsWithStoreCacheTags($data);
116+
117+
$this->assertArrayHasKey(StoreManager::PARAM_RUN_TYPE, $newData);
118+
$this->assertArrayHasKey(StoreManager::PARAM_RUN_CODE, $newData);
119+
$this->assertEquals($newData[StoreManager::PARAM_RUN_TYPE], 'store');
120+
$this->assertEquals($newData[StoreManager::PARAM_RUN_CODE], 'default');
121+
}
122+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9-
<type name="Magento\Framework\App\PageCache\Identifier">
10-
<plugin name="core-app-area-design-exception-plugin"
11-
type="Magento\PageCache\Model\App\CacheIdentifierPlugin" sortOrder="10"/>
12-
</type>
139
<type name="Magento\Framework\App\PageCache\Cache">
1410
<plugin name="fpc-type-plugin" type="Magento\PageCache\Model\App\PageCachePlugin"/>
1511
</type>
@@ -48,4 +44,5 @@
4844
<preference for="Magento\PageCache\Model\VclGeneratorInterface" type="Magento\PageCache\Model\Varnish\VclGenerator"/>
4945
<preference for="Magento\PageCache\Model\VclTemplateLocatorInterface" type="Magento\PageCache\Model\Varnish\VclTemplateLocator"/>
5046
<preference for="Magento\PageCache\Model\Spi\PageCacheTagsPreprocessorInterface" type="Magento\PageCache\Model\PageCacheTagsPreprocessorComposite"/>
47+
<preference for="Magento\Framework\App\PageCache\IdentifierInterface" type="Magento\PageCache\Model\App\Request\Http\IdentifierForSave"/>
5148
</config>

0 commit comments

Comments
 (0)