Skip to content
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

LoadCssAsync html format fixed for critical css #26764

Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions app/code/Magento/Theme/Controller/Result/AsyncCssPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class AsyncCssPlugin
{
private const XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';
const XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var ScopeConfigInterface
Expand Down Expand Up @@ -58,9 +58,9 @@ function ($matches) use (&$cssMatches) {
}
$media = $media ?? 'all';
$loadCssAsync = sprintf(
'<link rel="preload" as="style" media="%s" .
onload="this.onload=null;this.rel=\'stylesheet\'"' .
'href="%s">',
'<link rel="preload" as="style" media="%s"' .
' onload="this.onload=null;this.rel=\'stylesheet\'"' .
' href="%s" />',
$media,
$href
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?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\Framework\App\Response\Http;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

/**
* Unit test for Magento\Theme\Test\Unit\Controller\Result\AsyncCssPlugin.
*/
class AsyncCssPluginTest extends TestCase
{
/**
* @var AsyncCssPlugin
*/
private $plugin;

/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;

/**
* @var Http|MockObject
*/
private $httpMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->setMethods(['isSetFlag'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->httpMock = $this->createMock(Http::class);

$objectManager = new ObjectManagerHelper($this);
$this->plugin = $objectManager->getObject(
AsyncCssPlugin::class,
[
'scopeConfig' => $this->scopeConfigMock
]
);
}

/**
* Data Provider for before send response
*
* @return array
*/
public function sendResponseDataProvider(): array
{
return [
[
"content" => "<body><h1>Test Title</h1>" .
"<link rel=\"stylesheet\" href=\"css/critical.css\" />" .
"<p>Test Content</p></body>",
"flag" => true,
"result" => "<body><h1>Test Title</h1>" .
"<link rel=\"preload\" as=\"style\" media=\"all\"" .
" onload=\"this.onload=null;this.rel='stylesheet'\" href=\"css/critical.css\" />" .
"<p>Test Content</p>" .
"<link rel=\"stylesheet\" href=\"css/critical.css\" />" .
"\n</body>"
],
[
"content" => "<body><p>Test Content</p></body>",
"flag" => false,
"result" => "<body><p>Test Content</p></body>"
],
[
"content" => "<body><p>Test Content</p></body>",
"flag" => true,
"result" => "<body><p>Test Content</p></body>"
]
];
}

/**
* Test beforeSendResponse
*
* @param string $content
* @param bool $isSetFlag
* @param string $result
* @return void
* @dataProvider sendResponseDataProvider
*/
public function testBeforeSendResponse($content, $isSetFlag, $result): void
{
$this->httpMock->expects($this->once())
->method('getContent')
->willReturn($content);

$this->scopeConfigMock->expects($this->once())
->method('isSetFlag')
->with(
AsyncCssPlugin::XML_PATH_USE_CSS_CRITICAL_PATH,
ScopeInterface::SCOPE_STORE
)
->willReturn($isSetFlag);

$this->httpMock->expects($this->any())
->method('setContent')
->with($result);

$this->plugin->beforeSendResponse($this->httpMock);
}

/**
* Test BeforeSendResponse if content is not a string
*
* @return void
*/
public function testIfGetContentIsNotAString(): void
{
$this->httpMock->expects($this->once())
->method('getContent')
->willReturn([]);

$this->scopeConfigMock->expects($this->any())
->method('isSetFlag')
->with(
AsyncCssPlugin::XML_PATH_USE_CSS_CRITICAL_PATH,
ScopeInterface::SCOPE_STORE
)
->willReturn(false);

$this->plugin->beforeSendResponse($this->httpMock);
}
}