[BUGFIX] FAL: Misleading default filter array name
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ResourceStorageTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 require_once 'vfsStream/vfsStream.php';
28
29 /**
30 * Testcase for the VFS mount class
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
35 */
36 class ResourceStorageTest extends \TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase {
37
38 /**
39 * @var array A backup of registered singleton instances
40 */
41 protected $singletonInstances = array();
42
43 /**
44 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
45 */
46 private $fixture;
47
48 public function setUp() {
49 parent::setUp();
50 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
51 \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
52 }
53
54 public function tearDown() {
55 parent::tearDown();
56 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
57 }
58
59 /**
60 * Prepare fixture
61 *
62 * @param array $configuration
63 * @param boolean $mockPermissionChecks
64 * @return void
65 */
66 protected function prepareFixture($configuration, $mockPermissionChecks = FALSE, $driverObject = NULL, array $storageRecord = array()) {
67 $permissionMethods = array('isFileActionAllowed', 'isFolderActionAllowed', 'checkFileActionPermission', 'checkUserActionPermission');
68 $mockedMethods = NULL;
69 $configuration = $this->convertConfigurationArrayToFlexformXml($configuration);
70 $storageRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($storageRecord, array(
71 'configuration' => $configuration
72 ));
73 if ($driverObject == NULL) {
74 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\AbstractDriver */
75 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
76 }
77 if ($mockPermissionChecks) {
78 $mockedMethods = $permissionMethods;
79 }
80 if ($mockedMethods === NULL) {
81 $this->fixture = new \TYPO3\CMS\Core\Resource\ResourceStorage($driverObject, $storageRecord);
82 } else {
83 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', $mockedMethods, array($driverObject, $storageRecord));
84 foreach ($permissionMethods as $method) {
85 $this->fixture->expects($this->any())->method($method)->will($this->returnValue(TRUE));
86 }
87 }
88 }
89
90 /**
91 * Converts a simple configuration array into a FlexForm data structure serialized as XML
92 *
93 * @param array $configuration
94 * @return string
95 * @see t3lib_div::array2xml()
96 */
97 protected function convertConfigurationArrayToFlexformXml(array $configuration) {
98 $flexformArray = array('data' => array('sDEF' => array('lDEF' => array())));
99 foreach ($configuration as $key => $value) {
100 $flexformArray['data']['sDEF']['lDEF'][$key] = array('vDEF' => $value);
101 }
102 $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml($flexformArray);
103 return $configuration;
104 }
105
106 /**
107 * Creates a driver fixture object, optionally using a given mount object.
108 *
109 * IMPORTANT: Call this only after setting up the virtual file system (with the addTo* methods)!
110 *
111 * @param $driverConfiguration
112 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject
113 * @param array $mockedDriverMethods
114 * @return \TYPO3\CMS\Core\Resource\Driver\LocalDriver
115 */
116 protected function createDriverMock($driverConfiguration, \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject = NULL, $mockedDriverMethods = array()) {
117 $this->initializeVfs();
118 if ($storageObject == NULL) {
119 $storageObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
120 }
121
122 if (!isset($driverConfiguration['basePath'])) {
123 $driverConfiguration['basePath'] = $this->getMountRootUrl();
124 }
125
126 if ($mockedDriverMethods === NULL) {
127 $driver = new \TYPO3\CMS\Core\Resource\Driver\LocalDriver($driverConfiguration);
128 } else {
129 // We are using the LocalDriver here because PHPUnit can't mock concrete methods in abstract classes, so
130 // when using the AbstractDriver we would be in trouble when wanting to mock away some concrete method
131 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', $mockedDriverMethods, array($driverConfiguration));
132 }
133 $storageObject->setDriver($driver);
134 $driver->setStorage($storageObject);
135 $driver->processConfiguration();
136 $driver->initialize();
137 return $driver;
138 }
139
140 /**
141 * @test
142 */
143 public function baseUriGetsSlashAppended() {
144 $uri = 'http://example.org/somewhere/else';
145 $this->prepareFixture(array('baseUri' => $uri));
146 $this->assertEquals($uri . '/', $this->fixture->getBaseUri());
147 }
148
149 public function capabilitiesDataProvider() {
150 return array(
151 'only public' => array(
152 array(
153 'public' => TRUE,
154 'writable' => FALSE,
155 'browsable' => FALSE
156 )
157 ),
158 'only writable' => array(
159 array(
160 'public' => FALSE,
161 'writable' => TRUE,
162 'browsable' => FALSE
163 )
164 ),
165 'only browsable' => array(
166 array(
167 'public' => FALSE,
168 'writable' => FALSE,
169 'browsable' => TRUE
170 )
171 ),
172 'all capabilities' => array(
173 array(
174 'public' => TRUE,
175 'writable' => TRUE,
176 'browsable' => TRUE
177 )
178 ),
179 'none' => array(
180 array(
181 'public' => FALSE,
182 'writable' => FALSE,
183 'browsable' => FALSE
184 )
185 )
186 );
187 }
188
189 /**
190 * @test
191 * @dataProvider capabilitiesDataProvider
192 */
193 public function capabilitiesOfStorageObjectAreCorrectlySet(array $capabilites) {
194 $storageRecord = array(
195 'is_public' => $capabilites['public'],
196 'is_writable' => $capabilites['writable'],
197 'is_browsable' => $capabilites['browsable'],
198 'is_online' => TRUE
199 );
200 $mockedDriver = $this->createDriverMock(array(), $this->fixture, array('hasCapability'));
201 $mockedDriver->expects($this->any())->method('hasCapability')->will($this->returnValue(TRUE));
202 $this->prepareFixture(array(), FALSE, $mockedDriver, $storageRecord);
203 $this->assertEquals($capabilites['public'], $this->fixture->isPublic(), 'Capability "public" is not correctly set.');
204 $this->assertEquals($capabilites['writable'], $this->fixture->isWritable(), 'Capability "writable" is not correctly set.');
205 $this->assertEquals($capabilites['browsable'], $this->fixture->isBrowsable(), 'Capability "browsable" is not correctly set.');
206 }
207
208 /**
209 * @test
210 */
211 public function fileAndFolderListFiltersAreInitializedWithDefaultFilters() {
212 $this->prepareFixture(array());
213 $this->assertEquals($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'], $this->fixture->getFileAndFolderNameFilters());
214 }
215
216 /**
217 * @test
218 */
219 public function addFileFailsIfFileDoesNotExist() {
220 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
221 $this->setExpectedException('InvalidArgumentException', '', 1319552745);
222 $this->prepareFixture(array());
223 $this->fixture->addFile('/some/random/file', $mockedFolder);
224 }
225
226 /**
227 * @test
228 */
229 public function addFileCallsDriverWithCorrectArguments() {
230 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
231 $this->addToMount(array(
232 'targetFolder' => array(),
233 'file.ext' => 'ajslkd'
234 ));
235 $this->initializeVfs();
236 $localFilePath = $this->getUrlInMount('file.ext');
237 $this->prepareFixture(array());
238 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
239 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
240 $driver->expects($this->once())->method('addFile')->with($this->equalTo($localFilePath), $this->anything(), $this->equalTo('file.ext'));
241 $this->fixture->setDriver($driver);
242 $this->fixture->addFile($localFilePath, $mockedFolder);
243 }
244
245 /**
246 * @test
247 */
248 public function addFileChangesFilenameIfFileExists() {
249 $mockedFolder = $this->getSimpleFolderMock('/');
250 $this->addToMount(array(
251 'targetFolder' => array(
252 'file.ext' => 'asdf',
253 'file_01.ext' => 'asjdlkajs'
254 ),
255 'file.ext' => 'ajslkd'
256 ));
257 $this->initializeVfs();
258 $this->prepareFixture(array());
259 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
260 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile', 'fileExistsInFolder'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
261 $driver->expects($this->once())->method('addFile')->with($this->anything(), $this->anything(), $this->equalTo('file_02.ext'));
262 $driver->expects($this->exactly(3))->method('fileExistsInFolder')->will($this->onConsecutiveCalls($this->returnValue(TRUE), $this->returnValue(TRUE), $this->returnValue(FALSE)));
263 $this->fixture->setDriver($driver);
264 $this->fixture->addFile($this->getUrlInMount('file.ext'), $mockedFolder);
265 }
266
267 /**
268 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
269 *
270 * @return array
271 */
272 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
273 return array(
274 'read action on readable/writable folder' => array(
275 'read',
276 array('r' => TRUE, 'w' => TRUE),
277 ),
278 'read action on unreadable folder' => array(
279 'read',
280 array('r' => FALSE, 'w' => TRUE),
281 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderReadPermissionsException'
282 ),
283 'write action on read-only folder' => array(
284 'write',
285 array('r' => TRUE, 'w' => FALSE),
286 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderWritePermissionsException'
287 )
288 );
289 }
290
291 /**
292 * @test
293 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
294 * @param string $action 'read' or 'write'
295 * @param array $permissionsFromDriver The permissions as returned from the driver
296 * @param boolean $expectedException
297 */
298 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedException = '') {
299 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
300 $mockedDriver->expects($this->any())->method('getFolderPermissions')->will($this->returnValue($permissionsFromDriver));
301 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
302 // Let all other checks pass
303 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
304 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
305 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
306 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
307 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
308 $fixture->setDriver($mockedDriver);
309 if ($expectedException == '') {
310 $this->assertTrue($fixture->checkFolderActionPermission($action, $mockedFolder));
311 } else {
312 $this->markTestSkipped('The exception has been disable in TYPO3\\CMS\\Core\\Resource\\ResourceStorage');
313 $this->setExpectedException($expectedException);
314 $fixture->checkFolderActionPermission($action, $mockedFolder);
315 }
316 }
317
318 /**
319 * @test
320 */
321 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
322 $this->prepareFixture(array());
323 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
324 }
325
326 /**
327 * @test
328 */
329 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
330 $this->prepareFixture(array());
331 $this->fixture->injectUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
332 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
333 }
334
335 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
336 return array(
337 'all lower cased' => array(
338 array('readFolder' => TRUE),
339 'read',
340 'folder'
341 ),
342 'all upper case' => array(
343 array('readFolder' => TRUE),
344 'READ',
345 'FOLDER'
346 ),
347 'mixed case' => array(
348 array('readFolder' => TRUE),
349 'ReaD',
350 'FoLdEr'
351 )
352 );
353 }
354
355 /**
356 * @test
357 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
358 */
359 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
360 $this->prepareFixture(array());
361 $this->fixture->injectUserPermissions($permissions);
362 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
363 }
364
365 /**
366 * @test
367 */
368 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
369 $this->prepareFixture(array());
370 $this->fixture->injectUserPermissions(array('readFolder' => FALSE));
371 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
372 }
373
374 /**
375 * @test
376 */
377 public function userActionIsDisallowedIfPermissionIsNotSet() {
378 $this->prepareFixture(array());
379 $this->fixture->injectUserPermissions(array('readFolder' => TRUE));
380 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
381 }
382
383 /**
384 * @test
385 * @group integration
386 */
387 public function setFileContentsUpdatesObjectProperties() {
388 $fileContents = 'asdf';
389 $this->initializeVfs();
390 $this->prepareFixture(array(), TRUE);
391 $fileProperties = array(
392 'someProperty' => 'value',
393 'someOtherProperty' => 42
394 );
395 $hash = 'asdfg';
396 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
397 $driver->expects($this->once())->method('getFileInfo')->will($this->returnValue($fileProperties));
398 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
399 $this->fixture->setDriver($driver);
400 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
401 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
402 $mockedFile->expects($this->once())->method('updateProperties')->with($this->equalTo(array_merge($fileProperties, array('sha1' => $hash))));
403 $this->fixture->setFileContents($mockedFile, uniqid());
404 }
405
406 /**
407 * @test
408 * @group integration
409 */
410 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
411 $localFilePath = '/path/to/localFile';
412 $sourceFileIdentifier = '/sourceFile.ext';
413 $this->addToMount(array(
414 'targetFolder' => array()
415 ));
416 $this->initializeVfs();
417 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
418 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
419 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
420 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
421 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
422 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
423 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
424 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
425 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
426 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
427 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
428 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
429 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
430 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
431 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
432 }
433
434 /**
435 * @test
436 * @group integration
437 */
438 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
439 $localFilePath = '/path/to/localFile';
440 $sourceFileIdentifier = '/sourceFile.ext';
441 $this->addToMount(array(
442 'targetFolder' => array()
443 ));
444 $this->initializeVfs();
445 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
446 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
447 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
448 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
449 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
450 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
451 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
452 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
453 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
454 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
455 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
456 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
457 }
458
459 /**
460 * @test
461 * @group integration
462 */
463 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
464 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
465 $this->addToMount(array(
466 'mountFolder' => array(
467 'file' => 'asdfg'
468 )
469 ));
470 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
471 $this->initializeVfs();
472 $this->prepareFixture(array(), NULL, $mockedDriver);
473 $this->fixture->injectFileMount('/mountFolder');
474 $this->assertEquals(1, count($this->fixture->getFileMounts()));
475 $this->fixture->isWithinFileMountBoundaries($mockedFile);
476 }
477
478 /**
479 * This test is also valid for folders
480 *
481 * @test
482 */
483 public function getFileListReturnsFilesInCorrectOrder() {
484 $fileList = array(
485 'file10' => '',
486 'file2' => '',
487 'File' => '',
488 'fail' => ''
489 );
490 $this->prepareFixture(array());
491 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
492 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
493 $fileList = $this->fixture->getFileList('/');
494 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
495 }
496
497 /**
498 * @test
499 */
500 public function getFileListIgnoresCasingWhenSortingFilenames() {
501 $fileList = array(
502 'aFile' => 'dfsdg',
503 'zFile' => 'werw',
504 'BFile' => 'asd',
505 '12345' => 'fdsa',
506 'IMG_1234.jpg' => 'asdf'
507 );
508 $this->prepareFixture(array());
509 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
510 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
511 $fileList = $this->fixture->getFileList('/');
512 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
513 }
514
515 /**
516 * @test
517 */
518 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
519 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
520 $mockedDriver = $this->createDriverMock(array());
521 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
522 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
523 $this->prepareFixture(array(), TRUE);
524 $this->fixture->setDriver($mockedDriver);
525 $this->fixture->createFolder('newFolder', $mockedParentFolder);
526 }
527
528 /**
529 * @test
530 */
531 public function createFolderCallsDriverForFolderCreation() {
532 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
533 $this->prepareFixture(array(), TRUE);
534 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
535 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
536 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
537 $this->fixture->createFolder('newFolder', $mockedParentFolder);
538 }
539
540 /**
541 * @test
542 */
543 public function createFolderCanRecursivelyCreateFolders() {
544 $this->addToMount(array('someFolder' => array()));
545 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
546 $this->prepareFixture(array(), TRUE, $mockedDriver);
547 $parentFolder = $this->fixture->getFolder('/someFolder/');
548 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
549 $this->assertEquals('secondSubfolder', $newFolder->getName());
550 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
551 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
552 }
553
554 /**
555 * @test
556 */
557 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
558 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
559 $this->prepareFixture(array(), TRUE);
560 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
561 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
562 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
563 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
564 }
565
566 /**
567 * @test
568 */
569 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
570 $mockedRootFolder = $this->getSimpleFolderMock('/');
571 $this->prepareFixture(array(), TRUE);
572 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
573 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
574 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
575 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
576 $this->fixture->createFolder('someFolder');
577 }
578
579 /**
580 * @test
581 */
582 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
583 $this->addToMount(array(
584 'existingFolder' => array()
585 ));
586 $this->initializeVfs();
587 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
588 $this->prepareFixture(array(), TRUE, $mockedDriver);
589 $rootFolder = $this->fixture->getFolder('/');
590 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
591 $this->assertEquals('someFolder', $newFolder->getName());
592 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
593 }
594
595 /**
596 * @test
597 */
598 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
599 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
600 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
601 $this->prepareFixture(array(), TRUE);
602 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
603 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
604 $this->fixture->createFolder('newFolder', $mockedParentFolder);
605 }
606
607 /**
608 * @test
609 */
610 public function replaceFileFailsIfLocalFileDoesNotExist() {
611 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
612 $this->prepareFixture(array(), TRUE);
613 $mockedFile = $this->getSimpleFileMock('/someFile');
614 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
615 }
616
617 }
618
619 ?>