[TASK] Merge submodule workspaces into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ResourceStorageTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 use TYPO3\CMS\Core\Resource\ResourceStorage;
5
6 /***************************************************************
7 * Copyright notice
8 *
9 * (c) 2011-2013 Andreas Wolf <andreas.wolf@ikt-werk.de>
10 * All rights reserved
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 require_once 'vfsStream/vfsStream.php';
29
30 /**
31 * Testcase for the VFS mount class
32 *
33 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
34 */
35 class ResourceStorageTest extends \TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase {
36
37 /**
38 * @var array A backup of registered singleton instances
39 */
40 protected $singletonInstances = array();
41
42 /**
43 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
44 */
45 private $fixture;
46
47 public function setUp() {
48 parent::setUp();
49 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
50 \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
51 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(
52 'TYPO3\\CMS\\Core\\Resource\\FileRepository',
53 $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileRepository')
54 );
55
56 }
57
58 public function tearDown() {
59 parent::tearDown();
60 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
61 }
62
63 /**
64 * Prepare fixture
65 *
66 * @param array $configuration
67 * @param boolean $mockPermissionChecks
68 * @return void
69 */
70 protected function prepareFixture($configuration, $mockPermissionChecks = FALSE, $driverObject = NULL, array $storageRecord = array()) {
71 $permissionMethods = array('checkFolderActionPermission', 'checkFileActionPermission', 'checkUserActionPermission');
72 $mockedMethods = NULL;
73 $configuration = $this->convertConfigurationArrayToFlexformXml($configuration);
74 $storageRecord = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($storageRecord, array(
75 'configuration' => $configuration
76 ));
77 if ($driverObject == NULL) {
78 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\AbstractDriver */
79 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
80 }
81 if ($mockPermissionChecks) {
82 $mockedMethods = $permissionMethods;
83 }
84 if ($mockedMethods === NULL) {
85 $this->fixture = new \TYPO3\CMS\Core\Resource\ResourceStorage($driverObject, $storageRecord);
86 } else {
87 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', $mockedMethods, array($driverObject, $storageRecord));
88 foreach ($permissionMethods as $method) {
89 $this->fixture->expects($this->any())->method($method)->will($this->returnValue(TRUE));
90 }
91 }
92 }
93
94 /**
95 * Converts a simple configuration array into a FlexForm data structure serialized as XML
96 *
97 * @param array $configuration
98 * @return string
99 * @see \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml()
100 */
101 protected function convertConfigurationArrayToFlexformXml(array $configuration) {
102 $flexformArray = array('data' => array('sDEF' => array('lDEF' => array())));
103 foreach ($configuration as $key => $value) {
104 $flexformArray['data']['sDEF']['lDEF'][$key] = array('vDEF' => $value);
105 }
106 $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml($flexformArray);
107 return $configuration;
108 }
109
110 /**
111 * Creates a driver fixture object, optionally using a given mount object.
112 *
113 * IMPORTANT: Call this only after setting up the virtual file system (with the addTo* methods)!
114 *
115 * @param $driverConfiguration
116 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject
117 * @param array $mockedDriverMethods
118 * @return \TYPO3\CMS\Core\Resource\Driver\LocalDriver
119 */
120 protected function createDriverMock($driverConfiguration, \TYPO3\CMS\Core\Resource\ResourceStorage $storageObject = NULL, $mockedDriverMethods = array()) {
121 $this->initializeVfs();
122 if ($storageObject == NULL) {
123 $storageObject = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
124 }
125
126 if (!isset($driverConfiguration['basePath'])) {
127 $driverConfiguration['basePath'] = $this->getMountRootUrl();
128 }
129
130 if ($mockedDriverMethods === NULL) {
131 $driver = new \TYPO3\CMS\Core\Resource\Driver\LocalDriver($driverConfiguration);
132 } else {
133 // We are using the LocalDriver here because PHPUnit can't mock concrete methods in abstract classes, so
134 // when using the AbstractDriver we would be in trouble when wanting to mock away some concrete method
135 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', $mockedDriverMethods, array($driverConfiguration));
136 }
137 $storageObject->setDriver($driver);
138 $driver->setStorage($storageObject);
139 $driver->processConfiguration();
140 $driver->initialize();
141 return $driver;
142 }
143
144 /**
145 * @test
146 */
147 public function baseUriGetsSlashAppended() {
148 $uri = 'http://example.org/somewhere/else';
149 $this->prepareFixture(array('baseUri' => $uri));
150 $this->assertEquals($uri . '/', $this->fixture->getBaseUri());
151 }
152
153 /**
154 * @return array
155 */
156 public function capabilitiesDataProvider() {
157 return array(
158 'only public' => array(
159 array(
160 'public' => TRUE,
161 'writable' => FALSE,
162 'browsable' => FALSE
163 )
164 ),
165 'only writable' => array(
166 array(
167 'public' => FALSE,
168 'writable' => TRUE,
169 'browsable' => FALSE
170 )
171 ),
172 'only browsable' => array(
173 array(
174 'public' => FALSE,
175 'writable' => FALSE,
176 'browsable' => TRUE
177 )
178 ),
179 'all capabilities' => array(
180 array(
181 'public' => TRUE,
182 'writable' => TRUE,
183 'browsable' => TRUE
184 )
185 ),
186 'none' => array(
187 array(
188 'public' => FALSE,
189 'writable' => FALSE,
190 'browsable' => FALSE
191 )
192 )
193 );
194 }
195
196 /**
197 * @test
198 * @dataProvider capabilitiesDataProvider
199 */
200 public function capabilitiesOfStorageObjectAreCorrectlySet(array $capabilites) {
201 $storageRecord = array(
202 'is_public' => $capabilites['public'],
203 'is_writable' => $capabilites['writable'],
204 'is_browsable' => $capabilites['browsable'],
205 'is_online' => TRUE
206 );
207 $mockedDriver = $this->createDriverMock(array(), $this->fixture, array('hasCapability'));
208 $mockedDriver->expects($this->any())->method('hasCapability')->will($this->returnValue(TRUE));
209 $this->prepareFixture(array(), FALSE, $mockedDriver, $storageRecord);
210 $this->assertEquals($capabilites['public'], $this->fixture->isPublic(), 'Capability "public" is not correctly set.');
211 $this->assertEquals($capabilites['writable'], $this->fixture->isWritable(), 'Capability "writable" is not correctly set.');
212 $this->assertEquals($capabilites['browsable'], $this->fixture->isBrowsable(), 'Capability "browsable" is not correctly set.');
213 }
214
215 /**
216 * @test
217 */
218 public function fileAndFolderListFiltersAreInitializedWithDefaultFilters() {
219 $this->prepareFixture(array());
220 $this->assertEquals($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'], $this->fixture->getFileAndFolderNameFilters());
221 }
222
223 /**
224 * @test
225 */
226 public function addFileFailsIfFileDoesNotExist() {
227 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
228 $this->setExpectedException('InvalidArgumentException', '', 1319552745);
229 $this->prepareFixture(array());
230 $this->fixture->addFile('/some/random/file', $mockedFolder);
231 }
232
233 /**
234 * @test
235 */
236 public function addFileCallsDriverWithCorrectArguments() {
237 $mockedFolder = $this->getSimpleFolderMock('/targetFolder/');
238 $this->addToMount(array(
239 'targetFolder' => array(),
240 'file.ext' => 'ajslkd'
241 ));
242 $this->initializeVfs();
243 $localFilePath = $this->getUrlInMount('file.ext');
244 $this->prepareFixture(array());
245 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
246 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
247 $driver->expects($this->once())->method('addFile')->with($this->equalTo($localFilePath), $this->anything(), $this->equalTo('file.ext'));
248 $this->fixture->setDriver($driver);
249 $this->fixture->addFile($localFilePath, $mockedFolder);
250 }
251
252 /**
253 * @test
254 */
255 public function addFileChangesFilenameIfFileExists() {
256 $mockedFolder = $this->getSimpleFolderMock('/');
257 $this->addToMount(array(
258 'targetFolder' => array(
259 'file.ext' => 'asdf',
260 'file_01.ext' => 'asjdlkajs'
261 ),
262 'file.ext' => 'ajslkd'
263 ));
264 $this->initializeVfs();
265 $this->prepareFixture(array());
266 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
267 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile', 'fileExistsInFolder'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
268 $driver->expects($this->once())->method('addFile')->with($this->anything(), $this->anything(), $this->equalTo('file_02.ext'));
269 $driver->expects($this->exactly(3))->method('fileExistsInFolder')->will($this->onConsecutiveCalls($this->returnValue(TRUE), $this->returnValue(TRUE), $this->returnValue(FALSE)));
270 $this->fixture->setDriver($driver);
271 $this->fixture->addFile($this->getUrlInMount('file.ext'), $mockedFolder);
272 }
273
274 /**
275 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
276 *
277 * @return array
278 */
279 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
280 return array(
281 'read action on readable/writable folder' => array(
282 'read',
283 array('r' => TRUE, 'w' => TRUE),
284 ),
285 'read action on unreadable folder' => array(
286 'read',
287 array('r' => FALSE, 'w' => TRUE),
288 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderReadPermissionsException'
289 ),
290 'write action on read-only folder' => array(
291 'write',
292 array('r' => TRUE, 'w' => FALSE),
293 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderWritePermissionsException'
294 )
295 );
296 }
297
298 /**
299 * @test
300 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
301 * @param string $action 'read' or 'write'
302 * @param array $permissionsFromDriver The permissions as returned from the driver
303 * @param boolean $expectedException
304 */
305 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedException = '') {
306 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
307 $mockedDriver->expects($this->any())->method('getFolderPermissions')->will($this->returnValue($permissionsFromDriver));
308 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
309 // Let all other checks pass
310 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
311 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
312 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
313 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
314 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
315 $fixture->setDriver($mockedDriver);
316 if ($expectedException == '') {
317 $this->assertTrue($fixture->checkFolderActionPermission($action, $mockedFolder));
318 } else {
319 $this->markTestSkipped('The exception has been disable in TYPO3\\CMS\\Core\\Resource\\ResourceStorage');
320 $this->setExpectedException($expectedException);
321 $fixture->checkFolderActionPermission($action, $mockedFolder);
322 }
323 }
324
325 /**
326 * @test
327 */
328 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
329 $this->prepareFixture(array());
330 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
331 }
332
333 /**
334 * @test
335 */
336 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
337 $this->prepareFixture(array());
338 $this->fixture->setUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
339 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
340 }
341
342 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
343 return array(
344 'all lower cased' => array(
345 array('readFolder' => TRUE),
346 'read',
347 'folder'
348 ),
349 'all upper case' => array(
350 array('readFolder' => TRUE),
351 'READ',
352 'FOLDER'
353 ),
354 'mixed case' => array(
355 array('readFolder' => TRUE),
356 'ReaD',
357 'FoLdEr'
358 )
359 );
360 }
361
362 /**
363 * @test
364 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
365 */
366 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
367 $this->prepareFixture(array());
368 $this->fixture->setUserPermissions($permissions);
369 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
370 }
371
372 /**
373 * @test
374 */
375 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
376 $this->prepareFixture(array());
377 $this->fixture->setUserPermissions(array('readFolder' => FALSE));
378 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
379 }
380
381 /**
382 * @test
383 */
384 public function userActionIsDisallowedIfPermissionIsNotSet() {
385 $this->prepareFixture(array());
386 $this->fixture->setUserPermissions(array('readFolder' => TRUE));
387 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
388 }
389
390 /**
391 * @test
392 * @group integration
393 */
394 public function setFileContentsUpdatesObjectProperties() {
395 $fileContents = 'asdf';
396 $this->initializeVfs();
397 $this->prepareFixture(array(), TRUE);
398 $fileProperties = array(
399 'someProperty' => 'value',
400 'someOtherProperty' => 42
401 );
402 $hash = 'asdfg';
403 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
404 $driver->expects($this->once())->method('getFileInfo')->will($this->returnValue($fileProperties));
405 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
406 $this->fixture->setDriver($driver);
407 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
408 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
409 $mockedFile->expects($this->once())->method('updateProperties')->with($this->equalTo(array_merge($fileProperties, array('sha1' => $hash))));
410 $this->fixture->setFileContents($mockedFile, uniqid());
411 }
412
413 /**
414 * @test
415 * @group integration
416 */
417 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
418 $localFilePath = '/path/to/localFile';
419 $sourceFileIdentifier = '/sourceFile.ext';
420 $this->addToMount(array(
421 'targetFolder' => array()
422 ));
423 $this->initializeVfs();
424 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
425 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
426 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
427 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
428 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
429 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
430 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
431 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
432 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
433 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
434 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
435 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
436 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
437 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
438 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
439 }
440
441 /**
442 * @test
443 * @group integration
444 */
445 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
446 $localFilePath = '/path/to/localFile';
447 $sourceFileIdentifier = '/sourceFile.ext';
448 $this->addToMount(array(
449 'targetFolder' => array()
450 ));
451 $this->initializeVfs();
452 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
453 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
454 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
455 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
456 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
457 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
458 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
459 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
460 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
461 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
462 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
463 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
464 }
465
466 /**
467 * @test
468 * @group integration
469 */
470 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
471 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
472 $this->addToMount(array(
473 'mountFolder' => array(
474 'file' => 'asdfg'
475 )
476 ));
477 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
478 $this->initializeVfs();
479 $this->prepareFixture(array(), NULL, $mockedDriver);
480 $this->fixture->addFileMount('/mountFolder');
481 $this->assertEquals(1, count($this->fixture->getFileMounts()));
482 $this->fixture->isWithinFileMountBoundaries($mockedFile);
483 }
484
485 /**
486 * This test is also valid for folders
487 *
488 * @test
489 */
490 public function getFileListReturnsFilesInCorrectOrder() {
491 $fileList = array(
492 'file10' => '',
493 'file2' => '',
494 'File' => '',
495 'fail' => ''
496 );
497 $this->prepareFixture(array());
498 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
499 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
500 $fileList = $this->fixture->getFileList('/');
501 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
502 }
503
504 /**
505 * @test
506 */
507 public function getFileListIgnoresCasingWhenSortingFilenames() {
508 $fileList = array(
509 'aFile' => 'dfsdg',
510 'zFile' => 'werw',
511 'BFile' => 'asd',
512 '12345' => 'fdsa',
513 'IMG_1234.jpg' => 'asdf'
514 );
515 $this->prepareFixture(array());
516 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
517 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
518 $fileList = $this->fixture->getFileList('/');
519 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
520 }
521
522 /**
523 * @test
524 */
525 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
526 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
527 $mockedDriver = $this->createDriverMock(array());
528 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
529 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
530 $this->prepareFixture(array(), TRUE);
531 $this->fixture->setDriver($mockedDriver);
532 $this->fixture->createFolder('newFolder', $mockedParentFolder);
533 }
534
535 /**
536 * @test
537 */
538 public function createFolderCallsDriverForFolderCreation() {
539 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
540 $this->prepareFixture(array(), TRUE);
541 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
542 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
543 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
544 $this->fixture->createFolder('newFolder', $mockedParentFolder);
545 }
546
547 /**
548 * @test
549 */
550 public function createFolderCanRecursivelyCreateFolders() {
551 $this->addToMount(array('someFolder' => array()));
552 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
553 $this->prepareFixture(array(), TRUE, $mockedDriver);
554 $parentFolder = $this->fixture->getFolder('/someFolder/');
555 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
556 $this->assertEquals('secondSubfolder', $newFolder->getName());
557 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
558 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
559 }
560
561 /**
562 * @test
563 */
564 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
565 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
566 $this->prepareFixture(array(), TRUE);
567 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
568 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
569 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
570 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
571 }
572
573 /**
574 * @test
575 */
576 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
577 $mockedRootFolder = $this->getSimpleFolderMock('/');
578 $this->prepareFixture(array(), TRUE);
579 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
580 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
581 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
582 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
583 $this->fixture->createFolder('someFolder');
584 }
585
586 /**
587 * @test
588 */
589 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
590 $this->addToMount(array(
591 'existingFolder' => array()
592 ));
593 $this->initializeVfs();
594 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
595 $this->prepareFixture(array(), TRUE, $mockedDriver);
596 $rootFolder = $this->fixture->getFolder('/');
597 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
598 $this->assertEquals('someFolder', $newFolder->getName());
599 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
600 }
601
602 /**
603 * @test
604 */
605 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
606 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
607 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
608 $this->prepareFixture(array(), TRUE);
609 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
610 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
611 $this->fixture->createFolder('newFolder', $mockedParentFolder);
612 }
613
614 /**
615 * @test
616 */
617 public function replaceFileFailsIfLocalFileDoesNotExist() {
618 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
619 $this->prepareFixture(array(), TRUE);
620 $mockedFile = $this->getSimpleFileMock('/someFile');
621 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
622 }
623
624 /**
625 * @test
626 */
627 public function getFileListHandsOverRecursiveFALSEifNotExplicitlySet() {
628 $this->prepareFixture(array());
629 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
630 $driver->expects($this->once())
631 ->method('getFileList')
632 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
633 ->will($this->returnValue(array()));
634 $this->fixture->getFileList('/');
635 }
636
637 /**
638 * @test
639 */
640 public function getFileListHandsOverRecursiveTRUEifSet() {
641
642 $this->prepareFixture(array());
643 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
644 $driver->expects($this->once())
645 ->method('getFileList')
646 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
647 ->will($this->returnValue(array()));
648 $this->fixture->getFileList('/', 0, 0, TRUE, TRUE, TRUE);
649 }
650
651 /**
652 * @test
653 */
654 public function getRoleReturnsDefaultForRegularFolders() {
655 $folderIdentifier = uniqid();
656 $this->addToMount(array(
657 $folderIdentifier => array()
658 ));
659 $this->prepareFixture(array());
660
661 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
662
663 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
664 }
665
666 /**
667 * @test
668 */
669 public function getRoleReturnsCorrectValueForDefaultProcessingFolder() {
670 $this->prepareFixture(array());
671
672 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . ResourceStorage::DEFAULT_ProcessingFolder . '/'));
673
674 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
675 }
676
677 /**
678 * @test
679 */
680 public function getRoleReturnsCorrectValueForConfiguredProcessingFolder() {
681 $folderIdentifier = uniqid();
682 $this->addToMount(array(
683 $folderIdentifier => array()
684 ));
685 $this->prepareFixture(array(), FALSE, NULL, array('processingfolder' => '/' . $folderIdentifier . '/'));
686
687 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
688
689 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
690 }
691
692 /**
693 * Data provider for fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder function
694 */
695 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider() {
696 return array(
697 'Empty folderList returned' => array(
698 'path' => '/',
699 'processingFolder' => '_processed_',
700 'folderList' => array(),
701 'expectedItems' => array()
702 ),
703 'Empty _processed_ folder' => array(
704 'path' => '/',
705 'processingFolder' => '',
706 'folderList' => array(
707 '_processed_' => array(),
708 '_temp_' => array(),
709 'user_upload' => array()
710 ),
711 'expectedItems' => array(
712 'user_upload' => array(),
713 '_temp_' => array()
714 )
715 ),
716 '_processed_ folder not in folder list' => array(
717 'path' => '/',
718 'processingFolder' => '_processed_',
719 'folderList' => array(
720 '_temp_' => array()
721 ),
722 'expectedItems' => array(
723 '_temp_' => array()
724 )
725 ),
726 '_processed_ folder on root level' => array(
727 'path' => '/',
728 'processingFolder' => '_processed_',
729 'folderList' => array(
730 '_processed_' => array(),
731 '_temp_' => array(),
732 'user_upload' => array()
733 ),
734 'expectedItems' => array(
735 'user_upload' => array(),
736 '_temp_' => array()
737 )
738 ),
739 '_processed_ folder on second level' => array(
740 'path' => 'Public/',
741 'processingFolder' => 'Public/_processed_',
742 'folderList' => array(
743 '_processed_' => array(),
744 '_temp_' => array(),
745 'user_upload' => array()
746 ),
747 'expectedItems' => array(
748 'user_upload' => array(),
749 '_temp_' => array()
750 )
751 ),
752 '_processed_ folder on third level' => array(
753 'path' => 'Public/Files/',
754 'processingFolder' => 'Public/Files/_processed_',
755 'folderList' => array(
756 '_processed_' => array(),
757 '_temp_' => array(),
758 'user_upload' => array()
759 ),
760 'expectedItems' => array(
761 'user_upload' => array(),
762 '_temp_' => array()
763 )
764 )
765 );
766 }
767
768 /**
769 * @test
770 * @dataProvider fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider
771 */
772 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder($path, $processingFolder, $folderList, $expectedItems) {
773 $driverMock = $this->createDriverMock(array(), NULL, array('getFolderList', 'folderExists'));
774 $driverMock->expects($this->once())->method('getFolderList')->will($this->returnValue($folderList));
775 if (!empty($expectedItems)) {
776 // This function is called only if there were any folders retrieved
777 $driverMock->expects($this->once())->method('folderExists')->will($this->returnValue(TRUE));
778 }
779
780 $this->prepareFixture(array(), FALSE, $driverMock, array('processingfolder' => $processingFolder));
781
782 $this->assertSame($expectedItems, $this->fixture->fetchFolderListFromDriver($path));
783 }
784 }
785
786 ?>