‪TYPO3CMS  ‪main
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 PHPUnit\Framework\MockObject\MockObject;
21 use Psr\Container\ContainerInterface;
22 use Psr\EventDispatcher\EventDispatcherInterface;
33 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
36 final class ‪InstallUtilityTest extends UnitTestCase
37 {
38  protected bool ‪$resetSingletonInstances = true;
39 
40  private string ‪$extensionKey = 'dummy';
41  private array ‪$extensionData = [];
42  private array ‪$fakedExtensions = [];
43  private ‪InstallUtility&MockObject&AccessibleObjectInterface ‪$installMock;
44 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  $this->extensionData = [
49  'key' => ‪$this->extensionKey,
50  'packagePath' => '',
51  ];
52  $this->installMock = $this->getAccessibleMock(
53  InstallUtility::class,
54  [
55  'loadExtension',
56  'unloadExtension',
57  'updateDatabase',
58  'importStaticSqlFile',
59  'importT3DFile',
60  'reloadCaches',
61  'processExtensionSetup',
62  'saveDefaultConfiguration',
63  'getExtensionArray',
64  'enrichExtensionWithDetails',
65  'importInitialFiles',
66  ]
67  );
68  $eventDispatcher = new ‪NoopEventDispatcher();
69  $this->installMock->injectEventDispatcher($eventDispatcher);
70  $this->installMock->injectBootService($this->createMock(BootService::class));
71  $containerMock = $this->createMock(ContainerInterface::class);
72  $containerMock->method('get')->with(EventDispatcherInterface::class)->willReturn($eventDispatcher);
73  $bootServiceMock = $this->createMock(BootService::class);
74  $bootServiceMock->method('getContainer')->with(false)->willReturn($containerMock);
75  $bootServiceMock->method('makeCurrent')->with(self::anything())->willReturn([]);
76  $this->installMock->injectBootService($bootServiceMock);
77  $this->installMock
78  ->method('getExtensionArray')
79  ->with($this->extensionKey)
80  ->willReturnCallback($this->‪getExtensionData(...));
81  $this->installMock
82  ->method('enrichExtensionWithDetails')
83  ->with($this->extensionKey)
84  ->willReturnCallback($this->‪getExtensionData(...));
85 
86  $cacheManagerMock = $this->createMock(CacheManager::class);
87  $cacheManagerMock->method('getCache')->with('core')->willReturn(new ‪NullFrontend('core'));
88  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerMock);
89  }
90 
91  protected function ‪tearDown(): void
92  {
93  foreach ($this->fakedExtensions as $fakeExtkey => $fakeExtension) {
94  $this->testFilesToDelete[] = ‪Environment::getVarPath() . '/tests/' . $fakeExtkey;
95  }
96  parent::tearDown();
97  }
98 
99  public function ‪getExtensionData(): array
100  {
102  }
103 
110  private function ‪createFakeExtension(): string
111  {
112  $extKey = strtolower(‪StringUtility::getUniqueId('testing'));
113  $absExtPath = ‪Environment::getVarPath() . '/tests/' . $extKey;
114  ‪GeneralUtility::mkdir_deep($absExtPath);
115  $this->fakedExtensions[$extKey] = [
116  'packagePath' => $absExtPath,
117  ];
118  return $extKey;
119  }
120 
124  public function ‪installCallsUpdateDatabase(): void
125  {
126  $this->installMock->expects(self::once())->method('updateDatabase');
127 
128  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
129  $this->installMock->_set('cacheManager', $cacheManagerMock);
130  $this->installMock->install($this->extensionKey);
131  }
132 
136  public function ‪installCallsLoadExtension(): void
137  {
138  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
139  $this->installMock->_set('cacheManager', $cacheManagerMock);
140  $this->installMock->expects(self::once())->method('loadExtension');
141  $this->installMock->install($this->extensionKey);
142  }
143 
147  public function ‪installCallsFlushCaches(): void
148  {
149  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
150  $cacheManagerMock->expects(self::once())->method('flushCaches');
151  $this->installMock->_set('cacheManager', $cacheManagerMock);
152  $this->installMock->install($this->extensionKey);
153  }
154 
158  public function ‪installCallsReloadCaches(): void
159  {
160  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
161  $this->installMock->_set('cacheManager', $cacheManagerMock);
162  $this->installMock->expects(self::once())->method('reloadCaches');
163  $this->installMock->install($this->extensionKey);
164  }
165 
170  {
171  $cacheManagerMock = $this->getMockBuilder(CacheManager::class)->getMock();
172  $this->installMock->_set('cacheManager', $cacheManagerMock);
173  $this->installMock->expects(self::once())->method('saveDefaultConfiguration')->with($this->extensionKey);
174  $this->installMock->install($this->extensionKey);
175  }
176 
178  {
179  return [
180  'Import T3D file when T3D was imported before extension to XML' => [
181  'data.t3d',
182  'dataImported',
183  'data.t3d',
184  ],
185  'Import T3D file when a file was imported after extension to XML' => [
186  'data.t3d',
187  'data.t3d',
188  'dataImported',
189  ],
190  'Import XML file when T3D was imported before extension to XML' => [
191  'data.xml',
192  'dataImported',
193  'data.t3d',
194  ],
195  'Import XML file when a file was imported after extension to XML' => [
196  'data.xml',
197  'data.t3d',
198  'dataImported',
199  ],
200  ];
201  }
202 
207  public function ‪importT3DFileDoesNotImportFileIfAlreadyImported(string $fileName, string $registryNameReturnsFalse, string $registryNameReturnsTrue): void
208  {
209  $extKey = $this->‪createFakeExtension();
210  $absPath = $this->fakedExtensions[$extKey]['packagePath'];
211  $relPath = ‪PathUtility::stripPathSitePrefix($absPath);
212  ‪GeneralUtility::mkdir($absPath . 'Initialisation');
213  file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
214  $registryMock = $this->getMockBuilder(Registry::class)
215  ->onlyMethods(['get', 'set'])
216  ->getMock();
217  $registryMock
218  ->method('get')
219  ->willReturnMap(
220  [
221  ['extensionDataImport', $relPath . 'Initialisation/' . $registryNameReturnsFalse, null, false],
222  ['extensionDataImport', $relPath . 'Initialisation/' . $registryNameReturnsTrue, null, true],
223  ]
224  );
225  ‪$installMock = $this->getAccessibleMock(
226  InstallUtility::class,
227  null,
228  [],
229  '',
230  false
231  );
232  ‪$installMock->_set('registry', $registryMock);
233  ‪$installMock->_call('importT3DFile', $extKey, $this->fakedExtensions[$extKey]['packagePath']);
234  }
235 }
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsReloadCaches
‪installCallsReloadCaches()
Definition: InstallUtilityTest.php:158
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest
Definition: InstallUtilityTest.php:37
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static stripPathSitePrefix(string $path)
Definition: PathUtility.php:428
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility
Definition: DependencyUtilityTest.php:18
‪TYPO3\CMS\Core\Registry
Definition: Registry.php:33
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsSaveDefaultConfigurationWithExtensionKey
‪installCallsSaveDefaultConfigurationWithExtensionKey()
Definition: InstallUtilityTest.php:169
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$extensionKey
‪string $extensionKey
Definition: InstallUtilityTest.php:40
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: InstallUtilityTest.php:38
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$installMock
‪InstallUtility &MockObject &AccessibleObjectInterface $installMock
Definition: InstallUtilityTest.php:43
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider
‪static importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider()
Definition: InstallUtilityTest.php:177
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:30
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Core\BootService
Definition: BootService.php:32
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility
Definition: InstallUtility.php:55
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsLoadExtension
‪installCallsLoadExtension()
Definition: InstallUtilityTest.php:136
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:1753
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\tearDown
‪tearDown()
Definition: InstallUtilityTest.php:91
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\createFakeExtension
‪string createFakeExtension()
Definition: InstallUtilityTest.php:110
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$extensionData
‪array $extensionData
Definition: InstallUtilityTest.php:41
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsUpdateDatabase
‪installCallsUpdateDatabase()
Definition: InstallUtilityTest.php:124
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\installCallsFlushCaches
‪installCallsFlushCaches()
Definition: InstallUtilityTest.php:147
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\setUp
‪setUp()
Definition: InstallUtilityTest.php:45
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\getExtensionData
‪getExtensionData()
Definition: InstallUtilityTest.php:99
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\$fakedExtensions
‪array $fakedExtensions
Definition: InstallUtilityTest.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:1736
‪TYPO3\CMS\Extensionmanager\Tests\Unit\Utility\InstallUtilityTest\importT3DFileDoesNotImportFileIfAlreadyImported
‪importT3DFileDoesNotImportFileIfAlreadyImported(string $fileName, string $registryNameReturnsFalse, string $registryNameReturnsTrue)
Definition: InstallUtilityTest.php:207
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:29