[TASK] Clean up and fix unit tests for FAL
[Packages/TYPO3.CMS.git] / tests / t3lib / file / Driver / LocalDriverTest.php
1 <?php
2
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
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 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29
30 require_once 'vfsStream/vfsStream.php';
31 require_once dirname(dirname(__FILE__)) . '/BaseTestCase.php';
32 require_once dirname(__FILE__) . '/Fixtures/LocalDriverFilenameFilter.php';
33
34 /**
35 * Testcase for the local storage driver class of the TYPO3 VFS
36 *
37 * @package TYPO3
38 * @subpackage t3lib
39 *
40 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
41 */
42 class t3lib_file_Driver_LocalDriverTest extends t3lib_file_BaseTestCase {
43
44 private static $testDirs = array();
45
46 public function setUp() {
47 parent::setUp();
48
49 // use a mocked file repository to avoid updating the index when doing property update tests
50 $mockedRepository = $this->getMock('t3lib_file_Repository_FileRepository');
51 t3lib_div::setSingletonInstance('t3lib_file_Repository_FileRepository', $mockedRepository);
52 }
53
54 public function tearDown() {
55 t3lib_div::purgeInstances();
56 parent::tearDown();
57 }
58
59 public static function tearDownAfterClass() {
60 foreach (self::$testDirs as $dir) {
61 t3lib_div::rmdir($dir, TRUE);
62 }
63 }
64
65 /**
66 * Creates a "real" directory for doing tests. This is neccessary because some file system properties (e.g. permissions)
67 * cannot be reflected by vfsStream, and some methods (like touch()) don't work there either.
68 *
69 * Created directories are automatically destroyed by the tearDownAfterClass() method.
70 *
71 * @return string
72 */
73 protected function createRealTestdir() {
74 $basedir = PATH_site . 'typo3temp/fal-test-' . t3lib_div::shortMD5(microtime(TRUE));
75 mkdir($basedir);
76 self::$testDirs[] = $basedir;
77 return $basedir;
78 }
79
80 /**
81 * Creates a driver fixture object, optionally using a given mount object.
82 *
83 * IMPORTANT: Call this only after setting up the virtual file system (with the addTo* methods)!
84 *
85 * @param $driverConfiguration
86 * @param t3lib_file_Storage $storageObject
87 * @param array $mockedDriverMethods
88 * @return t3lib_file_Driver_LocalDriver
89 */
90 protected function createDriverFixture($driverConfiguration, t3lib_file_Storage $storageObject = NULL, $mockedDriverMethods = array()) {
91 $this->initializeVfs();
92
93 if ($storageObject == NULL) {
94 $storageObject = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
95 }
96
97 if (count($mockedDriverMethods) == 0) {
98 $driver = new t3lib_file_Driver_LocalDriver($driverConfiguration);
99 } else {
100 $driver = $this->getMock('t3lib_file_Driver_LocalDriver', $mockedDriverMethods, array($driverConfiguration));
101 }
102 $storageObject->setDriver($driver);
103 $driver->setStorage($storageObject);
104 $driver->processConfiguration();
105 $driver->initialize();
106 return $driver;
107 }
108
109
110 /**
111 * @test
112 */
113 public function rootLevelFolderIsCreatedWithCorrectArguments() {
114 $mockedMount = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
115 $fixture = $this->createDriverFixture(array(), $mockedMount);
116
117 $mockedFactory = $this->getMock('t3lib_file_Factory');
118 $mockedFactory->expects($this->once())->method('createFolderObject')->with(
119 $this->equalTo($mockedMount), $this->equalTo('/'), $this->equalTo(''));
120 t3lib_div::setSingletonInstance('t3lib_file_Factory', $mockedFactory);
121
122 $fixture->getRootLevelFolder();
123
124 t3lib_div::setSingletonInstance('t3lib_file_Factory', new t3lib_file_Factory());
125 }
126
127 /**
128 * @test
129 */
130 public function getDefaultFolderReturnsFolderForTemporaryPath() {
131 $fixture = $this->createDriverFixture(array(
132 'basePath' => $this->getMountRootUrl()
133 ));
134
135 $folder = $fixture->getDefaultFolder();
136 $this->assertStringEndsWith('_temp_/', $folder->getIdentifier());
137 }
138
139 /**
140 * @test
141 */
142 public function defaultLevelFolderFolderIsCreatedIfItDoesntExist() {
143 $fixture = $this->createDriverFixture(array(
144 'basePath' => $this->getMountRootUrl()
145 ));
146
147 $storageFolder = $fixture->getDefaultFolder();
148 $this->assertFileExists($this->getUrlInMount('/_temp_/'));
149 }
150
151 /**
152 * @test
153 */
154 public function getFolderInFolderReturnsCorrectFolderObject() {
155 $this->addToMount(array(
156 'someDir' => array(
157 'someSubdir' => array()
158 )
159 ));
160 $fixture = $this->createDriverFixture(array(
161 'basePath' => $this->getMountRootUrl()
162 ));
163
164 $parentFolder = $fixture->getFolder('/someDir');
165 $folder = $fixture->getFolderInFolder('someSubdir', $parentFolder);
166
167 $this->assertEquals('/someDir/someSubdir/', $folder->getIdentifier());
168 }
169
170 /**
171 * @test
172 */
173 public function createFolderCreatesFolderOnDisk() {
174 $this->addToMount(array('some' => array('folder' => array())));
175 $fixture = $this->createDriverFixture(array(
176 'basePath' => $this->getMountRootUrl()
177 ));
178
179 $mockedFolder = $this->getSimpleFolderMock('/some/folder/');
180
181 $fixture->createFolder('path', $mockedFolder);
182
183 $this->assertFileExists($this->getUrlInMount('/some/folder/'));
184 $this->assertFileExists($this->getUrlInMount('/some/folder/path'));
185 }
186
187 /**
188 * @test
189 */
190 public function createFolderReturnsFolderObject() {
191 $this->addToMount(array('some' => array('folder' => array())));
192 $fixture = $this->createDriverFixture(array(
193 'basePath' => $this->getMountRootUrl()
194 ));
195
196 $mockedFolder = $this->getSimpleFolderMock('/some/folder/');
197
198 $createdFolder = $fixture->createFolder('path', $mockedFolder);
199
200 $this->assertEquals('/some/folder/path/', $createdFolder->getIdentifier());
201 }
202
203 public function createFolderSanitizesFolderNameBeforeCreation_dataProvider() {
204 return array(
205 'folder name with NULL character' => array(
206 "some\x00Folder",
207 'some_Folder'
208 ),
209 'folder name with directory part' => array(
210 '../someFolder',
211 '.._someFolder'
212 ),
213 );
214 }
215
216 /**
217 * @test
218 * @dataProvider createFolderSanitizesFolderNameBeforeCreation_dataProvider
219 */
220 public function createFolderSanitizesFolderNameBeforeCreation($newFolderName, $expectedFolderName) {
221 $this->addToMount(array('some' => array('folder' => array())));
222 $fixture = $this->createDriverFixture(array(
223 'basePath' => $this->getMountRootUrl()
224 ));
225
226 $mockedFolder = $this->getSimpleFolderMock('/some/folder/');
227
228 $fixture->createFolder($newFolderName, $mockedFolder);
229
230 $this->assertFileExists($this->getUrlInMount('/some/folder/' . $expectedFolderName));
231 }
232
233 /**
234 * @test
235 */
236 public function driverConfigVerificationFailsIfConfiguredBasePathDoesNotExist() {
237 $this->setExpectedException('t3lib_file_exception_InvalidConfigurationException', '', 1299233097);
238
239 $driverConfiguration = array(
240 'basePath' => vfsStream::url($this->basedir . 'doesntexist/')
241 );
242
243 $this->assertFalse(file_exists($driverConfiguration['basePath']));
244 t3lib_file_Driver_LocalDriver::verifyConfiguration($driverConfiguration);
245 }
246
247 /**
248 * @test
249 */
250 public function basePathIsNormalizedWithTrailingSlash() {
251 $driverConfiguration = array(
252 'basePath' => $this->getMountRootUrl()
253 );
254
255 $fixture = $this->createDriverFixture($driverConfiguration);
256 $this->assertEquals('/', substr($fixture->getAbsoluteBasePath(), -1));
257 }
258
259 /**
260 * @test
261 */
262 public function noSecondSlashIsAddedIfBasePathAlreadyHasTrailingSlash() {
263 $driverConfiguration = array(
264 'basePath' => $this->getMountRootUrl()
265 );
266
267 $fixture = $this->createDriverFixture($driverConfiguration);
268 $this->assertNotEquals('/', substr($fixture->getAbsoluteBasePath(), -2, 1));
269 }
270
271 /**
272 * @test
273 */
274 public function getAbsolutePathReturnsCorrectPath() {
275 $this->addToMount(array(
276 'someFolder' => array(
277 'file1.ext' => 'asdfg'
278 )
279 ));
280 $mockedFile = $this->getSimpleFileMock('someFolder/file1.ext');
281 $fixture = $this->createDriverFixture(array(
282 'basePath' => $this->getMountRootUrl()
283 ));
284
285 $path = $fixture->getAbsolutePath($mockedFile);
286 $this->assertTrue(file_exists($path));
287 $this->assertEquals($this->getUrlInMount('/someFolder/file1.ext'), $path);
288 }
289
290 /**
291 * @test
292 */
293 public function statReturnsCorrectFileInfo() {
294 $contents = 'asdfg';
295 $this->addToMount(array('file1.ext' => $contents));
296 $mockedFile = $this->getSimpleFileMock('file1.ext');
297 $fixture = $this->createDriverFixture(array(
298 'basePath' => $this->getMountRootUrl()
299 ));
300
301 $path = $fixture->getAbsolutePath($mockedFile);
302 $stat = $fixture->getLowLevelFileInfo($mockedFile);
303 $this->assertEquals(strlen($contents), $stat['size']);
304 $this->assertEquals(filectime($path), $stat['ctime']);
305 $this->assertEquals(fileatime($path), $stat['mtime']);
306 $this->assertEquals(filemtime($path), $stat['atime']);
307 }
308
309 /**
310 * @test
311 */
312 public function addFileMovesFileToCorrectLocation() {
313 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
314
315 $this->addToMount(array('targetFolder' => array()));
316 $this->addToVfs(array(
317 'sourceFolder' => array(
318 'file' => 'asdf'
319 )
320 ));
321 $fixture = $this->createDriverFixture(array(
322 'basePath' => $this->getMountRootUrl()
323 ));
324
325 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
326 $fixture->addFile($this->getUrl('sourceFolder/file'), $mockedFolder, 'file');
327 $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/file')));
328 }
329
330 /**
331 * @test
332 */
333 public function addFileUsesFilenameIfGiven() {
334 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
335
336 $this->addToMount(array('targetFolder' => array()));
337 $this->addToVfs(array(
338 'sourceFolder' => array(
339 'file' => 'asdf'
340 )
341 ));
342 $fixture = $this->createDriverFixture(array(
343 'basePath' => $this->getMountRootUrl()
344 ));
345
346 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
347 $fixture->addFile($this->getUrl('sourceFolder/file'), $mockedFolder, 'targetFile');
348 $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/targetFile')));
349 }
350
351 /**
352 * @test
353 */
354 public function addFileFailsIfFileIsInDriverStorage() {
355 $mockedFolder = $this->getMock('t3lib_file_Folder', array(), array(), '', FALSE);
356 $mockedFolder->expects($this->any())->method('getIdentifier')->will($this->returnValue('/targetFolder/'));
357
358 $this->setExpectedException('InvalidArgumentException', '', 1314778269);
359
360 $this->addToMount(array(
361 'targetFolder' => array(
362 'file' => 'asdf'
363 )
364 ));
365 $storageObject = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
366 $storageObject->expects($this->any())->method('getUid')->will($this->returnValue('1'));
367
368 $fixture = $this->createDriverFixture(array(
369 'basePath' => $this->getMountRootUrl()
370 ), $storageObject);
371
372 $fixture->addFile($this->getUrlInMount('/targetFolder/file'), $mockedFolder, 'file');
373 }
374
375 /**
376 * @test
377 */
378 public function addFileReturnsFileObject() {
379 $mockedFolder = $this->getMock('t3lib_file_Folder', array(), array(), '', FALSE);
380 $mockedFolder->expects($this->any())->method('getIdentifier')->will($this->returnValue('/targetFolder/'));
381
382 $this->addToMount(array('targetFolder' => array()));
383 $this->addToVfs(array(
384 'sourceFolder' => array(
385 'file' => 'asdf'
386 )
387 ));
388 $fixture = $this->createDriverFixture(array(
389 'basePath' => $this->getMountRootUrl()
390 ));
391
392 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
393 $fileObject = $fixture->addFile($this->getUrl('sourceFolder/file'), $mockedFolder, 'file');
394
395 $this->assertInstanceOf('t3lib_file_File', $fileObject);
396 $this->assertEquals('file', $fileObject->getName());
397 $this->assertEquals('/targetFolder/file', $fileObject->getIdentifier());
398 }
399
400 /**
401 * @test
402 */
403 public function addFileRawCreatesCopyOfFile() {
404 $this->addToMount(array('targetFolder' => array()));
405 $this->addToVfs(array(
406 'sourceFolder' => array(
407 'file' => 'asdf'
408 )
409 ));
410 $fixture = $this->createDriverFixture(array(
411 'basePath' => $this->getMountRootUrl()
412 ));
413
414 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
415 $fileIdentifier = $fixture->addFileRaw($this->getUrl('sourceFolder/file'), $this->getSimpleFolderMock('/targetFolder/'), 'somefile');
416 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
417 $this->assertTrue(file_exists($this->getUrlInMount('targetFolder/somefile')));
418 $this->assertEquals('/targetFolder/somefile', $fileIdentifier);
419 }
420
421 /**
422 * @test
423 */
424 public function deleteFileRawRemovesFile() {
425 $this->addToMount(array('targetFolder' => array(
426 'file' => 'asdjlkfa'
427 )));
428 $fixture = $this->createDriverFixture(array(
429 'basePath' => $this->getMountRootUrl()
430 ));
431
432 $this->assertTrue(file_exists($this->getUrlInMount('targetFolder/file')));
433 $fixture->deleteFileRaw('/targetFolder/file');
434 $this->assertFalse(file_exists($this->getUrlInMount('targetFolder/file')));
435 }
436
437 /**
438 * @test
439 */
440 public function replacingFileUpdatesMetadataInFileObject() {
441 $this->addToMount(array('targetFolder' => array(
442 'file' => 'asdjlkfa'
443 )));
444 $this->addToVfs(array('sourceFolder' => array(
445 'file' => 'asjdalks'
446 )));
447 $fixture = $this->createDriverFixture(array(
448 'basePath' => $this->getMountRootUrl()
449 ));
450
451 $mockedFile = $this->getSimpleFileMock('/targetFolder/file', array('updateProperties'));
452 $mockedFile->expects($this->once())->method('updateProperties');
453
454 $fixture->replaceFile($mockedFile, $this->getUrl('sourceFolder/file'));
455 }
456
457 /**
458 * @test
459 */
460 public function existenceChecksWorkForFilesAndFolders() {
461 $this->addToMount(array(
462 'file' => 'asdf',
463 'folder' => array()
464 ));
465 $fixture = $this->createDriverFixture(array(
466 'basePath' => $this->getMountRootUrl()
467 ));
468
469 // Using slashes at the beginning of paths because they will be stored in the DB this way.
470 $this->assertTrue($fixture->fileExists('/file'));
471 $this->assertTrue($fixture->folderExists('/folder/'));
472 $this->assertFalse($fixture->fileExists('/nonexistingFile'));
473 $this->assertFalse($fixture->folderExists('/nonexistingFolder/'));
474 }
475
476 /**
477 * @test
478 */
479 public function existenceChecksInFolderWorkForFilesAndFolders() {
480 $mockedFolder = $this->getSimpleFolderMock('/subfolder/');
481
482 $this->addToMount(array('subfolder' => array(
483 'file' => 'asdf',
484 'folder' => array()
485 )));
486 $fixture = $this->createDriverFixture(array(
487 'basePath' => $this->getMountRootUrl()
488 ));
489
490 $this->assertTrue($fixture->fileExistsInFolder('file', $mockedFolder));
491 $this->assertTrue($fixture->folderExistsInFolder('folder', $mockedFolder));
492 $this->assertFalse($fixture->fileExistsInFolder('nonexistingFile', $mockedFolder));
493 $this->assertFalse($fixture->folderExistsInFolder('nonexistingFolder', $mockedFolder));
494 }
495
496 /**
497 * @test
498 */
499 public function getPublicUrlReturnsCorrectUriForConfiguredBaseUri() {
500 $baseUri = 'http://example.org/foobar/' . uniqid();
501 $this->addToMount(array(
502 'file.ext' => 'asdf',
503 'subfolder' => array(
504 'file2.ext' => 'asdf'
505 )
506 ));
507 $fixture = $this->createDriverFixture(array(
508 'basePath' => $this->getMountRootUrl(),
509 'baseUri' => $baseUri
510 ));
511
512 $mockedFile1 = $this->getMock('t3lib_file_File', array(), array(), '', FALSE);
513 $mockedFile1->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
514 $mockedFile2 = $this->getMock('t3lib_file_File', array(), array(), '', FALSE);
515 $mockedFile2->expects($this->any())->method('getIdentifier')->will($this->returnValue('/subfolder/file2.ext'));
516
517 $this->assertEquals($baseUri . '/file.ext', $fixture->getPublicUrl($mockedFile1));
518 $this->assertEquals($baseUri . '/subfolder/file2.ext', $fixture->getPublicUrl($mockedFile2));
519 }
520
521 /**
522 * @test
523 */
524 public function fileContentsCanBeWrittenAndRead() {
525 $fileContents = 'asdf';
526 $this->addToMount(array(
527 'file.ext' => $fileContents
528 ));
529 $fixture = $this->createDriverFixture(array(
530 'basePath' => $this->getMountRootUrl()
531 ));
532
533 $mockedFile = $this->getMock('t3lib_file_File', array(), array(), '', FALSE);
534 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
535
536 $this->assertEquals($fileContents, $fixture->getFileContents($mockedFile), 'File contents could not be read');
537 $newFileContents = 'asdfgh';
538 $fixture->setFileContents($mockedFile, $newFileContents);
539 $this->assertEquals($newFileContents, $fixture->getFileContents($mockedFile), 'New file contents could not be read.');
540 }
541
542 /**
543 * @test
544 */
545 public function setFileContentsReturnsNumberOfBytesWrittenToFile() {
546 $fileContents = 'asdf';
547 $this->addToMount(array(
548 'file.ext' => $fileContents
549 ));
550 $fixture = $this->createDriverFixture(array(
551 'basePath' => $this->getMountRootUrl()
552 ));
553
554 $mockedFile = $this->getSimpleFileMock('/file.ext');
555
556 $newFileContents = 'asdfgh';
557 $bytesWritten = $fixture->setFileContents($mockedFile, $newFileContents);
558
559 $this->assertEquals(strlen($newFileContents), $bytesWritten);
560 }
561
562 /**
563 * @test
564 * @depends existenceChecksWorkForFilesAndFolders
565 * @return array The driver fixture, the mocked file
566 */
567 public function newFilesCanBeCreated() {
568 $this->addToMount(array(
569 'someDir' => array()
570 ));
571 /** @var $fixture t3lib_file_Driver_LocalDriver */
572 list($basedir, $fixture) = $this->prepareRealTestEnvironment();
573
574 mkdir($basedir . '/someDir');
575 $fixture->createFile('testfile.txt', $fixture->getFolder('someDir'));
576
577 $mockedFile = $this->getSimpleFileMock('/someDir/testfile.txt');
578
579 $this->assertTrue($fixture->fileExists('/someDir/testfile.txt'));
580
581 return array($fixture, $mockedFile);
582 }
583
584 /**
585 * @test
586 * @depends newFilesCanBeCreated
587 */
588 public function createdFilesAreEmpty(array $arguments) {
589 /** @var $fixture t3lib_file_Driver_LocalDriver */
590 list($fixture, $mockedFile) = $arguments;
591 $fileData = $fixture->getFileContents($mockedFile);
592 $this->assertEquals(0, strlen($fileData));
593 }
594
595 /**********************************
596 * File and directory listing
597 **********************************/
598
599 /**
600 * @test
601 */
602 public function getFileReturnsCorrectIdentifier() {
603 $this->addToMount(array(
604 'someDir' => array(
605 'someFile' => 'asdfg'
606 ),
607 'someFileAtRootLevel' => 'foobar'
608 ));
609 $fixture = $this->createDriverFixture(array(
610 'basePath' => $this->getMountRootUrl()
611 ));
612
613 $subdirFileInfo = $fixture->getFileInfoByIdentifier('/someDir/someFile');
614 $this->assertEquals('/someDir/someFile', $subdirFileInfo['identifier']);
615 $rootFileInfo = $fixture->getFileInfoByIdentifier('/someFileAtRootLevel');
616 $this->assertEquals('/someFileAtRootLevel', $rootFileInfo['identifier']);
617 }
618
619 /**
620 * @test
621 */
622 public function getFileThrowsExceptionIfFileDoesNotExist() {
623 $this->setExpectedException('InvalidArgumentException', '', 1314516809);
624 $fixture = $this->createDriverFixture(array(
625 'basePath' => $this->getMountRootUrl()
626 ));
627
628 $fixture->getFileInfoByIdentifier('/some/file/at/a/random/path');
629 }
630
631 /**
632 * @test
633 */
634 public function getFileListReturnsEmptyArrayForEmptyDirectory() {
635 $fixture = $this->createDriverFixture(array(
636 'basePath' => $this->getMountRootUrl()
637 ));
638
639 $fileList = $fixture->getFileList('/');
640
641 $this->assertEmpty($fileList);
642 }
643
644 /**
645 * @test
646 */
647 public function getFileListReturnsAllFilesInDirectory() {
648 $dirStructure = array(
649 'aDir' => array(),
650 'file1' => 'asdfg',
651 'file2' => 'fdsa'
652 );
653 $this->addToMount($dirStructure);
654 $fixture = $this->createDriverFixture(array(
655 'basePath' => $this->getMountRootUrl()
656 ));
657
658 $fileList = $fixture->getFileList('/');
659
660 $this->assertEquals(array('file1', 'file2'), array_keys($fileList));
661 }
662
663 /**
664 * @test
665 */
666 public function getFileListFailsIfDirectoryDoesNotExist() {
667 $this->setExpectedException('InvalidArgumentException', '', 1314349666);
668
669 $this->addToMount(array('somefile' => ''));
670 $fixture = $this->createDriverFixture(array(
671 'basePath' => $this->getMountRootUrl()
672 ));
673
674 $fixture->getFileList('somedir/');
675 }
676
677 /**
678 * @test
679 */
680 public function getFileListCallsConfiguredCallbackFunctionWithGivenItemName() {
681 $dirStructure = array(
682 'file2' => 'fdsa'
683 );
684 // register static callback to self
685 $callback = array(
686 array (
687 get_class($this),
688 'callbackStaticTestFunction'
689 )
690 );
691 $this->addToMount($dirStructure);
692 $fixture = $this->createDriverFixture(array(
693 'basePath' => $this->getMountRootUrl()
694 ));
695 // the callback function will throw an exception used to check if it was called with correct $itemName
696 $this->setExpectedException('InvalidArgumentException', '$itemName', 1336159604);
697 $fixture->getFileList('/', 0, 0, $callback);
698 }
699
700 /**
701 * Static callback function used to test if the filter callbacks work
702 * As it is static we are using an exception to test if it is really called and works
703 *
704 * @static
705 * @throws InvalidArgumentException
706 * @see getFileListCallsConfiguredCallbackFunction
707 */
708 public static function callbackStaticTestFunction() {
709 list($itemName) = func_get_args();
710 if($itemName === 'file2') {
711 throw new InvalidArgumentException('$itemName', 1336159604);
712 }
713 }
714
715 /**
716 * @test
717 */
718 public function getFileListFiltersItemsWithGivenFilterMethods() {
719 $dirStructure = array(
720 'fileA' => 'asdfg',
721 'fileB' => 'fdsa'
722 );
723 $this->addToMount($dirStructure);
724 $fixture = $this->createDriverFixture(array(
725 'basePath' => $this->getMountRootUrl()
726 ));
727 $filterCallbacks = array(
728 array('t3lib_file_Tests_Driver_Fixtures_LocalDriverFilenameFilter', 'filterFilename')
729 );
730
731 $fileList = $fixture->getFileList('/', 0, 0, $filterCallbacks);
732
733 $this->assertNotContains('fileA', array_keys($fileList));
734 }
735
736 /**
737 * @test
738 */
739 public function getFolderListReturnsAllDirectoriesInDirectory() {
740 $dirStructure = array(
741 'dir1' => array(),
742 'dir2' => array(),
743 'file' => 'asdfg'
744 );
745 $this->addToMount($dirStructure);
746
747 $fixture = $this->createDriverFixture(array(
748 'basePath' => $this->getMountRootUrl()
749 ));
750
751 $fileList = $fixture->getFolderList('/');
752
753 $this->assertEquals(array('dir1', 'dir2'), array_keys($fileList));
754 }
755
756 /**
757 * @test
758 */
759 public function getFolderListDoesNotReturnHiddenFoldersByDefault() {
760 $dirStructure = array(
761 '.someHiddenFile' => array(),
762 'aDir' => array(),
763 'file1' => '',
764 );
765 $this->addToMount($dirStructure);
766 $fixture = $this->createDriverFixture(array(
767 'basePath' => $this->getMountRootUrl()
768 ));
769
770 $fileList = $fixture->getFolderList('/');
771
772 $this->assertEquals(array('aDir'), array_keys($fileList));
773 }
774
775 /**
776 * @test
777 */
778 public function getFolderListUsesCorrectPathForItems() {
779 $this->addToMount(array(
780 'dir1' => array(
781 'subdir1' => array()
782 )
783 ));
784
785 $fixture = $this->createDriverFixture(array(
786 'basePath' => $this->getMountRootUrl()
787 ));
788
789 $FolderList = $fixture->getFolderList('/');
790 $this->assertEquals('/dir1/', $FolderList['dir1']['identifier']);
791
792 $FolderList = $fixture->getFolderList('/dir1/');
793 $this->assertEquals('/dir1/subdir1/', $FolderList['subdir1']['identifier']);
794 }
795
796 /**
797 * Checks if the folder names . and .. are ignored when listing subdirectories
798 *
799 * @test
800 */
801 public function getFolderListLeavesOutNavigationalEntries() {
802 // we have to add .. and . manually, as these are not included in vfsStream directory listings (as opposed
803 // to normal file listings)
804 $this->addToMount(array(
805 '..' => array(),
806 '.' => array()
807 ));
808 $fixture = $this->createDriverFixture(array(
809 'basePath' => $this->getMountRootUrl()
810 ));
811
812 $fileList = $fixture->getFolderList('/');
813
814 $this->assertEmpty($fileList);
815 }
816
817 /**
818 * @test
819 */
820 public function getFolderListFiltersItemsWithGivenFilterMethods() {
821 $dirStructure = array(
822 'folderA' => array(),
823 'folderB' => array()
824 );
825 $this->addToMount($dirStructure);
826 $fixture = $this->createDriverFixture(array(
827 'basePath' => $this->getMountRootUrl()
828 ));
829 $filterCallbacks = array(
830 array('t3lib_file_Tests_Driver_Fixtures_LocalDriverFilenameFilter', 'filterFilename')
831 );
832
833 $folderList = $fixture->getFolderList('/', 0, 0, $filterCallbacks);
834
835 $this->assertNotContains('folderA', array_keys($folderList));
836 }
837
838 /**
839 * @test
840 */
841 public function getFolderListFailsIfDirectoryDoesNotExist() {
842 $this->setExpectedException('InvalidArgumentException', '', 1314349666);
843
844 $fixture = $this->createDriverFixture(array(
845 'basePath' => $this->getMountRootUrl()
846 ));
847 vfsStream::create(array($this->basedir => array('somefile' => '')));
848
849 $fixture->getFolderList('somedir/');
850 }
851
852 /**
853 * @test
854 */
855 public function hashReturnsCorrectHashes() {
856 $contents = "68b329da9893e34099c7d8ad5cb9c940";
857 $expectedMd5Hash = "8c67dbaf0ba22f2e7fbc26413b86051b";
858 $expectedSha1Hash = "a60cd808ba7a0bcfa37fa7f3fb5998e1b8dbcd9d";
859
860 $mockedFile = $this->getSimpleFileMock('/hashFile');
861 $this->addToMount(array('hashFile' => $contents));
862 $fixture = $this->createDriverFixture(array(
863 'basePath' => $this->getMountRootUrl()
864 ));
865
866 $this->assertEquals($expectedSha1Hash, $fixture->hash($mockedFile, 'sha1'));
867 $this->assertEquals($expectedMd5Hash, $fixture->hash($mockedFile, 'md5'));
868 }
869
870 /**
871 * @test
872 */
873 public function hashingWithUnsupportedAlgorithmFails() {
874 $this->setExpectedException('InvalidArgumentException', '', 1304964032);
875
876 $fixture = $this->createDriverFixture(array(
877 'basePath' => $this->getMountRootUrl()
878 ));
879 $fixture->hash($this->getSimpleFileMock('/hashFile'), uniqid());
880 }
881
882 /**
883 * @test
884 * @covers t3lib_file_Driver_LocalDriver::getFileForLocalProcessing
885 */
886 public function getFileForLocalProcessingCreatesCopyOfFileByDefault() {
887 $fileContents = 'asdfgh';
888 $this->addToMount(array(
889 'someDir' => array(
890 'someFile' => $fileContents
891 )
892 ));
893 $fixture = $this->createDriverFixture(array(
894 'basePath' => $this->getMountRootUrl()
895 ), NULL, array('copyFileToTemporaryPath'));
896 $mockedFile = $this->getSimpleFileMock('/someDir/someFile');
897 // TODO add parameter expectation for $mockedFile as soon as PHPUnit supports object identity matching in parameter expectations
898 $fixture->expects($this->once())->method('copyFileToTemporaryPath');
899
900 $fixture->getFileForLocalProcessing($mockedFile);
901 }
902
903 /**
904 * @test
905 */
906 public function getFileForLocalProcessingReturnsOriginalFilepathForReadonlyAccess() {
907 $fileContents = 'asdfgh';
908 $this->addToMount(array(
909 'someDir' => array(
910 'someFile' => $fileContents
911 )
912 ));
913 $fixture = $this->createDriverFixture(array(
914 'basePath' => $this->getMountRootUrl()
915 ));
916 $mockedFile = $this->getSimpleFileMock('/someDir/someFile');
917
918 $filePath = $fixture->getFileForLocalProcessing($mockedFile, FALSE);
919
920 $this->assertEquals($filePath, $this->getMountRootUrl() . 'someDir/someFile');
921 }
922
923 /**
924 * @test
925 */
926 public function filesCanBeCopiedToATemporaryPath() {
927 $fileContents = 'asdfgh';
928 $this->addToMount(array(
929 'someDir' => array(
930 'someFile' => $fileContents
931 )
932 ));
933 $fixture = $this->createDriverFixture(array(
934 'basePath' => $this->getMountRootUrl()
935 ));
936
937 $mockedFile = $this->getSimpleFileMock('/someDir/someFile');
938
939 $filePath = $fixture->copyFileToTemporaryPath($mockedFile);
940
941 $this->assertContains('/typo3temp/', $filePath);
942 $this->assertEquals($fileContents, file_get_contents($filePath));
943 }
944
945 protected function prepareRealTestEnvironment() {
946 $basedir = $this->createRealTestdir();
947 $fixture = $this->createDriverFixture(array(
948 'basePath' => $basedir
949 ));
950
951 return array($basedir, $fixture);
952 }
953
954 /**
955 * @test
956 */
957 public function permissionsAreCorrectlyRetrieved() {
958 /** @var $fixture t3lib_file_Driver_LocalDriver */
959 list($basedir, $fixture) = $this->prepareRealTestEnvironment();
960 touch($basedir . '/someFile');
961 chmod($basedir . '/someFile', 0700);
962 touch($basedir . '/someForbiddenFile');
963 chmod($basedir . '/someForbiddenFile', 0000);
964 mkdir($basedir . '/someFolder');
965 chmod($basedir . '/someFolder', 0700);
966 mkdir($basedir . '/someForbiddenFolder');
967 chmod($basedir . '/someForbiddenFolder', 0000);
968 clearstatcache();
969
970 $this->assertEquals(array('r' => TRUE, 'w' => TRUE), $fixture->getFilePermissions($this->getSimpleFileMock('/someFile')));
971 $this->assertEquals(array('r' => FALSE, 'w' => FALSE), $fixture->getFilePermissions($this->getSimpleFileMock('/someForbiddenFile')));
972 $this->assertEquals(array('r' => TRUE, 'w' => TRUE), $fixture->getFolderPermissions($this->getSimpleFolderMock('/someFolder')));
973 $this->assertEquals(array('r' => FALSE, 'w' => FALSE), $fixture->getFolderPermissions($this->getSimpleFolderMock('/someForbiddenFolder')));
974 }
975
976 /**
977 * Dataprovider for getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser test
978 *
979 * @return array group, filemode and expected result
980 */
981 public function getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser_dataProvider() {
982 $data = array();
983
984 // On some OS, the posix_* functions do not exits
985 if (function_exists('posix_getgid')) {
986 $data = array(
987 'current group, readable/writable' => array(
988 posix_getgid(),
989 0060,
990 array('r' => TRUE, 'w' => TRUE)
991 ),
992 'current group, readable/not writable' => array(
993 posix_getgid(),
994 0040,
995 array('r' => TRUE, 'w' => FALSE)
996 ),
997 'current group, not readable/not writable' => array(
998 posix_getgid(),
999 0000,
1000 array('r' => FALSE, 'w' => FALSE)
1001 ),
1002 );
1003 }
1004
1005 $data = array_merge_recursive($data, array(
1006 'arbitrary group, readable/writable' => array(
1007 vfsStream::GROUP_USER_1,
1008 0006,
1009 array('r' => TRUE, 'w' => TRUE)
1010 ),
1011 'arbitrary group, readable/not writable' => array(
1012 vfsStream::GROUP_USER_1,
1013 0664,
1014 array('r' => TRUE, 'w' => FALSE)
1015 ),
1016 'arbitrary group, not readable/not writable' => array(
1017 vfsStream::GROUP_USER_1,
1018 0660,
1019 array('r' => FALSE, 'w' => FALSE)
1020 ),
1021 ));
1022
1023 return $data;
1024 }
1025
1026 /**
1027 * @test
1028 * @dataProvider getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser_dataProvider
1029 */
1030 public function getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser($group, $permissions, $expectedResult) {
1031 $this->addToMount(array(
1032 'testfile' => 'asdfg'
1033 ));
1034 $fixture = $this->createDriverFixture(array(
1035 'basePath' => $this->getMountRootUrl()
1036 ));
1037 /** @var $fileObject vfsStreamContent */
1038 $fileObject = vfsStreamWrapper::getRoot()->getChild($this->mountDir)->getChild('testfile');
1039 // just use an "arbitrary" user here - it is only important that
1040 $fileObject->chown(vfsStream::OWNER_USER_1);
1041 $fileObject->chgrp($group);
1042 $fileObject->chmod($permissions);
1043
1044 $this->assertEquals($expectedResult, $fixture->getFilePermissions($this->getSimpleFileMock('/testfile')));
1045 }
1046
1047 /**
1048 * @test
1049 */
1050 public function isWithinRecognizesFilesWithinFolderAndInOtherFolders() {
1051 $mockedStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1052
1053 $mockedFolder = new t3lib_file_Folder($mockedStorage, '/someFolder/', 'someFolder');
1054 $fixture = $this->createDriverFixture(array(
1055 'basePath' => $this->getMountRootUrl()
1056 ), $mockedStorage);
1057
1058 $this->assertTrue($fixture->isWithin($mockedFolder, '/someFolder/test.jpg'));
1059 $this->assertTrue($fixture->isWithin($mockedFolder, '/someFolder/subFolder/test.jpg'));
1060 $this->assertFalse($fixture->isWithin($mockedFolder, '/someFolderWithALongName/test.jpg'));
1061 }
1062
1063 /**
1064 * @test
1065 */
1066 public function isWithinAcceptsFileAndFolderObjectsAsContent() {
1067 $mockedStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1068
1069 $mockedFolder = new t3lib_file_Folder($mockedStorage, '/someFolder/', 'someFolder');
1070 $fixture = $this->createDriverFixture(array(
1071 'basePath' => $this->getMountRootUrl()
1072 ), $mockedStorage);
1073
1074 $mockedSubfolder = $this->getSimpleFolderMock('/someFolder/subfolder/');
1075 $mockedFile = $this->getSimpleFileMock('/someFolder/test.jpg');
1076
1077 $this->assertTrue($fixture->isWithin($mockedFolder, $mockedFile));
1078 $this->assertTrue($fixture->isWithin($mockedFolder, $mockedSubfolder));
1079 }
1080
1081 /**
1082 * @test
1083 */
1084 public function isWithinAlwaysReturnsFalseIfFolderIsWithinDifferentStorage() {
1085 $mockedStorage1 = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1086 $mockedStorage2 = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1087
1088 $mockedFolder = new t3lib_file_Folder($mockedStorage1, '/someFolder/', 'someFolder');
1089 $fixture = $this->createDriverFixture(array(
1090 'basePath' => $this->getMountRootUrl()
1091 ), $mockedStorage2);
1092
1093 $fileIdentifier = '/someFolder/test.jpg';
1094 $subfolderIdentifier = '/someFolder/subfolder/';
1095 $mockedFile = $this->getSimpleFileMock($fileIdentifier);
1096 $mockedSubfolder = $this->getSimpleFolderMock($subfolderIdentifier);
1097
1098 $this->assertFalse($fixture->isWithin($mockedFolder, $mockedFile));
1099 $this->assertFalse($fixture->isWithin($mockedFolder, $fileIdentifier));
1100 $this->assertFalse($fixture->isWithin($mockedFolder, $mockedSubfolder));
1101 $this->assertFalse($fixture->isWithin($mockedFolder, $subfolderIdentifier));
1102 }
1103
1104 /**********************************
1105 * Copy/move file
1106 **********************************/
1107
1108 /**
1109 * @param $identifier
1110 * @param null|t3lib_file_Storage $storage
1111 * @return t3lib_file_File
1112 */
1113 protected function mockFileForCopyingAndMoving($identifier, t3lib_file_Storage $storage = NULL) {
1114 if (!$storage) {
1115 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1116 }
1117
1118 $fileObject = new t3lib_file_File(array('identifier' => $identifier, 'name' => basename($identifier), 'storage' => $storage));
1119 return $fileObject;
1120 }
1121
1122 /**
1123 * @param $identifier
1124 * @param null|t3lib_file_Storage $storage
1125 * @return t3lib_file_Folder
1126 */
1127 protected function mockFolderForCopyingAndMoving($identifier, t3lib_file_Storage $storage = NULL) {
1128 if (!$storage) {
1129 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1130 }
1131
1132 $folderObject = new t3lib_file_Folder($storage, $identifier, basename($identifier), 0);
1133 return $folderObject;
1134 }
1135
1136 /**
1137 * Prepares a simple two-folder environment with /someFolder/ and /targetFolder/. /someFolder contains a file with random
1138 * contents
1139 *
1140 * @return array $mockedFolder, $sourceFolder, $fileContents, $fixture
1141 */
1142 protected function _prepareFolderEnvironmentForMoveTest() {
1143 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
1144 $sourceFolder = $this->getSimpleFolderMock('/someFolder/');
1145
1146 $fileContents = uniqid();
1147 $this->addToMount(array(
1148 'targetFolder' => array(),
1149 'someFolder' => array('file' => $fileContents)
1150 ));
1151 $fixture = $this->createDriverFixture(array(
1152 'basePath' => $this->getMountRootUrl()
1153 ));
1154 return array($mockedFolder, $sourceFolder, $fileContents, $fixture);
1155 }
1156
1157
1158 /**
1159 * @test
1160 */
1161 public function filesCanBeCopiedWithinStorage() {
1162 $fileContents = uniqid();
1163 $this->addToMount(array(
1164 'someFile' => $fileContents,
1165 'targetFolder' => array()
1166 ));
1167 $fixture = $this->createDriverFixture(array(
1168 'basePath' => $this->getMountRootUrl()
1169 ));
1170
1171 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1172 $sourceFile = $this->mockFileForCopyingAndMoving('/someFile', $storage);
1173 $targetFolder = $this->mockFolderForCopyingAndMoving('/targetFolder/', $storage);
1174
1175 $fixture->copyFileWithinStorage($sourceFile, $targetFolder, 'someFile');
1176 $this->assertFileEquals($this->getUrlInMount('/someFile'), $this->getUrlInMount('/targetFolder/someFile'));
1177 }
1178
1179 /**
1180 * @test
1181 */
1182 public function filesCanBeMovedWithinStorage() {
1183 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
1184 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1185 $sourceFile = $this->mockFileForCopyingAndMoving('/someFile', $storage);
1186
1187 $fileContents = uniqid();
1188 $this->addToMount(array(
1189 'targetFolder' => array(),
1190 'someFile' => $fileContents
1191 ));
1192 $fixture = $this->createDriverFixture(array(
1193 'basePath' => $this->getMountRootUrl()
1194 ));
1195
1196 $newIdentifier = $fixture->moveFileWithinStorage($sourceFile, $mockedFolder, 'file');
1197 $this->assertEquals($fileContents, file_get_contents($this->getUrlInMount('/targetFolder/file')));
1198 $this->assertFileNotExists($this->getUrlInMount('/someFile'));
1199 $this->assertEquals('/targetFolder/file', $newIdentifier);
1200 }
1201
1202 /**
1203 * @test
1204 */
1205 public function fileMetadataIsChangedAfterMovingFile() {
1206 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
1207 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1208 $sourceFile = $this->mockFileForCopyingAndMoving('/someFile', $storage);
1209
1210 $fileContents = uniqid();
1211 $this->addToMount(array(
1212 'targetFolder' => array(),
1213 'someFile' => $fileContents
1214 ));
1215 $fixture = $this->createDriverFixture(array(
1216 'basePath' => $this->getMountRootUrl()
1217 ));
1218
1219 $newIdentifier = $fixture->moveFileWithinStorage($sourceFile, $mockedFolder, 'file');
1220 $fileMetadata = $fixture->getFileInfoByIdentifier($newIdentifier);
1221 $this->assertEquals($newIdentifier, $fileMetadata['identifier']);
1222 }
1223
1224 public function renamingFiles_dataProvider() {
1225 return array(
1226 'file in subfolder' => array(
1227 array(
1228 'targetFolder' => array('file' => ''),
1229 ),
1230 '/targetFolder/file',
1231 'newFile',
1232 '/targetFolder/newFile'
1233 ),
1234 'file in rootfolder' => array(
1235 array(
1236 'fileInRoot' => ''
1237 ),
1238 '/fileInRoot',
1239 'newFile',
1240 '/newFile'
1241 )
1242 );
1243 }
1244
1245 /**
1246 * @test
1247 * @dataProvider renamingFiles_dataProvider
1248 */
1249 public function renamingFilesChangesFilenameOnDisk(array $filesystemStructure, $oldFileIdentifier, $newFileName, $expectedNewIdentifier) {
1250 $this->addToMount($filesystemStructure);
1251 $fixture = $this->createDriverFixture(array(
1252 'basePath' => $this->getMountRootUrl()
1253 ));
1254
1255 $file = $this->getSimpleFileMock($oldFileIdentifier);
1256
1257 $newIdentifier = $fixture->renameFile($file, $newFileName);
1258 $this->assertFalse($fixture->fileExists($oldFileIdentifier));
1259 $this->assertTrue($fixture->fileExists($newIdentifier));
1260 $this->assertEquals($expectedNewIdentifier, $newIdentifier);
1261 }
1262
1263 /**
1264 * @test
1265 */
1266 public function renamingFilesFailsIfTargetFileExists() {
1267 $this->setExpectedException('t3lib_file_exception_ExistingTargetFileNameException', '', 1320291063);
1268
1269 $this->addToMount(array(
1270 'targetFolder' => array('file' => '', 'newFile' => ''),
1271 ));
1272 $fixture = $this->createDriverFixture(array(
1273 'basePath' => $this->getMountRootUrl()
1274 ));
1275
1276 $file = $this->getSimpleFileMock('/targetFolder/file');
1277
1278 $fixture->renameFile($file, 'newFile');
1279 }
1280
1281 /**
1282 * We use this data provider for testing move methods because there are some issues with the
1283 *
1284 * @return array
1285 */
1286 public function renamingFolders_dataProvider() {
1287 return array(
1288 'folder in root folder' => array(
1289 array(
1290 'someFolder' => array(),
1291 ),
1292 '/someFolder/',
1293 'newFolder',
1294 '/newFolder/'
1295 ),
1296 'file in subfolder' => array(
1297 array(
1298 'subfolder' => array(
1299 'someFolder' => array()
1300 )
1301 ),
1302 '/subfolder/someFolder/',
1303 'newFolder',
1304 '/subfolder/newFolder/'
1305 )
1306 );
1307 }
1308
1309 /**
1310 * @test
1311 * @dataProvider renamingFolders_dataProvider
1312 */
1313 public function renamingFoldersChangesFolderNameOnDisk(array $filesystemStructure, $oldFolderIdentifier, $newFolderName, $expectedNewIdentifier) {
1314 $this->addToMount($filesystemStructure);
1315 $fixture = $this->createDriverFixture(array(
1316 'basePath' => $this->getMountRootUrl()
1317 ));
1318 $mockedFolder = $this->getSimpleFolderMock($oldFolderIdentifier);
1319
1320 $mapping = $fixture->renameFolder($mockedFolder, $newFolderName);
1321
1322 $this->assertFalse($fixture->folderExists($oldFolderIdentifier));
1323 $this->assertTrue($fixture->folderExists($expectedNewIdentifier));
1324 $this->assertEquals($expectedNewIdentifier, $mapping[$oldFolderIdentifier]);
1325 }
1326
1327 /**
1328 * @test
1329 */
1330 public function renameFolderReturnsCorrectMappingInformationForAllFiles() {
1331 $fileContents = 'asdfg';
1332 $this->addToMount(array(
1333 'sourceFolder' => array(
1334 'subFolder' => array('file' => $fileContents),
1335 'file' => 'asdfg'
1336 )
1337 ));
1338 $fixture = $this->createDriverFixture(array(
1339 'basePath' => $this->getMountRootUrl()
1340 ));
1341
1342 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1343
1344 $mappingInformation = $fixture->renameFolder($sourceFolder, 'newFolder');
1345
1346 $this->assertEquals('/newFolder/', $mappingInformation['/sourceFolder/']);
1347 $this->assertEquals('/newFolder/file', $mappingInformation['/sourceFolder/file']);
1348 $this->assertEquals('/newFolder/subFolder/file', $mappingInformation['/sourceFolder/subFolder/file']);
1349 $this->assertEquals('/newFolder/subFolder/', $mappingInformation['/sourceFolder/subFolder/']);
1350 }
1351
1352 /**
1353 * @test
1354 */
1355 public function renameFolderRevertsRenamingIfFilenameMapCannotBeCreated() {
1356 $this->setExpectedException('RuntimeException', '', 1334160746);
1357
1358 $this->addToMount(array(
1359 'sourceFolder' => array(
1360 'file' => 'asdfg'
1361 )
1362 ));
1363 $fixture = $this->createDriverFixture(array(
1364 'basePath' => $this->getMountRootUrl()
1365 ), NULL, array('createIdentifierMap'));
1366 $fixture->expects($this->atLeastOnce())->method('createIdentifierMap')
1367 ->will($this->throwException(new t3lib_file_exception_FileOperationErrorException()));
1368
1369 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1370
1371 $fixture->renameFolder($sourceFolder, 'newFolder');
1372
1373 $this->assertFileExists($this->getUrlInMount('/sourceFolder/file'));
1374 }
1375
1376 /**
1377 * @test
1378 */
1379 public function isFolderEmptyReturnsTrueForEmptyFolder() {
1380 // This also prepares the next few tests, so add more info than required for this test
1381 $this->addToMount(array(
1382 'emptyFolder' => array(),
1383 ));
1384 $fixture = $this->createDriverFixture(array(
1385 'basePath' => $this->getMountRootUrl()
1386 ));
1387 $mockedFolder = $this->getSimpleFolderMock('/emptyFolder/');
1388
1389 $this->assertTrue($fixture->isFolderEmpty($mockedFolder));
1390
1391 return $fixture;
1392 }
1393
1394 /**
1395 * @test
1396 */
1397 public function isFolderEmptyReturnsFalseIfFolderHasFile() {
1398 $this->addToMount(array(
1399 'folderWithFile' => array(
1400 'someFile' => ''
1401 ),
1402 ));
1403 $fixture = $this->createDriverFixture(array(
1404 'basePath' => $this->getMountRootUrl()
1405 ));
1406 $mockedFolder = $this->getSimpleFolderMock('/folderWithFile/');
1407
1408 $this->assertFalse($fixture->isFolderEmpty($mockedFolder));
1409 }
1410
1411 /**
1412 * @test
1413 */
1414 public function isFolderEmptyReturnsFalseIfFolderHasSubfolder() {
1415 $this->addToMount(array(
1416 'folderWithSubfolder' => array(
1417 'someFolder' => array()
1418 )
1419 ));
1420 $fixture = $this->createDriverFixture(array(
1421 'basePath' => $this->getMountRootUrl()
1422 ));
1423 $mockedFolder = $this->getSimpleFolderMock('/folderWithSubfolder/');
1424
1425 $this->assertFalse($fixture->isFolderEmpty($mockedFolder));
1426 }
1427
1428
1429 /**********************************
1430 * Copy/move folder
1431 **********************************/
1432
1433 /**
1434 * @test
1435 */
1436 public function foldersCanBeMovedWithinStorage() {
1437 /** @var t3lib_file_Driver_LocalDriver $fixture */
1438 list($mockedFolder, $sourceFolder, $fileContents, $fixture) = $this->_prepareFolderEnvironmentForMoveTest();
1439
1440 $fixture->moveFolderWithinStorage($sourceFolder, $mockedFolder, 'someFolder');
1441 $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/someFolder/')));
1442 $this->assertEquals($fileContents, file_get_contents($this->getUrlInMount('/targetFolder/someFolder/file')));
1443 $this->assertFileNotExists($this->getUrlInMount('/someFile'));
1444 }
1445
1446 /**
1447 * @test
1448 */
1449 public function moveFolderWithinStorageReturnsCorrectMappingInformationForAllFiles() {
1450 $fileContents = 'asdfg';
1451 $this->addToMount(array(
1452 'targetFolder' => array(),
1453 'sourceFolder' => array(
1454 'subFolder' => array('file' => $fileContents),
1455 'file' => 'asdfg'
1456 )
1457 ));
1458 $fixture = $this->createDriverFixture(array(
1459 'basePath' => $this->getMountRootUrl()
1460 ));
1461
1462 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1463 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
1464
1465 $mappingInformation = $fixture->moveFolderWithinStorage($sourceFolder, $targetFolder, 'sourceFolder');
1466
1467 $this->assertEquals('/targetFolder/sourceFolder/file', $mappingInformation['/sourceFolder/file']);
1468 $this->assertEquals('/targetFolder/sourceFolder/subFolder/file', $mappingInformation['/sourceFolder/subFolder/file']);
1469 $this->assertEquals('/targetFolder/sourceFolder/subFolder/', $mappingInformation['/sourceFolder/subFolder/']);
1470 }
1471
1472 /**
1473 * @test
1474 */
1475 public function folderCanBeRenamedWhenMoving() {
1476 /** @var t3lib_file_Driver_LocalDriver $fixture */
1477 list($mockedFolder, $sourceFolder, $fileContents, $fixture) = $this->_prepareFolderEnvironmentForMoveTest();
1478
1479 $fixture->moveFolderWithinStorage($sourceFolder, $mockedFolder, 'newFolder');
1480 $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/newFolder/')));
1481 }
1482
1483
1484 protected function _setupFolderForCopyTest() {
1485 $fileContents1 = uniqid();
1486 $fileContents2 = uniqid();
1487 $this->addToMount(array(
1488 'targetFolder' => array(),
1489 'sourceFolder' => array(
1490 'subFolder' => array('file' => $fileContents1),
1491 'file' => $fileContents2
1492 )
1493 ));
1494 $fixture = $this->createDriverFixture(array(
1495 'basePath' => $this->getMountRootUrl()
1496 ));
1497 return $fixture;
1498 }
1499
1500 /**
1501 * @test
1502 * @see _setupFolderForCopyTest
1503 */
1504 public function foldersCanBeCopiedWithinSameStorage() {
1505 $fixture = $this->_setupFolderForCopyTest();
1506
1507 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1508 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
1509
1510 $fixture->copyFolderWithinStorage($sourceFolder, $targetFolder, 'sourceFolder');
1511
1512 $this->assertTrue($fixture->folderExists('/targetFolder/sourceFolder/'));
1513 $this->assertTrue($fixture->fileExists('/targetFolder/sourceFolder/file'));
1514 $this->assertTrue($fixture->folderExists('/targetFolder/sourceFolder/subFolder/'));
1515 $this->assertTrue($fixture->fileExists('/targetFolder/sourceFolder/subFolder/file'));
1516 }
1517
1518 /**
1519 * @test
1520 * @see _setupFolderForCopyTest
1521 */
1522 public function folderNameCanBeChangedWhileCopying() {
1523 $fixture = $this->_setupFolderForCopyTest();
1524
1525 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1526 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
1527
1528 $fixture->copyFolderWithinStorage($sourceFolder, $targetFolder, 'newFolder');
1529
1530 $this->assertTrue($fixture->folderExists('/targetFolder/newFolder/'));
1531 $this->assertTrue($fixture->fileExists('/targetFolder/newFolder/file'));
1532 $this->assertFalse($fixture->folderExists('/targetFolder/sourceFolder/'));
1533 }
1534 }
1535 ?>