Skip to content

Commit 5c24c15

Browse files
Indrani SonawaneIndrani Sonawane
Indrani Sonawane
authored and
Indrani Sonawane
committed
Merge remote-tracking branch '39040/bug/AC-12731-csp-inline-critical-css' into compr_author
2 parents f1c890f + 751f00e commit 5c24c15

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

app/code/Magento/Theme/Controller/Result/AsyncCssPlugin.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
namespace Magento\Theme\Controller\Result;
99

10+
use Magento\Csp\Api\InlineUtilInterface;
1011
use Magento\Framework\App\Config\ScopeConfigInterface;
1112
use Magento\Store\Model\ScopeInterface;
12-
use Magento\Framework\App\Response\Http;
13+
use Magento\Framework\App\ObjectManager;
1314
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1415
use Magento\Framework\App\ResponseInterface;
1516
use Magento\Framework\View\Result\Layout;
@@ -26,12 +27,23 @@ class AsyncCssPlugin
2627
*/
2728
private $scopeConfig;
2829

30+
/**
31+
* @var InlineUtilInterface
32+
*/
33+
private $cspInlineUtil;
34+
2935
/**
3036
* @param ScopeConfigInterface $scopeConfig
37+
* @param InlineUtilInterface|null $cspInlineUtil
3138
*/
32-
public function __construct(ScopeConfigInterface $scopeConfig)
33-
{
39+
public function __construct(
40+
ScopeConfigInterface $scopeConfig,
41+
InlineUtilInterface $cspInlineUtil = null
42+
) {
3443
$this->scopeConfig = $scopeConfig;
44+
$this->cspInlineUtil = $cspInlineUtil ?: ObjectManager::getInstance()->get(
45+
InlineUtilInterface::class
46+
);
3547
}
3648

3749
/**
@@ -99,10 +111,13 @@ private function extractLinkTags(string &$content): string
99111
$media = $mediaAttribute[2];
100112
}
101113
$media = $media ?? 'all';
102-
114+
$onload = $this->cspInlineUtil->renderEventListener(
115+
'onload',
116+
sprintf('this.onload=null;this.media=\'%s\'', $media)
117+
);
103118
$style = sprintf(
104-
'<link rel="stylesheet" media="print" onload="this.onload=null;this.media=\'%s\'" href="%s">',
105-
$media,
119+
'<link rel="stylesheet" media="print" %s href="%s">',
120+
$onload,
106121
$href
107122
);
108123
$styles .= "\n" . $style;

app/code/Magento/Theme/Test/Unit/Controller/Result/AsyncCssPluginTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\Theme\Test\Unit\Controller\Result;
911

1012
use Magento\Theme\Controller\Result\AsyncCssPlugin;
13+
use Magento\Csp\Api\InlineUtilInterface;
1114
use Magento\Framework\App\Response\Http;
1215
use PHPUnit\Framework\TestCase;
1316
use PHPUnit\Framework\MockObject\MockObject;
@@ -21,7 +24,7 @@
2124
*/
2225
class AsyncCssPluginTest extends TestCase
2326
{
24-
const STUB_XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';
27+
private const STUB_XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';
2528

2629
/**
2730
* @var AsyncCssPlugin
@@ -38,9 +41,16 @@ class AsyncCssPluginTest extends TestCase
3841
*/
3942
private $httpMock;
4043

41-
/** @var Layout|MockObject */
44+
/**
45+
* @var Layout|MockObject
46+
*/
4247
private $layoutMock;
4348

49+
/**
50+
* @var InlineUtilInterface|MockObject
51+
*/
52+
private $cspInlineUtilMock;
53+
4454
/**
4555
* @inheritdoc
4656
*/
@@ -53,12 +63,14 @@ protected function setUp(): void
5363

5464
$this->httpMock = $this->createMock(Http::class);
5565
$this->layoutMock = $this->createMock(Layout::class);
66+
$this->cspInlineUtilMock = $this->createMock(InlineUtilInterface::class);
5667

5768
$objectManager = new ObjectManagerHelper($this);
5869
$this->plugin = $objectManager->getObject(
5970
AsyncCssPlugin::class,
6071
[
61-
'scopeConfig' => $this->scopeConfigMock
72+
'scopeConfig' => $this->scopeConfigMock,
73+
'cspInlineUtil' => $this->cspInlineUtilMock
6274
]
6375
);
6476
}
@@ -149,6 +161,14 @@ public function testAfterRenderResult(string $content, bool $isSetFlag, string $
149161
->with(self::STUB_XML_PATH_USE_CSS_CRITICAL_PATH, ScopeInterface::SCOPE_STORE)
150162
->willReturn($isSetFlag);
151163

164+
if ($isSetFlag) {
165+
$this->cspInlineUtilMock->expects($this->any())
166+
->method('renderEventListener')
167+
->with(
168+
'onload',
169+
"this.onload=null;this.media='all'"
170+
)->willReturn('onload="this.onload=null;this.media=\'all\'"');
171+
}
152172
// Expects
153173
$this->httpMock->expects($this->any())
154174
->method('setContent')

app/code/Magento/Theme/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"magento/module-backend": "*",
1111
"magento/module-cms": "*",
1212
"magento/module-config": "*",
13+
"magento/module-csp": "*",
1314
"magento/module-customer": "*",
1415
"magento/module-eav": "*",
1516
"magento/module-media-storage": "*",

app/code/Magento/Theme/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<sequence>
1111
<module name="Magento_Store"/>
1212
<module name="Magento_Directory"/>
13+
<module name="Magento_Csp"/>
1314
</sequence>
1415
</module>
1516
</config>

0 commit comments

Comments
 (0)