39e8d93edcbae8075ffc9d8968310e2ca02bc4f4
[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 $fixture = $this->createDriverFixture(array(
366 'basePath' => $this->getMountRootUrl()
367 ));
368
369 $fixture->addFile($this->getUrlInMount('/targetFolder/file'), $mockedFolder, 'file');
370 }
371
372 /**
373 * @test
374 */
375 public function addFileReturnsFileObject() {
376 $mockedFolder = $this->getMock('t3lib_file_Folder', array(), array(), '', FALSE);
377 $mockedFolder->expects($this->any())->method('getIdentifier')->will($this->returnValue('/targetFolder/'));
378
379 $this->addToMount(array('targetFolder' => array()));
380 $this->addToVfs(array(
381 'sourceFolder' => array(
382 'file' => 'asdf'
383 )
384 ));
385 $fixture = $this->createDriverFixture(array(
386 'basePath' => $this->getMountRootUrl()
387 ));
388
389 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
390 $fileObject = $fixture->addFile($this->getUrl('sourceFolder/file'), $mockedFolder, 'file');
391
392 $this->assertInstanceOf('t3lib_file_File', $fileObject);
393 $this->assertEquals('file', $fileObject->getName());
394 $this->assertEquals('/targetFolder/file', $fileObject->getIdentifier());
395 }
396
397 /**
398 * @test
399 */
400 public function addFileRawCreatesCopyOfFile() {
401 $this->addToMount(array('targetFolder' => array()));
402 $this->addToVfs(array(
403 'sourceFolder' => array(
404 'file' => 'asdf'
405 )
406 ));
407 $fixture = $this->createDriverFixture(array(
408 'basePath' => $this->getMountRootUrl()
409 ));
410
411 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
412 $fileIdentifier = $fixture->addFileRaw($this->getUrl('sourceFolder/file'), $this->getSimpleFolderMock('/targetFolder/'), 'somefile');
413 $this->assertTrue(file_exists($this->getUrl('sourceFolder/file')));
414 $this->assertTrue(file_exists($this->getUrlInMount('targetFolder/somefile')));
415 $this->assertEquals('/targetFolder/somefile', $fileIdentifier);
416 }
417
418 /**
419 * @test
420 */
421 public function deleteFileRawRemovesFile() {
422 $this->addToMount(array('targetFolder' => array(
423 'file' => 'asdjlkfa'
424 )));
425 $fixture = $this->createDriverFixture(array(
426 'basePath' => $this->getMountRootUrl()
427 ));
428
429 $this->assertTrue(file_exists($this->getUrlInMount('targetFolder/file')));
430 $fixture->deleteFileRaw('/targetFolder/file');
431 $this->assertFalse(file_exists($this->getUrlInMount('targetFolder/file')));
432 }
433
434 /**
435 * @test
436 */
437 public function replacingFileUpdatesMetadataInFileObject() {
438 $this->addToMount(array('targetFolder' => array(
439 'file' => 'asdjlkfa'
440 )));
441 $this->addToVfs(array('sourceFolder' => array(
442 'file' => 'asjdalks'
443 )));
444 $fixture = $this->createDriverFixture(array(
445 'basePath' => $this->getMountRootUrl()
446 ));
447
448 $mockedFile = $this->getSimpleFileMock('/targetFolder/file', array('updateProperties'));
449 $mockedFile->expects($this->once())->method('updateProperties');
450
451 $fixture->replaceFile($mockedFile, $this->getUrl('sourceFolder/file'));
452 }
453
454 /**
455 * @test
456 */
457 public function existenceChecksWorkForFilesAndFolders() {
458 $this->addToMount(array(
459 'file' => 'asdf',
460 'folder' => array()
461 ));
462 $fixture = $this->createDriverFixture(array(
463 'basePath' => $this->getMountRootUrl()
464 ));
465
466 // Using slashes at the beginning of paths because they will be stored in the DB this way.
467 $this->assertTrue($fixture->fileExists('/file'));
468 $this->assertTrue($fixture->folderExists('/folder/'));
469 $this->assertFalse($fixture->fileExists('/nonexistingFile'));
470 $this->assertFalse($fixture->folderExists('/nonexistingFolder/'));
471 }
472
473 /**
474 * @test
475 */
476 public function existenceChecksInFolderWorkForFilesAndFolders() {
477 $mockedFolder = $this->getSimpleFolderMock('/subfolder/');
478
479 $this->addToMount(array('subfolder' => array(
480 'file' => 'asdf',
481 'folder' => array()
482 )));
483 $fixture = $this->createDriverFixture(array(
484 'basePath' => $this->getMountRootUrl()
485 ));
486
487 $this->assertTrue($fixture->fileExistsInFolder('file', $mockedFolder));
488 $this->assertTrue($fixture->folderExistsInFolder('folder', $mockedFolder));
489 $this->assertFalse($fixture->fileExistsInFolder('nonexistingFile', $mockedFolder));
490 $this->assertFalse($fixture->folderExistsInFolder('nonexistingFolder', $mockedFolder));
491 }
492
493 /**
494 * @test
495 */
496 public function getPublicUrlReturnsCorrectUriForConfiguredBaseUri() {
497 $baseUri = 'http://example.org/foobar/' . uniqid();
498 $this->addToMount(array(
499 'file.ext' => 'asdf',
500 'subfolder' => array(
501 'file2.ext' => 'asdf'
502 )
503 ));
504 $fixture = $this->createDriverFixture(array(
505 'basePath' => $this->getMountRootUrl(),
506 'baseUri' => $baseUri
507 ));
508
509 $mockedFile1 = $this->getMock('t3lib_file_File', array(), array(), '', FALSE);
510 $mockedFile1->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
511 $mockedFile2 = $this->getMock('t3lib_file_File', array(), array(), '', FALSE);
512 $mockedFile2->expects($this->any())->method('getIdentifier')->will($this->returnValue('/subfolder/file2.ext'));
513
514 $this->assertEquals($baseUri . '/file.ext', $fixture->getPublicUrl($mockedFile1));
515 $this->assertEquals($baseUri . '/subfolder/file2.ext', $fixture->getPublicUrl($mockedFile2));
516 }
517
518 /**
519 * @test
520 */
521 public function fileContentsCanBeWrittenAndRead() {
522 $fileContents = 'asdf';
523 $this->addToMount(array(
524 'file.ext' => $fileContents
525 ));
526 $fixture = $this->createDriverFixture(array(
527 'basePath' => $this->getMountRootUrl()
528 ));
529
530 $mockedFile = $this->getMock('t3lib_file_File', array(), array(), '', FALSE);
531 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
532
533 $this->assertEquals($fileContents, $fixture->getFileContents($mockedFile), 'File contents could not be read');
534 $newFileContents = 'asdfgh';
535 $fixture->setFileContents($mockedFile, $newFileContents);
536 $this->assertEquals($newFileContents, $fixture->getFileContents($mockedFile), 'New file contents could not be read.');
537 }
538
539 /**
540 * @test
541 */
542 public function setFileContentsReturnsNumberOfBytesWrittenToFile() {
543 $fileContents = 'asdf';
544 $this->addToMount(array(
545 'file.ext' => $fileContents
546 ));
547 $fixture = $this->createDriverFixture(array(
548 'basePath' => $this->getMountRootUrl()
549 ));
550
551 $mockedFile = $this->getSimpleFileMock('/file.ext');
552
553 $newFileContents = 'asdfgh';
554 $bytesWritten = $fixture->setFileContents($mockedFile, $newFileContents);
555
556 $this->assertEquals(strlen($newFileContents), $bytesWritten);
557 }
558
559 /**
560 * @test
561 * @depends existenceChecksWorkForFilesAndFolders
562 * @return array The driver fixture, the mocked file
563 */
564 public function newFilesCanBeCreated() {
565 $this->addToMount(array(
566 'someDir' => array()
567 ));
568 /** @var $fixture t3lib_file_Driver_LocalDriver */
569 list($basedir, $fixture) = $this->prepareRealTestEnvironment();
570
571 mkdir($basedir . '/someDir');
572 $fixture->createFile('testfile.txt', $fixture->getFolder('someDir'));
573
574 $mockedFile = $this->getSimpleFileMock('/someDir/testfile.txt');
575
576 $this->assertTrue($fixture->fileExists('/someDir/testfile.txt'));
577
578 return array($fixture, $mockedFile);
579 }
580
581 /**
582 * @test
583 * @depends newFilesCanBeCreated
584 */
585 public function createdFilesAreEmpty(array $arguments) {
586 /** @var $fixture t3lib_file_Driver_LocalDriver */
587 list($fixture, $mockedFile) = $arguments;
588 $fileData = $fixture->getFileContents($mockedFile);
589 $this->assertEquals(0, strlen($fileData));
590 }
591
592 /**********************************
593 * File and directory listing
594 **********************************/
595
596 /**
597 * @test
598 */
599 public function getFileReturnsCorrectIdentifier() {
600 $this->addToMount(array(
601 'someDir' => array(
602 'someFile' => 'asdfg'
603 ),
604 'someFileAtRootLevel' => 'foobar'
605 ));
606 $fixture = $this->createDriverFixture(array(
607 'basePath' => $this->getMountRootUrl()
608 ));
609
610 $subdirFileInfo = $fixture->getFileInfoByIdentifier('/someDir/someFile');
611 $this->assertEquals('/someDir/someFile', $subdirFileInfo['identifier']);
612 $rootFileInfo = $fixture->getFileInfoByIdentifier('/someFileAtRootLevel');
613 $this->assertEquals('/someFileAtRootLevel', $rootFileInfo['identifier']);
614 }
615
616 /**
617 * @test
618 */
619 public function getFileThrowsExceptionIfFileDoesNotExist() {
620 $this->setExpectedException('InvalidArgumentException', '', 1314516809);
621 $fixture = $this->createDriverFixture(array(
622 'basePath' => $this->getMountRootUrl()
623 ));
624
625 $fixture->getFileInfoByIdentifier('/some/file/at/a/random/path');
626 }
627
628 /**
629 * @test
630 */
631 public function getFileListReturnsEmptyArrayForEmptyDirectory() {
632 $fixture = $this->createDriverFixture(array(
633 'basePath' => $this->getMountRootUrl()
634 ));
635
636 $fileList = $fixture->getFileList('/');
637
638 $this->assertEmpty($fileList);
639 }
640
641 /**
642 * @test
643 */
644 public function getFileListReturnsAllFilesInDirectory() {
645 $dirStructure = array(
646 'aDir' => array(),
647 'file1' => 'asdfg',
648 'file2' => 'fdsa'
649 );
650 $this->addToMount($dirStructure);
651 $fixture = $this->createDriverFixture(array(
652 'basePath' => $this->getMountRootUrl()
653 ));
654
655 $fileList = $fixture->getFileList('/');
656
657 $this->assertEquals(array('file1', 'file2'), array_keys($fileList));
658 }
659
660 /**
661 * @test
662 */
663 public function getFileListFailsIfDirectoryDoesNotExist() {
664 $this->setExpectedException('InvalidArgumentException', '', 1314349666);
665
666 $this->addToMount(array('somefile' => ''));
667 $fixture = $this->createDriverFixture(array(
668 'basePath' => $this->getMountRootUrl()
669 ));
670
671 $fixture->getFileList('somedir/');
672 }
673
674 /**
675 * @test
676 */
677 public function getFileListDoesNotReturnHiddenFilesByDefault() {
678 $dirStructure = array(
679 'aDir' => array(),
680 '.someHiddenFile' => 'asdf',
681 'file1' => 'asdfg',
682 'file2' => 'fdsa'
683 );
684 $this->addToMount($dirStructure);
685 $fixture = $this->createDriverFixture(array(
686 'basePath' => $this->getMountRootUrl()
687 ));
688
689 $fileList = $fixture->getFileList('/');
690
691 $this->assertEquals(array('file1', 'file2'), array_keys($fileList));
692 }
693
694 /**
695 * @test
696 */
697 public function getFileListFiltersItemsWithGivenFilterMethods() {
698 $dirStructure = array(
699 'fileA' => 'asdfg',
700 'fileB' => 'fdsa'
701 );
702 $this->addToMount($dirStructure);
703 $fixture = $this->createDriverFixture(array(
704 'basePath' => $this->getMountRootUrl()
705 ));
706 $filterCallbacks = array(
707 array('t3lib_file_Tests_Driver_Fixtures_LocalDriverFilenameFilter', 'filterFilename')
708 );
709
710 $fileList = $fixture->getFileList('/', 0, 0, $filterCallbacks);
711
712 $this->assertNotContains('fileA', array_keys($fileList));
713 }
714
715 /**
716 * @test
717 */
718 public function getFolderListReturnsAllDirectoriesInDirectory() {
719 $dirStructure = array(
720 'dir1' => array(),
721 'dir2' => array(),
722 'file' => 'asdfg'
723 );
724 $this->addToMount($dirStructure);
725
726 $fixture = $this->createDriverFixture(array(
727 'basePath' => $this->getMountRootUrl()
728 ));
729
730 $fileList = $fixture->getFolderList('/');
731
732 $this->assertEquals(array('dir1', 'dir2'), array_keys($fileList));
733 }
734
735 /**
736 * @test
737 */
738 public function getFolderListDoesNotReturnHiddenFoldersByDefault() {
739 $dirStructure = array(
740 '.someHiddenFile' => array(),
741 'aDir' => array(),
742 'file1' => '',
743 );
744 $this->addToMount($dirStructure);
745 $fixture = $this->createDriverFixture(array(
746 'basePath' => $this->getMountRootUrl()
747 ));
748
749 $fileList = $fixture->getFolderList('/');
750
751 $this->assertEquals(array('aDir'), array_keys($fileList));
752 }
753
754 /**
755 * @test
756 */
757 public function getFolderListUsesCorrectPathForItems() {
758 $this->addToMount(array(
759 'dir1' => array(
760 'subdir1' => array()
761 )
762 ));
763
764 $fixture = $this->createDriverFixture(array(
765 'basePath' => $this->getMountRootUrl()
766 ));
767
768 $FolderList = $fixture->getFolderList('/');
769 $this->assertEquals('/dir1/', $FolderList['dir1']['identifier']);
770
771 $FolderList = $fixture->getFolderList('/dir1/');
772 $this->assertEquals('/dir1/subdir1/', $FolderList['subdir1']['identifier']);
773 }
774
775 /**
776 * Checks if the folder names . and .. are ignored when listing subdirectories
777 *
778 * @test
779 */
780 public function getFolderListLeavesOutNavigationalEntries() {
781 // we have to add .. and . manually, as these are not included in vfsStream directory listings (as opposed
782 // to normal file listings)
783 $this->addToMount(array(
784 '..' => array(),
785 '.' => array()
786 ));
787 $fixture = $this->createDriverFixture(array(
788 'basePath' => $this->getMountRootUrl()
789 ));
790
791 $fileList = $fixture->getFolderList('/');
792
793 $this->assertEmpty($fileList);
794 }
795
796 /**
797 * @test
798 */
799 public function getFolderListFiltersItemsWithGivenFilterMethods() {
800 $dirStructure = array(
801 'folderA' => array(),
802 'folderB' => array()
803 );
804 $this->addToMount($dirStructure);
805 $fixture = $this->createDriverFixture(array(
806 'basePath' => $this->getMountRootUrl()
807 ));
808 $filterCallbacks = array(
809 array('t3lib_file_Tests_Driver_Fixtures_LocalDriverFilenameFilter', 'filterFilename')
810 );
811
812 $folderList = $fixture->getFolderList('/', 0, 0, $filterCallbacks);
813
814 $this->assertNotContains('folderA', array_keys($folderList));
815 }
816
817 /**
818 * @test
819 */
820 public function getFolderListFailsIfDirectoryDoesNotExist() {
821 $this->setExpectedException('InvalidArgumentException', '', 1314349666);
822
823 $fixture = $this->createDriverFixture(array(
824 'basePath' => $this->getMountRootUrl()
825 ));
826 vfsStream::create(array($this->basedir => array('somefile' => '')));
827
828 $fixture->getFolderList('somedir/');
829 }
830
831 /**
832 * @test
833 */
834 public function hashReturnsCorrectHashes() {
835 $contents = "68b329da9893e34099c7d8ad5cb9c940";
836 $expectedMd5Hash = "8c67dbaf0ba22f2e7fbc26413b86051b";
837 $expectedSha1Hash = "a60cd808ba7a0bcfa37fa7f3fb5998e1b8dbcd9d";
838
839 $mockedFile = $this->getSimpleFileMock('/hashFile');
840 $this->addToMount(array('hashFile' => $contents));
841 $fixture = $this->createDriverFixture(array(
842 'basePath' => $this->getMountRootUrl()
843 ));
844
845 $this->assertEquals($expectedSha1Hash, $fixture->hash($mockedFile, 'sha1'));
846 $this->assertEquals($expectedMd5Hash, $fixture->hash($mockedFile, 'md5'));
847 }
848
849 /**
850 * @test
851 */
852 public function hashingWithUnsupportedAlgorithmFails() {
853 $this->setExpectedException('InvalidArgumentException', '', 1304964032);
854
855 $fixture = $this->createDriverFixture(array(
856 'basePath' => $this->getMountRootUrl()
857 ));
858 $fixture->hash($this->getSimpleFileMock('/hashFile'), uniqid());
859 }
860
861 /**
862 * @test
863 * @covers t3lib_file_Driver_LocalDriver::getFileForLocalProcessing
864 */
865 public function getFileForLocalProcessingCreatesCopyOfFileByDefault() {
866 $fileContents = 'asdfgh';
867 $this->addToMount(array(
868 'someDir' => array(
869 'someFile' => $fileContents
870 )
871 ));
872 $fixture = $this->createDriverFixture(array(
873 'basePath' => $this->getMountRootUrl()
874 ), NULL, array('copyFileToTemporaryPath'));
875 $mockedFile = $this->getSimpleFileMock('/someDir/someFile');
876 // TODO add parameter expectation for $mockedFile as soon as PHPUnit supports object identity matching in parameter expectations
877 $fixture->expects($this->once())->method('copyFileToTemporaryPath');
878
879 $fixture->getFileForLocalProcessing($mockedFile);
880 }
881
882 /**
883 * @test
884 */
885 public function getFileForLocalProcessingReturnsOriginalFilepathForReadonlyAccess() {
886 $fileContents = 'asdfgh';
887 $this->addToMount(array(
888 'someDir' => array(
889 'someFile' => $fileContents
890 )
891 ));
892 $fixture = $this->createDriverFixture(array(
893 'basePath' => $this->getMountRootUrl()
894 ));
895 $mockedFile = $this->getSimpleFileMock('/someDir/someFile');
896
897 $filePath = $fixture->getFileForLocalProcessing($mockedFile, FALSE);
898
899 $this->assertEquals($filePath, $this->getMountRootUrl() . 'someDir/someFile');
900 }
901
902 /**
903 * @test
904 */
905 public function filesCanBeCopiedToATemporaryPath() {
906 $fileContents = 'asdfgh';
907 $this->addToMount(array(
908 'someDir' => array(
909 'someFile' => $fileContents
910 )
911 ));
912 $fixture = $this->createDriverFixture(array(
913 'basePath' => $this->getMountRootUrl()
914 ));
915
916 $mockedFile = $this->getSimpleFileMock('/someDir/someFile');
917
918 $filePath = $fixture->copyFileToTemporaryPath($mockedFile);
919
920 $this->assertContains('/typo3temp/', $filePath);
921 $this->assertEquals($fileContents, file_get_contents($filePath));
922 }
923
924 protected function prepareRealTestEnvironment() {
925 $basedir = $this->createRealTestdir();
926 $fixture = $this->createDriverFixture(array(
927 'basePath' => $basedir
928 ));
929
930 return array($basedir, $fixture);
931 }
932
933 /**
934 * @test
935 */
936 public function permissionsAreCorrectlyRetrieved() {
937 /** @var $fixture t3lib_file_Driver_LocalDriver */
938 list($basedir, $fixture) = $this->prepareRealTestEnvironment();
939 touch($basedir . '/someFile');
940 chmod($basedir . '/someFile', 0700);
941 touch($basedir . '/someForbiddenFile');
942 chmod($basedir . '/someForbiddenFile', 0000);
943 mkdir($basedir . '/someFolder');
944 chmod($basedir . '/someFolder', 0700);
945 mkdir($basedir . '/someForbiddenFolder');
946 chmod($basedir . '/someForbiddenFolder', 0000);
947 clearstatcache();
948
949 $this->assertEquals(array('r' => TRUE, 'w' => TRUE), $fixture->getFilePermissions($this->getSimpleFileMock('/someFile')));
950 $this->assertEquals(array('r' => FALSE, 'w' => FALSE), $fixture->getFilePermissions($this->getSimpleFileMock('/someForbiddenFile')));
951 $this->assertEquals(array('r' => TRUE, 'w' => TRUE), $fixture->getFolderPermissions($this->getSimpleFolderMock('/someFolder')));
952 $this->assertEquals(array('r' => FALSE, 'w' => FALSE), $fixture->getFolderPermissions($this->getSimpleFolderMock('/someForbiddenFolder')));
953 }
954
955 /**
956 * Dataprovider for getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser test
957 *
958 * @return array group, filemode and expected result
959 */
960 public function getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser_dataProvider() {
961 $data = array();
962
963 // On some OS, the posix_* functions do not exits
964 if (function_exists('posix_getgid')) {
965 $data = array(
966 'current group, readable/writable' => array(
967 posix_getgid(),
968 0060,
969 array('r' => TRUE, 'w' => TRUE)
970 ),
971 'current group, readable/not writable' => array(
972 posix_getgid(),
973 0040,
974 array('r' => TRUE, 'w' => FALSE)
975 ),
976 'current group, not readable/not writable' => array(
977 posix_getgid(),
978 0000,
979 array('r' => FALSE, 'w' => FALSE)
980 ),
981 );
982 }
983
984 $data = array_merge_recursive($data, array(
985 'arbitrary group, readable/writable' => array(
986 vfsStream::GROUP_USER_1,
987 0006,
988 array('r' => TRUE, 'w' => TRUE)
989 ),
990 'arbitrary group, readable/not writable' => array(
991 vfsStream::GROUP_USER_1,
992 0664,
993 array('r' => TRUE, 'w' => FALSE)
994 ),
995 'arbitrary group, not readable/not writable' => array(
996 vfsStream::GROUP_USER_1,
997 0660,
998 array('r' => FALSE, 'w' => FALSE)
999 ),
1000 ));
1001
1002 return $data;
1003 }
1004
1005 /**
1006 * @test
1007 * @dataProvider getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser_dataProvider
1008 */
1009 public function getFilePermissionsReturnsCorrectPermissionsForFilesNotOwnedByCurrentUser($group, $permissions, $expectedResult) {
1010 $this->addToMount(array(
1011 'testfile' => 'asdfg'
1012 ));
1013 $fixture = $this->createDriverFixture(array(
1014 'basePath' => $this->getMountRootUrl()
1015 ));
1016 /** @var $fileObject vfsStreamContent */
1017 $fileObject = vfsStreamWrapper::getRoot()->getChild($this->mountDir)->getChild('testfile');
1018 // just use an "arbitrary" user here - it is only important that
1019 $fileObject->chown(vfsStream::OWNER_USER_1);
1020 $fileObject->chgrp($group);
1021 $fileObject->chmod($permissions);
1022
1023 $this->assertEquals($expectedResult, $fixture->getFilePermissions($this->getSimpleFileMock('/testfile')));
1024 }
1025
1026 /**
1027 * @test
1028 */
1029 public function isWithinRecognizesFilesWithinFolderAndInOtherFolders() {
1030 $mockedStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1031
1032 $mockedFolder = new t3lib_file_Folder($mockedStorage, '/someFolder/', 'someFolder');
1033 $fixture = $this->createDriverFixture(array(
1034 'basePath' => $this->getMountRootUrl()
1035 ), $mockedStorage);
1036
1037 $this->assertTrue($fixture->isWithin($mockedFolder, '/someFolder/test.jpg'));
1038 $this->assertTrue($fixture->isWithin($mockedFolder, '/someFolder/subFolder/test.jpg'));
1039 $this->assertFalse($fixture->isWithin($mockedFolder, '/someFolderWithALongName/test.jpg'));
1040 }
1041
1042 /**
1043 * @test
1044 */
1045 public function isWithinAcceptsFileAndFolderObjectsAsContent() {
1046 $mockedStorage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1047
1048 $mockedFolder = new t3lib_file_Folder($mockedStorage, '/someFolder/', 'someFolder');
1049 $fixture = $this->createDriverFixture(array(
1050 'basePath' => $this->getMountRootUrl()
1051 ), $mockedStorage);
1052
1053 $mockedSubfolder = $this->getSimpleFolderMock('/someFolder/subfolder/');
1054 $mockedFile = $this->getSimpleFileMock('/someFolder/test.jpg');
1055
1056 $this->assertTrue($fixture->isWithin($mockedFolder, $mockedFile));
1057 $this->assertTrue($fixture->isWithin($mockedFolder, $mockedSubfolder));
1058 }
1059
1060 /**
1061 * @test
1062 */
1063 public function isWithinAlwaysReturnsFalseIfFolderIsWithinDifferentStorage() {
1064 $mockedStorage1 = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1065 $mockedStorage2 = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1066
1067 $mockedFolder = new t3lib_file_Folder($mockedStorage1, '/someFolder/', 'someFolder');
1068 $fixture = $this->createDriverFixture(array(
1069 'basePath' => $this->getMountRootUrl()
1070 ), $mockedStorage2);
1071
1072 $fileIdentifier = '/someFolder/test.jpg';
1073 $subfolderIdentifier = '/someFolder/subfolder/';
1074 $mockedFile = $this->getSimpleFileMock($fileIdentifier);
1075 $mockedSubfolder = $this->getSimpleFolderMock($subfolderIdentifier);
1076
1077 $this->assertFalse($fixture->isWithin($mockedFolder, $mockedFile));
1078 $this->assertFalse($fixture->isWithin($mockedFolder, $fileIdentifier));
1079 $this->assertFalse($fixture->isWithin($mockedFolder, $mockedSubfolder));
1080 $this->assertFalse($fixture->isWithin($mockedFolder, $subfolderIdentifier));
1081 }
1082
1083 /**********************************
1084 * Copy/move file
1085 **********************************/
1086
1087 /**
1088 * @param $identifier
1089 * @param null|t3lib_file_Storage $storage
1090 * @return t3lib_file_File
1091 */
1092 protected function mockFileForCopyingAndMoving($identifier, t3lib_file_Storage $storage = NULL) {
1093 if (!$storage) {
1094 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1095 }
1096
1097 $fileObject = new t3lib_file_File(array('identifier' => $identifier, 'name' => basename($identifier), 'storage' => $storage));
1098 return $fileObject;
1099 }
1100
1101 /**
1102 * @param $identifier
1103 * @param null|t3lib_file_Storage $storage
1104 * @return t3lib_file_Folder
1105 */
1106 protected function mockFolderForCopyingAndMoving($identifier, t3lib_file_Storage $storage = NULL) {
1107 if (!$storage) {
1108 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1109 }
1110
1111 $folderObject = new t3lib_file_Folder($storage, $identifier, basename($identifier), 0);
1112 return $folderObject;
1113 }
1114
1115 /**
1116 * Prepares a simple two-folder environment with /someFolder/ and /targetFolder/. /someFolder contains a file with random
1117 * contents
1118 *
1119 * @return array $mockedFolder, $sourceFolder, $fileContents, $fixture
1120 */
1121 protected function _prepareFolderEnvironmentForMoveTest() {
1122 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
1123 $sourceFolder = $this->getSimpleFolderMock('/someFolder/');
1124
1125 $fileContents = uniqid();
1126 $this->addToMount(array(
1127 'targetFolder' => array(),
1128 'someFolder' => array('file' => $fileContents)
1129 ));
1130 $fixture = $this->createDriverFixture(array(
1131 'basePath' => $this->getMountRootUrl()
1132 ));
1133 return array($mockedFolder, $sourceFolder, $fileContents, $fixture);
1134 }
1135
1136
1137 /**
1138 * @test
1139 */
1140 public function filesCanBeCopiedWithinStorage() {
1141 $fileContents = uniqid();
1142 $this->addToMount(array(
1143 'someFile' => $fileContents,
1144 'targetFolder' => array()
1145 ));
1146 $fixture = $this->createDriverFixture(array(
1147 'basePath' => $this->getMountRootUrl()
1148 ));
1149
1150 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1151 $sourceFile = $this->mockFileForCopyingAndMoving('/someFile', $storage);
1152 $targetFolder = $this->mockFolderForCopyingAndMoving('/targetFolder/', $storage);
1153
1154 $fixture->copyFileWithinStorage($sourceFile, $targetFolder, 'someFile');
1155 $this->assertFileEquals($this->getUrlInMount('/someFile'), $this->getUrlInMount('/targetFolder/someFile'));
1156 }
1157
1158 /**
1159 * @test
1160 */
1161 public function filesCanBeMovedWithinStorage() {
1162 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
1163 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1164 $sourceFile = $this->mockFileForCopyingAndMoving('/someFile', $storage);
1165
1166 $fileContents = uniqid();
1167 $this->addToMount(array(
1168 'targetFolder' => array(),
1169 'someFile' => $fileContents
1170 ));
1171 $fixture = $this->createDriverFixture(array(
1172 'basePath' => $this->getMountRootUrl()
1173 ));
1174
1175 $newIdentifier = $fixture->moveFileWithinStorage($sourceFile, $mockedFolder, 'file');
1176 $this->assertEquals($fileContents, file_get_contents($this->getUrlInMount('/targetFolder/file')));
1177 $this->assertFileNotExists($this->getUrlInMount('/someFile'));
1178 $this->assertEquals('/targetFolder/file', $newIdentifier);
1179 }
1180
1181 /**
1182 * @test
1183 */
1184 public function fileMetadataIsChangedAfterMovingFile() {
1185 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
1186 $storage = $this->getMock('t3lib_file_Storage', array(), array(), '', FALSE);
1187 $sourceFile = $this->mockFileForCopyingAndMoving('/someFile', $storage);
1188
1189 $fileContents = uniqid();
1190 $this->addToMount(array(
1191 'targetFolder' => array(),
1192 'someFile' => $fileContents
1193 ));
1194 $fixture = $this->createDriverFixture(array(
1195 'basePath' => $this->getMountRootUrl()
1196 ));
1197
1198 $newIdentifier = $fixture->moveFileWithinStorage($sourceFile, $mockedFolder, 'file');
1199 $fileMetadata = $fixture->getFileInfoByIdentifier($newIdentifier);
1200 $this->assertEquals($newIdentifier, $fileMetadata['identifier']);
1201 }
1202
1203 public function renamingFiles_dataProvider() {
1204 return array(
1205 'file in subfolder' => array(
1206 array(
1207 'targetFolder' => array('file' => ''),
1208 ),
1209 '/targetFolder/file',
1210 'newFile',
1211 '/targetFolder/newFile'
1212 ),
1213 'file in rootfolder' => array(
1214 array(
1215 'fileInRoot' => ''
1216 ),
1217 '/fileInRoot',
1218 'newFile',
1219 '/newFile'
1220 )
1221 );
1222 }
1223
1224 /**
1225 * @test
1226 * @dataProvider renamingFiles_dataProvider
1227 */
1228 public function renamingFilesChangesFilenameOnDisk(array $filesystemStructure, $oldFileIdentifier, $newFileName, $expectedNewIdentifier) {
1229 $this->addToMount($filesystemStructure);
1230 $fixture = $this->createDriverFixture(array(
1231 'basePath' => $this->getMountRootUrl()
1232 ));
1233
1234 $file = $this->getSimpleFileMock($oldFileIdentifier);
1235
1236 $newIdentifier = $fixture->renameFile($file, $newFileName);
1237 $this->assertFalse($fixture->fileExists($oldFileIdentifier));
1238 $this->assertTrue($fixture->fileExists($newIdentifier));
1239 $this->assertEquals($expectedNewIdentifier, $newIdentifier);
1240 }
1241
1242 /**
1243 * @test
1244 */
1245 public function renamingFilesFailsIfTargetFileExists() {
1246 $this->setExpectedException('t3lib_file_exception_ExistingTargetFileNameException', '', 1320291063);
1247
1248 $this->addToMount(array(
1249 'targetFolder' => array('file' => '', 'newFile' => ''),
1250 ));
1251 $fixture = $this->createDriverFixture(array(
1252 'basePath' => $this->getMountRootUrl()
1253 ));
1254
1255 $file = $this->getSimpleFileMock('/targetFolder/file');
1256
1257 $fixture->renameFile($file, 'newFile');
1258 }
1259
1260 /**
1261 * We use this data provider for testing move methods because there are some issues with the
1262 *
1263 * @return array
1264 */
1265 public function renamingFolders_dataProvider() {
1266 return array(
1267 'folder in root folder' => array(
1268 array(
1269 'someFolder' => array(),
1270 ),
1271 '/someFolder/',
1272 'newFolder',
1273 '/newFolder/'
1274 ),
1275 'file in subfolder' => array(
1276 array(
1277 'subfolder' => array(
1278 'someFolder' => array()
1279 )
1280 ),
1281 '/subfolder/someFolder/',
1282 'newFolder',
1283 '/subfolder/newFolder/'
1284 )
1285 );
1286 }
1287
1288 /**
1289 * @test
1290 * @dataProvider renamingFolders_dataProvider
1291 */
1292 public function renamingFoldersChangesFolderNameOnDisk(array $filesystemStructure, $oldFolderIdentifier, $newFolderName, $expectedNewIdentifier) {
1293 $this->addToMount($filesystemStructure);
1294 $fixture = $this->createDriverFixture(array(
1295 'basePath' => $this->getMountRootUrl()
1296 ));
1297 $mockedFolder = $this->getSimpleFolderMock($oldFolderIdentifier);
1298
1299 $mapping = $fixture->renameFolder($mockedFolder, $newFolderName);
1300
1301 $this->assertFalse($fixture->folderExists($oldFolderIdentifier));
1302 $this->assertTrue($fixture->folderExists($expectedNewIdentifier));
1303 $this->assertEquals($expectedNewIdentifier, $mapping[$oldFolderIdentifier]);
1304 }
1305
1306 /**
1307 * @test
1308 */
1309 public function renameFolderReturnsCorrectMappingInformationForAllFiles() {
1310 $fileContents = 'asdfg';
1311 $this->addToMount(array(
1312 'sourceFolder' => array(
1313 'subFolder' => array('file' => $fileContents),
1314 'file' => 'asdfg'
1315 )
1316 ));
1317 $fixture = $this->createDriverFixture(array(
1318 'basePath' => $this->getMountRootUrl()
1319 ));
1320
1321 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1322
1323 $mappingInformation = $fixture->renameFolder($sourceFolder, 'newFolder');
1324
1325 $this->assertEquals('/newFolder/', $mappingInformation['/sourceFolder/']);
1326 $this->assertEquals('/newFolder/file', $mappingInformation['/sourceFolder/file']);
1327 $this->assertEquals('/newFolder/subFolder/file', $mappingInformation['/sourceFolder/subFolder/file']);
1328 $this->assertEquals('/newFolder/subFolder/', $mappingInformation['/sourceFolder/subFolder/']);
1329 }
1330
1331 /**
1332 * @test
1333 */
1334 public function renameFolderRevertsRenamingIfFilenameMapCannotBeCreated() {
1335 $this->setExpectedException('RuntimeException', '', 1334160746);
1336
1337 $this->addToMount(array(
1338 'sourceFolder' => array(
1339 'file' => 'asdfg'
1340 )
1341 ));
1342 $fixture = $this->createDriverFixture(array(
1343 'basePath' => $this->getMountRootUrl()
1344 ), NULL, array('createIdentifierMap'));
1345 $fixture->expects($this->atLeastOnce())->method('createIdentifierMap')
1346 ->will($this->throwException(new t3lib_file_exception_FileOperationErrorException()));
1347
1348 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1349
1350 $fixture->renameFolder($sourceFolder, 'newFolder');
1351
1352 $this->assertFileExists($this->getUrlInMount('/sourceFolder/file'));
1353 }
1354
1355 /**
1356 * @test
1357 */
1358 public function isFolderEmptyReturnsTrueForEmptyFolder() {
1359 // This also prepares the next few tests, so add more info than required for this test
1360 $this->addToMount(array(
1361 'emptyFolder' => array(),
1362 ));
1363 $fixture = $this->createDriverFixture(array(
1364 'basePath' => $this->getMountRootUrl()
1365 ));
1366 $mockedFolder = $this->getSimpleFolderMock('/emptyFolder/');
1367
1368 $this->assertTrue($fixture->isFolderEmpty($mockedFolder));
1369
1370 return $fixture;
1371 }
1372
1373 /**
1374 * @test
1375 */
1376 public function isFolderEmptyReturnsFalseIfFolderHasFile() {
1377 $this->addToMount(array(
1378 'folderWithFile' => array(
1379 'someFile' => ''
1380 ),
1381 ));
1382 $fixture = $this->createDriverFixture(array(
1383 'basePath' => $this->getMountRootUrl()
1384 ));
1385 $mockedFolder = $this->getSimpleFolderMock('/folderWithFile/');
1386
1387 $this->assertFalse($fixture->isFolderEmpty($mockedFolder));
1388 }
1389
1390 /**
1391 * @test
1392 */
1393 public function isFolderEmptyReturnsFalseIfFolderHasSubfolder() {
1394 $this->addToMount(array(
1395 'folderWithSubfolder' => array(
1396 'someFolder' => array()
1397 )
1398 ));
1399 $fixture = $this->createDriverFixture(array(
1400 'basePath' => $this->getMountRootUrl()
1401 ));
1402 $mockedFolder = $this->getSimpleFolderMock('/folderWithSubfolder/');
1403
1404 $this->assertFalse($fixture->isFolderEmpty($mockedFolder));
1405 }
1406
1407
1408 /**********************************
1409 * Copy/move folder
1410 **********************************/
1411
1412 /**
1413 * @test
1414 */
1415 public function foldersCanBeMovedWithinStorage() {
1416 /** @var t3lib_file_Driver_LocalDriver $fixture */
1417 list($mockedFolder, $sourceFolder, $fileContents, $fixture) = $this->_prepareFolderEnvironmentForMoveTest();
1418
1419 $fixture->moveFolderWithinStorage($sourceFolder, $mockedFolder, 'someFolder');
1420 $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/someFolder/')));
1421 $this->assertEquals($fileContents, file_get_contents($this->getUrlInMount('/targetFolder/someFolder/file')));
1422 $this->assertFileNotExists($this->getUrlInMount('/someFile'));
1423 }
1424
1425 /**
1426 * @test
1427 */
1428 public function moveFolderWithinStorageReturnsCorrectMappingInformationForAllFiles() {
1429 $fileContents = 'asdfg';
1430 $this->addToMount(array(
1431 'targetFolder' => array(),
1432 'sourceFolder' => array(
1433 'subFolder' => array('file' => $fileContents),
1434 'file' => 'asdfg'
1435 )
1436 ));
1437 $fixture = $this->createDriverFixture(array(
1438 'basePath' => $this->getMountRootUrl()
1439 ));
1440
1441 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1442 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
1443
1444 $mappingInformation = $fixture->moveFolderWithinStorage($sourceFolder, $targetFolder, 'sourceFolder');
1445
1446 $this->assertEquals('/targetFolder/sourceFolder/file', $mappingInformation['/sourceFolder/file']);
1447 $this->assertEquals('/targetFolder/sourceFolder/subFolder/file', $mappingInformation['/sourceFolder/subFolder/file']);
1448 $this->assertEquals('/targetFolder/sourceFolder/subFolder/', $mappingInformation['/sourceFolder/subFolder/']);
1449 }
1450
1451 /**
1452 * @test
1453 */
1454 public function folderCanBeRenamedWhenMoving() {
1455 /** @var t3lib_file_Driver_LocalDriver $fixture */
1456 list($mockedFolder, $sourceFolder, $fileContents, $fixture) = $this->_prepareFolderEnvironmentForMoveTest();
1457
1458 $fixture->moveFolderWithinStorage($sourceFolder, $mockedFolder, 'newFolder');
1459 $this->assertTrue(file_exists($this->getUrlInMount('/targetFolder/newFolder/')));
1460 }
1461
1462
1463 protected function _setupFolderForCopyTest() {
1464 $fileContents1 = uniqid();
1465 $fileContents2 = uniqid();
1466 $this->addToMount(array(
1467 'targetFolder' => array(),
1468 'sourceFolder' => array(
1469 'subFolder' => array('file' => $fileContents1),
1470 'file' => $fileContents2
1471 )
1472 ));
1473 $fixture = $this->createDriverFixture(array(
1474 'basePath' => $this->getMountRootUrl()
1475 ));
1476 return $fixture;
1477 }
1478
1479 /**
1480 * @test
1481 * @see _setupFolderForCopyTest
1482 */
1483 public function foldersCanBeCopiedWithinSameStorage() {
1484 $fixture = $this->_setupFolderForCopyTest();
1485
1486 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1487 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
1488
1489 $fixture->copyFolderWithinStorage($sourceFolder, $targetFolder, 'sourceFolder');
1490
1491 $this->assertTrue($fixture->folderExists('/targetFolder/sourceFolder/'));
1492 $this->assertTrue($fixture->fileExists('/targetFolder/sourceFolder/file'));
1493 $this->assertTrue($fixture->folderExists('/targetFolder/sourceFolder/subFolder/'));
1494 $this->assertTrue($fixture->fileExists('/targetFolder/sourceFolder/subFolder/file'));
1495 }
1496
1497 /**
1498 * @test
1499 * @see _setupFolderForCopyTest
1500 */
1501 public function folderNameCanBeChangedWhileCopying() {
1502 $fixture = $this->_setupFolderForCopyTest();
1503
1504 $sourceFolder = $this->getSimpleFolderMock('/sourceFolder/');
1505 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
1506
1507 $fixture->copyFolderWithinStorage($sourceFolder, $targetFolder, 'newFolder');
1508
1509 $this->assertTrue($fixture->folderExists('/targetFolder/newFolder/'));
1510 $this->assertTrue($fixture->fileExists('/targetFolder/newFolder/file'));
1511 $this->assertFalse($fixture->folderExists('/targetFolder/sourceFolder/'));
1512 }
1513 }
1514 ?>