788290481418d582413aff122037de3e5a7ff64c
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Tests / Unit / Utility / FileHandlingUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012
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 * Testcase for the Tx_Extensionmanager_Utility_List class in the TYPO3 Core.
28 *
29 * @package Extension Manager
30 * @subpackage Tests
31 */
32 class FileHandlingUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
33
34 public $fakedExtensions;
35
36 /**
37 * @return void
38 */
39 public function tearDown() {
40 foreach ($this->fakedExtensions as $extension => $dummy) {
41 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir((PATH_site . 'typo3conf/ext/') . $extension, TRUE);
42 }
43 }
44
45 /**
46 * Creates a fake extension inside typo3temp/. No configuration is created,
47 * just the folder
48 *
49 * @param bool $extkeyOnly
50 * @return string The extension key
51 */
52 protected function createFakeExtension($extkeyOnly = FALSE) {
53 $extKey = strtolower(uniqid('testing'));
54 $absExtPath = ((PATH_site . 'typo3conf/ext/') . $extKey) . '/';
55 $relPath = ('typo3conf/ext/' . $extKey) . '/';
56 $this->fakedExtensions[$extKey] = array(
57 'siteRelPath' => $relPath,
58 'siteAbsPath' => $absExtPath
59 );
60 if ($extkeyOnly === TRUE) {
61 return $extKey;
62 }
63 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($absExtPath);
64 return $extKey;
65 }
66
67 /**
68 * @test
69 * @return void
70 */
71 public function makeAndClearExtensionDirRemovesExtensionDirIfAlreadyExists() {
72 $extKey = $this->createFakeExtension();
73 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('removeDirectory', 'addDirectory'));
74 $fileHandlerMock->expects($this->once())->method('removeDirectory')->with(((PATH_site . 'typo3conf/ext/') . $extKey) . '/');
75 $fileHandlerMock->_call('makeAndClearExtensionDir', $extKey);
76 }
77
78 /**
79 * @test
80 * @return void
81 */
82 public function makeAndClearExtensionDirAddsDir() {
83 $extKey = $this->createFakeExtension();
84 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('removeDirectory', 'addDirectory'));
85 $fileHandlerMock->expects($this->once())->method('addDirectory')->with(((PATH_site . 'typo3conf/ext/') . $extKey) . '/');
86 $fileHandlerMock->_call('makeAndClearExtensionDir', $extKey);
87 }
88
89 /**
90 * @test
91 * @expectedException \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
92 * @return void
93 */
94 public function makeAndClearExtensionDirThrowsExceptionOnInvalidPath() {
95 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('removeDirectory', 'addDirectory'));
96 $fileHandlerMock->_call('makeAndClearExtensionDir', 'testing123', 'fakepath');
97 }
98
99 /**
100 * @test
101 * @return void
102 */
103 public function addDirectoryAddsDirectory() {
104 $extDirPath = $this->fakedExtensions[$this->createFakeExtension(TRUE)]['siteAbsPath'];
105 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('dummy'));
106 $this->assertFalse(is_dir($extDirPath));
107 $fileHandlerMock->_call('addDirectory', $extDirPath);
108 $this->assertTrue(is_dir($extDirPath));
109 }
110
111 /**
112 * @test
113 * @expectedException \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
114 * @return void
115 */
116 public function addDirectoryThrowsExceptionOnError() {
117 $extDirPath = '/etc/test123/';
118 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('dummy'));
119 $fileHandlerMock->_call('addDirectory', $extDirPath);
120 }
121
122 /**
123 * @test
124 * @return void
125 */
126 public function removeDirectoryRemovesDirectory() {
127 $extDirPath = $this->fakedExtensions[$this->createFakeExtension()]['siteAbsPath'];
128 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('dummy'));
129 $this->assertTrue(is_dir($extDirPath));
130 $fileHandlerMock->_call('removeDirectory', $extDirPath);
131 $this->assertFalse(is_dir($extDirPath));
132 }
133
134 /**
135 * @test
136 * @expectedException \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
137 * @return void
138 */
139 public function removeDirectoryThrowsExceptionOnError() {
140 $extDirPath = '/etc/test123/';
141 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('dummy'));
142 $fileHandlerMock->_call('removeDirectory', $extDirPath);
143 }
144
145 /**
146 * @test
147 * @return void
148 */
149 public function unpackExtensionFromExtensionDataArrayCreatesTheExtensionDirectory() {
150 $extensionData = array(
151 'extKey' => 'test'
152 );
153 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array(
154 'makeAndClearExtensionDir',
155 'writeEmConfToFile',
156 'extractFilesArrayFromExtensionData',
157 'extractDirectoriesFromExtensionData',
158 'createDirectoriesForExtensionFiles',
159 'writeExtensionFiles'
160 ));
161 $fileHandlerMock->expects($this->once())->method('extractFilesArrayFromExtensionData')->will($this->returnValue(array()));
162 $fileHandlerMock->expects($this->once())->method('extractDirectoriesFromExtensionData')->will($this->returnValue(array()));
163 $fileHandlerMock->expects($this->once())->method('makeAndClearExtensionDir')->with($extensionData['extKey']);
164 $fileHandlerMock->_call('unpackExtensionFromExtensionDataArray', $extensionData);
165 }
166
167 /**
168 * @test
169 * @return void
170 */
171 public function extractFilesArrayFromExtensionDataReturnsFileArray() {
172 $extensionData = array(
173 'key' => 'test',
174 'FILES' => array(
175 'filename1' => 'dummycontent',
176 'filename2' => 'dummycontent2'
177 )
178 );
179 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
180 $extractedFiles = $fileHandlerMock->_call('extractFilesArrayFromExtensionData', $extensionData);
181 $this->assertArrayHasKey('filename1', $extractedFiles);
182 $this->assertArrayHasKey('filename2', $extractedFiles);
183 }
184
185 /**
186 * @test
187 * @return void
188 */
189 public function writeExtensionFilesWritesFiles() {
190 $files = array(
191 'ChangeLog' => array(
192 'name' => 'ChangeLog',
193 'size' => 4559,
194 'mtime' => 1219448527,
195 'is_executable' => FALSE,
196 'content' => 'some content to write'
197 ),
198 'README' => array(
199 'name' => 'README',
200 'size' => 4566,
201 'mtime' => 1219448533,
202 'is_executable' => FALSE,
203 'content' => 'FEEL FREE TO ADD SOME DOCUMENTATION HERE'
204 )
205 );
206 $rootPath = ($extDirPath = $this->fakedExtensions[$this->createFakeExtension()]['siteAbsPath']);
207 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
208 $fileHandlerMock->_call('writeExtensionFiles', $files, $rootPath);
209 $this->assertTrue(file_exists($rootPath . 'ChangeLog'));
210 }
211
212 /**
213 * @test
214 * @return void
215 */
216 public function extractDirectoriesFromExtensionDataExtractsDirectories() {
217 $files = array(
218 'doc/ChangeLog' => array(
219 'name' => 'ChangeLog',
220 'size' => 4559,
221 'mtime' => 1219448527,
222 'is_executable' => FALSE,
223 'content' => 'some content to write'
224 ),
225 'mod/doc/README' => array(
226 'name' => 'README',
227 'size' => 4566,
228 'mtime' => 1219448533,
229 'is_executable' => FALSE,
230 'content' => 'FEEL FREE TO ADD SOME DOCUMENTATION HERE'
231 )
232 );
233 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
234 $extractedDirectories = $fileHandlerMock->_call('extractDirectoriesFromExtensionData', $files);
235 $this->assertContains('doc/', $extractedDirectories);
236 $this->assertContains('mod/doc/', $extractedDirectories);
237 }
238
239 /**
240 * @test
241 * @return void
242 */
243 public function createDirectoriesForExtensionFilesCreatesDirectories() {
244 $rootPath = $this->fakedExtensions[$this->createFakeExtension()]['siteAbsPath'];
245 $directories = array(
246 'doc/',
247 'mod/doc/'
248 );
249 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
250 $this->assertFalse(is_dir($rootPath . 'doc/'));
251 $this->assertFalse(is_dir($rootPath . 'mod/doc/'));
252 $fileHandlerMock->_call('createDirectoriesForExtensionFiles', $directories, $rootPath);
253 $this->assertTrue(is_dir($rootPath . 'doc/'));
254 $this->assertTrue(is_dir($rootPath . 'mod/doc/'));
255 }
256
257 /**
258 * @test
259 * @return void
260 */
261 public function writeEmConfWritesEmConfFile() {
262 $extKey = $this->createFakeExtension();
263 $extensionData = array(
264 'extKey' => $extKey,
265 'EM_CONF' => array(
266 'title' => 'Plugin cache engine',
267 'description' => 'Provides an interface to cache plugin content elements based on 4.3 caching framework',
268 'category' => 'Frontend',
269 'shy' => 0
270 )
271 );
272 $rootPath = $this->fakedExtensions[$extKey]['siteAbsPath'];
273 $emConfUtilityMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\EmConfUtility', array('constructEmConf'));
274 $emConfUtilityMock->expects($this->once())->method('constructEmConf')->with($extensionData)->will($this->returnValue(var_export($extensionData['EM_CONF'], TRUE)));
275 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
276 $fileHandlerMock->_set('emConfUtility', $emConfUtilityMock);
277 $fileHandlerMock->_call('writeEmConfToFile', $extensionData, $rootPath);
278 $this->assertTrue(file_exists($rootPath . 'ext_emconf.php'));
279 }
280
281 }
282
283
284 ?>