[BUGFIX] Wrong note in ResourceFactory
[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 if ($storageObject == NULL) {
120 $storageObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
121 }
122
123 if (!isset($driverConfiguration['basePath'])) {
124 $driverConfiguration['basePath'] = $this->getMountRootUrl();
125 }
126
127 if ($mockedDriverMethods === NULL) {
128 $driver = new \TYPO3\CMS\Core\Resource\Driver\LocalDriver($driverConfiguration);
129 } else {
130 // We are using the LocalDriver here because PHPUnit can't mock concrete methods in abstract classes, so
131 // when using the AbstractDriver we would be in trouble when wanting to mock away some concrete method
132 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', $mockedDriverMethods, array($driverConfiguration));
133 }
134 $storageObject->setDriver($driver);
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(array(), $this->fixture, array('hasCapability'));
205 $mockedDriver->expects($this->any())->method('hasCapability')->will($this->returnValue(TRUE));
206 $this->prepareFixture(array(), FALSE, $mockedDriver, $storageRecord);
207 $this->assertEquals($capabilites['public'], $this->fixture->isPublic(), 'Capability "public" is not correctly set.');
208 $this->assertEquals($capabilites['writable'], $this->fixture->isWritable(), 'Capability "writable" is not correctly set.');
209 $this->assertEquals($capabilites['browsable'], $this->fixture->isBrowsable(), 'Capability "browsable" is not correctly set.');
210 }
211
212 /**
213 * @test
214 */
215 public function fileAndFolderListFiltersAreInitializedWithDefaultFilters() {
216 $this->prepareFixture(array());
217 $this->assertEquals($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'], $this->fixture->getFileAndFolderNameFilters());
218 }
219
220 /**
221 * @test
222 */
223 public function addFileFailsIfFileDoesNotExist() {
224 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
225 $this->setExpectedException('InvalidArgumentException', '', 1319552745);
226 $this->prepareFixture(array());
227 $this->fixture->addFile('/some/random/file', $mockedFolder);
228 }
229
230 /**
231 * @test
232 */
233 public function getPublicUrlReturnsNullIfStorageIsNotOnline() {
234 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
235 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
236 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
237 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isOnline'), array($driver, array('configuration' => array())));
238 $fixture->expects($this->once())->method('isOnline')->will($this->returnValue(FALSE));
239
240 $sourceFileIdentifier = '/sourceFile.ext';
241 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
242 $result = $fixture->getPublicUrl($sourceFile);
243 $this->assertSame($result, NULL);
244 }
245
246 /**
247 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
248 *
249 * @return array
250 */
251 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
252 return array(
253 'read action on readable/writable folder' => array(
254 'read',
255 array('r' => TRUE, 'w' => TRUE),
256 TRUE
257 ),
258 'read action on unreadable folder' => array(
259 'read',
260 array('r' => FALSE, 'w' => TRUE),
261 FALSE
262 ),
263 'write action on read-only folder' => array(
264 'write',
265 array('r' => TRUE, 'w' => FALSE),
266 FALSE
267 )
268 );
269 }
270
271 /**
272 * @test
273 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
274 * @param string $action 'read' or 'write'
275 * @param array $permissionsFromDriver The permissions as returned from the driver
276 * @param boolean $expectedResult
277 */
278 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedResult) {
279 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
280 $mockedDriver->expects($this->any())->method('getPermissions')->will($this->returnValue($permissionsFromDriver));
281 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
282 // Let all other checks pass
283 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
284 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
285 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
286 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
287 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
288 $fixture->setDriver($mockedDriver);
289
290 $this->assertSame($expectedResult, $fixture->checkFolderActionPermission($action, $mockedFolder));
291 }
292
293 /**
294 * @test
295 */
296 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
297 $this->prepareFixture(array());
298 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
299 }
300
301 /**
302 * @test
303 */
304 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
305 $this->prepareFixture(array());
306 $this->fixture->setUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
307 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
308 }
309
310 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
311 return array(
312 'all lower cased' => array(
313 array('readFolder' => TRUE),
314 'read',
315 'folder'
316 ),
317 'all upper case' => array(
318 array('readFolder' => TRUE),
319 'READ',
320 'FOLDER'
321 ),
322 'mixed case' => array(
323 array('readFolder' => TRUE),
324 'ReaD',
325 'FoLdEr'
326 )
327 );
328 }
329
330 /**
331 * @test
332 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
333 */
334 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
335 $this->prepareFixture(array());
336 $this->fixture->setUserPermissions($permissions);
337 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
338 }
339
340 /**
341 * @test
342 */
343 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
344 $this->prepareFixture(array());
345 $this->fixture->setEvaluatePermissions(TRUE);
346 $this->fixture->setUserPermissions(array('readFolder' => FALSE));
347 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
348 }
349
350 /**
351 * @test
352 */
353 public function userActionIsDisallowedIfPermissionIsNotSet() {
354 $this->prepareFixture(array());
355 $this->fixture->setEvaluatePermissions(TRUE);
356 $this->fixture->setUserPermissions(array('readFolder' => TRUE));
357 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
358 }
359
360 /**
361 * @test
362 * @group integration
363 */
364 public function setFileContentsUpdatesObjectProperties() {
365 $this->initializeVfs();
366 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
367 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFileIndexRepository', 'checkFileActionPermission'), array($driverObject, array()));
368 $this->fixture->expects($this->any())->method('checkFileActionPermission')->will($this->returnValue(TRUE));
369 $fileInfo = array(
370 'storage' => 'A',
371 'identifier' => 'B',
372 'mtime' => 'C',
373 'ctime' => 'D',
374 'mimetype' => 'E',
375 'size' => 'F',
376 'name' => 'G',
377 );
378 $newProperties = array(
379 'storage' => $fileInfo['storage'],
380 'identifier' => $fileInfo['identifier'],
381 'tstamp' => $fileInfo['mtime'],
382 'crdate' => $fileInfo['ctime'],
383 'mime_type' => $fileInfo['mimetype'],
384 'size' => $fileInfo['size'],
385 'name' => $fileInfo['name']
386 );
387 $hash = 'asdfg';
388 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
389 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfo));
390 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
391 $this->fixture->setDriver($driver);
392 $indexFileRepositoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
393 $this->fixture->expects($this->any())->method('getFileIndexRepository')->will($this->returnValue($indexFileRepositoryMock));
394 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
395 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue($fileInfo['identifier']));
396 // called by indexer because the properties are updated
397 $this->fixture->expects($this->any())->method('getFileInfoByIdentifier')->will($this->returnValue($newProperties));
398 $mockedFile->expects($this->any())->method('getStorage')->will($this->returnValue($this->fixture));
399 $mockedFile->expects($this->any())->method('getProperties')->will($this->returnValue(array_keys($fileInfo)));
400 $mockedFile->expects($this->any())->method('getUpdatedProperties')->will($this->returnValue(array_keys($newProperties)));
401 // do not update directly; that's up to the indexer
402 $indexFileRepositoryMock->expects($this->never())->method('update');
403 $this->fixture->setFileContents($mockedFile, uniqid());
404 }
405
406 /**
407 * @test
408 * @group integration
409 */
410 public function moveFileCallsDriversMethodsWithCorrectArguments() {
411 $localFilePath = '/path/to/localFile';
412 $sourceFileIdentifier = '/sourceFile.ext';
413 $fileInfoDummy = array(
414 'storage' => 'A',
415 'identifier' => 'B',
416 'mtime' => 'C',
417 'ctime' => 'D',
418 'mimetype' => 'E',
419 'size' => 'F',
420 'name' => 'G',
421 );
422 $this->addToMount(array(
423 'targetFolder' => array()
424 ));
425 $this->initializeVfs();
426 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
427 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
428 $sourceDriver->expects($this->once())->method('deleteFile')->with($this->equalTo($sourceFileIdentifier));
429 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
430 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
431 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
432 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
433 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
434 $sourceFile->expects($this->once())->method('getUpdatedProperties')->will($this->returnValue(array_keys($fileInfoDummy)));
435 $sourceFile->expects($this->once())->method('getProperties')->will($this->returnValue($fileInfoDummy));
436 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
437 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
438 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfoDummy));
439 $driver->expects($this->once())->method('addFile')->with($localFilePath, '/targetFolder/', $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
440 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
441 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileMovePermissions'), array($driver, array('configuration' => $configuration)));
442 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
443 }
444
445 /**
446 * @test
447 * @group integration
448 */
449 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
450 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
451 $this->addToMount(array(
452 'mountFolder' => array(
453 'file' => 'asdfg'
454 )
455 ));
456 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
457 $this->initializeVfs();
458 $this->prepareFixture(array(), NULL, $mockedDriver);
459 $this->fixture->addFileMount('/mountFolder');
460 $this->assertEquals(1, count($this->fixture->getFileMounts()));
461 $this->fixture->isWithinFileMountBoundaries($mockedFile);
462 }
463
464
465 /**
466 * @test
467 */
468 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
469 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
470 $mockedDriver = $this->createDriverMock(array());
471 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
472 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
473 $this->prepareFixture(array(), TRUE);
474 $this->fixture->setDriver($mockedDriver);
475 $this->fixture->createFolder('newFolder', $mockedParentFolder);
476 }
477
478 /**
479 * @test
480 * @expectedException \RuntimeException
481 */
482 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
483 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
484 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
485 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
486 $driverMock = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver');
487 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
488 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
489 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
490 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
491 $fixture->_set('driver', $driverMock);
492 $fixture->deleteFolder($folderMock, FALSE);
493 }
494
495 /**
496 * @test
497 */
498 public function createFolderCallsDriverForFolderCreation() {
499 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
500 $this->prepareFixture(array(), TRUE);
501 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
502 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
503 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
504 $this->fixture->createFolder('newFolder', $mockedParentFolder);
505 }
506
507 /**
508 * @test
509 */
510 public function createFolderCanRecursivelyCreateFolders() {
511 $this->addToMount(array('someFolder' => array()));
512 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
513 $this->prepareFixture(array(), TRUE, $mockedDriver);
514 $parentFolder = $this->fixture->getFolder('/someFolder/');
515 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
516 $this->assertEquals('secondSubfolder', $newFolder->getName());
517 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
518 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
519 }
520
521 /**
522 * @test
523 */
524 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
525 $this->prepareFixture(array(), TRUE);
526 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
527 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue('/'));
528 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
529 $this->fixture->createFolder('someFolder');
530 }
531
532 /**
533 * @test
534 */
535 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
536 $this->addToMount(array(
537 'existingFolder' => array()
538 ));
539 $this->initializeVfs();
540 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
541 $this->prepareFixture(array(), TRUE, $mockedDriver);
542 $rootFolder = $this->fixture->getFolder('/');
543 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
544 $this->assertEquals('someFolder', $newFolder->getName());
545 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
546 }
547
548 /**
549 * @test
550 */
551 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
552 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
553 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
554 $this->prepareFixture(array(), TRUE);
555 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
556 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
557 $this->fixture->createFolder('newFolder', $mockedParentFolder);
558 }
559
560 /**
561 * @test
562 */
563 public function replaceFileFailsIfLocalFileDoesNotExist() {
564 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
565 $this->prepareFixture(array(), TRUE);
566 $mockedFile = $this->getSimpleFileMock('/someFile');
567 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
568 }
569
570 /**
571 * @test
572 */
573 public function getRoleReturnsDefaultForRegularFolders() {
574 $folderIdentifier = uniqid();
575 $this->addToMount(array(
576 $folderIdentifier => array()
577 ));
578 $this->prepareFixture(array());
579
580 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
581
582 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
583 }
584
585 }