[BUGFIX] Renaming file does not update hash values
[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('assureFileAddPermissions', 'checkFolderActionPermission', 'checkFileActionPermission', 'checkUserActionPermission', 'checkFileExtensionPermission', 'isWithinFileMountBoundaries');
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(), TRUE);
244 /** @var $file \TYPO3\CMS\Core\Resource\FileInterface */
245 $file = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileInterface', array(), array(), '', FALSE);
246 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
247 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
248 $driver->expects($this->once())->method('addFile')->with($this->equalTo($localFilePath), $this->anything(), $this->equalTo('file.ext'))->will($this->returnValue($file));
249 $this->fixture->setDriver($driver);
250 $this->fixture->addFile($localFilePath, $mockedFolder);
251 }
252
253 /**
254 * @test
255 */
256 public function addFileChangesFilenameIfFileExists() {
257 $mockedFolder = $this->getSimpleFolderMock('/');
258 $this->addToMount(array(
259 'targetFolder' => array(
260 'file.ext' => 'asdf',
261 'file_01.ext' => 'asjdlkajs'
262 ),
263 'file.ext' => 'ajslkd'
264 ));
265 $this->initializeVfs();
266 $this->prepareFixture(array(), TRUE);
267 /** @var $file \TYPO3\CMS\Core\Resource\FileInterface */
268 $file = $this->getMock('TYPO3\\CMS\\Core\\Resource\\FileInterface', array(), array(), '', FALSE);
269 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
270 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array('addFile', 'fileExistsInFolder'), array(array('basePath' => $this->getUrlInMount('targetFolder/'))));
271 $driver->expects($this->once())->method('addFile')->with($this->anything(), $this->anything(), $this->equalTo('file_02.ext'))->will($this->returnValue($file));
272 $driver->expects($this->exactly(3))->method('fileExistsInFolder')->will($this->onConsecutiveCalls($this->returnValue(TRUE), $this->returnValue(TRUE), $this->returnValue(FALSE)));
273 $this->fixture->setDriver($driver);
274 $this->fixture->addFile($this->getUrlInMount('file.ext'), $mockedFolder);
275 }
276
277 /**
278 * @test
279 */
280 public function getPublicUrlReturnsNullIfStorageIsNotOnline() {
281 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
282 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
283 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
284 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isOnline'), array($driver, array('configuration' => array())));
285 $fixture->expects($this->once())->method('isOnline')->will($this->returnValue(FALSE));
286
287 $sourceFileIdentifier = '/sourceFile.ext';
288 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
289 $result = $fixture->getPublicUrl($sourceFile);
290 $this->assertSame($result, NULL);
291 }
292
293 /**
294 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
295 *
296 * @return array
297 */
298 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
299 return array(
300 'read action on readable/writable folder' => array(
301 'read',
302 array('r' => TRUE, 'w' => TRUE),
303 TRUE
304 ),
305 'read action on unreadable folder' => array(
306 'read',
307 array('r' => FALSE, 'w' => TRUE),
308 FALSE
309 ),
310 'write action on read-only folder' => array(
311 'write',
312 array('r' => TRUE, 'w' => FALSE),
313 FALSE
314 )
315 );
316 }
317
318 /**
319 * @test
320 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
321 * @param string $action 'read' or 'write'
322 * @param array $permissionsFromDriver The permissions as returned from the driver
323 * @param boolean $expectedResult
324 */
325 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedResult) {
326 $mockedDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
327 $mockedDriver->expects($this->any())->method('getFolderPermissions')->will($this->returnValue($permissionsFromDriver));
328 $mockedFolder = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
329 // Let all other checks pass
330 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
331 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
332 $fixture->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
333 $fixture->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
334 $fixture->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
335 $fixture->setDriver($mockedDriver);
336
337 $this->assertSame($expectedResult, $fixture->checkFolderActionPermission($action, $mockedFolder));
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->setEvaluatePermissions(TRUE);
393 $this->fixture->setUserPermissions(array('readFolder' => FALSE));
394 $this->assertFalse($this->fixture->checkUserActionPermission('read', 'folder'));
395 }
396
397 /**
398 * @test
399 */
400 public function userActionIsDisallowedIfPermissionIsNotSet() {
401 $this->prepareFixture(array());
402 $this->fixture->setEvaluatePermissions(TRUE);
403 $this->fixture->setUserPermissions(array('readFolder' => TRUE));
404 $this->assertFalse($this->fixture->checkUserActionPermission('write', 'folder'));
405 }
406
407 /**
408 * @test
409 * @group integration
410 */
411 public function setFileContentsUpdatesObjectProperties() {
412 $this->initializeVfs();
413 $driverObject = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver', array(), '', FALSE);
414 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('getFileIndexRepository', 'checkFileActionPermission'), array($driverObject, array()));
415 $this->fixture->expects($this->any())->method('checkFileActionPermission')->will($this->returnValue(TRUE));
416 $fileInfo = array(
417 'storage' => 'A',
418 'identifier' => 'B',
419 'mtime' => 'C',
420 'ctime' => 'D',
421 'mimetype' => 'E',
422 'size' => 'F',
423 'name' => 'G',
424 );
425 $newProperties = array(
426 'storage' => $fileInfo['storage'],
427 'identifier' => $fileInfo['identifier'],
428 'tstamp' => $fileInfo['mtime'],
429 'crdate' => $fileInfo['ctime'],
430 'mime_type' => $fileInfo['mimetype'],
431 'size' => $fileInfo['size'],
432 'name' => $fileInfo['name']
433 );
434 $hash = 'asdfg';
435 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
436 $driver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfo));
437 $driver->expects($this->once())->method('hash')->will($this->returnValue($hash));
438 $this->fixture->setDriver($driver);
439 $indexFileRepositoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
440 $this->fixture->expects($this->any())->method('getFileIndexRepository')->will($this->returnValue($indexFileRepositoryMock));
441 $mockedFile = $this->getMock('TYPO3\\CMS\\Core\\Resource\\File', array(), array(), '', FALSE);
442 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue($fileInfo['identifier']));
443 $mockedFile->expects($this->at(1))->method('updateProperties')->with($this->equalTo(array('sha1' => $hash)));
444 $mockedFile->expects($this->at(4))->method('updateProperties')->with($this->equalTo($newProperties));
445 $indexFileRepositoryMock->expects($this->once())->method('update')->with($mockedFile);
446 $this->fixture->setFileContents($mockedFile, uniqid());
447 }
448
449 /**
450 * @test
451 * @group integration
452 */
453 public function moveFileCallsDriversRawMethodsWithCorrectArguments() {
454 $localFilePath = '/path/to/localFile';
455 $sourceFileIdentifier = '/sourceFile.ext';
456 $this->addToMount(array(
457 'targetFolder' => array()
458 ));
459 $this->initializeVfs();
460 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
461 $sourceDriver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver');
462 $sourceDriver->expects($this->once())->method('deleteFileRaw')->with($this->equalTo($sourceFileIdentifier));
463 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
464 $sourceStorage = new \TYPO3\CMS\Core\Resource\ResourceStorage($sourceDriver, array('configuration' => $configuration));
465 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
466 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
467 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
468 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
469 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
470 $driver->expects($this->once())->method('addFileRaw')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
471 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
472 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileMovePermissions', 'updateFile'), array($driver, array('configuration' => $configuration)));
473 $fixture->expects($this->once())->method('updateFile')->with($this->equalTo($sourceFile), $this->equalTo('/targetFolder/file.ext'));
474 $fixture->moveFile($sourceFile, $targetFolder, 'file.ext');
475 }
476
477 /**
478 * @test
479 * @group integration
480 */
481 public function copyFileCallsDriversRawMethodsWithCorrectArguments() {
482 $localFilePath = '/path/to/localFile';
483 $sourceFileIdentifier = '/sourceFile.ext';
484 $this->addToMount(array(
485 'targetFolder' => array()
486 ));
487 $this->initializeVfs();
488 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
489 $storageConfiguration = $this->convertConfigurationArrayToFlexformXml(array());
490 $sourceStorage = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE);
491 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
492 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
493 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
494 /** @var $driver \TYPO3\CMS\Core\Resource\Driver\LocalDriver */
495 $driver = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Driver\\LocalDriver', array(), array(array('basePath' => $this->getMountRootUrl())));
496 $driver->expects($this->once())->method('addFile')->with($localFilePath, $targetFolder, $this->equalTo('file.ext'));
497 /** @var $fixture \TYPO3\CMS\Core\Resource\ResourceStorage */
498 $fixture = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('assureFileCopyPermissions'), array($driver, array('configuration' => $storageConfiguration)));
499 $fixture->copyFile($sourceFile, $targetFolder, 'file.ext');
500 }
501
502 /**
503 * @test
504 * @group integration
505 */
506 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
507 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
508 $this->addToMount(array(
509 'mountFolder' => array(
510 'file' => 'asdfg'
511 )
512 ));
513 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
514 $this->initializeVfs();
515 $this->prepareFixture(array(), NULL, $mockedDriver);
516 $this->fixture->addFileMount('/mountFolder');
517 $this->assertEquals(1, count($this->fixture->getFileMounts()));
518 $this->fixture->isWithinFileMountBoundaries($mockedFile);
519 }
520
521 /**
522 * This test is also valid for folders
523 *
524 * @test
525 */
526 public function getFileListReturnsFilesInCorrectOrder() {
527 $fileList = array(
528 'file10' => '',
529 'file2' => '',
530 'File' => '',
531 'fail' => ''
532 );
533 $this->prepareFixture(array(), TRUE);
534 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
535 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
536 $fileList = $this->fixture->getFileList('/');
537 $this->assertEquals(array('fail', 'File', 'file2', 'file10'), array_keys($fileList));
538 }
539
540 /**
541 * @test
542 */
543 public function getFileListIgnoresCasingWhenSortingFilenames() {
544 $fileList = array(
545 'aFile' => 'dfsdg',
546 'zFile' => 'werw',
547 'BFile' => 'asd',
548 '12345' => 'fdsa',
549 'IMG_1234.jpg' => 'asdf'
550 );
551 $this->prepareFixture(array(), TRUE);
552 $driver = $this->createDriverMock(array(), $this->fixture, array('getFileList'));
553 $driver->expects($this->once())->method('getFileList')->will($this->returnValue($fileList));
554 $fileList = $this->fixture->getFileList('/');
555 $this->assertEquals(array('12345', 'aFile', 'BFile', 'IMG_1234.jpg', 'zFile'), array_keys($fileList));
556 }
557
558 /**
559 * @test
560 */
561 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
562 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
563 $mockedDriver = $this->createDriverMock(array());
564 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
565 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
566 $this->prepareFixture(array(), TRUE);
567 $this->fixture->setDriver($mockedDriver);
568 $this->fixture->createFolder('newFolder', $mockedParentFolder);
569 }
570
571 /**
572 * @test
573 * @expectedException \RuntimeException
574 */
575 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
576 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
577 $folderMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\Folder', array(), array(), '', FALSE);
578 /** @var \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
579 $driverMock = $this->getMockForAbstractClass('TYPO3\\CMS\\Core\\Resource\\Driver\\AbstractDriver');
580 $driverMock->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
581 /** @var \TYPO3\CMS\Core\Resource\ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $fixture */
582 $fixture = $this->getAccessibleMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array('checkFolderActionPermission'), array(), '', FALSE);
583 $fixture->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
584 $fixture->_set('driver', $driverMock);
585 $fixture->deleteFolder($folderMock, FALSE);
586 }
587
588 /**
589 * @test
590 */
591 public function createFolderCallsDriverForFolderCreation() {
592 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
593 $this->prepareFixture(array(), TRUE);
594 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
595 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo($mockedParentFolder))->will($this->returnValue(TRUE));
596 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
597 $this->fixture->createFolder('newFolder', $mockedParentFolder);
598 }
599
600 /**
601 * @test
602 */
603 public function createFolderCanRecursivelyCreateFolders() {
604 $this->addToMount(array('someFolder' => array()));
605 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
606 $this->prepareFixture(array(), TRUE, $mockedDriver);
607 $parentFolder = $this->fixture->getFolder('/someFolder/');
608 $newFolder = $this->fixture->createFolder('subFolder/secondSubfolder', $parentFolder);
609 $this->assertEquals('secondSubfolder', $newFolder->getName());
610 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
611 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
612 }
613
614 /**
615 * @test
616 */
617 public function createFolderIgnoresLeadingAndTrailingSlashesWithFoldername() {
618 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
619 $this->prepareFixture(array(), TRUE);
620 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
621 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
622 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('subFolder'));
623 $this->fixture->createFolder('/subFolder/', $mockedParentFolder);
624 }
625
626 /**
627 * @test
628 */
629 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
630 $mockedRootFolder = $this->getSimpleFolderMock('/');
631 $this->prepareFixture(array(), TRUE);
632 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
633 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue($mockedRootFolder));
634 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/'))->will($this->returnValue(TRUE));
635 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
636 $this->fixture->createFolder('someFolder');
637 }
638
639 /**
640 * @test
641 */
642 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
643 $this->addToMount(array(
644 'existingFolder' => array()
645 ));
646 $this->initializeVfs();
647 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
648 $this->prepareFixture(array(), TRUE, $mockedDriver);
649 $rootFolder = $this->fixture->getFolder('/');
650 $newFolder = $this->fixture->createFolder('existingFolder/someFolder', $rootFolder);
651 $this->assertEquals('someFolder', $newFolder->getName());
652 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
653 }
654
655 /**
656 * @test
657 */
658 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
659 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
660 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
661 $this->prepareFixture(array(), TRUE);
662 $mockedDriver = $this->createDriverMock(array(), $this->fixture);
663 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
664 $this->fixture->createFolder('newFolder', $mockedParentFolder);
665 }
666
667 /**
668 * @test
669 */
670 public function replaceFileFailsIfLocalFileDoesNotExist() {
671 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
672 $this->prepareFixture(array(), TRUE);
673 $mockedFile = $this->getSimpleFileMock('/someFile');
674 $this->fixture->replaceFile($mockedFile, PATH_site . uniqid());
675 }
676
677 /**
678 * @test
679 */
680 public function getFileListHandsOverRecursiveFALSEifNotExplicitlySet() {
681 $this->prepareFixture(array(), TRUE);
682 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
683 $driver->expects($this->once())
684 ->method('getFileList')
685 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), FALSE)
686 ->will($this->returnValue(array()));
687 $this->fixture->getFileList('/');
688 }
689
690 /**
691 * @test
692 */
693 public function getFileListHandsOverRecursiveTRUEifSet() {
694
695 $this->prepareFixture(array(), TRUE);
696 $driver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), $this->fixture, array('getFileList'));
697 $driver->expects($this->once())
698 ->method('getFileList')
699 ->with($this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything(), TRUE)
700 ->will($this->returnValue(array()));
701 $this->fixture->getFileList('/', 0, 0, TRUE, TRUE, TRUE);
702 }
703
704 /**
705 * @test
706 */
707 public function getRoleReturnsDefaultForRegularFolders() {
708 $folderIdentifier = uniqid();
709 $this->addToMount(array(
710 $folderIdentifier => array()
711 ));
712 $this->prepareFixture(array());
713
714 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
715
716 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_DEFAULT, $role);
717 }
718
719 /**
720 * @test
721 */
722 public function getRoleReturnsCorrectValueForDefaultProcessingFolder() {
723 $this->prepareFixture(array());
724
725 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . ResourceStorage::DEFAULT_ProcessingFolder . '/'));
726
727 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
728 }
729
730 /**
731 * @test
732 */
733 public function getRoleReturnsCorrectValueForConfiguredProcessingFolder() {
734 $folderIdentifier = uniqid();
735 $this->addToMount(array(
736 $folderIdentifier => array()
737 ));
738 $this->prepareFixture(array(), FALSE, NULL, array('processingfolder' => '/' . $folderIdentifier . '/'));
739
740 $role = $this->fixture->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
741
742 $this->assertSame(\TYPO3\CMS\Core\Resource\FolderInterface::ROLE_PROCESSING, $role);
743 }
744
745 /**
746 * Data provider for fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder function
747 */
748 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider() {
749 return array(
750 'Empty folderList returned' => array(
751 'path' => '/',
752 'processingFolder' => '_processed_',
753 'folderList' => array(),
754 'expectedItems' => array()
755 ),
756 'Empty _processed_ folder' => array(
757 'path' => '/',
758 'processingFolder' => '',
759 'folderList' => array(
760 '_processed_' => array(),
761 '_temp_' => array(),
762 'user_upload' => array()
763 ),
764 'expectedItems' => array(
765 'user_upload' => array(),
766 '_temp_' => array()
767 )
768 ),
769 '_processed_ folder not in folder list' => array(
770 'path' => '/',
771 'processingFolder' => '_processed_',
772 'folderList' => array(
773 '_temp_' => array()
774 ),
775 'expectedItems' => array(
776 '_temp_' => array()
777 )
778 ),
779 '_processed_ folder on root level' => array(
780 'path' => '/',
781 'processingFolder' => '_processed_',
782 'folderList' => array(
783 '_processed_' => array(),
784 '_temp_' => array(),
785 'user_upload' => array()
786 ),
787 'expectedItems' => array(
788 'user_upload' => array(),
789 '_temp_' => array()
790 )
791 ),
792 '_processed_ folder on second level' => array(
793 'path' => 'Public/',
794 'processingFolder' => 'Public/_processed_',
795 'folderList' => array(
796 '_processed_' => array(),
797 '_temp_' => array(),
798 'user_upload' => array()
799 ),
800 'expectedItems' => array(
801 'user_upload' => array(),
802 '_temp_' => array()
803 )
804 ),
805 '_processed_ folder on third level' => array(
806 'path' => 'Public/Files/',
807 'processingFolder' => 'Public/Files/_processed_',
808 'folderList' => array(
809 '_processed_' => array(),
810 '_temp_' => array(),
811 'user_upload' => array()
812 ),
813 'expectedItems' => array(
814 'user_upload' => array(),
815 '_temp_' => array()
816 )
817 )
818 );
819 }
820
821 /**
822 * @test
823 * @dataProvider fetchFolderListFromDriverReturnsFolderWithoutProcessedFolderDataProvider
824 */
825 public function fetchFolderListFromDriverReturnsFolderWithoutProcessedFolder($path, $processingFolder, $folderList, $expectedItems) {
826 $driverMock = $this->createDriverMock(array(), NULL, array('getFolderList', 'folderExists'));
827 $driverMock->expects($this->once())->method('getFolderList')->will($this->returnValue($folderList));
828 $driverMock->expects($this->any())->method('folderExists')->will($this->returnValue(TRUE));
829
830 $this->prepareFixture(array(), TRUE, $driverMock, array('processingfolder' => $processingFolder));
831
832 $this->assertSame($expectedItems, $this->fixture->fetchFolderListFromDriver($path));
833 }
834 }