[TASK] Clean up and fix unit tests for FAL
[Packages/TYPO3.CMS.git] / tests / t3lib / class.t3lib_extfilefuncTest.php
1 <?php
2
3 /* * *************************************************************
4 * Copyright notice
5 *
6 * (c) 2011 Fabien Udriot <fabien.udriot@ecodev.ch>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 * ************************************************************* */
25
26 /**
27 * Testcase for class t3lib_extFileFunctions
28 *
29 * @author Fabien Udriot <fabien.udriot@ecodev.ch>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_extFileFunctionsTest extends tx_phpunit_testcase {
34
35 /**
36 * @var fileProcessor
37 */
38 protected $fileProcessor;
39
40 /**
41 * @var t3lib_file_Repository_StorageRepository
42 */
43 protected $storageRepository;
44
45 /**
46 * @var string
47 */
48 protected $newFileNameInput = '_unitTestedFile.txt';
49
50 /**
51 * @var string
52 */
53 protected $newFolderNameInput = '_unitTestedFolder';
54
55 /**
56 * @var string
57 */
58 protected $renameFileNameInput = '_unitTestedFileRenamed.txt';
59
60 /**
61 * @var string
62 */
63 protected $renameFolderNameInput = '_unitTestedFolderRenamed';
64
65 /**
66 * @var string
67 */
68 protected $copyFolderNameInput = '_unitTestedFolderCopied';
69
70 /**
71 * @var string
72 */
73 protected $moveFolderNameInput = '_unitTestedFolderMoved';
74
75 /**
76 * @var array
77 */
78 protected $objectsToTearDown = array();
79
80 /**
81 * Sets up this testcase
82 */
83 public function setUp() {
84
85 $this->storageRepository = t3lib_div::makeInstance('t3lib_file_Repository_StorageRepository');
86
87 // Initializing file processor
88 $GLOBALS['BE_USER'] = $this->getMock(
89 't3lib_beUserAuth', array('getSessionData', 'setAndSaveSessionData')
90 );
91 $GLOBALS['BE_USER']->user['uid'] = 1;
92
93 $GLOBALS['FILEMOUNTS'] = array();
94
95 // Initializing:
96 $this->fileProcessor = t3lib_div::makeInstance('t3lib_extFileFunctions');
97 $this->fileProcessor->init($GLOBALS['FILEMOUNTS'], $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 foreach ($this->objectsToTearDown as $object) {
107 if ($object instanceof t3lib_file_File || $object instanceof t3lib_file_Folder) {
108 $object->delete();
109 }
110 }
111 $this->objectsToTearDown = array();
112 }
113
114 /**
115 * @return t3lib_file_Storage
116 */
117 protected function getDefaultStorage() {
118
119 // Get the first storage available.
120 // Notice if no storage is found, a storage is created on the fly.
121 $storages = $this->storageRepository->findAll();
122
123 // Makes sure to return a storage having a local driver
124 foreach ($storages as $storage) {
125 // @todo how to retrieve information about the driver since @driver@ and @storageRecord@ are private
126 }
127
128 //* @var $storage t3lib_file_Storage */
129 return $storages[0];
130 }
131 /**
132 * @return string
133 */
134 protected function getRootFolderIdentifier() {
135 $storage = $this->getDefaultStorage();
136
137 $folderIdentifier = '/'; // the root of the storage
138 $folderCombinedIdentifier = $storage->getUid() . ':' . $folderIdentifier;
139 return $folderCombinedIdentifier;
140 }
141
142 /*********************************
143 * CREATE
144 ********************************/
145 /**
146 * @test
147 */
148 public function createNewFileInLocalStorage() {
149
150 // Defines values
151 $fileValues = array(
152 'newfile' => array(
153 array(
154 'data' => $this->newFileNameInput,
155 'target' => $this->getRootFolderIdentifier(),
156 )
157 )
158 );
159
160 $this->fileProcessor->start($fileValues);
161 $results = $this->fileProcessor->processData();
162
163 $fileObject = NULL;
164 if (!empty ($results['newfile'][0])) {
165 $fileObject = $results['newfile'][0];
166 }
167
168 $this->objectsToTearDown[] = $fileObject;
169 $this->assertEquals(TRUE, $fileObject instanceof t3lib_file_File);
170 }
171
172 /**
173 * @test
174 */
175 public function createNewFolderInLocalStorage() {
176
177 // Defines values
178 $fileValues = array(
179 'newfolder' => array(
180 array(
181 'data' => $this->newFolderNameInput,
182 'target' => $this->getRootFolderIdentifier(),
183 )
184 )
185 );
186
187 $this->fileProcessor->start($fileValues);
188 $results = $this->fileProcessor->processData();
189
190 $folderObject = NULL;
191 if (!empty ($results['newfolder'][0])) {
192 $folderObject = $results['newfolder'][0];
193 }
194
195 $this->objectsToTearDown[] = $folderObject;
196 $this->assertEquals(TRUE, $folderObject instanceof t3lib_file_Folder);
197 }
198
199 /*********************************
200 * DELETE
201 ********************************/
202 /**
203 * @test
204 */
205 public function deleteFileInLocalStorage() {
206
207 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
208 $storage = $this->getDefaultStorage();
209 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
210
211 // Defines values
212 $fileValues = array(
213 'newfile' => array(
214 array(
215 'data' => $this->newFileNameInput,
216 'target' => $this->getRootFolderIdentifier(),
217 )
218 ),
219 'delete' => array(
220 array(
221 'data' => $fileIdentifier,
222 )
223 )
224 );
225
226 $this->fileProcessor->start($fileValues);
227 $results = $this->fileProcessor->processData();
228
229 $this->assertEquals(TRUE, empty ($results['delete'][1]));
230 }
231
232 /**
233 * @test
234 */
235 public function deleteFolderInLocalStorage() {
236
237 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
238 $storage = $this->getDefaultStorage();
239 $folderIdentifier = $storage->getUid() . ':/' . $this->newFolderNameInput;
240
241 // Defines values
242 $fileValues = array(
243 'newfolder' => array(
244 array(
245 'data' => $this->newFolderNameInput,
246 'target' => $this->getRootFolderIdentifier(),
247 )
248 ),
249 'delete' => array(
250 array(
251 'data' => $folderIdentifier,
252 )
253 )
254 );
255
256 $this->fileProcessor->start($fileValues);
257 $results = $this->fileProcessor->processData();
258
259 $this->assertEquals(TRUE, $results['delete'][0]);
260 }
261
262 /*********************************
263 * RENAME
264 ********************************/
265 /**
266 * @test
267 */
268 public function renameFileInLocalStorage() {
269
270 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
271 $storage = $this->getDefaultStorage();
272 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
273
274 // Defines values
275 $fileValues = array(
276 'newfile' => array(
277 array(
278 'data' => $this->newFileNameInput,
279 'target' => $this->getRootFolderIdentifier(),
280 )
281 ),
282 'rename' => array(
283 array(
284 'data' => $fileIdentifier,
285 'target' => $this->renameFileNameInput,
286 )
287 )
288 );
289
290 $this->fileProcessor->start($fileValues);
291 $results = $this->fileProcessor->processData();
292
293 $fileObject = NULL;
294 if (!empty ($results['rename'][0])) {
295 $fileObject = $results['rename'][0];
296 }
297
298 $this->objectsToTearDown[] = $fileObject;
299 $this->assertEquals(TRUE, $fileObject instanceof t3lib_file_File);
300 }
301
302 /**
303 * @test
304 */
305 public function renameFolderInLocalStorage() {
306
307 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
308 $storage = $this->getDefaultStorage();
309 $folderIdentifier = $storage->getUid() . ':/' . $this->newFolderNameInput;
310
311 // Defines values
312 $fileValues = array(
313 'newfolder' => array(
314 array(
315 'data' => $this->newFolderNameInput,
316 'target' => $this->getRootFolderIdentifier(),
317 )
318 ),
319 'rename' => array(
320 array(
321 'data' => $folderIdentifier,
322 'target' => $this->renameFolderNameInput,
323 )
324 )
325 );
326
327 $this->fileProcessor->start($fileValues);
328 $results = $this->fileProcessor->processData();
329
330 $folderObject = NULL;
331 if (!empty ($results['rename'][0])) {
332 $folderObject = $results['rename'][0];
333 }
334
335 $this->objectsToTearDown[] = $folderObject;
336 $this->assertEquals(TRUE, $folderObject instanceof t3lib_file_Folder);
337 }
338
339 /*********************************
340 * MOVE
341 ********************************/
342 /**
343 * @test
344 */
345 public function moveFileInLocalStorage() {
346
347 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
348 $storage = $this->getDefaultStorage();
349 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
350 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
351
352 // Defines values
353 $fileValues = array(
354 'newfile' => array(
355 array(
356 'data' => $this->newFileNameInput,
357 'target' => $this->getRootFolderIdentifier(),
358 )
359 ),
360 'newfolder' => array(
361 array(
362 'data' => $this->newFolderNameInput,
363 'target' => $this->getRootFolderIdentifier(),
364 )
365 ),
366 'move' => array(
367 array(
368 'data' => $fileIdentifier,
369 'target' => $targetFolder,
370 )
371 )
372 );
373
374 $this->fileProcessor->start($fileValues);
375 $results = $this->fileProcessor->processData();
376
377 $fileObject = NULL;
378 if (!empty ($results['move'][0])) {
379 $fileObject = $results['move'][0];
380 }
381
382 // remove parent folder
383 if (!empty ($results['newfolder'][0])) {
384 $this->objectsToTearDown[] = $results['newfolder'][0];
385 }
386
387 $this->assertEquals(TRUE, $fileObject instanceof t3lib_file_File);
388 }
389
390 /**
391 * @test
392 */
393 public function moveFolderInLocalStorage() {
394
395 // Computes a $folderIdentifier which looks like 8:/folderName.txt where 8 is the storage Uid
396 $storage = $this->getDefaultStorage();
397 $folderIdentifier = $storage->getUid() . ':/' . $this->moveFolderNameInput;
398 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
399
400 // Defines values
401 $fileValues = array(
402 'newfolder' => array(
403 array(
404 'data' => $this->newFolderNameInput,
405 'target' => $this->getRootFolderIdentifier(),
406 ),
407 array(
408 'data' => $this->moveFolderNameInput,
409 'target' => $this->getRootFolderIdentifier(),
410 )
411 ),
412 'move' => array(
413 array(
414 'data' => $folderIdentifier,
415 'target' => $targetFolder,
416 )
417 )
418 );
419
420 $this->fileProcessor->start($fileValues);
421 $results = $this->fileProcessor->processData();
422
423 $folderObject = NULL;
424 if (!empty ($results['move'][0])) {
425 $folderObject = $results['move'][0];
426 }
427
428 // remove parent folder
429 if (!empty ($results['newfolder'][0])) {
430 $this->objectsToTearDown[] = $results['newfolder'][0];
431 }
432
433 $this->assertEquals(TRUE, $folderObject instanceof t3lib_file_Folder);
434 }
435
436 /*********************************
437 * COPY
438 ********************************/
439 /**
440 * @test
441 */
442 public function copyFileInLocalStorage() {
443
444 // Computes a $fileIdentifier which looks like 8:/fileName.txt where 8 is the storage Uid
445 $storage = $this->getDefaultStorage();
446 $fileIdentifier = $storage->getUid() . ':/' . $this->newFileNameInput;
447 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
448
449 // Defines values
450 $fileValues = array(
451 'newfile' => array(
452 array(
453 'data' => $this->newFileNameInput,
454 'target' => $this->getRootFolderIdentifier(),
455 )
456 ),
457 'newfolder' => array(
458 array(
459 'data' => $this->newFolderNameInput,
460 'target' => $this->getRootFolderIdentifier(),
461 )
462 ),
463 'copy' => array(
464 array(
465 'data' => $fileIdentifier,
466 'target' => $targetFolder,
467 )
468 )
469 );
470
471 $this->fileProcessor->start($fileValues);
472 $results = $this->fileProcessor->processData();
473
474 $fileObject = NULL;
475 if (!empty ($results['copy'][0])) {
476 $fileObject = $results['copy'][0];
477 }
478
479 // remove parent folder
480 if (!empty ($results['newfolder'][0])) {
481 $this->objectsToTearDown[] = $results['newfolder'][0];
482 }
483 if (!empty ($results['newfile'][0])) {
484 $this->objectsToTearDown[] = $results['newfile'][0];
485 }
486
487 $this->assertEquals(TRUE, $fileObject instanceof t3lib_file_File);
488 }
489
490 /**
491 * @test
492 */
493 public function copyFolderInLocalStorage() {
494
495 // Computes a $folderIdentifier which looks like 8:/folderName.txt where 8 is the storage Uid
496 $storage = $this->getDefaultStorage();
497 $folderIdentifier = $storage->getUid() . ':/' . $this->copyFolderNameInput;
498 $targetFolder = $this->getRootFolderIdentifier() . $this->newFolderNameInput;
499
500 // Defines values
501 $fileValues = array(
502 'newfolder' => array(
503 array(
504 'data' => $this->newFolderNameInput,
505 'target' => $this->getRootFolderIdentifier(),
506 ),
507 array(
508 'data' => $this->copyFolderNameInput,
509 'target' => $this->getRootFolderIdentifier(),
510 )
511 ),
512 'copy' => array(
513 array(
514 'data' => $folderIdentifier,
515 'target' => $targetFolder,
516 )
517 )
518 );
519
520 $this->fileProcessor->start($fileValues);
521 $results = $this->fileProcessor->processData();
522
523 $folderObject = NULL;
524 if (!empty ($results['copy'][0])) {
525 $folderObject = $results['copy'][0];
526 }
527
528 // remove parent folder
529 if (!empty ($results['newfolder'][0])) {
530 $this->objectsToTearDown[] = $results['newfolder'][0];
531 }
532 if (!empty ($results['newfolder'][1])) {
533 $this->objectsToTearDown[] = $results['newfolder'][1];
534 }
535
536 $this->assertEquals(TRUE, $folderObject instanceof t3lib_file_Folder);
537 }
538 }
539
540 ?>