[CLEANUP] Add missing empty lines before classes' closing curly braces
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / ResourceStorageTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\DatabaseConnection;
18 use TYPO3\CMS\Core\Resource\Driver\AbstractDriver;
19 use TYPO3\CMS\Core\Resource\Driver\LocalDriver;
20 use TYPO3\CMS\Core\Resource\File;
21 use TYPO3\CMS\Core\Resource\FileRepository;
22 use TYPO3\CMS\Core\Resource\Folder;
23 use TYPO3\CMS\Core\Resource\FolderInterface;
24 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
25 use TYPO3\CMS\Core\Resource\ResourceStorage;
26 use TYPO3\CMS\Core\Resource\ResourceStorageInterface;
27 use TYPO3\CMS\Core\Utility\ArrayUtility;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30 /**
31 * Test case for ResourceStorage class
32 *
33 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
34 */
35 class ResourceStorageTest extends BaseTestCase {
36
37 /**
38 * @var array A backup of registered singleton instances
39 */
40 protected $singletonInstances = array();
41
42 /**
43 * @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $subject;
46
47 public function setUp() {
48 parent::setUp();
49 $this->singletonInstances = GeneralUtility::getSingletonInstances();
50 /** @var FileRepository|\PHPUnit_Framework_MockObject_MockObject $fileRepositoryMock */
51 $fileRepositoryMock = $this->getMock(FileRepository::class);
52 GeneralUtility::setSingletonInstance(
53 FileRepository::class,
54 $fileRepositoryMock
55 );
56 $GLOBALS['TYPO3_DB'] = $this->getMock(DatabaseConnection::class);
57 }
58
59 public function tearDown() {
60 GeneralUtility::resetSingletonInstances($this->singletonInstances);
61 parent::tearDown();
62 }
63
64 /**
65 * Prepare ResourceStorage
66 *
67 * @param array $configuration
68 * @param bool $mockPermissionChecks
69 * @param AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverObject
70 * @param array $storageRecord
71 */
72 protected function prepareSubject(array $configuration, $mockPermissionChecks = FALSE, AbstractDriver $driverObject = NULL, array $storageRecord = array()) {
73 $permissionMethods = array('assureFileAddPermissions', 'checkFolderActionPermission', 'checkFileActionPermission', 'checkUserActionPermission', 'checkFileExtensionPermission', 'isWithinFileMountBoundaries');
74 $mockedMethods = array();
75 $configuration = $this->convertConfigurationArrayToFlexformXml($configuration);
76 $overruleArray = array('configuration' => $configuration);
77 ArrayUtility::mergeRecursiveWithOverrule($storageRecord, $overruleArray);
78 if ($driverObject == NULL) {
79 $driverObject = $this->getMockForAbstractClass(AbstractDriver::class, array(), '', FALSE);
80 }
81 if ($mockPermissionChecks) {
82 $mockedMethods = $permissionMethods;
83 }
84 $mockedMethods[] = 'getIndexer';
85
86 $this->subject = $this->getMock(ResourceStorage::class, $mockedMethods, array($driverObject, $storageRecord));
87 $this->subject->expects($this->any())->method('getIndexer')->will($this->returnValue($this->getMock(\TYPO3\CMS\Core\Resource\Index\Indexer::class, array(), array(), '', FALSE)));
88 foreach ($permissionMethods as $method) {
89 $this->subject->expects($this->any())->method($method)->will($this->returnValue(TRUE));
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 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 = 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 ResourceStorage $storageObject
116 * @param array $mockedDriverMethods
117 * @return \TYPO3\CMS\Core\Resource\Driver\LocalDriver|\PHPUnit_Framework_MockObject_MockObject
118 */
119 protected function createDriverMock($driverConfiguration, ResourceStorage $storageObject = NULL, $mockedDriverMethods = array()) {
120 $this->initializeVfs();
121
122 if (!isset($driverConfiguration['basePath'])) {
123 $driverConfiguration['basePath'] = $this->getMountRootUrl();
124 }
125
126 if ($mockedDriverMethods === NULL) {
127 $driver = new LocalDriver($driverConfiguration);
128 } else {
129 // We are using the LocalDriver here because PHPUnit can't mock concrete methods in abstract classes, so
130 // when using the AbstractDriver we would be in trouble when wanting to mock away some concrete method
131 $driver = $this->getMock(LocalDriver::class, $mockedDriverMethods, array($driverConfiguration));
132 }
133 if ($storageObject !== NULL) {
134 $storageObject->setDriver($driver);
135 }
136 $driver->setStorageUid(6);
137 $driver->processConfiguration();
138 $driver->initialize();
139 return $driver;
140 }
141
142 /**
143 * @return array
144 */
145 public function isWithinFileMountBoundariesDataProvider() {
146 return array(
147 'Access to file in ro file mount denied for write request' => array(
148 '$fileIdentifier' => '/fooBaz/bar.txt',
149 '$fileMountFolderIdentifier' => '/fooBaz/',
150 '$isFileMountReadOnly' => TRUE,
151 '$checkWriteAccess' => TRUE,
152 '$expectedResult' => FALSE,
153 ),
154 'Access to file in ro file mount allowed for read request' => array(
155 '$fileIdentifier' => '/fooBaz/bar.txt',
156 '$fileMountFolderIdentifier' => '/fooBaz/',
157 '$isFileMountReadOnly' => TRUE,
158 '$checkWriteAccess' => FALSE,
159 '$expectedResult' => TRUE,
160 ),
161 'Access to file in rw file mount allowed for write request' => array(
162 '$fileIdentifier' => '/fooBaz/bar.txt',
163 '$fileMountFolderIdentifier' => '/fooBaz/',
164 '$isFileMountReadOnly' => FALSE,
165 '$checkWriteAccess' => TRUE,
166 '$expectedResult' => TRUE,
167 ),
168 'Access to file in rw file mount allowed for read request' => array(
169 '$fileIdentifier' => '/fooBaz/bar.txt',
170 '$fileMountFolderIdentifier' => '/fooBaz/',
171 '$isFileMountReadOnly' => FALSE,
172 '$checkWriteAccess' => FALSE,
173 '$expectedResult' => TRUE,
174 ),
175 'Access to file not in file mount denied for write request' => array(
176 '$fileIdentifier' => '/fooBaz/bar.txt',
177 '$fileMountFolderIdentifier' => '/barBaz/',
178 '$isFileMountReadOnly' => FALSE,
179 '$checkWriteAccess' => TRUE,
180 '$expectedResult' => FALSE,
181 ),
182 'Access to file not in file mount denied for read request' => array(
183 '$fileIdentifier' => '/fooBaz/bar.txt',
184 '$fileMountFolderIdentifier' => '/barBaz/',
185 '$isFileMountReadOnly' => FALSE,
186 '$checkWriteAccess' => FALSE,
187 '$expectedResult' => FALSE,
188 ),
189 );
190 }
191
192 /**
193 * @param string $fileIdentifier
194 * @param string $fileMountFolderIdentifier
195 * @param bool $isFileMountReadOnly
196 * @param bool $checkWriteAccess
197 * @param bool $expectedResult
198 * @throws \TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException
199 * @test
200 * @dataProvider isWithinFileMountBoundariesDataProvider
201 */
202 public function isWithinFileMountBoundariesRespectsReadOnlyFileMounts($fileIdentifier, $fileMountFolderIdentifier, $isFileMountReadOnly, $checkWriteAccess, $expectedResult) {
203 /** @var AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
204 $driverMock = $this->getMockForAbstractClass(AbstractDriver::class, array(), '', FALSE);
205 $driverMock->expects($this->any())
206 ->method('getFolderInfoByIdentifier')
207 ->willReturnCallback(function($identifier) use ($isFileMountReadOnly) {
208 return array(
209 'identifier' => $identifier,
210 'name' => trim($identifier, '/'),
211 );
212 });
213 $driverMock->expects($this->any())
214 ->method('isWithin')
215 ->willReturnCallback(function($folderIdentifier, $fileIdentifier) {
216 if ($fileIdentifier === ResourceStorageInterface::DEFAULT_ProcessingFolder . '/') {
217 return FALSE;
218 } else {
219 return strpos($fileIdentifier, $folderIdentifier) === 0;
220 }
221 });
222 $this->prepareSubject(array(), FALSE, $driverMock);
223 $fileMock = $this->getSimpleFileMock($fileIdentifier);
224 $this->subject->setEvaluatePermissions(TRUE);
225 $this->subject->addFileMount('/' . uniqid('random') . '/', array('read_only' => FALSE));
226 $this->subject->addFileMount($fileMountFolderIdentifier, array('read_only' => $isFileMountReadOnly));
227 $this->subject->addFileMount('/' . uniqid('random') . '/', array('read_only' => FALSE));
228 $this->assertSame($expectedResult, $this->subject->isWithinFileMountBoundaries($fileMock, $checkWriteAccess));
229 }
230
231 /**
232 * @return array
233 */
234 public function capabilitiesDataProvider() {
235 return array(
236 'only public' => array(
237 array(
238 'public' => TRUE,
239 'writable' => FALSE,
240 'browsable' => FALSE
241 )
242 ),
243 'only writable' => array(
244 array(
245 'public' => FALSE,
246 'writable' => TRUE,
247 'browsable' => FALSE
248 )
249 ),
250 'only browsable' => array(
251 array(
252 'public' => FALSE,
253 'writable' => FALSE,
254 'browsable' => TRUE
255 )
256 ),
257 'all capabilities' => array(
258 array(
259 'public' => TRUE,
260 'writable' => TRUE,
261 'browsable' => TRUE
262 )
263 ),
264 'none' => array(
265 array(
266 'public' => FALSE,
267 'writable' => FALSE,
268 'browsable' => FALSE
269 )
270 )
271 );
272 }
273
274 /**
275 * @test
276 * @dataProvider capabilitiesDataProvider
277 * @TODO: Rewrite or move to functional suite
278 */
279 public function capabilitiesOfStorageObjectAreCorrectlySet(array $capabilities) {
280 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
281 $storageRecord = array(
282 'is_public' => $capabilities['public'],
283 'is_writable' => $capabilities['writable'],
284 'is_browsable' => $capabilities['browsable'],
285 'is_online' => TRUE
286 );
287 $mockedDriver = $this->createDriverMock(
288 array(
289 'pathType' => 'relative',
290 'basePath' => 'fileadmin/',
291 ),
292 $this->subject,
293 NULL
294 );
295 $this->prepareSubject(array(), FALSE, $mockedDriver, $storageRecord);
296 $this->assertEquals($capabilities['public'], $this->subject->isPublic(), 'Capability "public" is not correctly set.');
297 $this->assertEquals($capabilities['writable'], $this->subject->isWritable(), 'Capability "writable" is not correctly set.');
298 $this->assertEquals($capabilities['browsable'], $this->subject->isBrowsable(), 'Capability "browsable" is not correctly set.');
299 }
300
301 /**
302 * @test
303 * @TODO: Rewrite or move to functional suite
304 */
305 public function fileAndFolderListFiltersAreInitializedWithDefaultFilters() {
306 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
307 $this->prepareSubject(array());
308 $this->assertEquals($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'], $this->subject->getFileAndFolderNameFilters());
309 }
310
311 /**
312 * @test
313 */
314 public function addFileFailsIfFileDoesNotExist() {
315 /** @var Folder|\PHPUnit_Framework_MockObject_MockObject $mockedFolder */
316 $mockedFolder = $this->getMock(Folder::class, array(), array(), '', FALSE);
317 $this->setExpectedException('InvalidArgumentException', '', 1319552745);
318 $this->prepareSubject(array());
319 $this->subject->addFile('/some/random/file', $mockedFolder);
320 }
321
322 /**
323 * @test
324 */
325 public function getPublicUrlReturnsNullIfStorageIsNotOnline() {
326 /** @var $driver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
327 $driver = $this->getMock(LocalDriver::class, array(), array(array('basePath' => $this->getMountRootUrl())));
328 /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject */
329 $subject = $this->getMock(ResourceStorage::class, array('isOnline'), array($driver, array('configuration' => array())));
330 $subject->expects($this->once())->method('isOnline')->will($this->returnValue(FALSE));
331
332 $sourceFileIdentifier = '/sourceFile.ext';
333 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
334 $result = $subject->getPublicUrl($sourceFile);
335 $this->assertSame($result, NULL);
336 }
337
338 /**
339 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
340 *
341 * @return array
342 */
343 public function checkFolderPermissionsFilesystemPermissionsDataProvider() {
344 return array(
345 'read action on readable/writable folder' => array(
346 'read',
347 array('r' => TRUE, 'w' => TRUE),
348 TRUE
349 ),
350 'read action on unreadable folder' => array(
351 'read',
352 array('r' => FALSE, 'w' => TRUE),
353 FALSE
354 ),
355 'write action on read-only folder' => array(
356 'write',
357 array('r' => TRUE, 'w' => FALSE),
358 FALSE
359 )
360 );
361 }
362
363 /**
364 * @test
365 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
366 * @param string $action 'read' or 'write'
367 * @param array $permissionsFromDriver The permissions as returned from the driver
368 * @param bool $expectedResult
369 */
370 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedResult) {
371 /** @var $mockedDriver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
372 $mockedDriver = $this->getMock(LocalDriver::class);
373 $mockedDriver->expects($this->any())->method('getPermissions')->will($this->returnValue($permissionsFromDriver));
374 /** @var $mockedFolder Folder|\PHPUnit_Framework_MockObject_MockObject */
375 $mockedFolder = $this->getMock(Folder::class, array(), array(), '', FALSE);
376 // Let all other checks pass
377 /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject */
378 $subject = $this->getMock(ResourceStorage::class, array('isWritable', 'isBrowsable', 'checkUserActionPermission'), array($mockedDriver, array()), '', FALSE);
379 $subject->expects($this->any())->method('isWritable')->will($this->returnValue(TRUE));
380 $subject->expects($this->any())->method('isBrowsable')->will($this->returnValue(TRUE));
381 $subject->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(TRUE));
382 $subject->setDriver($mockedDriver);
383
384 $this->assertSame($expectedResult, $subject->checkFolderActionPermission($action, $mockedFolder));
385 }
386
387 /**
388 * @test
389 */
390 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet() {
391 $this->prepareSubject(array());
392 $this->assertTrue($this->subject->checkUserActionPermission('read', 'folder'));
393 }
394
395 /**
396 * @test
397 */
398 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero() {
399 $this->prepareSubject(array());
400 $this->subject->setUserPermissions(array('readFolder' => TRUE, 'writeFile' => TRUE));
401 $this->assertTrue($this->subject->checkUserActionPermission('read', 'folder'));
402 }
403
404 public function checkUserActionPermission_arbitraryPermissionDataProvider() {
405 return array(
406 'all lower cased' => array(
407 array('readFolder' => TRUE),
408 'read',
409 'folder'
410 ),
411 'all upper case' => array(
412 array('readFolder' => TRUE),
413 'READ',
414 'FOLDER'
415 ),
416 'mixed case' => array(
417 array('readFolder' => TRUE),
418 'ReaD',
419 'FoLdEr'
420 )
421 );
422 }
423
424 /**
425 * @param array $permissions
426 * @param string $action
427 * @param string $type
428 * @test
429 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
430 */
431 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments(array $permissions, $action, $type) {
432 $this->prepareSubject(array());
433 $this->subject->setUserPermissions($permissions);
434 $this->assertTrue($this->subject->checkUserActionPermission($action, $type));
435 }
436
437 /**
438 * @test
439 */
440 public function userActionIsDisallowedIfPermissionIsSetToFalse() {
441 $this->prepareSubject(array());
442 $this->subject->setEvaluatePermissions(TRUE);
443 $this->subject->setUserPermissions(array('readFolder' => FALSE));
444 $this->assertFalse($this->subject->checkUserActionPermission('read', 'folder'));
445 }
446
447 /**
448 * @test
449 */
450 public function userActionIsDisallowedIfPermissionIsNotSet() {
451 $this->prepareSubject(array());
452 $this->subject->setEvaluatePermissions(TRUE);
453 $this->subject->setUserPermissions(array('readFolder' => TRUE));
454 $this->assertFalse($this->subject->checkUserActionPermission('write', 'folder'));
455 }
456
457 /**
458 * @test
459 */
460 public function getEvaluatePermissionsWhenSetFalse() {
461 $this->prepareSubject(array());
462 $this->subject->setEvaluatePermissions(FALSE);
463 $this->assertFalse($this->subject->getEvaluatePermissions());
464 }
465
466 /**
467 * @test
468 */
469 public function getEvaluatePermissionsWhenSetTrue() {
470 $this->prepareSubject(array());
471 $this->subject->setEvaluatePermissions(TRUE);
472 $this->assertTrue($this->subject->getEvaluatePermissions());
473 }
474
475 /**
476 * @test
477 * @group integration
478 * @TODO: Rewrite or move to functional suite
479 */
480 public function setFileContentsUpdatesObjectProperties() {
481 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
482 $this->initializeVfs();
483 $driverObject = $this->getMockForAbstractClass(AbstractDriver::class, array(), '', FALSE);
484 $this->subject = $this->getMock(ResourceStorage::class, array('getFileIndexRepository', 'checkFileActionPermission'), array($driverObject, array()));
485 $this->subject->expects($this->any())->method('checkFileActionPermission')->will($this->returnValue(TRUE));
486 $fileInfo = array(
487 'storage' => 'A',
488 'identifier' => 'B',
489 'mtime' => 'C',
490 'ctime' => 'D',
491 'mimetype' => 'E',
492 'size' => 'F',
493 'name' => 'G',
494 );
495 $newProperties = array(
496 'storage' => $fileInfo['storage'],
497 'identifier' => $fileInfo['identifier'],
498 'tstamp' => $fileInfo['mtime'],
499 'crdate' => $fileInfo['ctime'],
500 'mime_type' => $fileInfo['mimetype'],
501 'size' => $fileInfo['size'],
502 'name' => $fileInfo['name']
503 );
504 $hash = 'asdfg';
505 /** @var $mockedDriver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
506 $mockedDriver = $this->getMock(LocalDriver::class, array(), array(array('basePath' => $this->getMountRootUrl())));
507 $mockedDriver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfo));
508 $mockedDriver->expects($this->once())->method('hash')->will($this->returnValue($hash));
509 $this->subject->setDriver($mockedDriver);
510 $indexFileRepositoryMock = $this->getMock(FileIndexRepository::class);
511 $this->subject->expects($this->any())->method('getFileIndexRepository')->will($this->returnValue($indexFileRepositoryMock));
512 /** @var $mockedFile File|\PHPUnit_Framework_MockObject_MockObject */
513 $mockedFile = $this->getMock(File::class, array(), array(), '', FALSE);
514 $mockedFile->expects($this->any())->method('getIdentifier')->will($this->returnValue($fileInfo['identifier']));
515 // called by indexer because the properties are updated
516 $this->subject->expects($this->any())->method('getFileInfoByIdentifier')->will($this->returnValue($newProperties));
517 $mockedFile->expects($this->any())->method('getStorage')->will($this->returnValue($this->subject));
518 $mockedFile->expects($this->any())->method('getProperties')->will($this->returnValue(array_keys($fileInfo)));
519 $mockedFile->expects($this->any())->method('getUpdatedProperties')->will($this->returnValue(array_keys($newProperties)));
520 // do not update directly; that's up to the indexer
521 $indexFileRepositoryMock->expects($this->never())->method('update');
522 $this->subject->setFileContents($mockedFile, uniqid());
523 }
524
525 /**
526 * @test
527 * @group integration
528 * @TODO: Rewrite or move to functional suite
529 */
530 public function moveFileCallsDriversMethodsWithCorrectArguments() {
531 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
532 $localFilePath = '/path/to/localFile';
533 $sourceFileIdentifier = '/sourceFile.ext';
534 $fileInfoDummy = array(
535 'storage' => 'A',
536 'identifier' => 'B',
537 'mtime' => 'C',
538 'ctime' => 'D',
539 'mimetype' => 'E',
540 'size' => 'F',
541 'name' => 'G',
542 );
543 $this->addToMount(array(
544 'targetFolder' => array()
545 ));
546 $this->initializeVfs();
547 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
548 /** @var $sourceDriver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
549 $sourceDriver = $this->getMock(LocalDriver::class);
550 $sourceDriver->expects($this->once())->method('deleteFile')->with($this->equalTo($sourceFileIdentifier));
551 $configuration = $this->convertConfigurationArrayToFlexformXml(array());
552 $sourceStorage = new ResourceStorage($sourceDriver, array('configuration' => $configuration));
553 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
554 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
555 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
556 $sourceFile->expects($this->once())->method('getUpdatedProperties')->will($this->returnValue(array_keys($fileInfoDummy)));
557 $sourceFile->expects($this->once())->method('getProperties')->will($this->returnValue($fileInfoDummy));
558 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
559 $mockedDriver = $this->getMock(LocalDriver::class, array(), array(array('basePath' => $this->getMountRootUrl())));
560 $mockedDriver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfoDummy));
561 $mockedDriver->expects($this->once())->method('addFile')->with($localFilePath, '/targetFolder/', $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
562 /** @var $subject ResourceStorage */
563 $subject = $this->getMock(ResourceStorage::class, array('assureFileMovePermissions'), array($mockedDriver, array('configuration' => $configuration)));
564 $subject->moveFile($sourceFile, $targetFolder, 'file.ext');
565 }
566
567 /**
568 * @test
569 * @group integration
570 * @TODO: Rewrite or move to functional suite
571 */
572 public function storageUsesInjectedFilemountsToCheckForMountBoundaries() {
573 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
574 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
575 $this->addToMount(array(
576 'mountFolder' => array(
577 'file' => 'asdfg'
578 )
579 ));
580 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
581 $this->initializeVfs();
582 $this->prepareSubject(array(), NULL, $mockedDriver);
583 $this->subject->addFileMount('/mountFolder');
584 $this->assertEquals(1, count($this->subject->getFileMounts()));
585 $this->subject->isWithinFileMountBoundaries($mockedFile);
586 }
587
588
589 /**
590 * @test
591 * @TODO: Rewrite or move to functional suite
592 */
593 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder() {
594 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
595 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
596 $mockedDriver = $this->createDriverMock(array());
597 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
598 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
599 $this->prepareSubject(array(), TRUE);
600 $this->subject->setDriver($mockedDriver);
601 $this->subject->createFolder('newFolder', $mockedParentFolder);
602 }
603
604 /**
605 * @test
606 * @expectedException \RuntimeException
607 */
608 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled() {
609 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
610 $folderMock = $this->getMock(Folder::class, array(), array(), '', FALSE);
611 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject */
612 $mockedDriver = $this->getMockForAbstractClass(AbstractDriver::class);
613 $mockedDriver->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(FALSE));
614 /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
615 $subject = $this->getAccessibleMock(ResourceStorage::class, array('checkFolderActionPermission'), array(), '', FALSE);
616 $subject->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(TRUE));
617 $subject->_set('driver', $mockedDriver);
618 $subject->deleteFolder($folderMock, FALSE);
619 }
620
621 /**
622 * @test
623 * @TODO: Rewrite or move to functional suite
624 */
625 public function createFolderCallsDriverForFolderCreation() {
626 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
627 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
628 $this->prepareSubject(array(), TRUE);
629 $mockedDriver = $this->createDriverMock(array(), $this->subject);
630 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
631 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(TRUE));
632 $this->subject->createFolder('newFolder', $mockedParentFolder);
633 }
634
635 /**
636 * @test
637 * @TODO: Rewrite or move to functional suite
638 */
639 public function createFolderCanRecursivelyCreateFolders() {
640 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
641 $this->addToMount(array('someFolder' => array()));
642 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
643 $this->prepareSubject(array(), TRUE, $mockedDriver);
644 $parentFolder = $this->subject->getFolder('/someFolder/');
645 $newFolder = $this->subject->createFolder('subFolder/secondSubfolder', $parentFolder);
646 $this->assertEquals('secondSubfolder', $newFolder->getName());
647 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
648 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
649 }
650
651 /**
652 * @test
653 * @TODO: Rewrite or move to functional suite
654 */
655 public function createFolderUsesRootFolderAsParentFolderIfNotGiven() {
656 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
657 $this->prepareSubject(array(), TRUE);
658 $mockedDriver = $this->createDriverMock(array(), $this->subject);
659 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue('/'));
660 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
661 $this->subject->createFolder('someFolder');
662 }
663
664 /**
665 * @test
666 * @TODO: Rewrite or move to functional suite
667 */
668 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist() {
669 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
670 $this->addToMount(array(
671 'existingFolder' => array()
672 ));
673 $this->initializeVfs();
674 $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), NULL, NULL);
675 $this->prepareSubject(array(), TRUE, $mockedDriver);
676 $rootFolder = $this->subject->getFolder('/');
677 $newFolder = $this->subject->createFolder('existingFolder/someFolder', $rootFolder);
678 $this->assertEquals('someFolder', $newFolder->getName());
679 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
680 }
681
682 /**
683 * @test
684 */
685 public function createFolderThrowsExceptionIfParentFolderDoesNotExist() {
686 $this->setExpectedException('InvalidArgumentException', '', 1325689164);
687 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
688 $this->prepareSubject(array(), TRUE);
689 $mockedDriver = $this->createDriverMock(array(), $this->subject);
690 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(FALSE));
691 $this->subject->createFolder('newFolder', $mockedParentFolder);
692 }
693
694 /**
695 * @test
696 */
697 public function replaceFileFailsIfLocalFileDoesNotExist() {
698 $this->setExpectedException('InvalidArgumentException', '', 1325842622);
699 $this->prepareSubject(array(), TRUE);
700 $mockedFile = $this->getSimpleFileMock('/someFile');
701 $this->subject->replaceFile($mockedFile, PATH_site . uniqid());
702 }
703
704 /**
705 * @test
706 * @TODO: Rewrite or move to functional suite
707 */
708 public function getRoleReturnsDefaultForRegularFolders() {
709 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
710 $folderIdentifier = uniqid();
711 $this->addToMount(array(
712 $folderIdentifier => array()
713 ));
714 $this->prepareSubject(array());
715
716 $role = $this->subject->getRole($this->getSimpleFolderMock('/' . $folderIdentifier . '/'));
717
718 $this->assertSame(FolderInterface::ROLE_DEFAULT, $role);
719 }
720
721 }