[BUGFIX] Warning in FAL StorageTest
[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 /**
241 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
242 *
243 * @return array
244 */
245 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
246 return array(
247 'read action on readable/writable folder' => array(
248 'read',
249 array('r' => TRUE, 'w' => TRUE),
250 ),
251 'read action on unreadable folder' => array(
252 'read',
253 array('r' => FALSE, 'w' => TRUE),
254 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderReadPermissionsException'
255 ),
256 'write action on read-only folder' => array(
257 'write',
258 array('r' => TRUE, 'w' => FALSE),
259 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderWritePermissionsException'
260 )
261 );
262 }
263
264 /**
265 * @test
266 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
267 * @param string $action 'read' or 'write'
268 * @param array $permissionsFromDriver The permissions as returned from the driver
269 * @param bool $expectedException
270 */
271 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedException = '') {
272 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
273 $mockedDriver->expects($this->any())->method('getFolderPermissions')->will($this->returnValue($permissionsFromDriver));
274 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
275 // let all other checks pass
276 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
277 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
278 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
279 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
280 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
281 $fixture->setDriver($mockedDriver);
282 if ($expectedException == '') {
283 $this->assertTrue($fixture->checkFolderActionPermission($action, $mockedFolder));
284 } else {
285 $this->markTestSkipped('The exception has been disable in TYPO3\\CMS\\Core\\Resource\\ResourceStorage');
286 $this->setExpectedException($expectedException);
287 $fixture->checkFolderActionPermission($action, $mockedFolder);
288 }
289 }
290
291 /**
292 * @test
293 */
294 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
295 $this->prepareFixture(array());
296 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
297 }
298
299 /**
300 * @test
301 */
302 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
303 $this->prepareFixture(array());
304 $this->fixture->injectUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
305 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
306 }
307
308 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
309 return array(
310 'all lower cased' => array(
311 array('readFolder' => TRUE),
312 'read',
313 'folder'
314 ),
315 'all upper case' => array(
316 array('readFolder' => TRUE),
317 'READ',
318 'FOLDER'
319 ),
320 'mixed case' => array(
321 array('readFolder' => TRUE),
322 'ReaD',
323 'FoLdEr'
324 )
325 );
326 }
327
328 /**
329 * @test
330 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
331 */
332 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
333 $this->prepareFixture(array());
334 $this->fixture->injectUserPermissions($permissions);
335 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
336 }
337
338 /**
339 * @test
340 */
341 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
342 $this->prepareFixture(array());
343 $this->fixture->injectUserPermissions(array('readFolder' => FALSE));
344 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
345 }
346
347 /**
348 * @test
349 */
350 public function userActionIsDisallowedIfPermissionIsNotSet() {
351 $this->prepareFixture(array());
352 $this->fixture->injectUserPermissions(array('readFolder' => TRUE));
353 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
354 }
355
356 /**
357 * @test
358 * @group integration
359 */
360 public function setFileContentsUpdatesObjectProperties() {
361 $fileContents = 'asdf';
362 $this->initializeVfs();
363 $this->prepareFixture(array(), TRUE);
364 $fileProperties = array(
365 'someProperty' => 'value',
366 'someOtherProperty' => 42
367 );
368 $hash = 'asdfg';
369 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
370 $driver->expects($this->once())->method('getFileInfo')->will($this->returnValue($fileProperties));
371 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
372 $this->fixture->setDriver($driver);
373 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
374 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
375 $mockedFile->expects($this->once())->method('updateProperties')->with($this->equalTo(array_merge($fileProperties, array('sha1' => $hash))));
376 $this->fixture->setFileContents($mockedFile, uniqid());
377 }
378
379 /**
380 * @test
381 * @group integration
382 */
383 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
384 $localFilePath = '/path/to/localFile';
385 $sourceFileIdentifier = '/sourceFile.ext';
386 $this->addToMount(array(
387 'targetFolder' => array()
388 ));
389 $this->initializeVfs();
390 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
391 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
392 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
393 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
394 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
395 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
396 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
397 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
398 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
399 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
400 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
401 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
402 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
403 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
404 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
405 }
406
407 /**
408 * @test
409 * @group integration
410 */
411 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
412 $localFilePath = '/path/to/localFile';
413 $sourceFileIdentifier = '/sourceFile.ext';
414 $this->addToMount(array(
415 'targetFolder' => array()
416 ));
417 $this->initializeVfs();
418 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
419 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
420 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
421 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
422 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
423 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
424 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
425 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
426 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
427 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
428 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
429 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
430 }
431
432 /**
433 * @test
434 * @group integration
435 */
436 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
437 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
438 $this->addToMount(array(
439 'mountFolder' => array(
440 'file' => 'asdfg'
441 )
442 ));
443 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
444 $this->initializeVfs();
445 $this->prepareFixture(array(), NULL, $mockedDriver);
446 $this->fixture->injectFileMount('/mountFolder');
447 $this->assertEquals(1, count($this->fixture->getFileMounts()));
448 $this->fixture->isWithinFileMountBoundaries($mockedFile);
449 }
450
451 /**
452 * This test is also valid for folders
453 *
454 * @test
455 */
456 public function getFileListReturnsFilesInCorrectOrder() {
457 $fileList = array(
458 'file10' => '',
459 'file2' => '',
460 'File' => '',
461 'fail' => ''
462 );
463 $this->prepareFixture(array());
464 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
465 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
466 $fileList = $this->fixture->getFileList('/');
467 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
468 }
469
470 /**
471 * @test
472 */
473 public function getFileListIgnoresCasingWhenSortingFilenames() {
474 $fileList = array(
475 'aFile' => 'dfsdg',
476 'zFile' => 'werw',
477 'BFile' => 'asd',
478 '12345' => 'fdsa',
479 'IMG_1234.jpg' => 'asdf'
480 );
481 $this->prepareFixture(array());
482 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
483 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
484 $fileList = $this->fixture->getFileList('/');
485 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
486 }
487
488 /**
489 * @test
490 */
491 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
492 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
493 $mockedDriver = $this->createDriverMock(array());
494 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
495 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
496 $this->prepareFixture(array(), TRUE);
497 $this->fixture->setDriver($mockedDriver);
498 $this->fixture->createFolder('newFolder', $mockedParentFolder);
499 }
500
501 /**
502 * @test
503 */
504 public function createFolderCallsDriverForFolderCreation() {
505 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
506 $this->prepareFixture(array(), TRUE);
507 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
508 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
509 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
510 $this->fixture->createFolder('newFolder', $mockedParentFolder);
511 }
512
513 /**
514 * @test
515 */
516 public function createFolderCanRecursivelyCreateFolders() {
517 $this->addToMount(array('someFolder' => array()));
518 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
519 $this->prepareFixture(array(), TRUE, $mockedDriver);
520 $parentFolder = $this->fixture->getFolder('/someFolder/');
521 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
522 $this->assertEquals('secondSubfolder', $newFolder->getName());
523 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
524 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
525 }
526
527 /**
528 * @test
529 */
530 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
531 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
532 $this->prepareFixture(array(), TRUE);
533 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
534 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
535 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
536 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
537 }
538
539 /**
540 * @test
541 */
542 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
543 $mockedRootFolder = $this->getSimpleFolderMock('/');
544 $this->prepareFixture(array(), TRUE);
545 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
546 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
547 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
548 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
549 $this->fixture->createFolder('someFolder');
550 }
551
552 /**
553 * @test
554 */
555 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
556 $this->addToMount(array(
557 'existingFolder' => array()
558 ));
559 $this->initializeVfs();
560 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
561 $this->prepareFixture(array(), TRUE, $mockedDriver);
562 $rootFolder = $this->fixture->getFolder('/');
563 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
564 $this->assertEquals('someFolder', $newFolder->getName());
565 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
566 }
567
568 /**
569 * @test
570 */
571 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
572 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
573 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
574 $this->prepareFixture(array(), TRUE);
575 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
576 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
577 $this->fixture->createFolder('newFolder', $mockedParentFolder);
578 }
579
580 /**
581 * @test
582 */
583 public function replaceFileFailsIfLocalFileDoesNotExist() {
584 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
585 $this->prepareFixture(array(), TRUE);
586 $mockedFile = $this->getSimpleFileMock('/someFile');
587 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
588 }
589
590 }
591
592 ?>