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