[TASK] Re-work/simplify copyright header in PHP files - Part 9
[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',
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');
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 $extension => $dummy) {
89 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir(PATH_site . 'typo3temp/' . $extension, TRUE);
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');
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');
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');
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 installationOfAnExtensionWillCallEnsureThatDirectoriesExist() {
151 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
152 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
153 $this->installMock->_set('cacheManager', $cacheManagerMock);
154 $this->installMock->expects($this->once())->method('ensureConfiguredDirectoriesExist');
155 $this->installMock->install($this->extensionKey);
156 }
157
158 /**
159 * @test
160 */
161 public function installCallsReloadCaches() {
162 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
163 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
164 $this->installMock->_set('cacheManager', $cacheManagerMock);
165 $this->installMock->expects($this->once())->method('reloadCaches');
166 $this->installMock->install('dummy');
167 }
168
169 /**
170 * @test
171 */
172 public function installCallsSaveDefaultConfigurationWithExtensionKey() {
173 $cacheManagerMock = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
174 $cacheManagerMock->expects($this->once())->method('flushCachesInGroup');
175 $this->installMock->_set('cacheManager', $cacheManagerMock);
176 $this->installMock->expects($this->once())->method('saveDefaultConfiguration')->with('dummy');
177 $this->installMock->install('dummy');
178 }
179
180 /**
181 * @test
182 */
183 public function uninstallCallsUnloadExtension() {
184 $this->installMock->expects($this->once())->method('unloadExtension');
185 $this->installMock->uninstall($this->extensionKey);
186 }
187
188 /**
189 * @test
190 */
191 public function processDatabaseUpdatesCallsUpdateDbWithExtTablesSql() {
192 $extKey = $this->createFakeExtension();
193 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
194 $extTablesFile = $extPath . 'ext_tables.sql';
195 $fileContent = 'DUMMY TEXT TO COMPARE';
196 file_put_contents($extTablesFile, $fileContent);
197 $installMock = $this->getAccessibleMock(
198 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
199 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'),
200 array(),
201 '',
202 FALSE
203 );
204 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
205 $installMock->_set('dependencyUtility', $dependencyUtility);
206
207 $installMock->expects($this->once())->method('updateDbWithExtTablesSql')->with($this->stringStartsWith($fileContent));
208 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
209 }
210
211 /**
212 * @test
213 */
214 public function processDatabaseUpdatesCallsImportStaticSqlFile() {
215 $extKey = $this->createFakeExtension();
216 $extRelPath = 'typo3temp/' . $extKey . '/';
217 $installMock = $this->getAccessibleMock(
218 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
219 array('importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile'),
220 array(),
221 '',
222 FALSE
223 );
224 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
225 $installMock->_set('dependencyUtility', $dependencyUtility);
226 $installMock->expects($this->once())->method('importStaticSqlFile')->with($extRelPath);
227 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
228 }
229
230 /**
231 * @return array
232 */
233 public function processDatabaseUpdatesCallsImportFileDataProvider() {
234 return array(
235 'T3D file' => array(
236 'data.t3d'
237 ),
238 'XML file' => array(
239 'data.xml'
240 )
241 );
242 }
243
244 /**
245 * @param string $fileName
246 * @test
247 * @dataProvider processDatabaseUpdatesCallsImportFileDataProvider
248 */
249 public function processDatabaseUpdatesCallsImportFile($fileName) {
250 $extKey = $this->createFakeExtension();
251 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
252 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
253 file_put_contents($absPath . '/Initialisation/' . $fileName, 'DUMMY');
254 $installMock = $this->getAccessibleMock(
255 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
256 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'),
257 array(),
258 '',
259 FALSE
260 );
261 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
262 $installMock->_set('dependencyUtility', $dependencyUtility);
263 $installMock->expects($this->once())->method('importT3DFile')->with($this->fakedExtensions[$extKey]['siteRelPath']);
264 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
265 }
266
267 /**
268 * @return array
269 */
270 public function importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider() {
271 return array(
272 'Import T3D file when T3D was imported before extension to XML' => array(
273 'data.t3d',
274 'dataImported',
275 'data.t3d',
276 ),
277 'Import T3D file when a file was imported after extension to XML' => array(
278 'data.t3d',
279 'data.t3d',
280 'dataImported'
281 ),
282 'Import XML file when T3D was imported before extension to XML' => array(
283 'data.xml',
284 'dataImported',
285 'data.t3d'
286 ),
287 'Import XML file when a file was imported after extension to XML' => array(
288 'data.xml',
289 'data.t3d',
290 'dataImported'
291 )
292 );
293 }
294
295 /**
296 *
297 * @param string $fileName
298 * @param string $registryNameReturnsFalse
299 * @param string $registryNameReturnsTrue
300 * @test
301 * @dataProvider importT3DFileDoesNotImportFileIfAlreadyImportedDataProvider
302 */
303 public function importT3DFileDoesNotImportFileIfAlreadyImported($fileName, $registryNameReturnsFalse, $registryNameReturnsTrue) {
304 $extKey = $this->createFakeExtension();
305 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
306 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
307 file_put_contents($absPath . 'Initialisation/' . $fileName, 'DUMMY');
308 $registryMock = $this->getMock('\\TYPO3\\CMS\\Core\\Registry', array('get', 'set'));
309 $registryMock
310 ->expects($this->any())
311 ->method('get')
312 ->will($this->returnValueMap(
313 array(
314 array('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsFalse, NULL, FALSE),
315 array('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . 'Initialisation/' . $registryNameReturnsTrue, NULL, TRUE),
316 )
317 ));
318 $installMock = $this->getAccessibleMock(
319 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
320 array('getRegistry', 'getImportExportUtility'),
321 array(),
322 '',
323 FALSE
324 );
325 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
326 $installMock->_set('dependencyUtility', $dependencyUtility);
327 $installMock->_set('registry', $registryMock);
328 $installMock->expects($this->never())->method('getImportExportUtility');
329 $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
330 }
331 }