661cdfcb3b6607a4996f4936f8fa96ca7f4e617e
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Utility / File / ExtendedFileUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\Utility\File;
3
4 /* * *************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Fabien Udriot <fabien.udriot@ecodev.ch>
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 TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
28 *
29 * @author Fabien Udriot <fabien.udriot@ecodev.ch>
30 */
31 class ExtendedFileUtilityTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
32
33 /**
34 * @var array A backup of registered singleton instances
35 */
36 protected $singletonInstances = array();
37
38 /**
39 * @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
40 */
41 protected $fileProcessor;
42
43 /**
44 * @var \TYPO3\CMS\Core\Resource\StorageRepository
45 */
46 protected $storageRepository;
47
48 /**
49 * @var string
50 */
51 protected $newFileNameInput = '_unitTestedFile.txt';
52
53 /**
54 * @var string
55 */
56 protected $newFolderNameInput = '_unitTestedFolder';
57
58 /**
59 * @var string
60 */
61 protected $renameFileNameInput = '_unitTestedFileRenamed.txt';
62
63 /**
64 * @var string
65 */
66 protected $renameFolderNameInput = '_unitTestedFolderRenamed';
67
68 /**
69 * @var string
70 */
71 protected $copyFolderNameInput = '_unitTestedFolderCopied';
72
73 /**
74 * @var string
75 */
76 protected $moveFolderNameInput = '_unitTestedFolderMoved';
77
78 /**
79 * @var array
80 */
81 protected $objectsToTearDown = array();
82
83 /**
84 * Sets up this testcase
85 */
86 public function setUp() {
87 $this->markTestIncomplete('needs to be fixed');
88 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
89 \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
90
91 $this->storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
92 // Initializing file processor
93 $GLOBALS['BE_USER'] = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array('getSessionData', 'setAndSaveSessionData'));
94 $GLOBALS['BE_USER']->user['uid'] = 1;
95 // Initializing:
96 $this->fileProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
97 $this->fileProcessor->init(array(), $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
98 $this->fileProcessor->init_actionPerms($GLOBALS['BE_USER']->getFileoperationPermissions());
99 $this->fileProcessor->dontCheckForUnique = 1;
100 }
101
102 /**
103 * Tears down this testcase
104 */
105 public function tearDown() {
106 // Disabled for now
107 return;
108
109 foreach ($this->objectsToTearDown as $object) {
110 if ($object instanceof \TYPO3\CMS\Core\Resource\File || $object instanceof \TYPO3\CMS\Core\Resource\Folder) {
111 $object->delete();
112 }
113 }
114 $this->objectsToTearDown = array();
115 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
116 }
117
118 /**
119 * @return \TYPO3\CMS\Core\Resource\ResourceStorage
120 */
121 protected function getDefaultStorage() {
122 // Get the first storage available.
123 // Notice if no storage is found, a storage is created on the fly.
124 $storages = $this->storageRepository->findAll();
125 // Makes sure to return a storage having a local driver
126 foreach ($storages as $storage) {
127
128 }
129 /** @var $storage \TYPO3\CMS\Core\Resource\ResourceStorage */
130 return $storages[0];
131 }
132
133 /**
134 * @return string
135 */
136 protected function getRootFolderIdentifier() {
137 $storage = $this->getDefaultStorage();
138 $folderIdentifier = '/';
139 // the root of the storage
140 $folderCombinedIdentifier = $storage->getUid() . ':' . $folderIdentifier;
141 return $folderCombinedIdentifier;
142 }
143
144 /*********************************
145 * CREATE
146 ********************************/
147 /**
148 * @test
149 */
150 public function createNewFileInLocalStorage() {
151 // Defines values
152 $fileValues = array(
153 'newfile' => array(
154 array(
155 'data' => $this->newFileNameInput,
156 'target' => $this->getRootFolderIdentifier()
157 )
158 )
159 );
160 $this->fileProcessor->start($fileValues);
161 $results = $this->fileProcessor->processData();
162 $fileObject = NULL;
163 if (!empty($results['newfile'][0])) {
164 $fileObject = $results['newfile'][0];
165 }
166 $this->objectsToTearDown[] = $fileObject;
167 $this->assertEquals(TRUE, $fileObject instanceof \TYPO3\CMS\Core\Resource\File);
168 }
169
170 /**
171 * @test
172 */
173 public function createNewFolderInLocalStorage() {
174 // Defines values
175 $fileValues = array(
176 'newfolder' => array(
177 array(
178 'data' => $this->newFolderNameInput,
179 'target' => $this->getRootFolderIdentifier()
180 )
181 )
182 );
183 $this->fileProcessor->start($fileValues);
184 $results = $this->fileProcessor->processData();
185 $folderObject = NULL;
186 if (!empty($results['newfolder'][0])) {
187 $folderObject = $results['newfolder'][0];
188 }
189 $this->objectsToTearDown[] = $folderObject;
190 $this->assertEquals(TRUE, $folderObject instanceof \TYPO3\CMS\Core\Resource\Folder);
191 }
192
193 /*********************************
194 * DELETE
195 ********************************/
196 /**
197 * @test
198 */
199 public function deleteFileInLocalStorage() {
200 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
201 $storage = $this->getDefaultStorage();
202 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
203 // Defines values
204 $fileValues = array(
205 'newfile' => array(
206 array(
207 'data' => $this->newFileNameInput,
208 'target' => $this->getRootFolderIdentifier()
209 )
210 ),
211 'delete' => array(
212 array(
213 'data' => $fileIdentifier
214 )
215 )
216 );
217 $this->fileProcessor->start($fileValues);
218 $results = $this->fileProcessor->processData();
219 $this->assertEquals(TRUE, empty($results['delete'][1]));
220 }
221
222 /**
223 * @test
224 */
225 public function deleteFolderInLocalStorage() {
226 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
227 $storage = $this->getDefaultStorage();
228 $folderIdentifier = $storage->getUid() . ':/' . $this->newFolderNameInput;
229 // Defines values
230 $fileValues = array(
231 'newfolder' => array(
232 array(
233 'data' => $this->newFolderNameInput,
234 'target' => $this->getRootFolderIdentifier()
235 )
236 ),
237 'delete' => array(
238 array(
239 'data' => $folderIdentifier
240 )
241 )
242 );
243 $this->fileProcessor->start($fileValues);
244 $results = $this->fileProcessor->processData();
245 $this->assertEquals(TRUE, $results['delete'][0]);
246 }
247
248 /*********************************
249 * RENAME
250 ********************************/
251 /**
252 * @test
253 */
254 public function renameFileInLocalStorage() {
255 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
256 $storage = $this->getDefaultStorage();
257 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
258 // Defines values
259 $fileValues = array(
260 'newfile' => array(
261 array(
262 'data' => $this->newFileNameInput,
263 'target' => $this->getRootFolderIdentifier()
264 )
265 ),
266 'rename' => array(
267 array(
268 'data' => $fileIdentifier,
269 'target' => $this->renameFileNameInput
270 )
271 )
272 );
273 $this->fileProcessor->start($fileValues);
274 $results = $this->fileProcessor->processData();
275 $fileObject = NULL;
276 if (!empty($results['rename'][0])) {
277 $fileObject = $results['rename'][0];
278 }
279 $this->objectsToTearDown[] = $fileObject;
280 $this->assertEquals(TRUE, $fileObject instanceof \TYPO3\CMS\Core\Resource\File);
281 }
282
283 /**
284 * @test
285 */
286 public function renameFolderInLocalStorage() {
287 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
288 $storage = $this->getDefaultStorage();
289 $folderIdentifier = $storage->getUid() . ':/' . $this->newFolderNameInput;
290 // Defines values
291 $fileValues = array(
292 'newfolder' => array(
293 array(
294 'data' => $this->newFolderNameInput,
295 'target' => $this->getRootFolderIdentifier()
296 )
297 ),
298 'rename' => array(
299 array(
300 'data' => $folderIdentifier,
301 'target' => $this->renameFolderNameInput
302 )
303 )
304 );
305 $this->fileProcessor->start($fileValues);
306 $results = $this->fileProcessor->processData();
307 $folderObject = NULL;
308 if (!empty($results['rename'][0])) {
309 $folderObject = $results['rename'][0];
310 }
311 $this->objectsToTearDown[] = $folderObject;
312 $this->assertEquals(TRUE, $folderObject instanceof \TYPO3\CMS\Core\Resource\Folder);
313 }
314
315 /*********************************
316 * MOVE
317 ********************************/
318 /**
319 * @test
320 */
321 public function moveFileInLocalStorage() {
322 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
323 $storage = $this->getDefaultStorage();
324 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
325 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
326 // Defines values
327 $fileValues = array(
328 'newfile' => array(
329 array(
330 'data' => $this->newFileNameInput,
331 'target' => $this->getRootFolderIdentifier()
332 )
333 ),
334 'newfolder' => array(
335 array(
336 'data' => $this->newFolderNameInput,
337 'target' => $this->getRootFolderIdentifier()
338 )
339 ),
340 'move' => array(
341 array(
342 'data' => $fileIdentifier,
343 'target' => $targetFolder
344 )
345 )
346 );
347 $this->fileProcessor->start($fileValues);
348 $results = $this->fileProcessor->processData();
349 $fileObject = NULL;
350 if (!empty($results['move'][0])) {
351 $fileObject = $results['move'][0];
352 }
353 // remove parent folder
354 if (!empty($results['newfolder'][0])) {
355 $this->objectsToTearDown[] = $results['newfolder'][0];
356 }
357 $this->assertEquals(TRUE, $fileObject instanceof \TYPO3\CMS\Core\Resource\File);
358 }
359
360 /**
361 * @test
362 */
363 public function moveFolderInLocalStorage() {
364 // Computes a $folderIdentifier which looks like 8:/folderName.txt where 8 is the storage Uid
365 $storage = $this->getDefaultStorage();
366 $folderIdentifier = $storage->getUid() . ':/' . $this->moveFolderNameInput;
367 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
368 // Defines values
369 $fileValues = array(
370 'newfolder' => array(
371 array(
372 'data' => $this->newFolderNameInput,
373 'target' => $this->getRootFolderIdentifier()
374 ),
375 array(
376 'data' => $this->moveFolderNameInput,
377 'target' => $this->getRootFolderIdentifier()
378 )
379 ),
380 'move' => array(
381 array(
382 'data' => $folderIdentifier,
383 'target' => $targetFolder
384 )
385 )
386 );
387 $this->fileProcessor->start($fileValues);
388 $results = $this->fileProcessor->processData();
389 $folderObject = NULL;
390 if (!empty($results['move'][0])) {
391 $folderObject = $results['move'][0];
392 }
393 // remove parent folder
394 if (!empty($results['newfolder'][0])) {
395 $this->objectsToTearDown[] = $results['newfolder'][0];
396 }
397 $this->assertEquals(TRUE, $folderObject instanceof \TYPO3\CMS\Core\Resource\Folder);
398 }
399
400 /*********************************
401 * COPY
402 ********************************/
403 /**
404 * @test
405 */
406 public function copyFileInLocalStorage() {
407 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
408 $storage = $this->getDefaultStorage();
409 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
410 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
411 // Defines values
412 $fileValues = array(
413 'newfile' => array(
414 array(
415 'data' => $this->newFileNameInput,
416 'target' => $this->getRootFolderIdentifier()
417 )
418 ),
419 'newfolder' => array(
420 array(
421 'data' => $this->newFolderNameInput,
422 'target' => $this->getRootFolderIdentifier()
423 )
424 ),
425 'copy' => array(
426 array(
427 'data' => $fileIdentifier,
428 'target' => $targetFolder
429 )
430 )
431 );
432 $this->fileProcessor->start($fileValues);
433 $results = $this->fileProcessor->processData();
434 $fileObject = NULL;
435 if (!empty($results['copy'][0])) {
436 $fileObject = $results['copy'][0];
437 }
438 // remove parent folder
439 if (!empty($results['newfolder'][0])) {
440 $this->objectsToTearDown[] = $results['newfolder'][0];
441 }
442 if (!empty($results['newfile'][0])) {
443 $this->objectsToTearDown[] = $results['newfile'][0];
444 }
445 $this->assertEquals(TRUE, $fileObject instanceof \TYPO3\CMS\Core\Resource\File);
446 }
447
448 /**
449 * @test
450 */
451 public function copyFolderInLocalStorage() {
452 // Computes a $folderIdentifier which looks like 8:/folderName.txt where 8 is the storage Uid
453 $storage = $this->getDefaultStorage();
454 $folderIdentifier = $storage->getUid() . ':/' . $this->copyFolderNameInput;
455 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
456 // Defines values
457 $fileValues = array(
458 'newfolder' => array(
459 array(
460 'data' => $this->newFolderNameInput,
461 'target' => $this->getRootFolderIdentifier()
462 ),
463 array(
464 'data' => $this->copyFolderNameInput,
465 'target' => $this->getRootFolderIdentifier()
466 )
467 ),
468 'copy' => array(
469 array(
470 'data' => $folderIdentifier,
471 'target' => $targetFolder
472 )
473 )
474 );
475 $this->fileProcessor->start($fileValues);
476 $results = $this->fileProcessor->processData();
477 $folderObject = NULL;
478 if (!empty($results['copy'][0])) {
479 $folderObject = $results['copy'][0];
480 }
481 // remove parent folder
482 if (!empty($results['newfolder'][0])) {
483 $this->objectsToTearDown[] = $results['newfolder'][0];
484 }
485 if (!empty($results['newfolder'][1])) {
486 $this->objectsToTearDown[] = $results['newfolder'][1];
487 }
488 $this->assertEquals(TRUE, $folderObject instanceof \TYPO3\CMS\Core\Resource\Folder);
489 }
490
491 }