[BUGFIX] Follow-up: Fix fileinfo property mapping in ResourceStorage
[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 $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
245 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
246 $driver->expects($this->once())->method('addFile')->with($this->equalTo($localFilePath), $this->anything(), $this->equalTo('file.ext'));
247 $this->fixture->setDriver($driver);
248 $this->fixture->addFile($localFilePath, $mockedFolder);
249 }
250
251 /**
252 * @test
253 */
254 public function addFileChangesFilenameIfFileExists() {
255 $mockedFolder = $this->getSimpleFolderMock('/');
256 $this->addToMount(array(
257 'targetFolder' => array(
258 'file.ext' => 'asdf',
259 'file_01.ext' => 'asjdlkajs'
260 ),
261 'file.ext' => 'ajslkd'
262 ));
263 $this->initializeVfs();
264 $this->prepareFixture(array(), TRUE);
265 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
266 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile', 'fileExistsInFolder'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
267 $driver->expects($this->once())->method('addFile')->with($this->anything(), $this->anything(), $this->equalTo('file_02.ext'));
268 $driver->expects($this->exactly(3))->method('fileExistsInFolder')->will($this->onConsecutiveCalls($this->returnValue(TRUE), $this->returnValue(TRUE), $this->returnValue(FALSE)));
269 $this->fixture->setDriver($driver);
270 $this->fixture->addFile($this->getUrlInMount('file.ext'), $mockedFolder);
271 }
272
273 /**
274 * @test
275 */
276 public function getPublicUrlReturnsNullIfStorageIsNotOnline() {
277 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
278 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
279 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
280 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isOnline'), array($driver, array('configuration' => array())));
281 $fixture->expects($this->once())->method('isOnline')->will($this->returnValue(FALSE));
282
283 $sourceFileIdentifier = '/sourceFile.ext';
284 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
285 $result = $fixture->getPublicUrl($sourceFile);
286 $this->assertSame($result, NULL);
287 }
288
289 /**
290 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
291 *
292 * @return array
293 */
294 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
295 return array(
296 'read action on readable/writable folder' => array(
297 'read',
298 array('r' => TRUE, 'w' => TRUE),
299 TRUE
300 ),
301 'read action on unreadable folder' => array(
302 'read',
303 array('r' => FALSE, 'w' => TRUE),
304 FALSE
305 ),
306 'write action on read-only folder' => array(
307 'write',
308 array('r' => TRUE, 'w' => FALSE),
309 FALSE
310 )
311 );
312 }
313
314 /**
315 * @test
316 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
317 * @param string $action 'read' or 'write'
318 * @param array $permissionsFromDriver The permissions as returned from the driver
319 * @param boolean $expectedResult
320 */
321 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedResult) {
322 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
323 $mockedDriver->expects($this->any())->method('getFolderPermissions')->will($this->returnValue($permissionsFromDriver));
324 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
325 // Let all other checks pass
326 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
327 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
328 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
329 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
330 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
331 $fixture->setDriver($mockedDriver);
332
333 $this->assertSame($expectedResult, $fixture->checkFolderActionPermission($action, $mockedFolder));
334 }
335
336 /**
337 * @test
338 */
339 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
340 $this->prepareFixture(array());
341 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
342 }
343
344 /**
345 * @test
346 */
347 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
348 $this->prepareFixture(array());
349 $this->fixture->setUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
350 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
351 }
352
353 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
354 return array(
355 'all lower cased' => array(
356 array('readFolder' => TRUE),
357 'read',
358 'folder'
359 ),
360 'all upper case' => array(
361 array('readFolder' => TRUE),
362 'READ',
363 'FOLDER'
364 ),
365 'mixed case' => array(
366 array('readFolder' => TRUE),
367 'ReaD',
368 'FoLdEr'
369 )
370 );
371 }
372
373 /**
374 * @test
375 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
376 */
377 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
378 $this->prepareFixture(array());
379 $this->fixture->setUserPermissions($permissions);
380 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
381 }
382
383 /**
384 * @test
385 */
386 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
387 $this->prepareFixture(array());
388 $this->fixture->setEvaluatePermissions(TRUE);
389 $this->fixture->setUserPermissions(array('readFolder' => FALSE));
390 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
391 }
392
393 /**
394 * @test
395 */
396 public function userActionIsDisallowedIfPermissionIsNotSet() {
397 $this->prepareFixture(array());
398 $this->fixture->setEvaluatePermissions(TRUE);
399 $this->fixture->setUserPermissions(array('readFolder' => TRUE));
400 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
401 }
402
403 /**
404 * @test
405 * @group integration
406 */
407 public function setFileContentsUpdatesObjectProperties() {
408 $this->initializeVfs();
409 $this->prepareFixture(array(), TRUE);
410 $fileInfo = array(
411 'storage' => 'A',
412 'identifier' => 'B',
413 'mtime' => 'C',
414 'ctime' => 'D',
415 'mimetype' => 'E',
416 'size' => 'F',
417 'name' => 'G',
418 );
419 $newProperties = array(
420 'storage' => $fileInfo['storage'],
421 'identifier' => $fileInfo['identifier'],
422 'tstamp' => $fileInfo['mtime'],
423 'crdate' => $fileInfo['ctime'],
424 'mime_type' => $fileInfo['mimetype'],
425 'size' => $fileInfo['size'],
426 'name' => $fileInfo['name']
427 );
428 $hash = 'asdfg';
429 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
430 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfo));
431 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
432 $this->fixture->setDriver($driver);
433 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
434 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
435 $mockedFile->expects($this->at(1))->method('updateProperties')->with($this->equalTo(array('sha1' => $hash)));
436 $mockedFile->expects($this->at(3))->method('updateProperties')->with($this->equalTo($newProperties));
437 $this->fixture->setFileContents($mockedFile, uniqid());
438 }
439
440 /**
441 * @test
442 * @group integration
443 */
444 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
445 $localFilePath = '/path/to/localFile';
446 $sourceFileIdentifier = '/sourceFile.ext';
447 $this->addToMount(array(
448 'targetFolder' => array()
449 ));
450 $this->initializeVfs();
451 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
452 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
453 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
454 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
455 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
456 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
457 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
458 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
459 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
460 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
461 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
462 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
463 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
464 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
465 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
466 }
467
468 /**
469 * @test
470 * @group integration
471 */
472 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
473 $localFilePath = '/path/to/localFile';
474 $sourceFileIdentifier = '/sourceFile.ext';
475 $this->addToMount(array(
476 'targetFolder' => array()
477 ));
478 $this->initializeVfs();
479 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
480 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
481 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
482 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
483 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
484 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
485 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
486 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
487 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
488 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
489 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
490 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
491 }
492
493 /**
494 * @test
495 * @group integration
496 */
497 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
498 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
499 $this->addToMount(array(
500 'mountFolder' => array(
501 'file' => 'asdfg'
502 )
503 ));
504 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
505 $this->initializeVfs();
506 $this->prepareFixture(array(), NULL, $mockedDriver);
507 $this->fixture->addFileMount('/mountFolder');
508 $this->assertEquals(1, count($this->fixture->getFileMounts()));
509 $this->fixture->isWithinFileMountBoundaries($mockedFile);
510 }
511
512 /**
513 * This test is also valid for folders
514 *
515 * @test
516 */
517 public function getFileListReturnsFilesInCorrectOrder() {
518 $fileList = array(
519 'file10' => '',
520 'file2' => '',
521 'File' => '',
522 'fail' => ''
523 );
524 $this->prepareFixture(array(), TRUE);
525 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
526 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
527 $fileList = $this->fixture->getFileList('/');
528 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
529 }
530
531 /**
532 * @test
533 */
534 public function getFileListIgnoresCasingWhenSortingFilenames() {
535 $fileList = array(
536 'aFile' => 'dfsdg',
537 'zFile' => 'werw',
538 'BFile' => 'asd',
539 '12345' => 'fdsa',
540 'IMG_1234.jpg' => 'asdf'
541 );
542 $this->prepareFixture(array(), TRUE);
543 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
544 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
545 $fileList = $this->fixture->getFileList('/');
546 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
547 }
548
549 /**
550 * @test
551 */
552 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
553 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
554 $mockedDriver = $this->createDriverMock(array());
555 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
556 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
557 $this->prepareFixture(array(), TRUE);
558 $this->fixture->setDriver($mockedDriver);
559 $this->fixture->createFolder('newFolder', $mockedParentFolder);
560 }
561
562 /**
563 * @test
564 * @expectedException \RuntimeException
565 */
566 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
567 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
568 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
569 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
570 $driverMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), array(), '', FALSE);
571 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
572 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
573 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
574 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
575 $fixture->_set('driver', $driverMock);
576 $fixture->deleteFolder($folderMock, FALSE);
577 }
578
579 /**
580 * @test
581 */
582 public function createFolderCallsDriverForFolderCreation() {
583 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
584 $this->prepareFixture(array(), TRUE);
585 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
586 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
587 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
588 $this->fixture->createFolder('newFolder', $mockedParentFolder);
589 }
590
591 /**
592 * @test
593 */
594 public function createFolderCanRecursivelyCreateFolders() {
595 $this->addToMount(array('someFolder' => array()));
596 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
597 $this->prepareFixture(array(), TRUE, $mockedDriver);
598 $parentFolder = $this->fixture->getFolder('/someFolder/');
599 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
600 $this->assertEquals('secondSubfolder', $newFolder->getName());
601 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
602 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
603 }
604
605 /**
606 * @test
607 */
608 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
609 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
610 $this->prepareFixture(array(), TRUE);
611 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
612 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
613 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
614 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
615 }
616
617 /**
618 * @test
619 */
620 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
621 $mockedRootFolder = $this->getSimpleFolderMock('/');
622 $this->prepareFixture(array(), TRUE);
623 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
624 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
625 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
626 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
627 $this->fixture->createFolder('someFolder');
628 }
629
630 /**
631 * @test
632 */
633 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
634 $this->addToMount(array(
635 'existingFolder' => array()
636 ));
637 $this->initializeVfs();
638 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
639 $this->prepareFixture(array(), TRUE, $mockedDriver);
640 $rootFolder = $this->fixture->getFolder('/');
641 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
642 $this->assertEquals('someFolder', $newFolder->getName());
643 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
644 }
645
646 /**
647 * @test
648 */
649 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
650 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
651 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
652 $this->prepareFixture(array(), TRUE);
653 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
654 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
655 $this->fixture->createFolder('newFolder', $mockedParentFolder);
656 }
657
658 /**
659 * @test
660 */
661 public function replaceFileFailsIfLocalFileDoesNotExist() {
662 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
663 $this->prepareFixture(array(), TRUE);
664 $mockedFile = $this->getSimpleFileMock('/someFile');
665 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
666 }
667
668 /**
669 * @test
670 */
671 public function getFileListHandsOverRecursiveFALSEifNotExplicitlySet() {
672 $this->prepareFixture(array(), TRUE);
673 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
674 $driver->expects($this->once())
675 ->method('getFileList')
676 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
677 ->will($this->returnValue(array()));
678 $this->fixture->getFileList('/');
679 }
680
681 /**
682 * @test
683 */
684 public function getFileListHandsOverRecursiveTRUEifSet() {
685
686 $this->prepareFixture(array(), TRUE);
687 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
688 $driver->expects($this->once())
689 ->method('getFileList')
690 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
691 ->will($this->returnValue(array()));
692 $this->fixture->getFileList('/', 0, 0, TRUE, TRUE, TRUE);
693 }
694
695 /**
696 * @test
697 */
698 public function getRoleReturnsDefaultForRegularFolders() {
699 $folderIdentifier = uniqid();
700 $this->addToMount(array(
701 $folderIdentifier => array()
702 ));
703 $this->prepareFixture(array());
704
705 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
706
707 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
708 }
709
710 /**
711 * @test
712 */
713 public function getRoleReturnsCorrectValueForDefaultProcessingFolder() {
714 $this->prepareFixture(array());
715
716 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . ResourceStorage::DEFAULT_ProcessingFolder . '/'));
717
718 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
719 }
720
721 /**
722 * @test
723 */
724 public function getRoleReturnsCorrectValueForConfiguredProcessingFolder() {
725 $folderIdentifier = uniqid();
726 $this->addToMount(array(
727 $folderIdentifier => array()
728 ));
729 $this->prepareFixture(array(), FALSE, NULL, array('processingfolder' => '/' . $folderIdentifier . '/'));
730
731 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
732
733 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
734 }
735
736 /**
737 * Data provider for fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder function
738 */
739 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider() {
740 return array(
741 'Empty folderList returned' => array(
742 'path' => '/',
743 'processingFolder' => '_processed_',
744 'folderList' => array(),
745 'expectedItems' => array()
746 ),
747 'Empty _processed_ folder' => array(
748 'path' => '/',
749 'processingFolder' => '',
750 'folderList' => array(
751 '_processed_' => array(),
752 '_temp_' => array(),
753 'user_upload' => array()
754 ),
755 'expectedItems' => array(
756 'user_upload' => array(),
757 '_temp_' => array()
758 )
759 ),
760 '_processed_ folder not in folder list' => array(
761 'path' => '/',
762 'processingFolder' => '_processed_',
763 'folderList' => array(
764 '_temp_' => array()
765 ),
766 'expectedItems' => array(
767 '_temp_' => array()
768 )
769 ),
770 '_processed_ folder on root level' => array(
771 'path' => '/',
772 'processingFolder' => '_processed_',
773 'folderList' => array(
774 '_processed_' => array(),
775 '_temp_' => array(),
776 'user_upload' => array()
777 ),
778 'expectedItems' => array(
779 'user_upload' => array(),
780 '_temp_' => array()
781 )
782 ),
783 '_processed_ folder on second level' => array(
784 'path' => 'Public/',
785 'processingFolder' => 'Public/_processed_',
786 'folderList' => array(
787 '_processed_' => array(),
788 '_temp_' => array(),
789 'user_upload' => array()
790 ),
791 'expectedItems' => array(
792 'user_upload' => array(),
793 '_temp_' => array()
794 )
795 ),
796 '_processed_ folder on third level' => array(
797 'path' => 'Public/Files/',
798 'processingFolder' => 'Public/Files/_processed_',
799 'folderList' => array(
800 '_processed_' => array(),
801 '_temp_' => array(),
802 'user_upload' => array()
803 ),
804 'expectedItems' => array(
805 'user_upload' => array(),
806 '_temp_' => array()
807 )
808 )
809 );
810 }
811
812 /**
813 * @test
814 * @dataProvider fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider
815 */
816 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder($path, $processingFolder, $folderList, $expectedItems) {
817 $driverMock = $this->createDriverMock(array(), NULL, array('getFolderList', 'folderExists'));
818 $driverMock->expects($this->once())->method('getFolderList')->will($this->returnValue($folderList));
819 $driverMock->expects($this->any())->method('folderExists')->will($this->returnValue(TRUE));
820
821 $this->prepareFixture(array(), TRUE, $driverMock, array('processingfolder' => $processingFolder));
822
823 $this->assertSame($expectedItems, $this->fixture->fetchFolderListFromDriver($path));
824 }
825 }
826
827 ?>