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