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