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