TYPO3 CMS  TYPO3_7-6
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 
21 {
25  protected $extensionKey;
26 
30  protected $extensionData = [];
31 
35  protected $fakedExtensions = [];
36 
40  protected $installMock;
41 
45  protected function setUp()
46  {
47  $this->extensionKey = 'dummy';
48  $this->extensionData = [
49  'key' => $this->extensionKey
50  ];
51  $this->installMock = $this->getAccessibleMock(
52  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
53  [
54  'isLoaded',
55  'loadExtension',
56  'unloadExtension',
57  'processDatabaseUpdates',
58  'processRuntimeDatabaseUpdates',
59  'reloadCaches',
60  'processCachingFrameworkUpdates',
61  'saveDefaultConfiguration',
62  'getExtensionArray',
63  'enrichExtensionWithDetails',
64  'ensureConfiguredDirectoriesExist',
65  'importInitialFiles',
66  'emitAfterExtensionInstallSignal'
67  ],
68  [],
69  '',
70  false
71  );
72  $dependencyUtility = $this->getMock(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class);
73  $this->installMock->_set('dependencyUtility', $dependencyUtility);
74  $this->installMock->expects($this->any())
75  ->method('getExtensionArray')
76  ->with($this->extensionKey)
77  ->will($this->returnCallback([$this, 'getExtensionData']));
78  $this->installMock->expects($this->any())
79  ->method('enrichExtensionWithDetails')
80  ->with($this->extensionKey)
81  ->will($this->returnCallback([$this, 'getExtensionData']));
82  }
83 
87  public function getExtensionData()
88  {
89  return $this->extensionData;
90  }
91 
95  protected function tearDown()
96  {
97  foreach ($this->fakedExtensions as $fakeExtkey => $fakeExtension) {
98  $this->testFilesToDelete[] = PATH_site . 'typo3temp/' . $fakeExtkey;
99  }
100  parent::tearDown();
101  }
102 
109  protected function createFakeExtension()
110  {
111  $extKey = strtolower($this->getUniqueId('testing'));
112  $absExtPath = PATH_site . 'typo3temp/' . $extKey;
113  $relPath = 'typo3temp/' . $extKey . '/';
115  $this->fakedExtensions[$extKey] = [
116  'siteRelPath' => $relPath
117  ];
118  return $extKey;
119  }
120 
125  {
126  $this->installMock->expects($this->once())
127  ->method('processRuntimeDatabaseUpdates')
128  ->with($this->extensionKey);
129 
130  $cacheManagerMock = $this->getMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
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->getMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
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->getMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
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->getMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
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->getMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
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->getMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
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('dummy');
194  }
195 
200  {
201  $cacheManagerMock = $this->getMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
202  $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
203  $this->installMock->_set('cacheManager', $cacheManagerMock);
204  $this->installMock->expects($this->once())->method('saveDefaultConfiguration')->with('dummy');
205  $this->installMock->install('dummy');
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/' . $extKey . '/';
224  $extTablesFile = $extPath . 'ext_tables.sql';
225  $fileContent = 'DUMMY TEXT TO COMPARE';
226  file_put_contents($extTablesFile, $fileContent);
228  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
229  ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
230  [],
231  '',
232  false
233  );
234  $dependencyUtility = $this->getMock(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class);
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/' . $extKey . '/';
249  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
250  ['importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile'],
251  [],
252  '',
253  false
254  );
255  $dependencyUtility = $this->getMock(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class);
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');
288  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
289  ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
290  [],
291  '',
292  false
293  );
294  $dependencyUtility = $this->getMock(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class);
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 
337  public function importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)
338  {
339  $extKey = $this->createFakeExtension();
340  $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
341  \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
342  file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
343  $registryMock = $this->getMock(\TYPO3\CMS\Core\Registry::class, ['get', 'set']);
344  $registryMock
345  ->expects($this->any())
346  ->method('get')
347  ->will($this->returnValueMap(
348  [
349  ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, null, false],
350  ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, null, true],
351  ]
352  ));
354  \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
355  ['getRegistry', 'getImportExportUtility'],
356  [],
357  '',
358  false
359  );
360  $dependencyUtility = $this->getMock(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class);
361  $installMock->_set('dependencyUtility', $dependencyUtility);
362  $installMock->_set('registry', $registryMock);
363  $installMock->expects($this->never())->method('getImportExportUtility');
364  $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
365  }
366 }
importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)