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