91bc2d875cf6ef53cce40c32e2d7e5ea767a0329
[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 * Copyright notice
6 *
7 * (c) 2012-2013 Susanne Moog, <susanne.moog@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case
29 */
30 class InstallUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @var string
34 */
35 protected $extensionKey;
36
37 /**
38 * @var array
39 */
40 protected $extensionData = array();
41
42 /**
43 * @var array List of created fake extensions to be deleted in tearDown() again
44 */
45 protected $fakedExtensions = array();
46
47 /**
48 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Extensionmanager\Utility\InstallUtility|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
49 */
50 protected $installMock;
51
52 /**
53 * @return void
54 */
55 public function setUp() {
56 $this->extensionKey = 'dummy';
57 $this->extensionData = array(
58 'key' => $this->extensionKey
59 );
60 $this->installMock = $this->getAccessibleMock(
61 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
62 array(
63 'isLoaded',
64 'loadExtension',
65 'unloadExtension',
66 'processDatabaseUpdates',
67 'processRuntimeDatabaseUpdates',
68 'reloadCaches',
69 'processCachingFrameworkUpdates',
70 'saveDefaultConfiguration',
71 'enrichExtensionWithDetails',
72 'ensureConfiguredDirectoriesExist',
73 'importInitialFiles'
74 ),
75 array(),
76 '',
77 FALSE
78 );
79 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
80 $this->installMock->_set('dependencyUtility', $dependencyUtility);
81 $this->installMock->expects($this->any())
82 ->method('enrichExtensionWithDetails')
83 ->with($this->extensionKey)
84 ->will($this->returnCallback(array($this, 'getExtensionData')));
85 }
86
87 /**
88 * @return array
89 */
90 public function getExtensionData() {
91 return $this->extensionData;
92 }
93
94 /**
95 * @return void
96 */
97 public function tearDown() {
98 foreach ($this->fakedExtensions as $extension => $dummy) {
99 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir(PATH_site . 'typo3temp/' . $extension, TRUE);
100 }
101 parent::tearDown();
102 }
103
104 /**
105 * Creates a fake extension inside typo3temp/. No configuration is created,
106 * just the folder
107 *
108 * @return string The extension key
109 */
110 protected function createFakeExtension() {
111 $extKey = strtolower(uniqid('testing'));
112 $absExtPath = PATH_site . 'typo3temp/' . $extKey;
113 $relPath = 'typo3temp/' . $extKey . '/';
114 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absExtPath);
115 $this->fakedExtensions[$extKey] = array(
116 'siteRelPath' => $relPath
117 );
118 return $extKey;
119 }
120
121 /**
122 * @test
123 */
124 public function installCallsProcessRuntimeDatabaseUpdates() {
125 $this->installMock->expects($this->once())
126 ->method('processRuntimeDatabaseUpdates')
127 ->with($this->extensionKey);
128
129 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
130 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
131 $this->installMock->_set('cacheManager', $cacheManagerMock);
132 $this->installMock->install($this->extensionKey);
133 }
134
135 /**
136 * @test
137 */
138 public function installCallsLoadExtension() {
139 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
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 $this->extensionData['clearcacheonload'] = TRUE;
151 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
152 $cacheManagerMock->expects($this->once())->method('flushCaches');
153 $this->installMock->_set('cacheManager', $cacheManagerMock);
154 $this->installMock->install($this->extensionKey);
155 }
156
157 /**
158 * @test
159 */
160 public function installationOfAnExtensionWillCallEnsureThatDirectoriesExist() {
161 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
162 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
163 $this->installMock->_set('cacheManager', $cacheManagerMock);
164 $this->installMock->expects($this->once())->method('ensureConfiguredDirectoriesExist');
165 $this->installMock->install($this->extensionKey);
166 }
167
168 /**
169 * @test
170 */
171 public function installCallsReloadCaches() {
172 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
173 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
174 $this->installMock->_set('cacheManager', $cacheManagerMock);
175 $this->installMock->expects($this->once())->method('reloadCaches');
176 $this->installMock->install('dummy');
177 }
178
179 /**
180 * @test
181 */
182 public function installCallsSaveDefaultConfigurationWithExtensionKey() {
183 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
184 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
185 $this->installMock->_set('cacheManager', $cacheManagerMock);
186 $this->installMock->expects($this->once())->method('saveDefaultConfiguration')->with('dummy');
187 $this->installMock->install('dummy');
188 }
189
190 /**
191 * @test
192 */
193 public function uninstallCallsUnloadExtension() {
194 $this->installMock->expects($this->once())->method('unloadExtension');
195 $this->installMock->uninstall($this->extensionKey);
196 }
197
198 /**
199 * @test
200 */
201 public function processDatabaseUpdatesCallsUpdateDbWithExtTablesSql() {
202 $extKey = $this->createFakeExtension();
203 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
204 $extTablesFile = $extPath . 'ext_tables.sql';
205 $fileContent = 'DUMMY TEXT TO COMPARE';
206 file_put_contents($extTablesFile, $fileContent);
207 $installMock = $this->getAccessibleMock(
208 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
209 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'),
210 array(),
211 '',
212 FALSE
213 );
214 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
215 $installMock->_set('dependencyUtility', $dependencyUtility);
216
217 $installMock->expects($this->once())->method('updateDbWithExtTablesSql')->with($this->stringStartsWith($fileContent));
218 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
219 }
220
221 /**
222 * @test
223 */
224 public function processDatabaseUpdatesCallsImportStaticSqlFile() {
225 $extKey = $this->createFakeExtension();
226 $extRelPath = 'typo3temp/' . $extKey . '/';
227 $installMock = $this->getAccessibleMock(
228 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
229 array('importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile'),
230 array(),
231 '',
232 FALSE
233 );
234 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
235 $installMock->_set('dependencyUtility', $dependencyUtility);
236 $installMock->expects($this->once())->method('importStaticSqlFile')->with($extRelPath);
237 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
238 }
239
240 /**
241 * @return array
242 */
243 public function processDatabaseUpdatesCallsImportFileDataProvider() {
244 return array(
245 'T3D file' => array(
246 'data.t3d'
247 ),
248 'XML file' => array(
249 'data.xml'
250 )
251 );
252 }
253
254 /**
255 * @param string $fileName
256 * @test
257 * @dataProvider processDatabaseUpdatesCallsImportFileDataProvider
258 */
259 public function processDatabaseUpdatesCallsImportFile($fileName) {
260 $extKey = $this->createFakeExtension();
261 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
262 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
263 file_put_contents($absPath . '/Initialisation/' . $fileName, 'DUMMY');
264 $installMock = $this->getAccessibleMock(
265 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
266 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'),
267 array(),
268 '',
269 FALSE
270 );
271 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
272 $installMock->_set('dependencyUtility', $dependencyUtility);
273 $installMock->expects($this->once())->method('importT3DFile')->with($this->fakedExtensions[$extKey]['siteRelPath']);
274 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
275 }
276
277 /**
278 * @return array
279 */
280 public function importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider() {
281 return array(
282 'Import T3D file when T3D was imported before extension to XML' => array(
283 'data.t3d',
284 'dataImported',
285 'data.t3d',
286 ),
287 'Import T3D file when a file was imported after extension to XML' => array(
288 'data.t3d',
289 'data.t3d',
290 'dataImported'
291 ),
292 'Import XML file when T3D was imported before extension to XML' => array(
293 'data.xml',
294 'dataImported',
295 'data.t3d'
296 ),
297 'Import XML file when a file was imported after extension to XML' => array(
298 'data.xml',
299 'data.t3d',
300 'dataImported'
301 )
302 );
303 }
304
305 /**
306 *
307 * @param string $fileName
308 * @param string $registryNameReturnsFalse
309 * @param string $registryNameReturnsTrue
310 * @test
311 * @dataProvider importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider
312 */
313 public function importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue) {
314 $extKey = $this->createFakeExtension();
315 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
316 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
317 file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
318 $registryMock = $this->getMock('\\TYPO3\\CMS\\Core\\Registry', array('get', 'set'));
319 $registryMock
320 ->expects($this->any())
321 ->method('get')
322 ->will($this->returnValueMap(
323 array(
324 array('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, NULL, FALSE),
325 array('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, NULL, TRUE),
326 )
327 ));
328 $installMock = $this->getAccessibleMock(
329 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
330 array('getRegistry', 'getImportExportUtility'),
331 array(),
332 '',
333 FALSE
334 );
335 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
336 $installMock->_set('dependencyUtility', $dependencyUtility);
337 $installMock->_set('registry', $registryMock);
338 $installMock->expects($this->never())->method('getImportExportUtility');
339 $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
340 }
341 }