18ff6da9412a3156cd521a13d5c62dcf9487c256
[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 * Testcase
29 *
30 */
31 class InstallUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
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('TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility', array(
61 'loadExtension',
62 'unloadExtension',
63 'processDatabaseUpdates',
64 'reloadCaches',
65 'processCachingFrameworkUpdates',
66 'saveDefaultConfiguration',
67 'enrichExtensionWithDetails',
68 'ensureConfiguredDirectoriesExist',
69 'importInitialFiles'
70 ));
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 }
92
93 /**
94 * Creates a fake extension inside typo3temp/. No configuration is created,
95 * just the folder
96 *
97 * @return string The extension key
98 */
99 protected function createFakeExtension() {
100 $extKey = strtolower(uniqid('testing'));
101 $absExtPath = PATH_site . 'typo3temp/' . $extKey;
102 $relPath = 'typo3temp/' . $extKey;
103 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absExtPath);
104 $this->fakedExtensions[$extKey] = array(
105 'siteRelPath' => $relPath
106 );
107 return $extKey;
108 }
109
110 /**
111 * @test
112 */
113 public function installCallsProcessDatabaseUpdates() {
114 $this->installMock->expects($this->once())
115 ->method('processDatabaseUpdates')
116 ->with($this->extensionData);
117
118 $this->installMock->install($this->extensionKey);
119 }
120
121 /**
122 * @test
123 */
124 public function installCallsProcessCachingFrameworkUpdates() {
125 $this->installMock->expects($this->once())
126 ->method('processCachingFrameworkUpdates');
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->getMock(
191 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
192 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile')
193 );
194 $installMock->expects($this->once())->method('updateDbWithExtTablesSql')->with($this->stringStartsWith($fileContent));
195 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
196 }
197
198 /**
199 * @test
200 */
201 public function processDatabaseUpdatesCallsImportStaticSqlFile() {
202 $extKey = $this->createFakeExtension();
203 $extRelPath = 'typo3temp/' . $extKey;
204 $installMock = $this->getMock(
205 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
206 array('importStaticSqlFile', 'updateDbWithExtTablesSql', 'importT3DFile')
207 );
208 $installMock->expects($this->once())->method('importStaticSqlFile')->with($extRelPath);
209 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
210 }
211
212 /**
213 * @test
214 */
215 public function processDatabaseUpdatesCallsImportT3DFile() {
216 $extKey = $this->createFakeExtension();
217 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
218 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . '/Initialisation');
219 file_put_contents($absPath . '/Initialisation/data.t3d', 'DUMMY');
220 $installMock = $this->getMock(
221 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
222 array('updateDbWithExtTablesSql', 'importStaticSqlFile', 'importT3DFile')
223 );
224 $installMock->expects($this->once())->method('importT3DFile')->with($this->fakedExtensions[$extKey]['siteRelPath']);
225 $installMock->processDatabaseUpdates($this->fakedExtensions[$extKey]);
226 }
227
228 /**
229 * @test
230 */
231 public function importT3DFileDoesNotImportFileIfAlreadyImported() {
232 $extKey = $this->createFakeExtension();
233 $absPath = PATH_site . $this->fakedExtensions[$extKey]['siteRelPath'];
234 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absPath . 'Initialisation');
235 file_put_contents($absPath . 'Initialisation/data.t3d', 'DUMMY');
236 $registryMock = $this->getMock('\\TYPO3\\CMS\\Core\\Registry', array('get', 'set'));
237 $registryMock
238 ->expects($this->once())
239 ->method('get')
240 ->with('extensionDataImport', $this->fakedExtensions[$extKey]['siteRelPath'] . '/Initialisation/data.t3d')
241 ->will($this->returnValue(TRUE)
242 );
243 $installMock = $this->getAccessibleMock(
244 'TYPO3\\CMS\\Extensionmanager\\Utility\\InstallUtility',
245 array('getRegistry', 'getImportExportUtility')
246 );
247 $installMock->_set('registry', $registryMock);
248 $installMock->expects($this->never())->method('getImportExportUtility');
249 $installMock->_call('importT3DFile', $this->fakedExtensions[$extKey]['siteRelPath']);
250 }
251
252 }