Skip to content
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
27 changes: 21 additions & 6 deletions app/code/Magento/Theme/Controller/Result/AsyncCssPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

namespace Magento\Theme\Controller\Result;

use Magento\Csp\Api\InlineUtilInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\View\Result\Layout;
Expand All @@ -26,12 +27,23 @@ class AsyncCssPlugin
*/
private $scopeConfig;

/**
* @var InlineUtilInterface
*/
private $cspInlineUtil;

/**
* @param ScopeConfigInterface $scopeConfig
* @param InlineUtilInterface|null $cspInlineUtil
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
public function __construct(
ScopeConfigInterface $scopeConfig,
InlineUtilInterface $cspInlineUtil = null
) {
$this->scopeConfig = $scopeConfig;
$this->cspInlineUtil = $cspInlineUtil ?: ObjectManager::getInstance()->get(
InlineUtilInterface::class
);
}

/**
Expand Down Expand Up @@ -99,10 +111,13 @@ private function extractLinkTags(string &$content): string
$media = $mediaAttribute[2];
}
$media = $media ?? 'all';

$onload = $this->cspInlineUtil->renderEventListener(
'onload',
sprintf('this.onload=null;this.media=\'%s\'', $media)
);
$style = sprintf(
'<link rel="stylesheet" media="print" onload="this.onload=null;this.media=\'%s\'" href="%s">',
$media,
'<link rel="stylesheet" media="print" %s href="%s">',
$onload,
$href
);
$styles .= "\n" . $style;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

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

use Magento\Theme\Controller\Result\AsyncCssPlugin;
use Magento\Csp\Api\InlineUtilInterface;
use Magento\Framework\App\Response\Http;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -21,7 +24,7 @@
*/
class AsyncCssPluginTest extends TestCase
{
const STUB_XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';
private const STUB_XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';

/**
* @var AsyncCssPlugin
Expand All @@ -38,9 +41,16 @@ class AsyncCssPluginTest extends TestCase
*/
private $httpMock;

/** @var Layout|MockObject */
/**
* @var Layout|MockObject
*/
private $layoutMock;

/**
* @var InlineUtilInterface|MockObject
*/
private $cspInlineUtilMock;

/**
* @inheritdoc
*/
Expand All @@ -53,12 +63,14 @@ protected function setUp(): void

$this->httpMock = $this->createMock(Http::class);
$this->layoutMock = $this->createMock(Layout::class);
$this->cspInlineUtilMock = $this->createMock(InlineUtilInterface::class);

$objectManager = new ObjectManagerHelper($this);
$this->plugin = $objectManager->getObject(
AsyncCssPlugin::class,
[
'scopeConfig' => $this->scopeConfigMock
'scopeConfig' => $this->scopeConfigMock,
'cspInlineUtil' => $this->cspInlineUtilMock
]
);
}
Expand Down Expand Up @@ -149,6 +161,14 @@ public function testAfterRenderResult(string $content, bool $isSetFlag, string $
->with(self::STUB_XML_PATH_USE_CSS_CRITICAL_PATH, ScopeInterface::SCOPE_STORE)
->willReturn($isSetFlag);

if ($isSetFlag) {
$this->cspInlineUtilMock->expects($this->any())
->method('renderEventListener')
->with(
'onload',
"this.onload=null;this.media='all'"
)->willReturn('onload="this.onload=null;this.media=\'all\'"');
}
// Expects
$this->httpMock->expects($this->any())
->method('setContent')
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Theme/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"magento/module-backend": "*",
"magento/module-cms": "*",
"magento/module-config": "*",
"magento/module-csp": "*",
"magento/module-customer": "*",
"magento/module-eav": "*",
"magento/module-media-storage": "*",
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Theme/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<sequence>
<module name="Magento_Store"/>
<module name="Magento_Directory"/>
<module name="Magento_Csp"/>
</sequence>
</module>
</config>