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