[BUGFIX] Storage is offline but is still used
[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 * @test
275 */
276 public function getPublicUrlReturnsNullIfStorageIsNotOnline() {
277 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
278 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
279 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
280 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isOnline'), array($driver, array('configuration' => array())));
281 $fixture->expects($this->once())->method('isOnline')->will($this->returnValue(FALSE));
282
283 $sourceFileIdentifier = '/sourceFile.ext';
284 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
285 $result = $fixture->getPublicUrl($sourceFile);
286 $this->assertSame($result, NULL);
287 }
288
289 /**
290 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
291 *
292 * @return array
293 */
294 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
295 return array(
296 'read action on readable/writable folder' => array(
297 'read',
298 array('r' => TRUE, 'w' => TRUE),
299 ),
300 'read action on unreadable folder' => array(
301 'read',
302 array('r' => FALSE, 'w' => TRUE),
303 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderReadPermissionsException'
304 ),
305 'write action on read-only folder' => array(
306 'write',
307 array('r' => TRUE, 'w' => FALSE),
308 'TYPO3\\CMS\\Core\\Resource\\Exception\\InsufficientFolderWritePermissionsException'
309 )
310 );
311 }
312
313 /**
314 * @test
315 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
316 * @param string $action 'read' or 'write'
317 * @param array $permissionsFromDriver The permissions as returned from the driver
318 * @param boolean $expectedException
319 */
320 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedException = '') {
321 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
322 $mockedDriver->expects($this->any())->method('getFolderPermissions')->will($this->returnValue($permissionsFromDriver));
323 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
324 // Let all other checks pass
325 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
326 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
327 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
328 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
329 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
330 $fixture->setDriver($mockedDriver);
331 if ($expectedException == '') {
332 $this->assertTrue($fixture->checkFolderActionPermission($action, $mockedFolder));
333 } else {
334 $this->markTestSkipped('The exception has been disable in TYPO3\\CMS\\Core\\Resource\\ResourceStorage');
335 $this->setExpectedException($expectedException);
336 $fixture->checkFolderActionPermission($action, $mockedFolder);
337 }
338 }
339
340 /**
341 * @test
342 */
343 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
344 $this->prepareFixture(array());
345 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
346 }
347
348 /**
349 * @test
350 */
351 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
352 $this->prepareFixture(array());
353 $this->fixture->setUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
354 $this->assertTrue($this->fixture->checkUserActionPermission('read', 'folder'));
355 }
356
357 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
358 return array(
359 'all lower cased' => array(
360 array('readFolder' => TRUE),
361 'read',
362 'folder'
363 ),
364 'all upper case' => array(
365 array('readFolder' => TRUE),
366 'READ',
367 'FOLDER'
368 ),
369 'mixed case' => array(
370 array('readFolder' => TRUE),
371 'ReaD',
372 'FoLdEr'
373 )
374 );
375 }
376
377 /**
378 * @test
379 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
380 */
381 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments($permissions, $action, $type) {
382 $this->prepareFixture(array());
383 $this->fixture->setUserPermissions($permissions);
384 $this->assertTrue($this->fixture->checkUserActionPermission($action, $type));
385 }
386
387 /**
388 * @test
389 */
390 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
391 $this->prepareFixture(array());
392 $this->fixture->setUserPermissions(array('readFolder' => FALSE));
393 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
394 }
395
396 /**
397 * @test
398 */
399 public function userActionIsDisallowedIfPermissionIsNotSet() {
400 $this->prepareFixture(array());
401 $this->fixture->setUserPermissions(array('readFolder' => TRUE));
402 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
403 }
404
405 /**
406 * @test
407 * @group integration
408 */
409 public function setFileContentsUpdatesObjectProperties() {
410 $fileContents = 'asdf';
411 $this->initializeVfs();
412 $this->prepareFixture(array(), TRUE);
413 $fileProperties = array(
414 'someProperty' => 'value',
415 'someOtherProperty' => 42
416 );
417 $hash = 'asdfg';
418 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
419 $driver->expects($this->once())->method('getFileInfo')->will($this->returnValue($fileProperties));
420 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
421 $this->fixture->setDriver($driver);
422 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
423 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue('/file.ext'));
424 $mockedFile->expects($this->once())->method('updateProperties')->with($this->equalTo(array_merge($fileProperties, array('sha1' => $hash))));
425 $this->fixture->setFileContents($mockedFile, uniqid());
426 }
427
428 /**
429 * @test
430 * @group integration
431 */
432 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
433 $localFilePath = '/path/to/localFile';
434 $sourceFileIdentifier = '/sourceFile.ext';
435 $this->addToMount(array(
436 'targetFolder' => array()
437 ));
438 $this->initializeVfs();
439 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
440 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
441 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
442 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
443 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
444 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
445 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
446 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
447 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
448 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
449 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
450 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
451 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
452 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
453 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
454 }
455
456 /**
457 * @test
458 * @group integration
459 */
460 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
461 $localFilePath = '/path/to/localFile';
462 $sourceFileIdentifier = '/sourceFile.ext';
463 $this->addToMount(array(
464 'targetFolder' => array()
465 ));
466 $this->initializeVfs();
467 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
468 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
469 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
470 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
471 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
472 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
473 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
474 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
475 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
476 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
477 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
478 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
479 }
480
481 /**
482 * @test
483 * @group integration
484 */
485 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
486 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
487 $this->addToMount(array(
488 'mountFolder' => array(
489 'file' => 'asdfg'
490 )
491 ));
492 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
493 $this->initializeVfs();
494 $this->prepareFixture(array(), NULL, $mockedDriver);
495 $this->fixture->addFileMount('/mountFolder');
496 $this->assertEquals(1, count($this->fixture->getFileMounts()));
497 $this->fixture->isWithinFileMountBoundaries($mockedFile);
498 }
499
500 /**
501 * This test is also valid for folders
502 *
503 * @test
504 */
505 public function getFileListReturnsFilesInCorrectOrder() {
506 $fileList = array(
507 'file10' => '',
508 'file2' => '',
509 'File' => '',
510 'fail' => ''
511 );
512 $this->prepareFixture(array());
513 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
514 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
515 $fileList = $this->fixture->getFileList('/');
516 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
517 }
518
519 /**
520 * @test
521 */
522 public function getFileListIgnoresCasingWhenSortingFilenames() {
523 $fileList = array(
524 'aFile' => 'dfsdg',
525 'zFile' => 'werw',
526 'BFile' => 'asd',
527 '12345' => 'fdsa',
528 'IMG_1234.jpg' => 'asdf'
529 );
530 $this->prepareFixture(array());
531 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
532 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
533 $fileList = $this->fixture->getFileList('/');
534 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
535 }
536
537 /**
538 * @test
539 */
540 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
541 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
542 $mockedDriver = $this->createDriverMock(array());
543 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
544 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
545 $this->prepareFixture(array(), TRUE);
546 $this->fixture->setDriver($mockedDriver);
547 $this->fixture->createFolder('newFolder', $mockedParentFolder);
548 }
549
550 /**
551 * @test
552 * @expectedException \RuntimeException
553 */
554 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
555 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
556 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
557 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
558 $driverMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), array(), '', FALSE);
559 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
560 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
561 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
562 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
563 $fixture->_set('driver', $driverMock);
564 $fixture->deleteFolder($folderMock, FALSE);
565 }
566
567 /**
568 * @test
569 */
570 public function createFolderCallsDriverForFolderCreation() {
571 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
572 $this->prepareFixture(array(), TRUE);
573 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
574 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
575 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
576 $this->fixture->createFolder('newFolder', $mockedParentFolder);
577 }
578
579 /**
580 * @test
581 */
582 public function createFolderCanRecursivelyCreateFolders() {
583 $this->addToMount(array('someFolder' => array()));
584 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
585 $this->prepareFixture(array(), TRUE, $mockedDriver);
586 $parentFolder = $this->fixture->getFolder('/someFolder/');
587 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
588 $this->assertEquals('secondSubfolder', $newFolder->getName());
589 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
590 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
591 }
592
593 /**
594 * @test
595 */
596 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
597 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
598 $this->prepareFixture(array(), TRUE);
599 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
600 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
601 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
602 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
603 }
604
605 /**
606 * @test
607 */
608 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
609 $mockedRootFolder = $this->getSimpleFolderMock('/');
610 $this->prepareFixture(array(), TRUE);
611 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
612 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
613 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
614 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
615 $this->fixture->createFolder('someFolder');
616 }
617
618 /**
619 * @test
620 */
621 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
622 $this->addToMount(array(
623 'existingFolder' => array()
624 ));
625 $this->initializeVfs();
626 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
627 $this->prepareFixture(array(), TRUE, $mockedDriver);
628 $rootFolder = $this->fixture->getFolder('/');
629 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
630 $this->assertEquals('someFolder', $newFolder->getName());
631 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
632 }
633
634 /**
635 * @test
636 */
637 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
638 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
639 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
640 $this->prepareFixture(array(), TRUE);
641 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
642 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
643 $this->fixture->createFolder('newFolder', $mockedParentFolder);
644 }
645
646 /**
647 * @test
648 */
649 public function replaceFileFailsIfLocalFileDoesNotExist() {
650 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
651 $this->prepareFixture(array(), TRUE);
652 $mockedFile = $this->getSimpleFileMock('/someFile');
653 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
654 }
655
656 /**
657 * @test
658 */
659 public function getFileListHandsOverRecursiveFALSEifNotExplicitlySet() {
660 $this->prepareFixture(array());
661 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
662 $driver->expects($this->once())
663 ->method('getFileList')
664 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
665 ->will($this->returnValue(array()));
666 $this->fixture->getFileList('/');
667 }
668
669 /**
670 * @test
671 */
672 public function getFileListHandsOverRecursiveTRUEifSet() {
673
674 $this->prepareFixture(array());
675 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
676 $driver->expects($this->once())
677 ->method('getFileList')
678 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
679 ->will($this->returnValue(array()));
680 $this->fixture->getFileList('/', 0, 0, TRUE, TRUE, TRUE);
681 }
682
683 /**
684 * @test
685 */
686 public function getRoleReturnsDefaultForRegularFolders() {
687 $folderIdentifier = uniqid();
688 $this->addToMount(array(
689 $folderIdentifier => array()
690 ));
691 $this->prepareFixture(array());
692
693 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
694
695 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
696 }
697
698 /**
699 * @test
700 */
701 public function getRoleReturnsCorrectValueForDefaultProcessingFolder() {
702 $this->prepareFixture(array());
703
704 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . ResourceStorage::DEFAULT_ProcessingFolder . '/'));
705
706 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
707 }
708
709 /**
710 * @test
711 */
712 public function getRoleReturnsCorrectValueForConfiguredProcessingFolder() {
713 $folderIdentifier = uniqid();
714 $this->addToMount(array(
715 $folderIdentifier => array()
716 ));
717 $this->prepareFixture(array(), FALSE, NULL, array('processingfolder' => '/' . $folderIdentifier . '/'));
718
719 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
720
721 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
722 }
723
724 /**
725 * Data provider for fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder function
726 */
727 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider() {
728 return array(
729 'Empty folderList returned' => array(
730 'path' => '/',
731 'processingFolder' => '_processed_',
732 'folderList' => array(),
733 'expectedItems' => array()
734 ),
735 'Empty _processed_ folder' => array(
736 'path' => '/',
737 'processingFolder' => '',
738 'folderList' => array(
739 '_processed_' => array(),
740 '_temp_' => array(),
741 'user_upload' => array()
742 ),
743 'expectedItems' => array(
744 'user_upload' => array(),
745 '_temp_' => array()
746 )
747 ),
748 '_processed_ folder not in folder list' => array(
749 'path' => '/',
750 'processingFolder' => '_processed_',
751 'folderList' => array(
752 '_temp_' => array()
753 ),
754 'expectedItems' => array(
755 '_temp_' => array()
756 )
757 ),
758 '_processed_ folder on root level' => array(
759 'path' => '/',
760 'processingFolder' => '_processed_',
761 'folderList' => array(
762 '_processed_' => array(),
763 '_temp_' => array(),
764 'user_upload' => array()
765 ),
766 'expectedItems' => array(
767 'user_upload' => array(),
768 '_temp_' => array()
769 )
770 ),
771 '_processed_ folder on second level' => array(
772 'path' => 'Public/',
773 'processingFolder' => 'Public/_processed_',
774 'folderList' => array(
775 '_processed_' => array(),
776 '_temp_' => array(),
777 'user_upload' => array()
778 ),
779 'expectedItems' => array(
780 'user_upload' => array(),
781 '_temp_' => array()
782 )
783 ),
784 '_processed_ folder on third level' => array(
785 'path' => 'Public/Files/',
786 'processingFolder' => 'Public/Files/_processed_',
787 'folderList' => array(
788 '_processed_' => array(),
789 '_temp_' => array(),
790 'user_upload' => array()
791 ),
792 'expectedItems' => array(
793 'user_upload' => array(),
794 '_temp_' => array()
795 )
796 )
797 );
798 }
799
800 /**
801 * @test
802 * @dataProvider fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider
803 */
804 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder($path, $processingFolder, $folderList, $expectedItems) {
805 $driverMock = $this->createDriverMock(array(), NULL, array('getFolderList', 'folderExists'));
806 $driverMock->expects($this->once())->method('getFolderList')->will($this->returnValue($folderList));
807 if (!empty($expectedItems)) {
808 // This function is called only if there were any folders retrieved
809 $driverMock->expects($this->once())->method('folderExists')->will($this->returnValue(TRUE));
810 }
811
812 $this->prepareFixture(array(), FALSE, $driverMock, array('processingfolder' => $processingFolder));
813
814 $this->assertSame($expectedItems, $this->fixture->fetchFolderListFromDriver($path));
815 }
816 }
817
818 ?>