[BUGFIX] File identifiers are case-insensitive
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ResourceStorageTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 use TYPO3\CMS\Core\Resource\ResourceStorage;
5
6 /***************************************************************
7 * Copyright notice
8 *
9 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
10 * All rights reserved
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
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 * Testcase for the VFS mount class
31 *
32 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
33 */
34 class ResourceStorageTest extends \TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase {
35
36 /**
37 * @var array A backup of registered singleton instances
38 */
39 protected $singletonInstances = array();
40
41 /**
42 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
43 */
44 private $fixture;
45
46 public function setUp() {
47 parent::setUp();
48 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
49 \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
50 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(
51 'TYPO3\\CMS\\Core\\Resource\\FileRepository',
52 $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository')
53 );
54
55 }
56
57 public function tearDown() {
58 parent::tearDown();
59 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
60 }
61
62 /**
63 * Prepare fixture
64 *
65 * @param array $configuration
66 * @param boolean $mockPermissionChecks
67 * @return void
68 */
69 protected function prepareFixture($configuration, $mockPermissionChecks = FALSE, $driverObject = NULL, array $storageRecord = array()) {
70 $permissionMethods = array('assureFileAddPermissions', 'checkFolderActionPermission', 'checkFileActionPermission', 'checkUserActionPermission', 'checkFileExtensionPermission', 'isWithinFileMountBoundaries');
71 $mockedMethods = NULL;
72 $configuration = $this->convertConfigurationArrayToFlexformXml($configuration);
73 $storageRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($storageRecord, array(
74 'configuration' => $configuration
75 ));
76 if ($driverObject == NULL) {
77 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\AbstractDriver */
78 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
79 }
80 if ($mockPermissionChecks) {
81 $mockedMethods = $permissionMethods;
82 }
83 if ($mockedMethods === NULL) {
84 $this->fixture = new \TYPO3\CMS\Core\Resource\ResourceStorage($driverObject, $storageRecord);
85 } else {
86 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', $mockedMethods, array($driverObject, $storageRecord));
87 foreach ($permissionMethods as $method) {
88 $this->fixture->expects($this->any())->method($method)->will($this->returnValue(TRUE));
89 }
90 }
91 }
92
93 /**
94 * Converts a simple configuration array into a FlexForm data structure serialized as XML
95 *
96 * @param array $configuration
97 * @return string
98 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml()
99 */
100 protected function convertConfigurationArrayToFlexformXml(array $configuration) {
101 $flexformArray = array('data' => array('sDEF' => array('lDEF' => array())));
102 foreach ($configuration as $key => $value) {
103 $flexformArray['data']['sDEF']['lDEF'][$key] = array('vDEF' => $value);
104 }
105 $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml($flexformArray);
106 return $configuration;
107 }
108
109 /**
110 * Creates a driver fixture object, optionally using a given mount object.
111 *
112 * IMPORTANT: Call this only after setting up the virtual file system (with the addTo* methods)!
113 *
114 * @param $driverConfiguration
115 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject
116 * @param array $mockedDriverMethods
117 * @return \TYPO3\CMS\Core\Resource\Driver\LocalDriver
118 */
119 protected function createDriverMock($driverConfiguration, \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject = NULL, $mockedDriverMethods = array()) {
120 $this->initializeVfs();
121 if ($storageObject == NULL) {
122 $storageObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
123 }
124
125 if (!isset($driverConfiguration['basePath'])) {
126 $driverConfiguration['basePath'] = $this->getMountRootUrl();
127 }
128
129 if ($mockedDriverMethods === NULL) {
130 $driver = new \TYPO3\CMS\Core\Resource\Driver\LocalDriver($driverConfiguration);
131 } else {
132 // We are using the LocalDriver here because PHPUnit can't mock concrete methods in abstract classes, so
133 // when using the AbstractDriver we would be in trouble when wanting to mock away some concrete method
134 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', $mockedDriverMethods, array($driverConfiguration));
135 }
136 $storageObject->setDriver($driver);
137 $driver->setStorage($storageObject);
138 $driver->processConfiguration();
139 $driver->initialize();
140 return $driver;
141 }
142
143 /**
144 * @test
145 */
146 public function baseUriGetsSlashAppended() {
147 $uri = 'http://example.org/somewhere/else';
148 $this->prepareFixture(array('baseUri' => $uri));
149 $this->assertEquals($uri . '/', $this->fixture->getBaseUri());
150 }
151
152 /**
153 * @return array
154 */
155 public function capabilitiesDataProvider() {
156 return array(
157 'only public' => array(
158 array(
159 'public' => TRUE,
160 'writable' => FALSE,
161 'browsable' => FALSE
162 )
163 ),
164 'only writable' => array(
165 array(
166 'public' => FALSE,
167 'writable' => TRUE,
168 'browsable' => FALSE
169 )
170 ),
171 'only browsable' => array(
172 array(
173 'public' => FALSE,
174 'writable' => FALSE,
175 'browsable' => TRUE
176 )
177 ),
178 'all capabilities' => array(
179 array(
180 'public' => TRUE,
181 'writable' => TRUE,
182 'browsable' => TRUE
183 )
184 ),
185 'none' => array(
186 array(
187 'public' => FALSE,
188 'writable' => FALSE,
189 'browsable' => FALSE
190 )
191 )
192 );
193 }
194
195 /**
196 * @test
197 * @dataProvider capabilitiesDataProvider
198 */
199 public function capabilitiesOfStorageObjectAreCorrectlySet(array $capabilites) {
200 $storageRecord = array(
201 'is_public' => $capabilites['public'],
202 'is_writable' => $capabilites['writable'],
203 'is_browsable' => $capabilites['browsable'],
204 'is_online' => TRUE
205 );
206 $mockedDriver = $this->createDriverMock(array(), $this->fixture, array('hasCapability'));
207 $mockedDriver->expects($this->any())->method('hasCapability')->will($this->returnValue(TRUE));
208 $this->prepareFixture(array(), FALSE, $mockedDriver, $storageRecord);
209 $this->assertEquals($capabilites['public'], $this->fixture->isPublic(), 'Capability "public" is not correctly set.');
210 $this->assertEquals($capabilites['writable'], $this->fixture->isWritable(), 'Capability "writable" is not correctly set.');
211 $this->assertEquals($capabilites['browsable'], $this->fixture->isBrowsable(), 'Capability "browsable" is not correctly set.');
212 }
213
214 /**
215 * @test
216 */
217 public function fileAndFolderListFiltersAreInitializedWithDefaultFilters() {
218 $this->prepareFixture(array());
219 $this->assertEquals($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'], $this->fixture->getFileAndFolderNameFilters());
220 }
221
222 /**
223 * @test
224 */
225 public function addFileFailsIfFileDoesNotExist() {
226 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
227 $this->setExpectedException('InvalidArgumentException', '', 1319552745);
228 $this->prepareFixture(array());
229 $this->fixture->addFile('/some/random/file', $mockedFolder);
230 }
231
232 /**
233 * @test
234 */
235 public function addFileCallsDriverWithCorrectArguments() {
236 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
237 $this->addToMount(array(
238 'targetFolder' => array(),
239 'file.ext' => 'ajslkd'
240 ));
241 $this->initializeVfs();
242 $localFilePath = $this->getUrlInMount('file.ext');
243 $this->prepareFixture(array(), TRUE);
244 /** @var $file \TYPO3\CMS\Core\Resource\FileInterface */
245 $file = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileInterface', array(), array(), '', FALSE);
246 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
247 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
248 $driver->expects($this->once())->method('addFile')->with($this->equalTo($localFilePath), $this->anything(), $this->equalTo('file.ext'))->will($this->returnValue($file));
249 $this->fixture->setDriver($driver);
250 $this->fixture->addFile($localFilePath, $mockedFolder);
251 }
252
253 /**
254 * @test
255 */
256 public function addFileChangesFilenameIfFileExists() {
257 $mockedFolder = $this->getSimpleFolderMock('/');
258 $this->addToMount(array(
259 'targetFolder' => array(
260 'file.ext' => 'asdf',
261 'file_01.ext' => 'asjdlkajs'
262 ),
263 'file.ext' => 'ajslkd'
264 ));
265 $this->initializeVfs();
266 $this->prepareFixture(array(), TRUE);
267 /** @var $file \TYPO3\CMS\Core\Resource\FileInterface */
268 $file = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileInterface', array(), array(), '', FALSE);
269 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
270 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile', 'fileExistsInFolder'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
271 $driver->expects($this->once())->method('addFile')->with($this->anything(), $this->anything(), $this->equalTo('file_02.ext'))->will($this->returnValue($file));
272 $driver->expects($this->exactly(3))->method('fileExistsInFolder')->will($this->onConsecutiveCalls($this->returnValue(TRUE), $this->returnValue(TRUE), $this->returnValue(FALSE)));
273 $this->fixture->setDriver($driver);
274 $this->fixture->addFile($this->getUrlInMount('file.ext'), $mockedFolder);
275 }
276
277 /**
278 * @test
279 */
280 public function getPublicUrlReturnsNullIfStorageIsNotOnline() {
281 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
282 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
283 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
284 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isOnline'), array($driver, array('configuration' => array())));
285 $fixture->expects($this->once())->method('isOnline')->will($this->returnValue(FALSE));
286
287 $sourceFileIdentifier = '/sourceFile.ext';
288 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
289 $result = $fixture->getPublicUrl($sourceFile);
290 $this->assertSame($result, NULL);
291 }
292
293 /**
294 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
295 *
296 * @return array
297 */
298 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
299 return array(
300 'read action on readable/writable folder' => array(
301 'read',
302 array('r' => TRUE, 'w' => TRUE),
303 TRUE
304 ),
305 'read action on unreadable folder' => array(
306 'read',
307 array('r' => FALSE, 'w' => TRUE),
308 FALSE
309 ),
310 'write action on read-only folder' => array(
311 'write',
312 array('r' => TRUE, 'w' => FALSE),
313 FALSE
314 )
315 );
316 }
317
318 /**
319 * @test
320 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
321 * @param string $action 'read' or 'write'
322 * @param array $permissionsFromDriver The permissions as returned from the driver
323 * @param boolean $expectedResult
324 */
325 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedResult) {
326 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
327 $mockedDriver->expects($this->any())->method('getFolderPermissions')->will($this->returnValue($permissionsFromDriver));
328 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
329 // Let all other checks pass
330 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
331 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
332 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
333 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
334 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
335 $fixture->setDriver($mockedDriver);
336
337 $this->assertSame($expectedResult, $fixture->checkFolderActionPermission($action, $mockedFolder));
338 }
339
340 /**
341 * @test
342 */
343 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
344 $this->prepareFixture(array());
345 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
346 }
347
348 /**
349 * @test
350 */
351 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
352 $this->prepareFixture(array());
353 $this->fixture->setUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
354 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
355 }
356
357 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
358 return array(
359 'all lower cased' => array(
360 array('readFolder' => TRUE),
361 'read',
362 'folder'
363 ),
364 'all upper case' => array(
365 array('readFolder' => TRUE),
366 'READ',
367 'FOLDER'
368 ),
369 'mixed case' => array(
370 array('readFolder' => TRUE),
371 'ReaD',
372 'FoLdEr'
373 )
374 );
375 }
376
377 /**
378 * @test
379 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
380 */
381 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
382 $this->prepareFixture(array());
383 $this->fixture->setUserPermissions($permissions);
384 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
385 }
386
387 /**
388 * @test
389 */
390 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
391 $this->prepareFixture(array());
392 $this->fixture->setEvaluatePermissions(TRUE);
393 $this->fixture->setUserPermissions(array('readFolder' => FALSE));
394 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
395 }
396
397 /**
398 * @test
399 */
400 public function userActionIsDisallowedIfPermissionIsNotSet() {
401 $this->prepareFixture(array());
402 $this->fixture->setEvaluatePermissions(TRUE);
403 $this->fixture->setUserPermissions(array('readFolder' => TRUE));
404 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
405 }
406
407 /**
408 * @test
409 * @group integration
410 */
411 public function setFileContentsUpdatesObjectProperties() {
412 $this->initializeVfs();
413 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
414 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFileIndexRepository', 'checkFileActionPermission'), array($driverObject, array()));
415 $this->fixture->expects($this->any())->method('checkFileActionPermission')->will($this->returnValue(TRUE));
416 $fileInfo = array(
417 'storage' => 'A',
418 'identifier' => 'B',
419 'mtime' => 'C',
420 'ctime' => 'D',
421 'mimetype' => 'E',
422 'size' => 'F',
423 'name' => 'G',
424 );
425 $newProperties = array(
426 'storage' => $fileInfo['storage'],
427 'identifier' => $fileInfo['identifier'],
428 'tstamp' => $fileInfo['mtime'],
429 'crdate' => $fileInfo['ctime'],
430 'mime_type' => $fileInfo['mimetype'],
431 'size' => $fileInfo['size'],
432 'name' => $fileInfo['name']
433 );
434 $hash = 'asdfg';
435 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
436 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfo));
437 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
438 $this->fixture->setDriver($driver);
439 $indexFileRepositoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
440 $this->fixture->expects($this->any())->method('getFileIndexRepository')->will($this->returnValue($indexFileRepositoryMock));
441 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
442 $mockedFile->expects($this->at(1))->method('updateProperties')->with($this->equalTo(array('sha1' => $hash)));
443 $mockedFile->expects($this->at(3))->method('updateProperties')->with($this->equalTo($newProperties));
444 $indexFileRepositoryMock->expects($this->once())->method('update')->with($mockedFile);
445 $this->fixture->setFileContents($mockedFile, uniqid());
446 }
447
448 /**
449 * @test
450 * @group integration
451 */
452 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
453 $localFilePath = '/path/to/localFile';
454 $sourceFileIdentifier = '/sourceFile.ext';
455 $this->addToMount(array(
456 'targetFolder' => array()
457 ));
458 $this->initializeVfs();
459 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
460 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
461 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
462 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
463 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
464 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
465 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
466 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
467 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
468 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
469 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
470 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
471 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
472 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
473 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
474 }
475
476 /**
477 * @test
478 * @group integration
479 */
480 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
481 $localFilePath = '/path/to/localFile';
482 $sourceFileIdentifier = '/sourceFile.ext';
483 $this->addToMount(array(
484 'targetFolder' => array()
485 ));
486 $this->initializeVfs();
487 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
488 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
489 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
490 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
491 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
492 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
493 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
494 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
495 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
496 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
497 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
498 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
499 }
500
501 /**
502 * @test
503 * @group integration
504 */
505 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
506 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
507 $this->addToMount(array(
508 'mountFolder' => array(
509 'file' => 'asdfg'
510 )
511 ));
512 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
513 $this->initializeVfs();
514 $this->prepareFixture(array(), NULL, $mockedDriver);
515 $this->fixture->addFileMount('/mountFolder');
516 $this->assertEquals(1, count($this->fixture->getFileMounts()));
517 $this->fixture->isWithinFileMountBoundaries($mockedFile);
518 }
519
520 /**
521 * This test is also valid for folders
522 *
523 * @test
524 */
525 public function getFileListReturnsFilesInCorrectOrder() {
526 $fileList = array(
527 'file10' => '',
528 'file2' => '',
529 'File' => '',
530 'fail' => ''
531 );
532 $this->prepareFixture(array(), TRUE);
533 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
534 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
535 $fileList = $this->fixture->getFileList('/');
536 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
537 }
538
539 /**
540 * @test
541 */
542 public function getFileListIgnoresCasingWhenSortingFilenames() {
543 $fileList = array(
544 'aFile' => 'dfsdg',
545 'zFile' => 'werw',
546 'BFile' => 'asd',
547 '12345' => 'fdsa',
548 'IMG_1234.jpg' => 'asdf'
549 );
550 $this->prepareFixture(array(), TRUE);
551 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
552 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
553 $fileList = $this->fixture->getFileList('/');
554 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
555 }
556
557 /**
558 * @test
559 */
560 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
561 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
562 $mockedDriver = $this->createDriverMock(array());
563 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
564 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
565 $this->prepareFixture(array(), TRUE);
566 $this->fixture->setDriver($mockedDriver);
567 $this->fixture->createFolder('newFolder', $mockedParentFolder);
568 }
569
570 /**
571 * @test
572 * @expectedException \RuntimeException
573 */
574 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
575 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
576 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
577 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
578 $driverMock = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver');
579 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
580 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
581 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
582 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
583 $fixture->_set('driver', $driverMock);
584 $fixture->deleteFolder($folderMock, FALSE);
585 }
586
587 /**
588 * @test
589 */
590 public function createFolderCallsDriverForFolderCreation() {
591 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
592 $this->prepareFixture(array(), TRUE);
593 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
594 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
595 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
596 $this->fixture->createFolder('newFolder', $mockedParentFolder);
597 }
598
599 /**
600 * @test
601 */
602 public function createFolderCanRecursivelyCreateFolders() {
603 $this->addToMount(array('someFolder' => array()));
604 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
605 $this->prepareFixture(array(), TRUE, $mockedDriver);
606 $parentFolder = $this->fixture->getFolder('/someFolder/');
607 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
608 $this->assertEquals('secondSubfolder', $newFolder->getName());
609 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
610 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
611 }
612
613 /**
614 * @test
615 */
616 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
617 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
618 $this->prepareFixture(array(), TRUE);
619 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
620 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
621 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
622 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
623 }
624
625 /**
626 * @test
627 */
628 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
629 $mockedRootFolder = $this->getSimpleFolderMock('/');
630 $this->prepareFixture(array(), TRUE);
631 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
632 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
633 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
634 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
635 $this->fixture->createFolder('someFolder');
636 }
637
638 /**
639 * @test
640 */
641 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
642 $this->addToMount(array(
643 'existingFolder' => array()
644 ));
645 $this->initializeVfs();
646 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
647 $this->prepareFixture(array(), TRUE, $mockedDriver);
648 $rootFolder = $this->fixture->getFolder('/');
649 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
650 $this->assertEquals('someFolder', $newFolder->getName());
651 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
652 }
653
654 /**
655 * @test
656 */
657 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
658 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
659 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
660 $this->prepareFixture(array(), TRUE);
661 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
662 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
663 $this->fixture->createFolder('newFolder', $mockedParentFolder);
664 }
665
666 /**
667 * @test
668 */
669 public function replaceFileFailsIfLocalFileDoesNotExist() {
670 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
671 $this->prepareFixture(array(), TRUE);
672 $mockedFile = $this->getSimpleFileMock('/someFile');
673 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
674 }
675
676 /**
677 * @test
678 */
679 public function getFileListHandsOverRecursiveFALSEifNotExplicitlySet() {
680 $this->prepareFixture(array(), TRUE);
681 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
682 $driver->expects($this->once())
683 ->method('getFileList')
684 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
685 ->will($this->returnValue(array()));
686 $this->fixture->getFileList('/');
687 }
688
689 /**
690 * @test
691 */
692 public function getFileListHandsOverRecursiveTRUEifSet() {
693
694 $this->prepareFixture(array(), TRUE);
695 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
696 $driver->expects($this->once())
697 ->method('getFileList')
698 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
699 ->will($this->returnValue(array()));
700 $this->fixture->getFileList('/', 0, 0, TRUE, TRUE, TRUE);
701 }
702
703 /**
704 * @test
705 */
706 public function getRoleReturnsDefaultForRegularFolders() {
707 $folderIdentifier = uniqid();
708 $this->addToMount(array(
709 $folderIdentifier => array()
710 ));
711 $this->prepareFixture(array());
712
713 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
714
715 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
716 }
717
718 /**
719 * @test
720 */
721 public function getRoleReturnsCorrectValueForDefaultProcessingFolder() {
722 $this->prepareFixture(array());
723
724 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . ResourceStorage::DEFAULT_ProcessingFolder . '/'));
725
726 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
727 }
728
729 /**
730 * @test
731 */
732 public function getRoleReturnsCorrectValueForConfiguredProcessingFolder() {
733 $folderIdentifier = uniqid();
734 $this->addToMount(array(
735 $folderIdentifier => array()
736 ));
737 $this->prepareFixture(array(), FALSE, NULL, array('processingfolder' => '/' . $folderIdentifier . '/'));
738
739 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
740
741 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
742 }
743
744 /**
745 * Data provider for fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder function
746 */
747 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider() {
748 return array(
749 'Empty folderList returned' => array(
750 'path' => '/',
751 'processingFolder' => '_processed_',
752 'folderList' => array(),
753 'expectedItems' => array()
754 ),
755 'Empty _processed_ folder' => array(
756 'path' => '/',
757 'processingFolder' => '',
758 'folderList' => array(
759 '_processed_' => array(),
760 '_temp_' => array(),
761 'user_upload' => array()
762 ),
763 'expectedItems' => array(
764 'user_upload' => array(),
765 '_temp_' => array()
766 )
767 ),
768 '_processed_ folder not in folder list' => array(
769 'path' => '/',
770 'processingFolder' => '_processed_',
771 'folderList' => array(
772 '_temp_' => array()
773 ),
774 'expectedItems' => array(
775 '_temp_' => array()
776 )
777 ),
778 '_processed_ folder on root level' => array(
779 'path' => '/',
780 'processingFolder' => '_processed_',
781 'folderList' => array(
782 '_processed_' => array(),
783 '_temp_' => array(),
784 'user_upload' => array()
785 ),
786 'expectedItems' => array(
787 'user_upload' => array(),
788 '_temp_' => array()
789 )
790 ),
791 '_processed_ folder on second level' => array(
792 'path' => 'Public/',
793 'processingFolder' => 'Public/_processed_',
794 'folderList' => array(
795 '_processed_' => array(),
796 '_temp_' => array(),
797 'user_upload' => array()
798 ),
799 'expectedItems' => array(
800 'user_upload' => array(),
801 '_temp_' => array()
802 )
803 ),
804 '_processed_ folder on third level' => array(
805 'path' => 'Public/Files/',
806 'processingFolder' => 'Public/Files/_processed_',
807 'folderList' => array(
808 '_processed_' => array(),
809 '_temp_' => array(),
810 'user_upload' => array()
811 ),
812 'expectedItems' => array(
813 'user_upload' => array(),
814 '_temp_' => array()
815 )
816 )
817 );
818 }
819
820 /**
821 * @test
822 * @dataProvider fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider
823 */
824 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder($path, $processingFolder, $folderList, $expectedItems) {
825 $driverMock = $this->createDriverMock(array(), NULL, array('getFolderList', 'folderExists'));
826 $driverMock->expects($this->once())->method('getFolderList')->will($this->returnValue($folderList));
827 $driverMock->expects($this->any())->method('folderExists')->will($this->returnValue(TRUE));
828
829 $this->prepareFixture(array(), TRUE, $driverMock, array('processingfolder' => $processingFolder));
830
831 $this->assertSame($expectedItems, $this->fixture->fetchFolderListFromDriver($path));
832 }
833 }