Skip to content

Commit 6949d13

Browse files
ihor-svizievtuna2smc
authored andcommitted
#32922 Add logic check only import css from enabled modules
Add test coverage
1 parent 31bd392 commit 6949d13

File tree

1 file changed

+104
-34
lines changed

1 file changed

+104
-34
lines changed

lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php

Lines changed: 104 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Css\PreProcessor\ErrorHandlerInterface;
1111
use Magento\Framework\Css\PreProcessor\Instruction\Import;
1212
use Magento\Framework\Css\PreProcessor\Instruction\MagentoImport;
13+
use Magento\Framework\Module\Manager as ModuleManager;
1314
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1415
use Magento\Framework\View\Asset\File;
1516
use Magento\Framework\View\Asset\File\FallbackContext;
@@ -30,32 +31,37 @@ class MagentoImportTest extends TestCase
3031
/**
3132
* @var DesignInterface|MockObject
3233
*/
33-
private $design;
34+
private $designMock;
3435

3536
/**
3637
* @var CollectorInterface|MockObject
3738
*/
38-
private $fileSource;
39+
private $fileSourceMock;
3940

4041
/**
4142
* @var ErrorHandlerInterface|MockObject
4243
*/
43-
private $errorHandler;
44+
private $errorHandlerMock;
4445

4546
/**
4647
* @var File|MockObject
4748
*/
48-
private $asset;
49+
private $assetMock;
4950

5051
/**
5152
* @var Repository|MockObject
5253
*/
53-
private $assetRepo;
54+
private $assetRepoMock;
5455

5556
/**
5657
* @var ThemeProviderInterface|MockObject
5758
*/
58-
private $themeProvider;
59+
private $themeProviderMock;
60+
61+
/**
62+
* @var ModuleManager|MockObject
63+
*/
64+
private $moduleManagerMock;
5965

6066
/**
6167
* @var Import
@@ -64,21 +70,23 @@ class MagentoImportTest extends TestCase
6470

6571
protected function setUp(): void
6672
{
67-
$this->design = $this->getMockForAbstractClass(DesignInterface::class);
68-
$this->fileSource = $this->getMockForAbstractClass(CollectorInterface::class);
69-
$this->errorHandler = $this->getMockForAbstractClass(
70-
ErrorHandlerInterface::class
71-
);
72-
$this->asset = $this->createMock(File::class);
73-
$this->asset->expects($this->any())->method('getContentType')->willReturn('css');
74-
$this->assetRepo = $this->createMock(Repository::class);
75-
$this->themeProvider = $this->getMockForAbstractClass(ThemeProviderInterface::class);
73+
$this->designMock = $this->getMockForAbstractClass(DesignInterface::class);
74+
$this->fileSourceMock = $this->getMockForAbstractClass(CollectorInterface::class);
75+
$this->errorHandlerMock = $this->getMockForAbstractClass(ErrorHandlerInterface::class);
76+
$this->assetMock = $this->createMock(File::class);
77+
$this->assetMock->expects($this->any())->method('getContentType')->willReturn('css');
78+
$this->assetRepoMock = $this->createMock(Repository::class);
79+
$this->themeProviderMock = $this->getMockForAbstractClass(ThemeProviderInterface::class);
80+
$this->moduleManagerMock = $this->createMock(ModuleManager::class);
81+
7682
$this->object = (new ObjectManager($this))->getObject(MagentoImport::class, [
77-
'design' => $this->design,
78-
'fileSource' => $this->fileSource,
79-
'errorHandler' => $this->errorHandler,
80-
'assetRepo' => $this->assetRepo,
81-
'themeProvider' => $this->themeProvider
83+
'design' => $this->designMock,
84+
'fileSource' => $this->fileSourceMock,
85+
'errorHandler' => $this->errorHandlerMock,
86+
'assetRepo' => $this->assetRepoMock,
87+
'moduleManager' => $this->moduleManagerMock,
88+
// Mocking private property
89+
'themeProvider' => $this->themeProviderMock,
8290
]);
8391
}
8492

@@ -88,24 +96,32 @@ protected function setUp(): void
8896
* @param string $resolvedPath
8997
* @param array $foundFiles
9098
* @param string $expectedContent
99+
* @param array $enabledModules
91100
*
92101
* @dataProvider processDataProvider
93102
*/
94-
public function testProcess($originalContent, $foundPath, $resolvedPath, $foundFiles, $expectedContent)
103+
public function testProcess(
104+
string $originalContent,
105+
string $foundPath,
106+
string $resolvedPath,
107+
array $foundFiles,
108+
string $expectedContent,
109+
array $enabledModules
110+
): void
95111
{
96-
$chain = new Chain($this->asset, $originalContent, 'css', 'path');
112+
$chain = new Chain($this->assetMock, $originalContent, 'css', 'path');
97113
$relatedAsset = $this->createMock(File::class);
98114
$relatedAsset->expects($this->once())
99115
->method('getFilePath')
100116
->willReturn($resolvedPath);
101117
$context = $this->createMock(FallbackContext::class);
102-
$this->assetRepo->expects($this->once())
118+
$this->assetRepoMock->expects($this->once())
103119
->method('createRelated')
104-
->with($foundPath, $this->asset)
120+
->with($foundPath, $this->assetMock)
105121
->willReturn($relatedAsset);
106122
$relatedAsset->expects($this->once())->method('getContext')->willReturn($context);
107123
$theme = $this->getMockForAbstractClass(ThemeInterface::class);
108-
$this->themeProvider->expects($this->once())->method('getThemeByFullPath')->willReturn($theme);
124+
$this->themeProviderMock->expects($this->once())->method('getThemeByFullPath')->willReturn($theme);
109125
$files = [];
110126
foreach ($foundFiles as $file) {
111127
$fileObject = $this->createMock(\Magento\Framework\View\File::class);
@@ -117,10 +133,16 @@ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundF
117133
->willReturn($file['filename']);
118134
$files[] = $fileObject;
119135
}
120-
$this->fileSource->expects($this->once())
136+
$this->fileSourceMock->expects($this->once())
121137
->method('getFiles')
122138
->with($theme, $resolvedPath)
123139
->willReturn($files);
140+
141+
$this->moduleManagerMock->expects($this->any())->method('isEnabled')
142+
->willReturnCallback(function ($moduleName) use ($enabledModules) {
143+
return in_array($moduleName, $enabledModules, true);
144+
});
145+
124146
$this->object->process($chain);
125147
$this->assertEquals($expectedContent, $chain->getContent());
126148
$this->assertEquals('css', $chain->getContentType());
@@ -129,7 +151,7 @@ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundF
129151
/**
130152
* @return array
131153
*/
132-
public function processDataProvider()
154+
public function processDataProvider(): array
133155
{
134156
return [
135157
'non-modular notation' => [
@@ -141,6 +163,7 @@ public function processDataProvider()
141163
['module' => null, 'filename' => 'theme/some/file.css'],
142164
],
143165
"@import 'some/file.css';\n@import 'some/file.css';\n",
166+
[],
144167
],
145168
'modular' => [
146169
'//@magento_import "Magento_Module::some/file.css";',
@@ -151,6 +174,29 @@ public function processDataProvider()
151174
['module' => 'Magento_Two', 'filename' => 'some/file.css'],
152175
],
153176
"@import 'Magento_Module::some/file.css';\n@import 'Magento_Two::some/file.css';\n",
177+
['Magento_Module', 'Magento_Two'],
178+
],
179+
'modular with disabled module' => [
180+
'//@magento_import "Magento_Module::some/file.css";',
181+
'Magento_Module::some/file.css',
182+
'some/file.css',
183+
[
184+
['module' => 'Magento_Module', 'filename' => 'some/file.css'],
185+
['module' => 'Magento_Two', 'filename' => 'some/file.css'],
186+
],
187+
"@import 'Magento_Two::some/file.css';\n",
188+
['Magento_Two'],
189+
],
190+
'modular with disabled all modules' => [
191+
'//@magento_import "Magento_Module::some/file.css";',
192+
'Magento_Module::some/file.css',
193+
'some/file.css',
194+
[
195+
['module' => 'Magento_Module', 'filename' => 'some/file.css'],
196+
['module' => 'Magento_Two', 'filename' => 'some/file.css'],
197+
],
198+
'',
199+
[],
154200
],
155201
'non-modular reference notation' => [
156202
'//@magento_import (reference) "some/file.css";',
@@ -161,6 +207,7 @@ public function processDataProvider()
161207
['module' => null, 'filename' => 'theme/some/file.css'],
162208
],
163209
"@import (reference) 'some/file.css';\n@import (reference) 'some/file.css';\n",
210+
[],
164211
],
165212
'modular reference' => [
166213
'//@magento_import (reference) "Magento_Module::some/file.css";',
@@ -172,35 +219,58 @@ public function processDataProvider()
172219
],
173220
"@import (reference) 'Magento_Module::some/file.css';\n" .
174221
"@import (reference) 'Magento_Two::some/file.css';\n",
222+
['Magento_Module', 'Magento_Two'],
223+
],
224+
'modular reference with disabled module' => [
225+
'//@magento_import (reference) "Magento_Module::some/file.css";',
226+
'Magento_Module::some/file.css',
227+
'some/file.css',
228+
[
229+
['module' => 'Magento_Module', 'filename' => 'some/file.css'],
230+
['module' => 'Magento_Two', 'filename' => 'some/file.css'],
231+
],
232+
"@import (reference) 'Magento_Module::some/file.css';\n",
233+
['Magento_Module'],
234+
],
235+
'modular reference with disabled all modules' => [
236+
'//@magento_import (reference) "Magento_Module::some/file.css";',
237+
'Magento_Module::some/file.css',
238+
'some/file.css',
239+
[
240+
['module' => 'Magento_Module', 'filename' => 'some/file.css'],
241+
['module' => 'Magento_Two', 'filename' => 'some/file.css'],
242+
],
243+
'',
244+
[],
175245
],
176246
];
177247
}
178248

179-
public function testProcessNoImport()
249+
public function testProcessNoImport(): void
180250
{
181251
$originalContent = 'color: #000000;';
182252
$expectedContent = 'color: #000000;';
183-
$chain = new Chain($this->asset, $originalContent, 'css', 'orig');
184-
$this->assetRepo->expects($this->never())
253+
$chain = new Chain($this->assetMock, $originalContent, 'css', 'orig');
254+
$this->assetRepoMock->expects($this->never())
185255
->method('createRelated');
186256
$this->object->process($chain);
187257
$this->assertEquals($expectedContent, $chain->getContent());
188258
$this->assertEquals('css', $chain->getContentType());
189259
}
190260

191-
public function testProcessException()
261+
public function testProcessException(): void
192262
{
193263
$chain = new Chain(
194-
$this->asset,
264+
$this->assetMock,
195265
'//@magento_import "some/file.css";',
196266
'css',
197267
'path'
198268
);
199269
$exception = new \LogicException('Error happened');
200-
$this->assetRepo->expects($this->once())
270+
$this->assetRepoMock->expects($this->once())
201271
->method('createRelated')
202272
->willThrowException($exception);
203-
$this->errorHandler->expects($this->once())
273+
$this->errorHandlerMock->expects($this->once())
204274
->method('processException')
205275
->with($exception);
206276
$this->object->process($chain);

0 commit comments

Comments
 (0)