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