[FEATURE] Allow Storages outside the webroot
[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 protected $fixture;
45
46 public function setUp() {
47 parent::setUp();
48 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
49 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(
50 'TYPO3\\CMS\\Core\\Resource\\FileRepository',
51 $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository')
52 );
53
54 }
55
56 public function tearDown() {
57 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
58 parent::tearDown();
59 }
60
61 /**
62 * Prepare fixture
63 *
64 * @param array $configuration
65 * @param boolean $mockPermissionChecks
66 * @return void
67 */
68 protected function prepareFixture($configuration, $mockPermissionChecks = FALSE, $driverObject = NULL, array $storageRecord = array()) {
69 $permissionMethods = array('assureFileAddPermissions', 'checkFolderActionPermission', 'checkFileActionPermission', 'checkUserActionPermission', 'checkFileExtensionPermission', 'isWithinFileMountBoundaries');
70 $mockedMethods = array();
71 $configuration = $this->convertConfigurationArrayToFlexformXml($configuration);
72 $overruleArray = array('configuration' => $configuration);
73 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($storageRecord, $overruleArray);
74 if ($driverObject == NULL) {
75 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\AbstractDriver */
76 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
77 }
78 if ($mockPermissionChecks) {
79 $mockedMethods = $permissionMethods;
80 }
81 $mockedMethods[] = 'getIndexer';
82
83
84 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', $mockedMethods, array($driverObject, $storageRecord));
85 $this->fixture->expects($this->any())->method('getIndexer')->will($this->returnValue($this->getMock('TYPO3\CMS\Core\Resource\Index\Indexer', array(), array(), '', FALSE)));
86 foreach ($permissionMethods as $method) {
87 $this->fixture->expects($this->any())->method($method)->will($this->returnValue(TRUE));
88 }
89 }
90
91 /**
92 * Converts a simple configuration array into a FlexForm data structure serialized as XML
93 *
94 * @param array $configuration
95 * @return string
96 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml()
97 */
98 protected function convertConfigurationArrayToFlexformXml(array $configuration) {
99 $flexformArray = array('data' => array('sDEF' => array('lDEF' => array())));
100 foreach ($configuration as $key => $value) {
101 $flexformArray['data']['sDEF']['lDEF'][$key] = array('vDEF' => $value);
102 }
103 $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml($flexformArray);
104 return $configuration;
105 }
106
107 /**
108 * Creates a driver fixture object, optionally using a given mount object.
109 *
110 * IMPORTANT: Call this only after setting up the virtual file system (with the addTo* methods)!
111 *
112 * @param $driverConfiguration
113 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject
114 * @param array $mockedDriverMethods
115 * @return \TYPO3\CMS\Core\Resource\Driver\LocalDriver
116 */
117 protected function createDriverMock($driverConfiguration, \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject = NULL, $mockedDriverMethods = array()) {
118 $this->initializeVfs();
119
120
121 if (!isset($driverConfiguration['basePath'])) {
122 $driverConfiguration['basePath'] = $this->getMountRootUrl();
123 }
124
125 if ($mockedDriverMethods === NULL) {
126 $driver = new \TYPO3\CMS\Core\Resource\Driver\LocalDriver($driverConfiguration);
127 } else {
128 // We are using the LocalDriver here because PHPUnit can't mock concrete methods in abstract classes, so
129 // when using the AbstractDriver we would be in trouble when wanting to mock away some concrete method
130 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', $mockedDriverMethods, array($driverConfiguration));
131 }
132 if ($storageObject !== NULL) {
133 $storageObject->setDriver($driver);
134 }
135 $driver->setStorageUid(6);
136 $driver->processConfiguration();
137 $driver->initialize();
138 return $driver;
139 }
140
141 /**
142 * @test
143 */
144 public function baseUriGetsSlashAppended() {
145 $uri = 'http://example.org/somewhere/else';
146 $this->prepareFixture(array('baseUri' => $uri));
147 $this->assertEquals($uri . '/', $this->fixture->getBaseUri());
148 }
149
150 /**
151 * @return array
152 */
153 public function capabilitiesDataProvider() {
154 return array(
155 'only public' => array(
156 array(
157 'public' => TRUE,
158 'writable' => FALSE,
159 'browsable' => FALSE
160 )
161 ),
162 'only writable' => array(
163 array(
164 'public' => FALSE,
165 'writable' => TRUE,
166 'browsable' => FALSE
167 )
168 ),
169 'only browsable' => array(
170 array(
171 'public' => FALSE,
172 'writable' => FALSE,
173 'browsable' => TRUE
174 )
175 ),
176 'all capabilities' => array(
177 array(
178 'public' => TRUE,
179 'writable' => TRUE,
180 'browsable' => TRUE
181 )
182 ),
183 'none' => array(
184 array(
185 'public' => FALSE,
186 'writable' => FALSE,
187 'browsable' => FALSE
188 )
189 )
190 );
191 }
192
193 /**
194 * @test
195 * @dataProvider capabilitiesDataProvider
196 */
197 public function capabilitiesOfStorageObjectAreCorrectlySet(array $capabilites) {
198 $storageRecord = array(
199 'is_public' => $capabilites['public'],
200 'is_writable' => $capabilites['writable'],
201 'is_browsable' => $capabilites['browsable'],
202 'is_online' => TRUE
203 );
204 $mockedDriver = $this->createDriverMock(
205 array(
206 'pathType' => 'relative',
207 'basePath' => 'fileadmin/',
208 'baseUri' => 'http://www.test.de/'
209 ),
210 $this->fixture,
211 NULL
212 );
213 $this->prepareFixture(array(), FALSE, $mockedDriver, $storageRecord);
214 $this->assertEquals($capabilites['public'], $this->fixture->isPublic(), 'Capability "public" is not correctly set.');
215 $this->assertEquals($capabilites['writable'], $this->fixture->isWritable(), 'Capability "writable" is not correctly set.');
216 $this->assertEquals($capabilites['browsable'], $this->fixture->isBrowsable(), 'Capability "browsable" is not correctly set.');
217 }
218
219 /**
220 * @test
221 */
222 public function fileAndFolderListFiltersAreInitializedWithDefaultFilters() {
223 $this->prepareFixture(array());
224 $this->assertEquals($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'], $this->fixture->getFileAndFolderNameFilters());
225 }
226
227 /**
228 * @test
229 */
230 public function addFileFailsIfFileDoesNotExist() {
231 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
232 $this->setExpectedException('InvalidArgumentException', '', 1319552745);
233 $this->prepareFixture(array());
234 $this->fixture->addFile('/some/random/file', $mockedFolder);
235 }
236
237 /**
238 * @test
239 */
240 public function getPublicUrlReturnsNullIfStorageIsNotOnline() {
241 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
242 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
243 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
244 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isOnline'), array($driver, array('configuration' => array())));
245 $fixture->expects($this->once())->method('isOnline')->will($this->returnValue(FALSE));
246
247 $sourceFileIdentifier = '/sourceFile.ext';
248 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
249 $result = $fixture->getPublicUrl($sourceFile);
250 $this->assertSame($result, NULL);
251 }
252
253 /**
254 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
255 *
256 * @return array
257 */
258 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
259 return array(
260 'read action on readable/writable folder' => array(
261 'read',
262 array('r' => TRUE, 'w' => TRUE),
263 TRUE
264 ),
265 'read action on unreadable folder' => array(
266 'read',
267 array('r' => FALSE, 'w' => TRUE),
268 FALSE
269 ),
270 'write action on read-only folder' => array(
271 'write',
272 array('r' => TRUE, 'w' => FALSE),
273 FALSE
274 )
275 );
276 }
277
278 /**
279 * @test
280 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
281 * @param string $action 'read' or 'write'
282 * @param array $permissionsFromDriver The permissions as returned from the driver
283 * @param boolean $expectedResult
284 */
285 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedResult) {
286 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
287 $mockedDriver->expects($this->any())->method('getPermissions')->will($this->returnValue($permissionsFromDriver));
288 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
289 // Let all other checks pass
290 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
291 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
292 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
293 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
294 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
295 $fixture->setDriver($mockedDriver);
296
297 $this->assertSame($expectedResult, $fixture->checkFolderActionPermission($action, $mockedFolder));
298 }
299
300 /**
301 * @test
302 */
303 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
304 $this->prepareFixture(array());
305 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
306 }
307
308 /**
309 * @test
310 */
311 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
312 $this->prepareFixture(array());
313 $this->fixture->setUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
314 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
315 }
316
317 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
318 return array(
319 'all lower cased' => array(
320 array('readFolder' => TRUE),
321 'read',
322 'folder'
323 ),
324 'all upper case' => array(
325 array('readFolder' => TRUE),
326 'READ',
327 'FOLDER'
328 ),
329 'mixed case' => array(
330 array('readFolder' => TRUE),
331 'ReaD',
332 'FoLdEr'
333 )
334 );
335 }
336
337 /**
338 * @test
339 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
340 */
341 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
342 $this->prepareFixture(array());
343 $this->fixture->setUserPermissions($permissions);
344 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
345 }
346
347 /**
348 * @test
349 */
350 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
351 $this->prepareFixture(array());
352 $this->fixture->setEvaluatePermissions(TRUE);
353 $this->fixture->setUserPermissions(array('readFolder' => FALSE));
354 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
355 }
356
357 /**
358 * @test
359 */
360 public function userActionIsDisallowedIfPermissionIsNotSet() {
361 $this->prepareFixture(array());
362 $this->fixture->setEvaluatePermissions(TRUE);
363 $this->fixture->setUserPermissions(array('readFolder' => TRUE));
364 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
365 }
366
367 /**
368 * @test
369 * @group integration
370 */
371 public function setFileContentsUpdatesObjectProperties() {
372 $this->initializeVfs();
373 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
374 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFileIndexRepository', 'checkFileActionPermission'), array($driverObject, array()));
375 $this->fixture->expects($this->any())->method('checkFileActionPermission')->will($this->returnValue(TRUE));
376 $fileInfo = array(
377 'storage' => 'A',
378 'identifier' => 'B',
379 'mtime' => 'C',
380 'ctime' => 'D',
381 'mimetype' => 'E',
382 'size' => 'F',
383 'name' => 'G',
384 );
385 $newProperties = array(
386 'storage' => $fileInfo['storage'],
387 'identifier' => $fileInfo['identifier'],
388 'tstamp' => $fileInfo['mtime'],
389 'crdate' => $fileInfo['ctime'],
390 'mime_type' => $fileInfo['mimetype'],
391 'size' => $fileInfo['size'],
392 'name' => $fileInfo['name']
393 );
394 $hash = 'asdfg';
395 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
396 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfo));
397 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
398 $this->fixture->setDriver($driver);
399 $indexFileRepositoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
400 $this->fixture->expects($this->any())->method('getFileIndexRepository')->will($this->returnValue($indexFileRepositoryMock));
401 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
402 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue($fileInfo['identifier']));
403 // called by indexer because the properties are updated
404 $this->fixture->expects($this->any())->method('getFileInfoByIdentifier')->will($this->returnValue($newProperties));
405 $mockedFile->expects($this->any())->method('getStorage')->will($this->returnValue($this->fixture));
406 $mockedFile->expects($this->any())->method('getProperties')->will($this->returnValue(array_keys($fileInfo)));
407 $mockedFile->expects($this->any())->method('getUpdatedProperties')->will($this->returnValue(array_keys($newProperties)));
408 // do not update directly; that's up to the indexer
409 $indexFileRepositoryMock->expects($this->never())->method('update');
410 $this->fixture->setFileContents($mockedFile, uniqid());
411 }
412
413 /**
414 * @test
415 * @group integration
416 */
417 public function moveFileCallsDriversMethodsWithCorrectArguments() {
418 $localFilePath = '/path/to/localFile';
419 $sourceFileIdentifier = '/sourceFile.ext';
420 $fileInfoDummy = array(
421 'storage' => 'A',
422 'identifier' => 'B',
423 'mtime' => 'C',
424 'ctime' => 'D',
425 'mimetype' => 'E',
426 'size' => 'F',
427 'name' => 'G',
428 );
429 $this->addToMount(array(
430 'targetFolder' => array()
431 ));
432 $this->initializeVfs();
433 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
434 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
435 $sourceDriver->expects($this->once())->method('deleteFile')->with($this->equalTo($sourceFileIdentifier));
436 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
437 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
438 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
439 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
440 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
441 $sourceFile->expects($this->once())->method('getUpdatedProperties')->will($this->returnValue(array_keys($fileInfoDummy)));
442 $sourceFile->expects($this->once())->method('getProperties')->will($this->returnValue($fileInfoDummy));
443 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
444 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
445 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfoDummy));
446 $driver->expects($this->once())->method('addFile')->with($localFilePath, '/targetFolder/', $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
447 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
448 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileMovePermissions'), array($driver, array('configuration' => $configuration)));
449 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
450 }
451
452 /**
453 * @test
454 * @group integration
455 */
456 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
457 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
458 $this->addToMount(array(
459 'mountFolder' => array(
460 'file' => 'asdfg'
461 )
462 ));
463 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
464 $this->initializeVfs();
465 $this->prepareFixture(array(), NULL, $mockedDriver);
466 $this->fixture->addFileMount('/mountFolder');
467 $this->assertEquals(1, count($this->fixture->getFileMounts()));
468 $this->fixture->isWithinFileMountBoundaries($mockedFile);
469 }
470
471
472 /**
473 * @test
474 */
475 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
476 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
477 $mockedDriver = $this->createDriverMock(array());
478 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
479 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
480 $this->prepareFixture(array(), TRUE);
481 $this->fixture->setDriver($mockedDriver);
482 $this->fixture->createFolder('newFolder', $mockedParentFolder);
483 }
484
485 /**
486 * @test
487 * @expectedException \RuntimeException
488 */
489 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
490 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
491 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
492 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
493 $driverMock = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver');
494 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
495 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
496 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
497 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
498 $fixture->_set('driver', $driverMock);
499 $fixture->deleteFolder($folderMock, FALSE);
500 }
501
502 /**
503 * @test
504 */
505 public function createFolderCallsDriverForFolderCreation() {
506 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
507 $this->prepareFixture(array(), TRUE);
508 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
509 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
510 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
511 $this->fixture->createFolder('newFolder', $mockedParentFolder);
512 }
513
514 /**
515 * @test
516 */
517 public function createFolderCanRecursivelyCreateFolders() {
518 $this->addToMount(array('someFolder' => array()));
519 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
520 $this->prepareFixture(array(), TRUE, $mockedDriver);
521 $parentFolder = $this->fixture->getFolder('/someFolder/');
522 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
523 $this->assertEquals('secondSubfolder', $newFolder->getName());
524 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
525 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
526 }
527
528 /**
529 * @test
530 */
531 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
532 $this->prepareFixture(array(), TRUE);
533 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
534 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue('/'));
535 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
536 $this->fixture->createFolder('someFolder');
537 }
538
539 /**
540 * @test
541 */
542 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
543 $this->addToMount(array(
544 'existingFolder' => array()
545 ));
546 $this->initializeVfs();
547 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
548 $this->prepareFixture(array(), TRUE, $mockedDriver);
549 $rootFolder = $this->fixture->getFolder('/');
550 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
551 $this->assertEquals('someFolder', $newFolder->getName());
552 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
553 }
554
555 /**
556 * @test
557 */
558 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
559 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
560 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
561 $this->prepareFixture(array(), TRUE);
562 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
563 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
564 $this->fixture->createFolder('newFolder', $mockedParentFolder);
565 }
566
567 /**
568 * @test
569 */
570 public function replaceFileFailsIfLocalFileDoesNotExist() {
571 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
572 $this->prepareFixture(array(), TRUE);
573 $mockedFile = $this->getSimpleFileMock('/someFile');
574 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
575 }
576
577 /**
578 * @test
579 */
580 public function getRoleReturnsDefaultForRegularFolders() {
581 $folderIdentifier = uniqid();
582 $this->addToMount(array(
583 $folderIdentifier => array()
584 ));
585 $this->prepareFixture(array());
586
587 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
588
589 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
590 }
591
592 }