TYPO3 CMS  TYPO3_8-7
InstallUtilityTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 class InstallUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
25  protected $extensionKey;
26 
30  protected $extensionData = [];
31 
35  protected $fakedExtensions = [];
36 
40  protected $installMock;
41 
44  protected function setUp()
45  {
46  $this->extensionKey = 'dummy';
47  $this->extensionData = [
48  'key' => $this->extensionKey,
49  'siteRelPath' => '',
50  ];
51  $this->installMock = $this->getAccessibleMock(
52  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
53  [
54  'isLoaded',
55  'loadExtension',
56  'unloadExtension',
57  'updateDatabase',
58  'importStaticSqlFile',
59  'importT3DFile',
60  'reloadCaches',
61  'processCachingFrameworkUpdates',
62  'saveDefaultConfiguration',
63  'getExtensionArray',
64  'enrichExtensionWithDetails',
65  'ensureConfiguredDirectoriesExist',
66  'importInitialFiles',
67  'emitAfterExtensionInstallSignal',
68  ],
69  [],
70  '',
71  false
72  );
73  $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
74  $this->installMock->_set('dependencyUtility', $dependencyUtility);
75  $this->installMock->expects($this->any())
76  ->method('getExtensionArray')
77  ->with($this->extensionKey)
78  ->will($this->returnCallback([$this, 'getExtensionData']));
79  $this->installMock->expects($this->any())
80  ->method('enrichExtensionWithDetails')
81  ->with($this->extensionKey)
82  ->will($this->returnCallback([$this, 'getExtensionData']));
83  }
84 
88  public function getExtensionData()
89  {
90  return $this->extensionData;
91  }
92 
95  protected function tearDown()
96  {
97  foreach ($this->fakedExtensions as $fakeExtkey => $fakeExtension) {
98  $this->testFilesToDelete[] = PATH_site . 'typo3temp/var/tests/' . $fakeExtkey;
99  }
100  parent::tearDown();
101  }
102 
109  protected function createFakeExtension()
110  {
111  $extKey = strtolower($this->getUniqueId('testing'));
112  $absExtPath = PATH_site . 'typo3temp/var/tests/' . $extKey;
113  $relPath = 'typo3temp/var/tests/' . $extKey . '/';
115  $this->fakedExtensions[$extKey] = [
116  'siteRelPath' => $relPath
117  ];
118  return $extKey;
119  }
120 
124  public function installCallsUpdateDatabase()
125  {
126  $this->installMock->expects($this->once())
127  ->method('updateDatabase')
128  ->with([$this->extensionKey]);
129 
130  $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
131  $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
132  $this->installMock->_set('cacheManager', $cacheManagerMock);
133  $this->installMock->install($this->extensionKey);
134  }
135 
139  public function installCallsLoadExtension()
140  {
141  $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
142  $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
143  $this->installMock->_set('cacheManager', $cacheManagerMock);
144  $this->installMock->expects($this->once())->method('loadExtension');
145  $this->installMock->install($this->extensionKey);
146  }
147 
152  {
153  $this->extensionData['clearcacheonload'] = true;
154  $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
155  $cacheManagerMock->expects($this->once())->method('flushCaches');
156  $this->installMock->_set('cacheManager', $cacheManagerMock);
157  $this->installMock->install($this->extensionKey);
158  }
159 
164  {
165  $this->extensionData['clearCacheOnLoad'] = true;
166  $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
167  $cacheManagerMock->expects($this->once())->method('flushCaches');
168  $this->installMock->_set('cacheManager', $cacheManagerMock);
169  $this->installMock->install($this->extensionKey);
170  }
171 
176  {
177  $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
178  $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
179  $this->installMock->_set('cacheManager', $cacheManagerMock);
180  $this->installMock->expects($this->once())->method('ensureConfiguredDirectoriesExist');
181  $this->installMock->install($this->extensionKey);
182  }
183 
187  public function installCallsReloadCaches()
188  {
189  $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
190  $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
191  $this->installMock->_set('cacheManager', $cacheManagerMock);
192  $this->installMock->expects($this->once())->method('reloadCaches');
193  $this->installMock->install($this->extensionKey);
194  }
195 
200  {
201  $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
202  $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
203  $this->installMock->_set('cacheManager', $cacheManagerMock);
204  $this->installMock->expects($this->once())->method('saveDefaultConfiguration')->with($this->extensionKey);
205  $this->installMock->install($this->extensionKey);
206  }
207 
212  {
213  $this->installMock->expects($this->once())->method('unloadExtension');
214  $this->installMock->uninstall($this->extensionKey);
215  }
216 
221  {
222  $extKey = $this->createFakeExtension();
223  $extPath = PATH_site . 'typo3temp/var/tests/' . $extKey . '/';
224  $extTablesFile = $extPath . 'ext_tables.sql';
225  $fileContent = 'DUMMY TEXT TO COMPARE';
226  file_put_contents($extTablesFile, $fileContent);
227  $installMock = $this->getAccessibleMock(
228  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
229  ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
230  [],
231  '',
232  false
233  );
234  $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
235  $installMock->_set('dependencyUtility', $dependencyUtility);
236 
237  $installMock->expects($this->once())->method('updateDbWithExtTablesSql')->with($this->stringStartsWith($fileContent));
238  $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
239  }
240 
245  {
246  $extKey = $this->createFakeExtension();
247  $extRelPath = 'typo3temp/var/tests/' . $extKey . '/';
248  $installMock = $this->getAccessibleMock(
249  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
250  ['importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile'],
251  [],
252  '',
253  false
254  );
255  $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
256  $installMock->_set('dependencyUtility', $dependencyUtility);
257  $installMock->expects($this->once())->method('importStaticSqlFile')->with($extRelPath);
258  $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
259  }
260 
265  {
266  return [
267  'T3D file' => [
268  'data.t3d'
269  ],
270  'XML file' => [
271  'data.xml'
272  ]
273  ];
274  }
275 
281  public function processDatabaseUpdatesCallsImportFile($fileName)
282  {
283  $extKey = $this->createFakeExtension();
284  $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
285  \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
286  file_put_contents($absPath . '/Initialisation/' . $fileName, 'DUMMY');
287  $installMock = $this->getAccessibleMock(
288  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
289  ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
290  [],
291  '',
292  false
293  );
294  $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
295  $installMock->_set('dependencyUtility', $dependencyUtility);
296  $installMock->expects($this->once())->method('importT3DFile')->with($this->fakedExtensions[$extKey]['siteRelPath']);
297  $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
298  }
299 
304  {
305  return [
306  'Import T3D file when T3D was imported before extension to XML' => [
307  'data.t3d',
308  'dataImported',
309  'data.t3d',
310  ],
311  'Import T3D file when a file was imported after extension to XML' => [
312  'data.t3d',
313  'data.t3d',
314  'dataImported'
315  ],
316  'Import XML file when T3D was imported before extension to XML' => [
317  'data.xml',
318  'dataImported',
319  'data.t3d'
320  ],
321  'Import XML file when a file was imported after extension to XML' => [
322  'data.xml',
323  'data.t3d',
324  'dataImported'
325  ]
326  ];
327  }
328 
336  public function importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)
337  {
338  $extKey = $this->createFakeExtension();
339  $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
340  \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
341  file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
342  $registryMock = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
343  ->setMethods(['get', 'set'])
344  ->getMock();
345  $registryMock
346  ->expects($this->any())
347  ->method('get')
348  ->will($this->returnValueMap(
349  [
350  ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, null, false],
351  ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, null, true],
352  ]
353  ));
354  $installMock = $this->getAccessibleMock(
355  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
356  ['getRegistry', 'getImportExportUtility'],
357  [],
358  '',
359  false
360  );
361  $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
362  $installMock->_set('dependencyUtility', $dependencyUtility);
363  $installMock->_set('registry', $registryMock);
364  $installMock->expects($this->never())->method('getImportExportUtility');
365  $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
366  }
367 }
importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)