‪TYPO3CMS  10.4
InstallUtilityTest.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 Prophecy\Argument;
21 use Psr\Container\ContainerInterface;
22 use Psr\EventDispatcher\EventDispatcherInterface;
23 use Symfony\Component\Yaml\Yaml;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
40 class ‪InstallUtilityTest extends UnitTestCase
41 {
45  protected ‪$extensionKey;
46 
50  protected ‪$extensionData = [];
51 
55  protected ‪$fakedExtensions = [];
56 
57  protected ‪$backupEnvironment = true;
58 
59  protected ‪$resetSingletonInstances = true;
60 
64  protected ‪$installMock;
65 
66  protected function ‪setUp(): void
67  {
68  parent::setUp();
69  $this->extensionKey = 'dummy';
70  $this->extensionData = [
71  'key' => ‪$this->extensionKey,
72  'siteRelPath' => '',
73  ];
74  $this->installMock = $this->getAccessibleMock(
75  InstallUtility::class,
76  [
77  'isLoaded',
78  'loadExtension',
79  'unloadExtension',
80  'updateDatabase',
81  'importStaticSqlFile',
82  'importT3DFile',
83  'reloadCaches',
84  'processCachingFrameworkUpdates',
85  'saveDefaultConfiguration',
86  'getExtensionArray',
87  'enrichExtensionWithDetails',
88  'importInitialFiles',
89  ]
90  );
91  $eventDispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
92  $this->installMock->injectEventDispatcher($eventDispatcherProphecy->reveal());
93  $this->installMock->injectLateBootService($this->prophesize(LateBootService::class)->reveal());
94  $containerProphecy = $this->prophesize(ContainerInterface::class);
95  $containerProphecy->get(EventDispatcherInterface::class)->willReturn($eventDispatcherProphecy->reveal());
96  $lateBootServiceProphecy = $this->prophesize(LateBootService::class);
97  $lateBootServiceProphecy->getContainer()->willReturn($containerProphecy->reveal());
98  $lateBootServiceProphecy->makeCurrent(Argument::cetera())->willReturn([]);
99  $this->installMock->injectLateBootService($lateBootServiceProphecy->reveal());
100  $dependencyUtility = $this->getMockBuilder(DependencyUtility::class)->getMock();
101  $this->installMock->_set('dependencyUtility', $dependencyUtility);
102  $this->installMock->expects(self::any())
103  ->method('getExtensionArray')
104  ->with($this->extensionKey)
105  ->willReturnCallback([$this, 'getExtensionData']);
106  $this->installMock->expects(self::any())
107  ->method('enrichExtensionWithDetails')
108  ->with($this->extensionKey)
109  ->willReturnCallback([$this, 'getExtensionData']);
110 
111  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
112  $cacheManagerProphecy->getCache('core')->willReturn(new ‪NullFrontend('core'));
113  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
114  }
115 
116  protected function ‪tearDown(): void
117  {
118  foreach ($this->fakedExtensions as $fakeExtkey => $fakeExtension) {
119  $this->testFilesToDelete[] = ‪Environment::getVarPath() . '/tests/' . $fakeExtkey;
120  }
121  parent::tearDown();
122  }
123 
127  public function ‪getExtensionData(): array
128  {
130  }
131 
138  protected function ‪createFakeExtension(): string
139  {
140  $extKey = strtolower(‪StringUtility::getUniqueId('testing'));
141  $absExtPath = ‪Environment::getVarPath() . '/tests/' . $extKey;
142  $relativeVarPath = ltrim(str_replace(‪Environment::getProjectPath(), '', ‪Environment::getVarPath()), '/');
143  $relPath = $relativeVarPath . '/tests/' . $extKey . '/';
144  ‪GeneralUtility::mkdir($absExtPath);
145  $this->fakedExtensions[$extKey] = [
146  'siteRelPath' => $relPath
147  ];
148  return $extKey;
149  }
150 
154  public function ‪installCallsUpdateDatabase()
155  {
156  $this->installMock->expects(self::once())->method('updateDatabase');
157 
158  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
159  $cacheManagerMock->expects(self::once())->method('flushCachesInGroup');
160  $this->installMock->_set('cacheManager', $cacheManagerMock);
161  $this->installMock->install($this->extensionKey);
162  }
163 
167  public function ‪installCallsLoadExtension()
168  {
169  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
170  $cacheManagerMock->expects(self::once())->method('flushCachesInGroup');
171  $this->installMock->_set('cacheManager', $cacheManagerMock);
172  $this->installMock->expects(self::once())->method('loadExtension');
173  $this->installMock->install($this->extensionKey);
174  }
175 
180  {
181  $this->extensionData['clearcacheonload'] = true;
182  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
183  $cacheManagerMock->expects(self::once())->method('flushCaches');
184  $this->installMock->_set('cacheManager', $cacheManagerMock);
185  $this->installMock->install($this->extensionKey);
186  }
187 
192  {
193  $this->extensionData['clearCacheOnLoad'] = true;
194  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
195  $cacheManagerMock->expects(self::once())->method('flushCaches');
196  $this->installMock->_set('cacheManager', $cacheManagerMock);
197  $this->installMock->install($this->extensionKey);
198  }
199 
203  public function ‪installCallsReloadCaches()
204  {
205  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
206  $cacheManagerMock->expects(self::once())->method('flushCachesInGroup');
207  $this->installMock->_set('cacheManager', $cacheManagerMock);
208  $this->installMock->expects(self::once())->method('reloadCaches');
209  $this->installMock->install($this->extensionKey);
210  }
211 
216  {
217  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
218  $cacheManagerMock->expects(self::once())->method('flushCachesInGroup');
219  $this->installMock->_set('cacheManager', $cacheManagerMock);
220  $this->installMock->expects(self::once())->method('saveDefaultConfiguration')->with($this->extensionKey);
221  $this->installMock->install($this->extensionKey);
222  }
223 
227  public function ‪uninstallCallsUnloadExtension()
228  {
229  $this->installMock->expects(self::once())->method('unloadExtension');
230  $this->installMock->uninstall($this->extensionKey);
231  }
232 
237  {
238  return [
239  'Import T3D file when T3D was imported before extension to XML' => [
240  'data.t3d',
241  'dataImported',
242  'data.t3d',
243  ],
244  'Import T3D file when a file was imported after extension to XML' => [
245  'data.t3d',
246  'data.t3d',
247  'dataImported'
248  ],
249  'Import XML file when T3D was imported before extension to XML' => [
250  'data.xml',
251  'dataImported',
252  'data.t3d'
253  ],
254  'Import XML file when a file was imported after extension to XML' => [
255  'data.xml',
256  'data.t3d',
257  'dataImported'
258  ]
259  ];
260  }
261 
269  public function ‪importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)
270  {
271  $extKey = $this->‪createFakeExtension();
272  $absPath = ‪Environment::getProjectPath() . '/' . $this->fakedExtensions[$extKey]['siteRelPath'];
273  ‪GeneralUtility::mkdir($absPath . 'Initialisation');
274  file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
275  $registryMock = $this->getMockBuilder(Registry::class)
276  ->setMethods(['get', 'set'])
277  ->getMock();
278  $registryMock
279  ->expects(self::any())
280  ->method('get')
281  ->willReturnMap(
282  [
283  ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, null, false],
284  ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, null, true],
285  ]
286  );
287  ‪$installMock = $this->getAccessibleMock(
288  InstallUtility::class,
289  ['getRegistry', 'getImportExportUtility'],
290  [],
291  '',
292  false
293  );
294  $dependencyUtility = $this->getMockBuilder(DependencyUtility::class)->getMock();
295  ‪$installMock->_set('dependencyUtility', $dependencyUtility);
296  ‪$installMock->_set('registry', $registryMock);
297  ‪$installMock->expects(self::never())->method('getImportExportUtility');
298  ‪$installMock->_call('importT3DFile', $extKey, $this->fakedExtensions[$extKey]['siteRelPath']);
299  }
300 
304  public function ‪siteConfigGetsMovedIntoPlace()
305  {
306  // prepare an extension with a shipped site config
307  $extKey = $this->‪createFakeExtension();
308  $absPath = ‪Environment::getProjectPath() . '/' . $this->fakedExtensions[$extKey]['siteRelPath'];
309  $config = Yaml::dump(['dummy' => true]);
310  $siteIdentifier = 'site_identifier';
311  ‪GeneralUtility::mkdir_deep($absPath . 'Initialisation/Site/' . $siteIdentifier);
312  file_put_contents($absPath . 'Initialisation/Site/' . $siteIdentifier . '/config.yaml', $config);
313 
314  GeneralUtility::setSingletonInstance(SiteConfiguration::class, new ‪SiteConfiguration(‪Environment::getConfigPath() . '/sites'));
315 
316  $subject = new ‪InstallUtility();
317  $subject->injectEventDispatcher($this->prophesize(EventDispatcherInterface::class)->reveal());
318  $listUtility = $this->prophesize(ListUtility::class);
319  $listUtility->injectEventDispatcher($this->prophesize(EventDispatcherInterface::class)->reveal());
320  $subject->injectListUtility($listUtility->reveal());
321 
322  $availableExtensions = [
323  $extKey => [
324  'siteRelPath' => $this->fakedExtensions[$extKey]['siteRelPath'],
325  ],
326  ];
327  $listUtility->enrichExtensionsWithEmConfInformation($availableExtensions)->willReturn($availableExtensions);
328  $listUtility->getAvailableExtensions()->willReturn($availableExtensions);
329  $registry = $this->prophesize(Registry::class);
330  $registry->get('extensionDataImport', Argument::any())->willReturn('some folder name');
331  $registry->get('siteConfigImport', Argument::any())->willReturn(null);
332  $registry->set('siteConfigImport', Argument::cetera())->shouldBeCalled();
333  $subject->injectRegistry($registry->reveal());
334 
335  // provide function result inside test output folder
336  $environment = new ‪Environment();
337  $configDir = $absPath . 'Result/config';
338  if (!file_exists($configDir)) {
339  ‪GeneralUtility::mkdir_deep($configDir);
340  }
341  $environment::initialize(
348  $configDir,
350  'UNIX'
351  );
352  $subject->processExtensionSetup($extKey);
353 
354  $registry->set('siteConfigImport', $siteIdentifier, 1)->shouldHaveBeenCalled();
355  $siteConfigFile = $configDir . '/sites/' . $siteIdentifier . '/config.yaml';
356  self::assertFileExists($siteConfigFile);
357  self::assertStringEqualsFile($siteConfigFile, $config);
358  }
359 
364  {
365  // prepare an extension with a shipped site config
366  $extKey = $this->‪createFakeExtension();
367  $absPath = ‪Environment::getProjectPath() . '/' . $this->fakedExtensions[$extKey]['siteRelPath'];
368  $siteIdentifier = 'site_identifier';
369  ‪GeneralUtility::mkdir_deep($absPath . 'Initialisation/Site/' . $siteIdentifier);
370  file_put_contents($absPath . 'Initialisation/Site/' . $siteIdentifier . '/config.yaml', Yaml::dump(['dummy' => true]));
371 
372  // fake an already existing site config in test output folder
373  $configDir = $absPath . 'Result/config';
374  if (!file_exists($configDir)) {
375  ‪GeneralUtility::mkdir_deep($configDir);
376  }
377  $config = Yaml::dump(['foo' => 'bar']);
378  $existingSiteConfig = 'sites/' . $siteIdentifier . '/config.yaml';
379  ‪GeneralUtility::mkdir_deep($configDir . '/sites/' . $siteIdentifier);
380  file_put_contents($configDir . '/' . $existingSiteConfig, $config);
381 
382  GeneralUtility::setSingletonInstance(SiteConfiguration::class, new ‪SiteConfiguration(‪Environment::getConfigPath() . '/sites'));
383 
384  $subject = new ‪InstallUtility();
385  $subject->injectEventDispatcher($this->prophesize(EventDispatcherInterface::class)->reveal());
386  $listUtility = $this->prophesize(ListUtility::class);
387  $listUtility->injectEventDispatcher($this->prophesize(EventDispatcherInterface::class)->reveal());
388  $subject->injectListUtility($listUtility->reveal());
389 
390  $availableExtensions = [
391  $extKey => [
392  'siteRelPath' => $this->fakedExtensions[$extKey]['siteRelPath'],
393  ],
394  ];
395  $listUtility->enrichExtensionsWithEmConfInformation($availableExtensions)->willReturn($availableExtensions);
396  $listUtility->getAvailableExtensions()->willReturn($availableExtensions);
397  $registry = $this->prophesize(Registry::class);
398  $registry->get('extensionDataImport', Argument::any())->willReturn('some folder name');
399  $registry->get('siteConfigImport', Argument::any())->willReturn(null);
400  $registry->set('siteConfigImport', Argument::cetera())->shouldNotBeCalled();
401  $subject->injectRegistry($registry->reveal());
402 
403  $environment = new ‪Environment();
404 
405  $environment::initialize(
412  $configDir,
414  'UNIX'
415  );
416  $subject->processExtensionSetup($extKey);
417 
418  $siteConfigFile = $configDir . '/sites/' . $siteIdentifier . '/config.yaml';
419  self::assertFileExists($siteConfigFile);
420  self::assertStringEqualsFile($siteConfigFile, $config);
421  }
422 }
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsReloadCaches
‪installCallsReloadCaches()
Definition: InstallUtilityTest.php:199
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsFlushCachesIfClearCacheOnLoadCamelCasedIsSet
‪installCallsFlushCachesIfClearCacheOnLoadCamelCasedIsSet()
Definition: InstallUtilityTest.php:187
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest
Definition: InstallUtilityTest.php:41
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility
Definition: DependencyUtilityTest.php:16
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:33
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsSaveDefaultConfigurationWithExtensionKey
‪installCallsSaveDefaultConfigurationWithExtensionKey()
Definition: InstallUtilityTest.php:211
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$extensionKey
‪string $extensionKey
Definition: InstallUtilityTest.php:44
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static string getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:29
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\importT3DFileDoesNotImportFileIfAlreadyImported
‪importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)
Definition: InstallUtilityTest.php:265
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$backupEnvironment
‪$backupEnvironment
Definition: InstallUtilityTest.php:54
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\siteConfigGetsNotOverriddenIfExistsAlready
‪siteConfigGetsNotOverriddenIfExistsAlready()
Definition: InstallUtilityTest.php:359
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility
Definition: ListUtility.php:41
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:41
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\uninstallCallsUnloadExtension
‪uninstallCallsUnloadExtension()
Definition: InstallUtilityTest.php:223
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:133
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility
Definition: InstallUtility.php:55
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsLoadExtension
‪installCallsLoadExtension()
Definition: InstallUtilityTest.php:163
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider
‪array importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider()
Definition: InstallUtilityTest.php:232
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:2022
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\tearDown
‪tearDown()
Definition: InstallUtilityTest.php:112
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\createFakeExtension
‪string createFakeExtension()
Definition: InstallUtilityTest.php:134
‪TYPO3\CMS\Extensionmanager\Utility\DependencyUtility
Definition: DependencyUtility.php:38
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$extensionData
‪array $extensionData
Definition: InstallUtilityTest.php:48
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsUpdateDatabase
‪installCallsUpdateDatabase()
Definition: InstallUtilityTest.php:150
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Install\Service\LateBootService
Definition: LateBootService.php:34
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsFlushCachesIfClearCacheOnLoadIsSet
‪installCallsFlushCachesIfClearCacheOnLoadIsSet()
Definition: InstallUtilityTest.php:175
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\setUp
‪setUp()
Definition: InstallUtilityTest.php:62
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static bool isComposerMode()
Definition: Environment.php:144
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:210
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\siteConfigGetsMovedIntoPlace
‪siteConfigGetsMovedIntoPlace()
Definition: InstallUtilityTest.php:300
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: InstallUtilityTest.php:56
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\getExtensionData
‪array getExtensionData()
Definition: InstallUtilityTest.php:123
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$fakedExtensions
‪array $fakedExtensions
Definition: InstallUtilityTest.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:2005
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:154
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$installMock
‪PHPUnit Framework MockObject MockObject InstallUtility TYPO3 TestingFramework Core AccessibleObjectInterface $installMock
Definition: InstallUtilityTest.php:60
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192