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