90e2516e0aa89184e894c8a2ef591562211e49ae
[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
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('checkFolderActionPermission', '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->setUserPermissions(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->setUserPermissions($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->setUserPermissions(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->setUserPermissions(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->addFileMount('/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 * @expectedException \RuntimeException
537 */
538 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
539 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
540 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
541 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
542 $driverMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), array(), '', FALSE);
543 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
544 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
545 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
546 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
547 $fixture->_set('driver', $driverMock);
548 $fixture->deleteFolder($folderMock, FALSE);
549 }
550
551 /**
552 * @test
553 */
554 public function createFolderCallsDriverForFolderCreation() {
555 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
556 $this->prepareFixture(array(), TRUE);
557 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
558 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
559 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
560 $this->fixture->createFolder('newFolder', $mockedParentFolder);
561 }
562
563 /**
564 * @test
565 */
566 public function createFolderCanRecursivelyCreateFolders() {
567 $this->addToMount(array('someFolder' => array()));
568 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
569 $this->prepareFixture(array(), TRUE, $mockedDriver);
570 $parentFolder = $this->fixture->getFolder('/someFolder/');
571 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
572 $this->assertEquals('secondSubfolder', $newFolder->getName());
573 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
574 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
575 }
576
577 /**
578 * @test
579 */
580 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
581 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
582 $this->prepareFixture(array(), TRUE);
583 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
584 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
585 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
586 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
587 }
588
589 /**
590 * @test
591 */
592 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
593 $mockedRootFolder = $this->getSimpleFolderMock('/');
594 $this->prepareFixture(array(), TRUE);
595 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
596 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
597 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
598 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
599 $this->fixture->createFolder('someFolder');
600 }
601
602 /**
603 * @test
604 */
605 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
606 $this->addToMount(array(
607 'existingFolder' => array()
608 ));
609 $this->initializeVfs();
610 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
611 $this->prepareFixture(array(), TRUE, $mockedDriver);
612 $rootFolder = $this->fixture->getFolder('/');
613 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
614 $this->assertEquals('someFolder', $newFolder->getName());
615 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
616 }
617
618 /**
619 * @test
620 */
621 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
622 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
623 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
624 $this->prepareFixture(array(), TRUE);
625 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
626 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
627 $this->fixture->createFolder('newFolder', $mockedParentFolder);
628 }
629
630 /**
631 * @test
632 */
633 public function replaceFileFailsIfLocalFileDoesNotExist() {
634 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
635 $this->prepareFixture(array(), TRUE);
636 $mockedFile = $this->getSimpleFileMock('/someFile');
637 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
638 }
639
640 /**
641 * @test
642 */
643 public function getFileListHandsOverRecursiveFALSEifNotExplicitlySet() {
644 $this->prepareFixture(array());
645 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
646 $driver->expects($this->once())
647 ->method('getFileList')
648 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
649 ->will($this->returnValue(array()));
650 $this->fixture->getFileList('/');
651 }
652
653 /**
654 * @test
655 */
656 public function getFileListHandsOverRecursiveTRUEifSet() {
657
658 $this->prepareFixture(array());
659 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
660 $driver->expects($this->once())
661 ->method('getFileList')
662 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
663 ->will($this->returnValue(array()));
664 $this->fixture->getFileList('/', 0, 0, TRUE, TRUE, TRUE);
665 }
666
667 /**
668 * @test
669 */
670 public function getRoleReturnsDefaultForRegularFolders() {
671 $folderIdentifier = uniqid();
672 $this->addToMount(array(
673 $folderIdentifier => array()
674 ));
675 $this->prepareFixture(array());
676
677 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
678
679 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
680 }
681
682 /**
683 * @test
684 */
685 public function getRoleReturnsCorrectValueForDefaultProcessingFolder() {
686 $this->prepareFixture(array());
687
688 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . ResourceStorage::DEFAULT_ProcessingFolder . '/'));
689
690 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
691 }
692
693 /**
694 * @test
695 */
696 public function getRoleReturnsCorrectValueForConfiguredProcessingFolder() {
697 $folderIdentifier = uniqid();
698 $this->addToMount(array(
699 $folderIdentifier => array()
700 ));
701 $this->prepareFixture(array(), FALSE, NULL, array('processingfolder' => '/' . $folderIdentifier . '/'));
702
703 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
704
705 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
706 }
707
708 /**
709 * Data provider for fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder function
710 */
711 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider() {
712 return array(
713 'Empty folderList returned' => array(
714 'path' => '/',
715 'processingFolder' => '_processed_',
716 'folderList' => array(),
717 'expectedItems' => array()
718 ),
719 'Empty _processed_ folder' => array(
720 'path' => '/',
721 'processingFolder' => '',
722 'folderList' => array(
723 '_processed_' => array(),
724 '_temp_' => array(),
725 'user_upload' => array()
726 ),
727 'expectedItems' => array(
728 'user_upload' => array(),
729 '_temp_' => array()
730 )
731 ),
732 '_processed_ folder not in folder list' => array(
733 'path' => '/',
734 'processingFolder' => '_processed_',
735 'folderList' => array(
736 '_temp_' => array()
737 ),
738 'expectedItems' => array(
739 '_temp_' => array()
740 )
741 ),
742 '_processed_ folder on root level' => array(
743 'path' => '/',
744 'processingFolder' => '_processed_',
745 'folderList' => array(
746 '_processed_' => array(),
747 '_temp_' => array(),
748 'user_upload' => array()
749 ),
750 'expectedItems' => array(
751 'user_upload' => array(),
752 '_temp_' => array()
753 )
754 ),
755 '_processed_ folder on second level' => array(
756 'path' => 'Public/',
757 'processingFolder' => 'Public/_processed_',
758 'folderList' => array(
759 '_processed_' => array(),
760 '_temp_' => array(),
761 'user_upload' => array()
762 ),
763 'expectedItems' => array(
764 'user_upload' => array(),
765 '_temp_' => array()
766 )
767 ),
768 '_processed_ folder on third level' => array(
769 'path' => 'Public/Files/',
770 'processingFolder' => 'Public/Files/_processed_',
771 'folderList' => array(
772 '_processed_' => array(),
773 '_temp_' => array(),
774 'user_upload' => array()
775 ),
776 'expectedItems' => array(
777 'user_upload' => array(),
778 '_temp_' => array()
779 )
780 )
781 );
782 }
783
784 /**
785 * @test
786 * @dataProvider fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider
787 */
788 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder($path, $processingFolder, $folderList, $expectedItems) {
789 $driverMock = $this->createDriverMock(array(), NULL, array('getFolderList', 'folderExists'));
790 $driverMock->expects($this->once())->method('getFolderList')->will($this->returnValue($folderList));
791 if (!empty($expectedItems)) {
792 // This function is called only if there were any folders retrieved
793 $driverMock->expects($this->once())->method('folderExists')->will($this->returnValue(TRUE));
794 }
795
796 $this->prepareFixture(array(), FALSE, $driverMock, array('processingfolder' => $processingFolder));
797
798 $this->assertSame($expectedItems, $this->fixture->fetchFolderListFromDriver($path));
799 }
800 }
801
802 ?>