Revert "[BUGFIX] Replace the table definition manipulation by signals"
[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\Extbase\Tests\Unit\BaseTestCase {
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
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 'reloadCaches',
68 'processCachingFrameworkUpdates',
69 'saveDefaultConfiguration',
70 'enrichExtensionWithDetails',
71 'ensureConfiguredDirectoriesExist',
72 'importInitialFiles'
73 ),
74 array(),
75 '',
76 FALSE
77 );
78 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
79 $this->installMock->_set('dependencyUtility', $dependencyUtility);
80 $this->installMock->expects($this->any())
81 ->method('enrichExtensionWithDetails')
82 ->with($this->extensionKey)
83 ->will($this->returnCallback(array($this, 'getExtensionData')));
84 }
85
86 /**
87 * @return array
88 */
89 public function getExtensionData() {
90 return $this->extensionData;
91 }
92
93 /**
94 * @return void
95 */
96 public function tearDown() {
97 foreach ($this->fakedExtensions as $extension => $dummy) {
98 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir(PATH_site . 'typo3temp/' . $extension, TRUE);
99 }
100 }
101
102 /**
103 * Creates a fake extension inside typo3temp/. No configuration is created,
104 * just the folder
105 *
106 * @return string The extension key
107 */
108 protected function createFakeExtension() {
109 $extKey = strtolower(uniqid('testing'));
110 $absExtPath = PATH_site . 'typo3temp/' . $extKey;
111 $relPath = 'typo3temp/' . $extKey;
112 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absExtPath);
113 $this->fakedExtensions[$extKey] = array(
114 'siteRelPath' => $relPath
115 );
116 return $extKey;
117 }
118
119 /**
120 * @test
121 */
122 public function installCallsProcessDatabaseUpdates() {
123 $this->installMock->expects($this->once())
124 ->method('processDatabaseUpdates')
125 ->with($this->extensionData);
126
127 $this->installMock->install($this->extensionKey);
128 }
129
130 /**
131 * @test
132 */
133 public function installCallsProcessCachingFrameworkUpdates() {
134 $this->installMock->expects($this->once())
135 ->method('processCachingFrameworkUpdates');
136
137 $this->installMock->install($this->extensionKey);
138 }
139
140 /**
141 * @test
142 */
143 public function installCallsLoadExtenion() {
144 $this->installMock->expects($this->once())->method('loadExtension');
145 $this->installMock->install($this->extensionKey);
146 }
147
148 /**
149 * @test
150 */
151 public function installCallsFlushCachesIfClearCacheOnLoadIsSet() {
152 $this->extensionData['clearcacheonload'] = TRUE;
153 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager');
154 $GLOBALS['typo3CacheManager']->expects($this->once())->method('flushCaches');
155 $this->installMock->install($this->extensionKey);
156 }
157
158 /**
159 * @test
160 */
161 public function installationOfAnExtensionWillCallEnsureThatDirectoriesExist() {
162 $this->installMock->expects($this->once())->method('ensureConfiguredDirectoriesExist');
163 $this->installMock->install($this->extensionKey);
164 }
165
166 /**
167 * @test
168 */
169 public function installCallsReloadCaches() {
170 $this->installMock->expects($this->once())->method('reloadCaches');
171 $this->installMock->install('dummy');
172 }
173
174 /**
175 * @test
176 */
177 public function installCallsSaveDefaultConfigurationWithExtensionKey() {
178 $this->installMock->expects($this->once())->method('saveDefaultConfiguration')->with('dummy');
179 $this->installMock->install('dummy');
180 }
181
182 /**
183 * @test
184 */
185 public function uninstallCallsUnloadExtension() {
186 $this->installMock->expects($this->once())->method('unloadExtension');
187 $this->installMock->uninstall($this->extensionKey);
188 }
189
190 /**
191 * @test
192 */
193 public function processDatabaseUpdatesCallsUpdateDbWithExtTablesSql() {
194 $extKey = $this->createFakeExtension();
195 $extPath = PATH_site . 'typo3temp/' . $extKey . '/';
196 $extTablesFile = $extPath . 'ext_tables.sql';
197 $fileContent = 'DUMMY TEXT TO COMPARE';
198 file_put_contents($extTablesFile, $fileContent);
199 $installMock = $this->getAccessibleMock(
200 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
201 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'),
202 array(),
203 '',
204 FALSE
205 );
206 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
207 $installMock->_set('dependencyUtility', $dependencyUtility);
208
209 $installMock->expects($this->once())->method('updateDbWithExtTablesSql')->with($this->stringStartsWith($fileContent));
210 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
211 }
212
213 /**
214 * @test
215 */
216 public function processDatabaseUpdatesCallsImportStaticSqlFile() {
217 $extKey = $this->createFakeExtension();
218 $extRelPath = 'typo3temp/' . $extKey;
219 $installMock = $this->getAccessibleMock(
220 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
221 array('importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile'),
222 array(),
223 '',
224 FALSE
225 );
226 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
227 $installMock->_set('dependencyUtility', $dependencyUtility);
228 $installMock->expects($this->once())->method('importStaticSqlFile')->with($extRelPath);
229 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
230 }
231
232 /**
233 * @test
234 */
235 public function processDatabaseUpdatesCallsImportT3DFile() {
236 $extKey = $this->createFakeExtension();
237 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
238 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
239 file_put_contents($absPath . '/Initialisation/data.t3d', 'DUMMY');
240 $installMock = $this->getAccessibleMock(
241 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
242 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile'),
243 array(),
244 '',
245 FALSE
246 );
247 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
248 $installMock->_set('dependencyUtility', $dependencyUtility);
249 $installMock->expects($this->once())->method('importT3DFile')->with($this->fakedExtensions[$extKey]['siteRelPath']);
250 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
251 }
252
253 /**
254 * @test
255 */
256 public function importT3DFileDoesNotImportFileIfAlreadyImported() {
257 $extKey = $this->createFakeExtension();
258 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
259 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
260 file_put_contents($absPath . 'Initialisation/data.t3d', 'DUMMY');
261 $registryMock = $this->getMock('\\TYPO3\\CMS\\Core\\Registry', array('get', 'set'));
262 $registryMock
263 ->expects($this->once())
264 ->method('get')
265 ->with('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . '/Initialisation/data.t3d')
266 ->will($this->returnValue(TRUE)
267 );
268 $installMock = $this->getAccessibleMock(
269 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
270 array('getRegistry', 'getImportExportUtility'),
271 array(),
272 '',
273 FALSE
274 );
275 $dependencyUtility = $this->getMock('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
276 $installMock->_set('dependencyUtility', $dependencyUtility);
277 $installMock->_set('registry', $registryMock);
278 $installMock->expects($this->never())->method('getImportExportUtility');
279 $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
280 }
281 }