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