Skip to content

Commit 07b5099

Browse files
committed
add patch to fix tests
1 parent 391ee41 commit 07b5099

File tree

5 files changed

+131
-30
lines changed

5 files changed

+131
-30
lines changed

lib/internal/Magento/Framework/Css/PreProcessor/Adapter/Less/Processor.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\Css\PreProcessor\Adapter\Less;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\App\State;
1011
use Magento\Framework\Css\PreProcessor\File\Temporary;
1112
use Magento\Framework\Phrase;
@@ -41,6 +42,10 @@ class Processor implements ContentProcessorInterface
4142
* @var Temporary
4243
*/
4344
private $temporaryFile;
45+
46+
/**
47+
* @var DirectoryList
48+
*/
4449
private DirectoryList $directoryList;
4550

4651
/**
@@ -50,19 +55,20 @@ class Processor implements ContentProcessorInterface
5055
* @param State $appState
5156
* @param Source $assetSource
5257
* @param Temporary $temporaryFile
58+
* @param ?DirectoryList $directoryList
5359
*/
5460
public function __construct(
5561
LoggerInterface $logger,
5662
State $appState,
5763
Source $assetSource,
5864
Temporary $temporaryFile,
59-
DirectoryList $directoryList
65+
?DirectoryList $directoryList = null,
6066
) {
6167
$this->logger = $logger;
6268
$this->appState = $appState;
6369
$this->assetSource = $assetSource;
6470
$this->temporaryFile = $temporaryFile;
65-
$this->directoryList = $directoryList;
71+
$this->directoryList = $directoryList ?: ObjectManager::getInstance()->get(DirectoryList::class);
6672
}
6773

6874
/**
@@ -73,13 +79,18 @@ public function processContent(File $asset)
7379
$path = $asset->getPath();
7480
try {
7581
$mode = $this->appState->getMode();
82+
$sourceMapBasePath = sprintf(
83+
'%s/pub/',
84+
$this->directoryList->getPath(DirectoryList::TEMPLATE_MINIFICATION_DIR),
85+
);
86+
7687
$parser = new \Less_Parser(
7788
[
7889
'relativeUrls' => false,
7990
'compress' => $mode !== State::MODE_DEVELOPER,
8091
'sourceMap' => $mode === State::MODE_DEVELOPER,
8192
'sourceMapRootpath' => '/',
82-
'sourceMapBasepath' => $this->directoryList->getPath(DirectoryList::TEMPLATE_MINIFICATION_DIR) . '/pub/'
93+
'sourceMapBasepath' => $sourceMapBasePath,
8394
]
8495
);
8596

lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Adapter/Less/ProcessorTest.php

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Framework\Css\Test\Unit\PreProcessor\Adapter\Less;
99

10+
use Magento\Framework\App\Filesystem\DirectoryList;
1011
use Magento\Framework\App\State;
1112
use Magento\Framework\Css\PreProcessor\Adapter\Less\Processor;
1213
use Magento\Framework\Css\PreProcessor\File\Temporary;
@@ -23,8 +24,8 @@ class ProcessorTest extends TestCase
2324
const ASSET_PATH = 'test-path';
2425

2526
const TMP_PATH_LESS = '_file/test.less';
26-
27-
const TMP_PATH_CSS = '_file/test.css';
27+
const TMP_PATH_CSS_PRODUCTION = '_file/test-production.css';
28+
const TMP_PATH_CSS_DEVELOPER = '_file/test-developer.css';
2829

2930
const ERROR_MESSAGE = 'Test exception';
3031

@@ -52,6 +53,10 @@ class ProcessorTest extends TestCase
5253
* @var Temporary|MockObject
5354
*/
5455
private $temporaryFileMock;
56+
/**
57+
* @var DirectoryList|MockObject
58+
*/
59+
private $directoryListMock;
5560

5661
/**
5762
* Set up
@@ -69,12 +74,16 @@ protected function setUp(): void
6974
$this->temporaryFileMock = $this->getMockBuilder(Temporary::class)
7075
->disableOriginalConstructor()
7176
->getMock();
77+
$this->directoryListMock = $this->getMockBuilder(DirectoryList::class)
78+
->disableOriginalConstructor()
79+
->getMock();
7280

7381
$this->processor = new Processor(
7482
$this->loggerMock,
7583
$this->appStateMock,
7684
$this->assetSourceMock,
77-
$this->temporaryFileMock
85+
$this->temporaryFileMock,
86+
$this->directoryListMock,
7887
);
7988
}
8089

@@ -89,7 +98,7 @@ public function testProcessContentException()
8998

9099
$this->appStateMock->expects(self::once())
91100
->method('getMode')
92-
->willReturn(State::MODE_DEVELOPER);
101+
->willReturn(State::MODE_PRODUCTION);
93102

94103
$this->assetSourceMock->expects(self::once())
95104
->method('getContent')
@@ -120,7 +129,7 @@ public function testProcessContentEmpty()
120129

121130
$this->appStateMock->expects(self::once())
122131
->method('getMode')
123-
->willReturn(State::MODE_DEVELOPER);
132+
->willReturn(State::MODE_PRODUCTION);
124133

125134
$this->assetSourceMock->expects(self::once())
126135
->method('getContent')
@@ -141,15 +150,15 @@ public function testProcessContentEmpty()
141150
}
142151

143152
/**
144-
* Test for processContent method (not empty content)
153+
* Test for processContent method in production mode (not empty content)
145154
*/
146155
public function testProcessContentNotEmpty()
147156
{
148157
$assetMock = $this->getAssetMock();
149158

150159
$this->appStateMock->expects(self::once())
151160
->method('getMode')
152-
->willReturn(State::MODE_DEVELOPER);
161+
->willReturn(State::MODE_PRODUCTION);
153162

154163
$this->assetSourceMock->expects(self::once())
155164
->method('getContent')
@@ -170,11 +179,46 @@ public function testProcessContentNotEmpty()
170179

171180
$clearSymbol = ["\n", "\r", "\t", ' '];
172181
self::assertEquals(
173-
trim(str_replace($clearSymbol, '', file_get_contents(__DIR__ . '/' . self::TMP_PATH_CSS))),
182+
trim(str_replace($clearSymbol, '', file_get_contents(__DIR__ . '/' . self::TMP_PATH_CSS_PRODUCTION))),
174183
trim(str_replace($clearSymbol, '', $this->processor->processContent($assetMock)))
175184
);
176185
}
177186

187+
/**
188+
* Test for processContent method in developer mode (not empty content)
189+
*/
190+
public function testProcessContentNotEmptyInDeveloperMode()
191+
{
192+
$assetMock = $this->getAssetMock();
193+
194+
$this->appStateMock->expects(self::once())
195+
->method('getMode')
196+
->willReturn(State::MODE_DEVELOPER);
197+
198+
$this->assetSourceMock->expects(self::once())
199+
->method('getContent')
200+
->with($assetMock)
201+
->willReturn(self::TEST_CONTENT);
202+
203+
$this->temporaryFileMock->expects(self::once())
204+
->method('createFile')
205+
->with(self::ASSET_PATH, self::TEST_CONTENT)
206+
->willReturn(__DIR__ . '/' . self::TMP_PATH_LESS);
207+
208+
$assetMock->expects(self::once())
209+
->method('getPath')
210+
->willReturn(self::ASSET_PATH);
211+
212+
$this->loggerMock->expects(self::never())
213+
->method('critical');
214+
215+
$clearSymbol = ["\n", "\r", "\t", ' '];
216+
self::assertEquals(
217+
trim(str_replace($clearSymbol, '', file_get_contents(__DIR__ . '/' . self::TMP_PATH_CSS_DEVELOPER))),
218+
trim(str_replace($clearSymbol, '', $this->normalizeInlineSourceMap($this->processor->processContent($assetMock))))
219+
);
220+
}
221+
178222
/**
179223
* @return File|MockObject
180224
*/
@@ -186,4 +230,34 @@ private function getAssetMock()
186230

187231
return $assetMock;
188232
}
233+
234+
/**
235+
* - find json part of sourcemap
236+
* - url decode it
237+
* - replace \/ with / in source filenames
238+
* - remove absolute path in filename, make it a relative path
239+
*/
240+
private function normalizeInlineSourceMap(string $css): string
241+
{
242+
$regexBegin = 'sourceMappingURL=data:application/json,';
243+
$regexEnd = '*/';
244+
$regex = '@' . preg_quote($regexBegin, '@') . '([^\*]+)' . preg_quote($regexEnd, '@') . '@';
245+
246+
if (preg_match($regex, $css, $matches) === 1) {
247+
$inlineSourceMapJson = $matches[1];
248+
$inlineSourceMapJson = urldecode($inlineSourceMapJson);
249+
$inlineSourceMapJson = json_decode($inlineSourceMapJson, true, 512, JSON_UNESCAPED_SLASHES);
250+
251+
$relativeFilenames = [];
252+
foreach ($inlineSourceMapJson['sources'] as $filename) {
253+
$relativeFilenames[] = str_replace(sprintf('%s/', BP), '', $filename);
254+
}
255+
$inlineSourceMapJson['sources'] = $relativeFilenames;
256+
$inlineSourceMapJson = json_encode($inlineSourceMapJson, JSON_UNESCAPED_SLASHES);
257+
258+
$css = preg_replace($regex, sprintf('%s%s%s', $regexBegin, $inlineSourceMapJson, $regexEnd), $css);
259+
}
260+
261+
return $css;
262+
}
189263
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
body {
6+
background: #333333;
7+
color: #454545;
8+
}
9+
a {
10+
color: #ff9900;
11+
}
12+
h1,
13+
h2,
14+
h3,
15+
h4,
16+
h5,
17+
h6 {
18+
color: #333333;
19+
}
20+
/*#sourceMappingURL=data:application/json,{"version":3,"sources":["lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Adapter/Less/_file/test.less"],"names":[],"mappings":";;;;AASA;EACE,mBAAA;EACA,cAAA;;AAEF;EACE,cAAA;;AAEF;AAAI;AAAI;AAAI;AAAI;AAAI;EAClB,cAAA"}*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
body {
2+
background: #333333;
3+
color: #454545
4+
}
5+
a {
6+
color: #ff9900
7+
}
8+
h1,
9+
h2,
10+
h3,
11+
h4,
12+
h5,
13+
h6 {
14+
color: #333333
15+
}

lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Adapter/Less/_file/test.css

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)