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