3d64c7c17e9a822a3f878d006847184408f1f9d0
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Tests / Unit / Utility / InstallUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Utility;
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
17 /**
18 * Test case
19 */
20 class InstallUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
22 /**
23 * @var string
24 */
25 protected $extensionKey;
26
27 /**
28 * @var array
29 */
30 protected $extensionData = [];
31
32 /**
33 * @var array List of created fake extensions to be deleted in tearDown() again
34 */
35 protected $fakedExtensions = [];
36
37 /**
38 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Extensionmanager\Utility\InstallUtility|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
39 */
40 protected $installMock;
41
42 /**
43 */
44 protected function setUp()
45 {
46 $this->extensionKey = 'dummy';
47 $this->extensionData = [
48 'key' => $this->extensionKey
49 ];
50 $this->installMock = $this->getAccessibleMock(
51 \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
52 [
53 'isLoaded',
54 'loadExtension',
55 'unloadExtension',
56 'processDatabaseUpdates',
57 'processRuntimeDatabaseUpdates',
58 'reloadCaches',
59 'processCachingFrameworkUpdates',
60 'saveDefaultConfiguration',
61 'getExtensionArray',
62 'enrichExtensionWithDetails',
63 'ensureConfiguredDirectoriesExist',
64 'importInitialFiles',
65 'emitAfterExtensionInstallSignal'
66 ],
67 [],
68 '',
69 false
70 );
71 $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
72 $this->installMock->_set('dependencyUtility', $dependencyUtility);
73 $this->installMock->expects($this->any())
74 ->method('getExtensionArray')
75 ->with($this->extensionKey)
76 ->will($this->returnCallback([$this, 'getExtensionData']));
77 $this->installMock->expects($this->any())
78 ->method('enrichExtensionWithDetails')
79 ->with($this->extensionKey)
80 ->will($this->returnCallback([$this, 'getExtensionData']));
81 }
82
83 /**
84 * @return array
85 */
86 public function getExtensionData()
87 {
88 return $this->extensionData;
89 }
90
91 /**
92 */
93 protected function tearDown()
94 {
95 foreach ($this->fakedExtensions as $fakeExtkey => $fakeExtension) {
96 $this->testFilesToDelete[] = PATH_site . 'typo3temp/var/tests/' . $fakeExtkey;
97 }
98 parent::tearDown();
99 }
100
101 /**
102 * Creates a fake extension inside typo3temp/. No configuration is created,
103 * just the folder
104 *
105 * @return string The extension key
106 */
107 protected function createFakeExtension()
108 {
109 $extKey = strtolower($this->getUniqueId('testing'));
110 $absExtPath = PATH_site . 'typo3temp/var/tests/' . $extKey;
111 $relPath = 'typo3temp/var/tests/' . $extKey . '/';
112 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absExtPath);
113 $this->fakedExtensions[$extKey] = [
114 'siteRelPath' => $relPath
115 ];
116 return $extKey;
117 }
118
119 /**
120 * @test
121 */
122 public function installCallsProcessRuntimeDatabaseUpdates()
123 {
124 $this->installMock->expects($this->once())
125 ->method('processRuntimeDatabaseUpdates')
126 ->with($this->extensionKey);
127
128 $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
129 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
130 $this->installMock->_set('cacheManager', $cacheManagerMock);
131 $this->installMock->install($this->extensionKey);
132 }
133
134 /**
135 * @test
136 */
137 public function installCallsLoadExtension()
138 {
139 $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
140 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
141 $this->installMock->_set('cacheManager', $cacheManagerMock);
142 $this->installMock->expects($this->once())->method('loadExtension');
143 $this->installMock->install($this->extensionKey);
144 }
145
146 /**
147 * @test
148 */
149 public function installCallsFlushCachesIfClearCacheOnLoadIsSet()
150 {
151 $this->extensionData['clearcacheonload'] = true;
152 $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
153 $cacheManagerMock->expects($this->once())->method('flushCaches');
154 $this->installMock->_set('cacheManager', $cacheManagerMock);
155 $this->installMock->install($this->extensionKey);
156 }
157
158 /**
159 * @test
160 */
161 public function installCallsFlushCachesIfClearCacheOnLoadCamelCasedIsSet()
162 {
163 $this->extensionData['clearCacheOnLoad'] = true;
164 $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
165 $cacheManagerMock->expects($this->once())->method('flushCaches');
166 $this->installMock->_set('cacheManager', $cacheManagerMock);
167 $this->installMock->install($this->extensionKey);
168 }
169
170 /**
171 * @test
172 */
173 public function installationOfAnExtensionWillCallEnsureThatDirectoriesExist()
174 {
175 $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
176 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
177 $this->installMock->_set('cacheManager', $cacheManagerMock);
178 $this->installMock->expects($this->once())->method('ensureConfiguredDirectoriesExist');
179 $this->installMock->install($this->extensionKey);
180 }
181
182 /**
183 * @test
184 */
185 public function installCallsReloadCaches()
186 {
187 $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
188 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
189 $this->installMock->_set('cacheManager', $cacheManagerMock);
190 $this->installMock->expects($this->once())->method('reloadCaches');
191 $this->installMock->install('dummy');
192 }
193
194 /**
195 * @test
196 */
197 public function installCallsSaveDefaultConfigurationWithExtensionKey()
198 {
199 $cacheManagerMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\CacheManager::class)->getMock();
200 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
201 $this->installMock->_set('cacheManager', $cacheManagerMock);
202 $this->installMock->expects($this->once())->method('saveDefaultConfiguration')->with('dummy');
203 $this->installMock->install('dummy');
204 }
205
206 /**
207 * @test
208 */
209 public function uninstallCallsUnloadExtension()
210 {
211 $this->installMock->expects($this->once())->method('unloadExtension');
212 $this->installMock->uninstall($this->extensionKey);
213 }
214
215 /**
216 * @test
217 */
218 public function processDatabaseUpdatesCallsUpdateDbWithExtTablesSql()
219 {
220 $extKey = $this->createFakeExtension();
221 $extPath = PATH_site . 'typo3temp/var/tests/' . $extKey . '/';
222 $extTablesFile = $extPath . 'ext_tables.sql';
223 $fileContent = 'DUMMY TEXT TO COMPARE';
224 file_put_contents($extTablesFile, $fileContent);
225 $installMock = $this->getAccessibleMock(
226 \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
227 ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
228 [],
229 '',
230 false
231 );
232 $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
233 $installMock->_set('dependencyUtility', $dependencyUtility);
234
235 $installMock->expects($this->once())->method('updateDbWithExtTablesSql')->with($this->stringStartsWith($fileContent));
236 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
237 }
238
239 /**
240 * @test
241 */
242 public function processDatabaseUpdatesCallsImportStaticSqlFile()
243 {
244 $extKey = $this->createFakeExtension();
245 $extRelPath = 'typo3temp/var/tests/' . $extKey . '/';
246 $installMock = $this->getAccessibleMock(
247 \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
248 ['importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile'],
249 [],
250 '',
251 false
252 );
253 $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
254 $installMock->_set('dependencyUtility', $dependencyUtility);
255 $installMock->expects($this->once())->method('importStaticSqlFile')->with($extRelPath);
256 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
257 }
258
259 /**
260 * @return array
261 */
262 public function processDatabaseUpdatesCallsImportFileDataProvider()
263 {
264 return [
265 'T3D file' => [
266 'data.t3d'
267 ],
268 'XML file' => [
269 'data.xml'
270 ]
271 ];
272 }
273
274 /**
275 * @param string $fileName
276 * @test
277 * @dataProvider processDatabaseUpdatesCallsImportFileDataProvider
278 */
279 public function processDatabaseUpdatesCallsImportFile($fileName)
280 {
281 $extKey = $this->createFakeExtension();
282 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
283 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
284 file_put_contents($absPath . '/Initialisation/' . $fileName, 'DUMMY');
285 $installMock = $this->getAccessibleMock(
286 \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
287 ['updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'],
288 [],
289 '',
290 false
291 );
292 $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
293 $installMock->_set('dependencyUtility', $dependencyUtility);
294 $installMock->expects($this->once())->method('importT3DFile')->with($this->fakedExtensions[$extKey]['siteRelPath']);
295 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
296 }
297
298 /**
299 * @return array
300 */
301 public function importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider()
302 {
303 return [
304 'Import T3D file when T3D was imported before extension to XML' => [
305 'data.t3d',
306 'dataImported',
307 'data.t3d',
308 ],
309 'Import T3D file when a file was imported after extension to XML' => [
310 'data.t3d',
311 'data.t3d',
312 'dataImported'
313 ],
314 'Import XML file when T3D was imported before extension to XML' => [
315 'data.xml',
316 'dataImported',
317 'data.t3d'
318 ],
319 'Import XML file when a file was imported after extension to XML' => [
320 'data.xml',
321 'data.t3d',
322 'dataImported'
323 ]
324 ];
325 }
326
327 /**
328 * @param string $fileName
329 * @param string $registryNameReturnsFalse
330 * @param string $registryNameReturnsTrue
331 * @test
332 * @dataProvider importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider
333 */
334 public function importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue)
335 {
336 $extKey = $this->createFakeExtension();
337 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
338 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
339 file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
340 $registryMock = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
341 ->setMethods(['get', 'set'])
342 ->getMock();
343 $registryMock
344 ->expects($this->any())
345 ->method('get')
346 ->will($this->returnValueMap(
347 [
348 ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, null, false],
349 ['extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, null, true],
350 ]
351 ));
352 $installMock = $this->getAccessibleMock(
353 \TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
354 ['getRegistry', 'getImportExportUtility'],
355 [],
356 '',
357 false
358 );
359 $dependencyUtility = $this->getMockBuilder(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility::class)->getMock();
360 $installMock->_set('dependencyUtility', $dependencyUtility);
361 $installMock->_set('registry', $registryMock);
362 $installMock->expects($this->never())->method('getImportExportUtility');
363 $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
364 }
365 }