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