Skip to content

Commit 3cac42e

Browse files
committed
Merge remote-tracking branch 'okapis/MAGETWO-64085' into pr-aug29
2 parents 4ecb6a5 + d4e302a commit 3cac42e

File tree

7 files changed

+384
-6
lines changed

7 files changed

+384
-6
lines changed

app/code/Magento/Email/Model/Template/Filter.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ class Filter extends \Magento\Framework\Filter\Template
150150

151151
/**
152152
* @var \Pelago\Emogrifier
153+
* @deprecated
153154
*/
154155
protected $emogrifier;
155156

157+
/**
158+
* @var \Magento\Framework\Css\PreProcessor\Adapter\CssInliner
159+
*/
160+
private $cssInliner;
161+
156162
/**
157163
* @var \Magento\Email\Model\Source\Variables
158164
*/
@@ -183,6 +189,7 @@ class Filter extends \Magento\Framework\Filter\Template
183189
* @param \Pelago\Emogrifier $emogrifier
184190
* @param \Magento\Email\Model\Source\Variables $configVariables
185191
* @param array $variables
192+
* @param \Magento\Framework\Css\PreProcessor\Adapter\CssInliner|null $cssInliner
186193
*
187194
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
188195
*/
@@ -200,7 +207,8 @@ public function __construct(
200207
\Magento\Framework\UrlInterface $urlModel,
201208
\Pelago\Emogrifier $emogrifier,
202209
\Magento\Email\Model\Source\Variables $configVariables,
203-
$variables = []
210+
$variables = [],
211+
\Magento\Framework\Css\PreProcessor\Adapter\CssInliner $cssInliner = null
204212
) {
205213
$this->_escaper = $escaper;
206214
$this->_assetRepo = $assetRepo;
@@ -214,6 +222,8 @@ public function __construct(
214222
$this->_appState = $appState;
215223
$this->urlModel = $urlModel;
216224
$this->emogrifier = $emogrifier;
225+
$this->cssInliner = $cssInliner ?: \Magento\Framework\App\ObjectManager::getInstance()
226+
->get(\Magento\Framework\Css\PreProcessor\Adapter\CssInliner::class);
217227
$this->configVariables = $configVariables;
218228
parent::__construct($string, $variables);
219229
}
@@ -975,14 +985,14 @@ public function applyInlineCss($html)
975985
);
976986
}
977987

978-
$emogrifier = $this->emogrifier;
979-
$emogrifier->setHtml($html);
980-
$emogrifier->setCss($cssToInline);
988+
$this->cssInliner->setHtml($html);
989+
990+
$this->cssInliner->setCss($cssToInline);
981991

982992
// Don't parse inline <style> tags, since existing tag is intentionally for non-inline styles
983-
$emogrifier->disableStyleBlocksParsing();
993+
$this->cssInliner->disableStyleBlocksParsing();
984994

985-
$processedHtml = $emogrifier->emogrify();
995+
$processedHtml = $this->cssInliner->process();
986996
} catch (\Exception $e) {
987997
if ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
988998
$processedHtml = __('CSS inlining error:') . PHP_EOL . $e->getMessage()

app/code/Magento/Email/Test/Unit/Model/Template/FilterTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class FilterTest extends \PHPUnit\Framework\TestCase
8787
*/
8888
private $emogrifier;
8989

90+
/**
91+
* @var \Magento\Framework\Css\PreProcessor\Adapter\CssInliner
92+
*/
93+
private $cssInliner;
94+
9095
protected function setUp()
9196
{
9297
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -140,6 +145,10 @@ protected function setUp()
140145
$this->configVariables = $this->getMockBuilder(\Magento\Email\Model\Source\Variables::class)
141146
->disableOriginalConstructor()
142147
->getMock();
148+
149+
$this->cssInliner = $this->objectManager->getObject(
150+
\Magento\Framework\Css\PreProcessor\Adapter\CssInliner::class
151+
);
143152
}
144153

145154
/**
@@ -164,6 +173,7 @@ protected function getModel($mockedMethods = null)
164173
$this->emogrifier,
165174
$this->configVariables,
166175
[],
176+
$this->cssInliner,
167177
])
168178
->setMethods($mockedMethods)
169179
->getMock();
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Css\PreProcessor\Adapter;
7+
8+
class CssInlinerTest extends \PHPUnit\Framework\TestCase
9+
{
10+
/**
11+
* @var \Magento\Framework\Css\PreProcessor\Adapter\CssInliner
12+
*/
13+
private $model;
14+
15+
/**
16+
* @var \Magento\Framework\ObjectManagerInterface
17+
*/
18+
private $objectManager;
19+
20+
protected function setUp()
21+
{
22+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
23+
24+
$this->model = $this->objectManager->create(\Magento\Framework\Css\PreProcessor\Adapter\CssInliner::class);
25+
}
26+
27+
/**
28+
* @param string $htmlFilePath
29+
* @param string $cssFilePath
30+
* @param string $cssExpected
31+
* @dataProvider getFilesDataProvider
32+
*/
33+
public function testGetFiles($htmlFilePath, $cssFilePath, $cssExpected)
34+
{
35+
$html = file_get_contents($htmlFilePath);
36+
$css = file_get_contents($cssFilePath);
37+
$this->model->setCss($css);
38+
$this->model->setHtml($html);
39+
$result = $this->model->process();
40+
$this->assertContains($cssExpected, $result);
41+
}
42+
43+
/**
44+
* @return array
45+
*/
46+
public function getFilesDataProvider()
47+
{
48+
$fixtureDir = dirname(dirname(__DIR__));
49+
return [
50+
'noSpacesCss'=>[
51+
'resultHtml' => $fixtureDir . "/_files/css/test-input.html",
52+
'cssWithoutSpaces' => $fixtureDir . "/_files/css/test-css-no-spaces.css",
53+
'vertical-align: top; padding: 10px 10px 10px 0; width: 50%;'
54+
],
55+
'withSpacesCss'=>[
56+
'resultHtml' => $fixtureDir . "/_files/css/test-input.html",
57+
'cssWithSpaces' => $fixtureDir . "/_files/css/test-css-with-spaces.css",
58+
'vertical-align: top; padding: 10px 10px 10px 0; width: 50%;'
59+
],
60+
];
61+
}
62+
63+
/**
64+
* @param string $htmlFilePath
65+
* @param string $cssFilePath
66+
* @param string $cssExpected
67+
* @dataProvider getFilesDataProviderEmogrifier
68+
*/
69+
public function testGetFilesEmogrifier($htmlFilePath, $cssFilePath, $cssExpected)
70+
{
71+
$emogrifier = new \Pelago\Emogrifier;
72+
73+
$html = file_get_contents($htmlFilePath);
74+
$css = file_get_contents($cssFilePath);
75+
$emogrifier->setCss($css);
76+
$emogrifier->setHtml($html);
77+
$result = $emogrifier->emogrify();
78+
/**
79+
* Tests a bug in the library where there's no spaces to CSS string before passing to Emogrifier
80+
* to fix known parsing issue with library.
81+
* This test should will fail when this bug is fixed in the library and we should fix the adapter.
82+
* https://github.com/jjriv/emogrifier/issues/370
83+
*/
84+
$this->assertNotContains($cssExpected, $result);
85+
}
86+
87+
/**
88+
* @return array
89+
*/
90+
public function getFilesDataProviderEmogrifier()
91+
{
92+
$fixtureDir = dirname(dirname(__DIR__));
93+
return [
94+
'noSpacesCss'=>[
95+
'resultHtml' => $fixtureDir . "/_files/css/test-input.html",
96+
'cssWithoutSpaces' => $fixtureDir . "/_files/css/test-css-no-spaces.css",
97+
'vertical-align: top; padding: 10px 10px 10px 0; width: 50%;'
98+
]
99+
];
100+
}
101+
}

dev/tests/integration/testsuite/Magento/Framework/Css/_files/css/test-css-no-spaces.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)