‪TYPO3CMS  ‪main
ImportMapTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\CMS\Core\Package\PackageManager;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪ImportMapTest extends UnitTestCase
32 {
33  protected array ‪$packages = [];
34 
35  protected bool ‪$backupEnvironment = true;
36 
37  protected ?PackageManager ‪$backupPackageManager = null;
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43 
49  __DIR__,
53  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
54  );
55  $this->backupPackageManager = \Closure::bind(fn(): PackageManager => ‪ExtensionManagementUtility::$packageManager, null, ExtensionManagementUtility::class)();
56  $this->hashService = new ‪HashService();
58  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
59  }
60 
61  protected function ‪tearDown(): void
62  {
63  ‪ExtensionManagementUtility::setPackageManager($this->backupPackageManager);
64  $this->backupPackageManager = null;
65  $this->packages = [];
66  parent::tearDown();
67  }
68 
69  #[Test]
70  public function ‪emptyOutputIfNoModuleIsLoaded(): void
71  {
72  $this->packages = ['core'];
73 
74  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
75  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
76 
77  self::assertSame('', ‪$output);
78  }
79 
80  #[Test]
81  public function ‪emptyOutputIfNoModuleIsDefined(): void
82  {
83  $this->packages = ['package1'];
84 
85  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
86  ‪$url = $importMap->resolveImport('lit');
87  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
88 
89  self::assertSame('', ‪$output);
90  self::assertNull(‪$url);
91  }
92 
93  #[Test]
94  public function ‪resolveImport(): void
95  {
96  $this->packages = ['core'];
97 
98  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
99  ‪$url = $importMap->resolveImport('lit');
100 
101  self::assertStringStartsWith('Fixtures/ImportMap/core/Resources/Public/JavaScript/Contrib/lit/index.js?bust=', ‪$url);
102  }
103 
104  #[Test]
106  {
107  $this->packages = ['core'];
108 
109  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
110  ‪$url = $importMap->resolveImport('lit');
111  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
112 
113  self::assertStringStartsWith('Fixtures/ImportMap/core/Resources/Public/JavaScript/Contrib/lit/index.js?bust=', ‪$url);
114  self::assertStringContainsString('"lit/":"/Fixtures/ImportMap/core/Resources/Public/JavaScript/Contrib/lit/"', ‪$output);
115  self::assertStringContainsString('"@typo3/core/Module1.js":"/Fixtures/ImportMap/core/Resources/Public/JavaScript/Module1.js?bust=', ‪$output);
116  ‪ExtensionManagementUtility::setPackageManager($this->createMock(PackageManager::class));
117  }
118 
119  #[Test]
120  public function ‪renderIncludedImportConfiguration(): void
121  {
122  $this->packages = ['core'];
123 
124  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
125  $importMap->includeImportsFor('@typo3/core/Module1.js');
126  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
127 
128  self::assertStringContainsString('"@typo3/core/":"/Fixtures/ImportMap/core/Resources/Public/JavaScript/', ‪$output);
129  self::assertStringContainsString('"@typo3/core/Module1.js":"/Fixtures/ImportMap/core/Resources/Public/JavaScript/Module1.js?bust=', ‪$output);
130  }
131 
132  #[Test]
133  public function ‪composesTwoImportMaps(): void
134  {
135  $this->packages = ['core', 'package2'];
136 
137  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
138  $importMap->includeImportsFor('@typo3/core/Module1.js');
139  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
140 
141  self::assertStringContainsString('"@typo3/core/":"/Fixtures/ImportMap/core/Resources/Public/JavaScript/', ‪$output);
142  self::assertStringContainsString('"@typo3/core/Module1.js":"/Fixtures/ImportMap/core/Resources/Public/JavaScript/Module1.js?bust=', ‪$output);
143  ‪ExtensionManagementUtility::setPackageManager($this->createMock(PackageManager::class));
144  }
145 
146  #[Test]
147  public function ‪handlesImportMapOverwrites(): void
148  {
149  $this->packages = ['package2', 'package3'];
150 
151  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
152  $importMap->includeImportsFor('@typo3/package2/File.js');
153  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
154 
155  self::assertStringContainsString('"@typo3/package2/File.js":"/Fixtures/ImportMap/package3/Resources/Public/JavaScript/Overrides/Package2/File.js?bust=', ‪$output);
156  self::assertThat(
157  ‪$output,
158  self::logicalNot(
159  self::stringContains('"@typo3/package2/File.js":"/Fixtures/ImportMap/package2/')
160  )
161  );
162  }
163 
164  #[Test]
165  public function ‪dependenciesAreLoaded(): void
166  {
167  $this->packages = ['core', 'package2'];
168  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
169  $importMap->includeImportsFor('@typo3/package2/file.js');
170  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
171 
172  self::assertStringContainsString('@typo3/core/', ‪$output);
173  }
174 
175  #[Test]
176  public function ‪unusedConfigurationsAreOmitted(): void
177  {
178  $this->packages = ['core', 'package2', 'package4'];
179  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
180  $importMap->includeImportsFor('@typo3/package2/file.js');
181  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
182 
183  self::assertThat(
184  ‪$output,
185  self::logicalNot(
186  self::stringContains('@acme/package4/Backend/Helper.js')
187  )
188  );
189  }
190 
191  #[Test]
192  public function ‪includeAllImportsIsRespected(): void
193  {
194  $this->packages = ['core', 'package2', 'package4'];
195  $importMap = new ‪ImportMap($this->hashService, $this->‪getPackages());
196  $importMap->includeAllImports();
197  $importMap->includeImportsFor('@typo3/package2/file.js');
198  ‪$output = $importMap->render('/', new ‪ConsumableNonce());
199 
200  self::assertStringContainsString('@typo3/core/', ‪$output);
201  self::assertStringContainsString('@acme/package4/Backend/Helper.js', ‪$output);
202  }
203 
204  protected function ‪getPackages(): array
205  {
206  $packageInstances = [];
207  foreach ($this->packages as $key) {
208  $packageMock = $this->createMock(PackageInterface::class);
209  $packageMock->method('getPackagePath')->willReturn(__DIR__ . '/Fixtures/ImportMap/' . $key . '/');
210  $packageMock->method('getPackageKey')->willReturn($key);
211  $packageMetadataMock = $this->createMock(MetaData::class);
212  $packageMetadataMock->method('getVersion')->willReturn('1.0.0');
213  $packageMock->method('getPackageMetadata')->willReturn($packageMetadataMock);
214  $packageInstances[$key] = $packageMock;
215  }
216 
217  return $packageInstances;
218  }
219 
220  protected function ‪mockPackageManager(): PackageManager
221  {
222  $test = $this;
223  $packageManagerMock = $this->createMock(PackageManager::class);
224  $packageManagerMock->method('resolvePackagePath')->willReturnCallback(
225  fn(string $path): string => str_replace(
226  array_map(fn(‪PackageInterface $package): string => 'EXT:' . $package->‪getPackageKey() . '/', $test->getPackages()),
227  array_map(fn(‪PackageInterface $package): string => $package->‪getPackagePath(), $test->getPackages()),
228  $path
229  )
230  );
231  return $packageManagerMock;
232  }
233 }
‪TYPO3\CMS\Core\Package\PackageInterface\getPackageKey
‪string getPackageKey()
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\emptyOutputIfNoModuleIsDefined
‪emptyOutputIfNoModuleIsDefined()
Definition: ImportMapTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Page
Definition: AssetCollectorTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\mockPackageManager
‪mockPackageManager()
Definition: ImportMapTest.php:220
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static isComposerMode()
Definition: Environment.php:137
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\emptyOutputIfNoModuleIsLoaded
‪emptyOutputIfNoModuleIsLoaded()
Definition: ImportMapTest.php:70
‪TYPO3\CMS\Core\Package\PackageInterface\getPackagePath
‪string getPackagePath()
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\ConsumableNonce
Definition: ConsumableNonce.php:24
‪TYPO3\CMS\Core\Page\ImportMap
Definition: ImportMap.php:35
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\includeAllImportsIsRespected
‪includeAllImportsIsRespected()
Definition: ImportMapTest.php:192
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\composesTwoImportMaps
‪composesTwoImportMaps()
Definition: ImportMapTest.php:133
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\$hashService
‪HashService $hashService
Definition: ImportMapTest.php:38
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\setUp
‪setUp()
Definition: ImportMapTest.php:40
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:24
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static getConfigPath()
Definition: Environment.php:212
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\$backupEnvironment
‪bool $backupEnvironment
Definition: ImportMapTest.php:35
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:32
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\$backupPackageManager
‪PackageManager $backupPackageManager
Definition: ImportMapTest.php:37
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\setPackageManager
‪static setPackageManager(PackageManager $packageManager)
Definition: ExtensionManagementUtility.php:41
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest
Definition: ImportMapTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\resolveAndImplicitlyIncludeModuleConfiguration
‪resolveAndImplicitlyIncludeModuleConfiguration()
Definition: ImportMapTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\renderIncludedImportConfiguration
‪renderIncludedImportConfiguration()
Definition: ImportMapTest.php:120
‪TYPO3\CMS\Core\Core\Environment\initialize
‪static initialize(ApplicationContext $context, bool $cli, bool $composerMode, string $projectPath, string $publicPath, string $varPath, string $configPath, string $currentScript, string $os)
Definition: Environment.php:100
‪TYPO3\CMS\Core\Package\MetaData
Definition: PackageConstraint.php:16
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\unusedConfigurationsAreOmitted
‪unusedConfigurationsAreOmitted()
Definition: ImportMapTest.php:176
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\$packageManager
‪static PackageManager $packageManager
Definition: ExtensionManagementUtility.php:33
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\dependenciesAreLoaded
‪dependenciesAreLoaded()
Definition: ImportMapTest.php:165
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\$packages
‪array $packages
Definition: ImportMapTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\getPackages
‪getPackages()
Definition: ImportMapTest.php:204
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\tearDown
‪tearDown()
Definition: ImportMapTest.php:61
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\handlesImportMapOverwrites
‪handlesImportMapOverwrites()
Definition: ImportMapTest.php:147
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Tests\Unit\Page\ImportMapTest\resolveImport
‪resolveImport()
Definition: ImportMapTest.php:94
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276