[TASK] Extension Manager: Remove superfluous tests
[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 * @return void
114 */
115 public function removeDirectoryRemovesDirectory() {
116 $extDirPath = $this->fakedExtensions[$this->createFakeExtension()]['siteAbsPath'];
117 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('dummy'));
118 $this->assertTrue(is_dir($extDirPath));
119 $fileHandlerMock->_call('removeDirectory', $extDirPath);
120 $this->assertFalse(is_dir($extDirPath));
121 }
122
123 /**
124 * @test
125 * @return void
126 */
127 public function unpackExtensionFromExtensionDataArrayCreatesTheExtensionDirectory() {
128 $extensionData = array(
129 'extKey' => 'test'
130 );
131 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array(
132 'makeAndClearExtensionDir',
133 'writeEmConfToFile',
134 'extractFilesArrayFromExtensionData',
135 'extractDirectoriesFromExtensionData',
136 'createDirectoriesForExtensionFiles',
137 'writeExtensionFiles'
138 ));
139 $fileHandlerMock->expects($this->once())->method('extractFilesArrayFromExtensionData')->will($this->returnValue(array()));
140 $fileHandlerMock->expects($this->once())->method('extractDirectoriesFromExtensionData')->will($this->returnValue(array()));
141 $fileHandlerMock->expects($this->once())->method('makeAndClearExtensionDir')->with($extensionData['extKey']);
142 $fileHandlerMock->_call('unpackExtensionFromExtensionDataArray', $extensionData);
143 }
144
145 /**
146 * @test
147 * @return void
148 */
149 public function extractFilesArrayFromExtensionDataReturnsFileArray() {
150 $extensionData = array(
151 'key' => 'test',
152 'FILES' => array(
153 'filename1' => 'dummycontent',
154 'filename2' => 'dummycontent2'
155 )
156 );
157 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
158 $extractedFiles = $fileHandlerMock->_call('extractFilesArrayFromExtensionData', $extensionData);
159 $this->assertArrayHasKey('filename1', $extractedFiles);
160 $this->assertArrayHasKey('filename2', $extractedFiles);
161 }
162
163 /**
164 * @test
165 * @return void
166 */
167 public function writeExtensionFilesWritesFiles() {
168 $files = array(
169 'ChangeLog' => array(
170 'name' => 'ChangeLog',
171 'size' => 4559,
172 'mtime' => 1219448527,
173 'is_executable' => FALSE,
174 'content' => 'some content to write'
175 ),
176 'README' => array(
177 'name' => 'README',
178 'size' => 4566,
179 'mtime' => 1219448533,
180 'is_executable' => FALSE,
181 'content' => 'FEEL FREE TO ADD SOME DOCUMENTATION HERE'
182 )
183 );
184 $rootPath = ($extDirPath = $this->fakedExtensions[$this->createFakeExtension()]['siteAbsPath']);
185 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
186 $fileHandlerMock->_call('writeExtensionFiles', $files, $rootPath);
187 $this->assertTrue(file_exists($rootPath . 'ChangeLog'));
188 }
189
190 /**
191 * @test
192 * @return void
193 */
194 public function extractDirectoriesFromExtensionDataExtractsDirectories() {
195 $files = array(
196 'doc/ChangeLog' => array(
197 'name' => 'ChangeLog',
198 'size' => 4559,
199 'mtime' => 1219448527,
200 'is_executable' => FALSE,
201 'content' => 'some content to write'
202 ),
203 'mod/doc/README' => array(
204 'name' => 'README',
205 'size' => 4566,
206 'mtime' => 1219448533,
207 'is_executable' => FALSE,
208 'content' => 'FEEL FREE TO ADD SOME DOCUMENTATION HERE'
209 )
210 );
211 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
212 $extractedDirectories = $fileHandlerMock->_call('extractDirectoriesFromExtensionData', $files);
213 $this->assertContains('doc/', $extractedDirectories);
214 $this->assertContains('mod/doc/', $extractedDirectories);
215 }
216
217 /**
218 * @test
219 * @return void
220 */
221 public function createDirectoriesForExtensionFilesCreatesDirectories() {
222 $rootPath = $this->fakedExtensions[$this->createFakeExtension()]['siteAbsPath'];
223 $directories = array(
224 'doc/',
225 'mod/doc/'
226 );
227 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
228 $this->assertFalse(is_dir($rootPath . 'doc/'));
229 $this->assertFalse(is_dir($rootPath . 'mod/doc/'));
230 $fileHandlerMock->_call('createDirectoriesForExtensionFiles', $directories, $rootPath);
231 $this->assertTrue(is_dir($rootPath . 'doc/'));
232 $this->assertTrue(is_dir($rootPath . 'mod/doc/'));
233 }
234
235 /**
236 * @test
237 * @return void
238 */
239 public function writeEmConfWritesEmConfFile() {
240 $extKey = $this->createFakeExtension();
241 $extensionData = array(
242 'extKey' => $extKey,
243 'EM_CONF' => array(
244 'title' => 'Plugin cache engine',
245 'description' => 'Provides an interface to cache plugin content elements based on 4.3 caching framework',
246 'category' => 'Frontend',
247 'shy' => 0
248 )
249 );
250 $rootPath = $this->fakedExtensions[$extKey]['siteAbsPath'];
251 $emConfUtilityMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\EmConfUtility', array('constructEmConf'));
252 $emConfUtilityMock->expects($this->once())->method('constructEmConf')->with($extensionData)->will($this->returnValue(var_export($extensionData['EM_CONF'], TRUE)));
253 $fileHandlerMock = $this->getAccessibleMock('TYPO3\\CMS\\Extensionmanager\\Utility\\FileHandlingUtility', array('makeAndClearExtensionDir'));
254 $fileHandlerMock->_set('emConfUtility', $emConfUtilityMock);
255 $fileHandlerMock->_call('writeEmConfToFile', $extensionData, $rootPath);
256 $this->assertTrue(file_exists($rootPath . 'ext_emconf.php'));
257 }
258
259 }
260
261
262 ?>