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