[TASK] Remove closing PHP tags
[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 $this->prepareFixture(array(), TRUE);
414 $fileInfo = array(
415 'storage' => 'A',
416 'identifier' => 'B',
417 'mtime' => 'C',
418 'ctime' => 'D',
419 'mimetype' => 'E',
420 'size' => 'F',
421 'name' => 'G',
422 );
423 $newProperties = array(
424 'storage' => $fileInfo['storage'],
425 'identifier' => $fileInfo['identifier'],
426 'tstamp' => $fileInfo['mtime'],
427 'crdate' => $fileInfo['ctime'],
428 'mime_type' => $fileInfo['mimetype'],
429 'size' => $fileInfo['size'],
430 'name' => $fileInfo['name']
431 );
432 $hash = 'asdfg';
433 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
434 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfo));
435 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
436 $this->fixture->setDriver($driver);
437 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
438 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
439 $mockedFile->expects($this->at(1))->method('updateProperties')->with($this->equalTo(array('sha1' => $hash)));
440 $mockedFile->expects($this->at(3))->method('updateProperties')->with($this->equalTo($newProperties));
441 $this->fixture->setFileContents($mockedFile, uniqid());
442 }
443
444 /**
445 * @test
446 * @group integration
447 */
448 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
449 $localFilePath = '/path/to/localFile';
450 $sourceFileIdentifier = '/sourceFile.ext';
451 $this->addToMount(array(
452 'targetFolder' => array()
453 ));
454 $this->initializeVfs();
455 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
456 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
457 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
458 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
459 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
460 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
461 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
462 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
463 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
464 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
465 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
466 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
467 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
468 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
469 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
470 }
471
472 /**
473 * @test
474 * @group integration
475 */
476 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
477 $localFilePath = '/path/to/localFile';
478 $sourceFileIdentifier = '/sourceFile.ext';
479 $this->addToMount(array(
480 'targetFolder' => array()
481 ));
482 $this->initializeVfs();
483 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
484 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
485 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
486 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
487 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
488 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
489 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
490 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
491 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
492 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
493 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
494 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
495 }
496
497 /**
498 * @test
499 * @group integration
500 */
501 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
502 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
503 $this->addToMount(array(
504 'mountFolder' => array(
505 'file' => 'asdfg'
506 )
507 ));
508 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
509 $this->initializeVfs();
510 $this->prepareFixture(array(), NULL, $mockedDriver);
511 $this->fixture->addFileMount('/mountFolder');
512 $this->assertEquals(1, count($this->fixture->getFileMounts()));
513 $this->fixture->isWithinFileMountBoundaries($mockedFile);
514 }
515
516 /**
517 * This test is also valid for folders
518 *
519 * @test
520 */
521 public function getFileListReturnsFilesInCorrectOrder() {
522 $fileList = array(
523 'file10' => '',
524 'file2' => '',
525 'File' => '',
526 'fail' => ''
527 );
528 $this->prepareFixture(array(), TRUE);
529 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
530 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
531 $fileList = $this->fixture->getFileList('/');
532 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
533 }
534
535 /**
536 * @test
537 */
538 public function getFileListIgnoresCasingWhenSortingFilenames() {
539 $fileList = array(
540 'aFile' => 'dfsdg',
541 'zFile' => 'werw',
542 'BFile' => 'asd',
543 '12345' => 'fdsa',
544 'IMG_1234.jpg' => 'asdf'
545 );
546 $this->prepareFixture(array(), TRUE);
547 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
548 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
549 $fileList = $this->fixture->getFileList('/');
550 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
551 }
552
553 /**
554 * @test
555 */
556 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
557 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
558 $mockedDriver = $this->createDriverMock(array());
559 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
560 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
561 $this->prepareFixture(array(), TRUE);
562 $this->fixture->setDriver($mockedDriver);
563 $this->fixture->createFolder('newFolder', $mockedParentFolder);
564 }
565
566 /**
567 * @test
568 * @expectedException \RuntimeException
569 */
570 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
571 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
572 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
573 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
574 $driverMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), array(), '', FALSE);
575 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
576 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
577 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
578 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
579 $fixture->_set('driver', $driverMock);
580 $fixture->deleteFolder($folderMock, FALSE);
581 }
582
583 /**
584 * @test
585 */
586 public function createFolderCallsDriverForFolderCreation() {
587 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
588 $this->prepareFixture(array(), TRUE);
589 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
590 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
591 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
592 $this->fixture->createFolder('newFolder', $mockedParentFolder);
593 }
594
595 /**
596 * @test
597 */
598 public function createFolderCanRecursivelyCreateFolders() {
599 $this->addToMount(array('someFolder' => array()));
600 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
601 $this->prepareFixture(array(), TRUE, $mockedDriver);
602 $parentFolder = $this->fixture->getFolder('/someFolder/');
603 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
604 $this->assertEquals('secondSubfolder', $newFolder->getName());
605 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
606 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
607 }
608
609 /**
610 * @test
611 */
612 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
613 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
614 $this->prepareFixture(array(), TRUE);
615 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
616 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
617 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
618 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
619 }
620
621 /**
622 * @test
623 */
624 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
625 $mockedRootFolder = $this->getSimpleFolderMock('/');
626 $this->prepareFixture(array(), TRUE);
627 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
628 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
629 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
630 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
631 $this->fixture->createFolder('someFolder');
632 }
633
634 /**
635 * @test
636 */
637 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
638 $this->addToMount(array(
639 'existingFolder' => array()
640 ));
641 $this->initializeVfs();
642 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
643 $this->prepareFixture(array(), TRUE, $mockedDriver);
644 $rootFolder = $this->fixture->getFolder('/');
645 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
646 $this->assertEquals('someFolder', $newFolder->getName());
647 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
648 }
649
650 /**
651 * @test
652 */
653 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
654 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
655 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
656 $this->prepareFixture(array(), TRUE);
657 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
658 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
659 $this->fixture->createFolder('newFolder', $mockedParentFolder);
660 }
661
662 /**
663 * @test
664 */
665 public function replaceFileFailsIfLocalFileDoesNotExist() {
666 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
667 $this->prepareFixture(array(), TRUE);
668 $mockedFile = $this->getSimpleFileMock('/someFile');
669 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
670 }
671
672 /**
673 * @test
674 */
675 public function getFileListHandsOverRecursiveFALSEifNotExplicitlySet() {
676 $this->prepareFixture(array(), TRUE);
677 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
678 $driver->expects($this->once())
679 ->method('getFileList')
680 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
681 ->will($this->returnValue(array()));
682 $this->fixture->getFileList('/');
683 }
684
685 /**
686 * @test
687 */
688 public function getFileListHandsOverRecursiveTRUEifSet() {
689
690 $this->prepareFixture(array(), TRUE);
691 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
692 $driver->expects($this->once())
693 ->method('getFileList')
694 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
695 ->will($this->returnValue(array()));
696 $this->fixture->getFileList('/', 0, 0, TRUE, TRUE, TRUE);
697 }
698
699 /**
700 * @test
701 */
702 public function getRoleReturnsDefaultForRegularFolders() {
703 $folderIdentifier = uniqid();
704 $this->addToMount(array(
705 $folderIdentifier => array()
706 ));
707 $this->prepareFixture(array());
708
709 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
710
711 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
712 }
713
714 /**
715 * @test
716 */
717 public function getRoleReturnsCorrectValueForDefaultProcessingFolder() {
718 $this->prepareFixture(array());
719
720 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . ResourceStorage::DEFAULT_ProcessingFolder . '/'));
721
722 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
723 }
724
725 /**
726 * @test
727 */
728 public function getRoleReturnsCorrectValueForConfiguredProcessingFolder() {
729 $folderIdentifier = uniqid();
730 $this->addToMount(array(
731 $folderIdentifier => array()
732 ));
733 $this->prepareFixture(array(), FALSE, NULL, array('processingfolder' => '/' . $folderIdentifier . '/'));
734
735 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
736
737 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
738 }
739
740 /**
741 * Data provider for fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder function
742 */
743 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider() {
744 return array(
745 'Empty folderList returned' => array(
746 'path' => '/',
747 'processingFolder' => '_processed_',
748 'folderList' => array(),
749 'expectedItems' => array()
750 ),
751 'Empty _processed_ folder' => array(
752 'path' => '/',
753 'processingFolder' => '',
754 'folderList' => array(
755 '_processed_' => array(),
756 '_temp_' => array(),
757 'user_upload' => array()
758 ),
759 'expectedItems' => array(
760 'user_upload' => array(),
761 '_temp_' => array()
762 )
763 ),
764 '_processed_ folder not in folder list' => array(
765 'path' => '/',
766 'processingFolder' => '_processed_',
767 'folderList' => array(
768 '_temp_' => array()
769 ),
770 'expectedItems' => array(
771 '_temp_' => array()
772 )
773 ),
774 '_processed_ folder on root level' => array(
775 'path' => '/',
776 'processingFolder' => '_processed_',
777 'folderList' => array(
778 '_processed_' => array(),
779 '_temp_' => array(),
780 'user_upload' => array()
781 ),
782 'expectedItems' => array(
783 'user_upload' => array(),
784 '_temp_' => array()
785 )
786 ),
787 '_processed_ folder on second level' => array(
788 'path' => 'Public/',
789 'processingFolder' => 'Public/_processed_',
790 'folderList' => array(
791 '_processed_' => array(),
792 '_temp_' => array(),
793 'user_upload' => array()
794 ),
795 'expectedItems' => array(
796 'user_upload' => array(),
797 '_temp_' => array()
798 )
799 ),
800 '_processed_ folder on third level' => array(
801 'path' => 'Public/Files/',
802 'processingFolder' => 'Public/Files/_processed_',
803 'folderList' => array(
804 '_processed_' => array(),
805 '_temp_' => array(),
806 'user_upload' => array()
807 ),
808 'expectedItems' => array(
809 'user_upload' => array(),
810 '_temp_' => array()
811 )
812 )
813 );
814 }
815
816 /**
817 * @test
818 * @dataProvider fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider
819 */
820 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder($path, $processingFolder, $folderList, $expectedItems) {
821 $driverMock = $this->createDriverMock(array(), NULL, array('getFolderList', 'folderExists'));
822 $driverMock->expects($this->once())->method('getFolderList')->will($this->returnValue($folderList));
823 $driverMock->expects($this->any())->method('folderExists')->will($this->returnValue(TRUE));
824
825 $this->prepareFixture(array(), TRUE, $driverMock, array('processingfolder' => $processingFolder));
826
827 $this->assertSame($expectedItems, $this->fixture->fetchFolderListFromDriver($path));
828 }
829 }