[TASK] Remove TYPO3_DB from Tests
[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\Index\FileIndexRepository;
24 use TYPO3\CMS\Core\Resource\ResourceStorage;
25 use TYPO3\CMS\Core\Utility\ArrayUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28 /**
29 * Test case for ResourceStorage class
30 */
31 class ResourceStorageTest extends BaseTestCase
32 {
33 /**
34 * @var array A backup of registered singleton instances
35 */
36 protected $singletonInstances = [];
37
38 /**
39 * @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject
40 */
41 protected $subject;
42
43 protected function setUp()
44 {
45 parent::setUp();
46 $this->singletonInstances = GeneralUtility::getSingletonInstances();
47 /** @var FileRepository|\PHPUnit_Framework_MockObject_MockObject $fileRepositoryMock */
48 $fileRepositoryMock = $this->createMock(FileRepository::class);
49 GeneralUtility::setSingletonInstance(
50 FileRepository::class,
51 $fileRepositoryMock
52 );
53 }
54
55 protected function tearDown()
56 {
57 GeneralUtility::resetSingletonInstances($this->singletonInstances);
58 parent::tearDown();
59 }
60
61 /**
62 * Prepare ResourceStorage
63 *
64 * @param array $configuration
65 * @param bool $mockPermissionChecks
66 * @param AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverObject
67 * @param array $storageRecord
68 */
69 protected function prepareSubject(array $configuration, $mockPermissionChecks = false, AbstractDriver $driverObject = null, array $storageRecord = [])
70 {
71 $permissionMethods = ['assureFileAddPermissions', 'checkFolderActionPermission', 'checkFileActionPermission', 'checkUserActionPermission', 'checkFileExtensionPermission', 'isWithinFileMountBoundaries'];
72 $mockedMethods = [];
73 $configuration = $this->convertConfigurationArrayToFlexformXml($configuration);
74 $overruleArray = ['configuration' => $configuration];
75 ArrayUtility::mergeRecursiveWithOverrule($storageRecord, $overruleArray);
76 if ($driverObject == null) {
77 $driverObject = $this->getMockForAbstractClass(AbstractDriver::class, [], '', false);
78 }
79 if ($mockPermissionChecks) {
80 $mockedMethods = $permissionMethods;
81 }
82 $mockedMethods[] = 'getIndexer';
83
84 $this->subject = $this->getMockBuilder(ResourceStorage::class)
85 ->setMethods($mockedMethods)
86 ->setConstructorArgs([$driverObject, $storageRecord])
87 ->getMock();
88 $this->subject->expects($this->any())->method('getIndexer')->will($this->returnValue($this->createMock(\TYPO3\CMS\Core\Resource\Index\Indexer::class)));
89 if ($mockPermissionChecks) {
90 foreach ($permissionMethods as $method) {
91 $this->subject->expects($this->any())->method($method)->will($this->returnValue(true));
92 }
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 = ['data' => ['sDEF' => ['lDEF' => []]]];
106 foreach ($configuration as $key => $value) {
107 $flexFormArray['data']['sDEF']['lDEF'][$key] = ['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 = [])
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->getMockBuilder(LocalDriver::class)
137 ->setMethods($mockedDriverMethods)
138 ->setConstructorArgs([$driverConfiguration])
139 ->getMock();
140 }
141 if ($storageObject !== null) {
142 $storageObject->setDriver($driver);
143 }
144 $driver->setStorageUid(6);
145 $driver->processConfiguration();
146 $driver->initialize();
147 return $driver;
148 }
149
150 /**
151 * @return array
152 */
153 public function fileExtensionPermissionDataProvider()
154 {
155 return [
156 'Permissions evaluated, extension not in allowed list' => [
157 'fileName' => 'foo.txt',
158 'configuration' => ['allow' => 'jpg'],
159 'evaluatePermissions' => true,
160 'isAllowed' => true,
161 ],
162 'Permissions evaluated, extension in deny list' => [
163 'fileName' => 'foo.txt',
164 'configuration' => ['deny' => 'txt'],
165 'evaluatePermissions' => true,
166 'isAllowed' => false,
167 ],
168 'Permissions not evaluated, extension is php' => [
169 'fileName' => 'foo.php',
170 'configuration' => [],
171 'evaluatePermissions' => false,
172 'isAllowed' => false,
173 ],
174 'Permissions evaluated, extension is php' => [
175 'fileName' => 'foo.php',
176 // It is not possible to allow php file extension through configuration
177 'configuration' => ['allow' => 'php'],
178 'evaluatePermissions' => true,
179 'isAllowed' => false,
180 ],
181 ];
182 }
183
184 /**
185 * @param string $fileName
186 * @param array $configuration
187 * @param bool $evaluatePermissions
188 * @param bool $isAllowed
189 * @test
190 * @dataProvider fileExtensionPermissionDataProvider
191 */
192 public function fileExtensionPermissionIsWorkingCorrectly($fileName, array $configuration, $evaluatePermissions, $isAllowed)
193 {
194 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']['webspace'] = $configuration;
195 $driverMock = $this->getMockForAbstractClass(AbstractDriver::class, [], '', false);
196 $subject = $this->getAccessibleMock(ResourceStorage::class, ['dummy'], [$driverMock, []]);
197 $subject->_set('evaluatePermissions', $evaluatePermissions);
198 $this->assertSame($isAllowed, $subject->_call('checkFileExtensionPermission', $fileName));
199 }
200
201 /**
202 * @return array
203 */
204 public function capabilitiesDataProvider()
205 {
206 return [
207 'only public' => [
208 [
209 'public' => true,
210 'writable' => false,
211 'browsable' => false
212 ]
213 ],
214 'only writable' => [
215 [
216 'public' => false,
217 'writable' => true,
218 'browsable' => false
219 ]
220 ],
221 'only browsable' => [
222 [
223 'public' => false,
224 'writable' => false,
225 'browsable' => true
226 ]
227 ],
228 'all capabilities' => [
229 [
230 'public' => true,
231 'writable' => true,
232 'browsable' => true
233 ]
234 ],
235 'none' => [
236 [
237 'public' => false,
238 'writable' => false,
239 'browsable' => false
240 ]
241 ]
242 ];
243 }
244
245 /**
246 * @test
247 * @dataProvider capabilitiesDataProvider
248 * @TODO: Rewrite or move to functional suite
249 */
250 public function capabilitiesOfStorageObjectAreCorrectlySet(array $capabilities)
251 {
252 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
253 $storageRecord = [
254 'is_public' => $capabilities['public'],
255 'is_writable' => $capabilities['writable'],
256 'is_browsable' => $capabilities['browsable'],
257 'is_online' => true
258 ];
259 $mockedDriver = $this->createDriverMock(
260 [
261 'pathType' => 'relative',
262 'basePath' => 'fileadmin/',
263 ],
264 $this->subject,
265 null
266 );
267 $this->prepareSubject([], false, $mockedDriver, $storageRecord);
268 $this->assertEquals($capabilities['public'], $this->subject->isPublic(), 'Capability "public" is not correctly set.');
269 $this->assertEquals($capabilities['writable'], $this->subject->isWritable(), 'Capability "writable" is not correctly set.');
270 $this->assertEquals($capabilities['browsable'], $this->subject->isBrowsable(), 'Capability "browsable" is not correctly set.');
271 }
272
273 /**
274 * @test
275 * @TODO: Rewrite or move to functional suite
276 */
277 public function fileAndFolderListFiltersAreInitializedWithDefaultFilters()
278 {
279 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
280 $this->prepareSubject([]);
281 $this->assertEquals($GLOBALS['TYPO3_CONF_VARS']['SYS']['fal']['defaultFilterCallbacks'], $this->subject->getFileAndFolderNameFilters());
282 }
283
284 /**
285 * @test
286 */
287 public function addFileFailsIfFileDoesNotExist()
288 {
289 /** @var Folder|\PHPUnit_Framework_MockObject_MockObject $mockedFolder */
290 $mockedFolder = $this->createMock(Folder::class);
291 $this->expectException(\InvalidArgumentException::class);
292 $this->expectExceptionCode(1319552745);
293 $this->prepareSubject([]);
294 $this->subject->addFile('/some/random/file', $mockedFolder);
295 }
296
297 /**
298 * @test
299 */
300 public function getPublicUrlReturnsNullIfStorageIsNotOnline()
301 {
302 /** @var $driver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
303 $driver = $this->getMockBuilder(LocalDriver::class)
304 ->setConstructorArgs([['basePath' => $this->getMountRootUrl()]])
305 ->getMock();
306 /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject */
307 $subject = $this->getMockBuilder(ResourceStorage::class)
308 ->setMethods(['isOnline'])
309 ->setConstructorArgs([$driver, ['configuration' => []]])
310 ->getMock();
311 $subject->expects($this->once())->method('isOnline')->will($this->returnValue(false));
312
313 $sourceFileIdentifier = '/sourceFile.ext';
314 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
315 $result = $subject->getPublicUrl($sourceFile);
316 $this->assertSame($result, null);
317 }
318
319 /**
320 * Data provider for checkFolderPermissionsRespectsFilesystemPermissions
321 *
322 * @return array
323 */
324 public function checkFolderPermissionsFilesystemPermissionsDataProvider()
325 {
326 return [
327 'read action on readable/writable folder' => [
328 'read',
329 ['r' => true, 'w' => true],
330 true
331 ],
332 'read action on unreadable folder' => [
333 'read',
334 ['r' => false, 'w' => true],
335 false
336 ],
337 'write action on read-only folder' => [
338 'write',
339 ['r' => true, 'w' => false],
340 false
341 ]
342 ];
343 }
344
345 /**
346 * @test
347 * @dataProvider checkFolderPermissionsFilesystemPermissionsDataProvider
348 * @param string $action 'read' or 'write'
349 * @param array $permissionsFromDriver The permissions as returned from the driver
350 * @param bool $expectedResult
351 */
352 public function checkFolderPermissionsRespectsFilesystemPermissions($action, $permissionsFromDriver, $expectedResult)
353 {
354 /** @var $mockedDriver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
355 $mockedDriver = $this->createMock(LocalDriver::class);
356 $mockedDriver->expects($this->any())->method('getPermissions')->will($this->returnValue($permissionsFromDriver));
357 /** @var $mockedFolder Folder|\PHPUnit_Framework_MockObject_MockObject */
358 $mockedFolder = $this->createMock(Folder::class);
359 // Let all other checks pass
360 /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject */
361 $subject = $this->getMockBuilder(ResourceStorage::class)
362 ->setMethods(['isWritable', 'isBrowsable', 'checkUserActionPermission'])
363 ->setConstructorArgs([$mockedDriver, []])
364 ->getMock();
365 $subject->expects($this->any())->method('isWritable')->will($this->returnValue(true));
366 $subject->expects($this->any())->method('isBrowsable')->will($this->returnValue(true));
367 $subject->expects($this->any())->method('checkUserActionPermission')->will($this->returnValue(true));
368 $subject->setDriver($mockedDriver);
369
370 $this->assertSame($expectedResult, $subject->checkFolderActionPermission($action, $mockedFolder));
371 }
372
373 /**
374 * @test
375 */
376 public function checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet()
377 {
378 $this->prepareSubject([]);
379 $this->assertTrue($this->subject->checkUserActionPermission('read', 'folder'));
380 }
381
382 /**
383 * @test
384 */
385 public function checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero()
386 {
387 $this->prepareSubject([]);
388 $this->subject->setUserPermissions(['readFolder' => true, 'writeFile' => true]);
389 $this->assertTrue($this->subject->checkUserActionPermission('read', 'folder'));
390 }
391
392 public function checkUserActionPermission_arbitraryPermissionDataProvider()
393 {
394 return [
395 'all lower cased' => [
396 ['readFolder' => true],
397 'read',
398 'folder'
399 ],
400 'all upper case' => [
401 ['readFolder' => true],
402 'READ',
403 'FOLDER'
404 ],
405 'mixed case' => [
406 ['readFolder' => true],
407 'ReaD',
408 'FoLdEr'
409 ]
410 ];
411 }
412
413 /**
414 * @param array $permissions
415 * @param string $action
416 * @param string $type
417 * @test
418 * @dataProvider checkUserActionPermission_arbitraryPermissionDataProvider
419 */
420 public function checkUserActionPermissionAcceptsArbitrarilyCasedArguments(array $permissions, $action, $type)
421 {
422 $this->prepareSubject([]);
423 $this->subject->setUserPermissions($permissions);
424 $this->assertTrue($this->subject->checkUserActionPermission($action, $type));
425 }
426
427 /**
428 * @test
429 */
430 public function userActionIsDisallowedIfPermissionIsSetToFalse()
431 {
432 $this->prepareSubject([]);
433 $this->subject->setEvaluatePermissions(true);
434 $this->subject->setUserPermissions(['readFolder' => false]);
435 $this->assertFalse($this->subject->checkUserActionPermission('read', 'folder'));
436 }
437
438 /**
439 * @test
440 */
441 public function userActionIsDisallowedIfPermissionIsNotSet()
442 {
443 $this->prepareSubject([]);
444 $this->subject->setEvaluatePermissions(true);
445 $this->subject->setUserPermissions(['readFolder' => true]);
446 $this->assertFalse($this->subject->checkUserActionPermission('write', 'folder'));
447 }
448
449 /**
450 * @test
451 */
452 public function getEvaluatePermissionsWhenSetFalse()
453 {
454 $this->prepareSubject([]);
455 $this->subject->setEvaluatePermissions(false);
456 $this->assertFalse($this->subject->getEvaluatePermissions());
457 }
458
459 /**
460 * @test
461 */
462 public function getEvaluatePermissionsWhenSetTrue()
463 {
464 $this->prepareSubject([]);
465 $this->subject->setEvaluatePermissions(true);
466 $this->assertTrue($this->subject->getEvaluatePermissions());
467 }
468
469 /**
470 * @test
471 * @group integration
472 * @TODO: Rewrite or move to functional suite
473 */
474 public function setFileContentsUpdatesObjectProperties()
475 {
476 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
477 $this->initializeVfs();
478 $driverObject = $this->getMockForAbstractClass(AbstractDriver::class, [], '', false);
479 $this->subject = $this->getMockBuilder(ResourceStorage::class)
480 ->setMethods(['getFileIndexRepository', 'checkFileActionPermission'])
481 ->setConstructorArgs([$driverObject, []])
482 ->getMock();
483 $this->subject->expects($this->any())->method('checkFileActionPermission')->will($this->returnValue(true));
484 $fileInfo = [
485 'storage' => 'A',
486 'identifier' => 'B',
487 'mtime' => 'C',
488 'ctime' => 'D',
489 'mimetype' => 'E',
490 'size' => 'F',
491 'name' => 'G',
492 ];
493 $newProperties = [
494 'storage' => $fileInfo['storage'],
495 'identifier' => $fileInfo['identifier'],
496 'tstamp' => $fileInfo['mtime'],
497 'crdate' => $fileInfo['ctime'],
498 'mime_type' => $fileInfo['mimetype'],
499 'size' => $fileInfo['size'],
500 'name' => $fileInfo['name']
501 ];
502 $hash = 'asdfg';
503 /** @var $mockedDriver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
504 $mockedDriver = $this->getMockBuilder(LocalDriver::class)
505 ->setConstructorArgs([['basePath' => $this->getMountRootUrl()]])
506 ->getMock();
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->createMock(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->createMock(File::class);
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, $this->getUniqueId());
523 }
524
525 /**
526 * @test
527 * @group integration
528 * @TODO: Rewrite or move to functional suite
529 */
530 public function moveFileCallsDriversMethodsWithCorrectArguments()
531 {
532 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
533 $localFilePath = '/path/to/localFile';
534 $sourceFileIdentifier = '/sourceFile.ext';
535 $fileInfoDummy = [
536 'storage' => 'A',
537 'identifier' => 'B',
538 'mtime' => 'C',
539 'ctime' => 'D',
540 'mimetype' => 'E',
541 'size' => 'F',
542 'name' => 'G',
543 ];
544 $this->addToMount([
545 'targetFolder' => []
546 ]);
547 $this->initializeVfs();
548 $targetFolder = $this->getSimpleFolderMock('/targetFolder/');
549 /** @var $sourceDriver LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
550 $sourceDriver = $this->createMock(LocalDriver::class);
551 $sourceDriver->expects($this->once())->method('deleteFile')->with($this->equalTo($sourceFileIdentifier));
552 $configuration = $this->convertConfigurationArrayToFlexformXml([]);
553 $sourceStorage = new ResourceStorage($sourceDriver, ['configuration' => $configuration]);
554 $sourceFile = $this->getSimpleFileMock($sourceFileIdentifier);
555 $sourceFile->expects($this->once())->method('getForLocalProcessing')->will($this->returnValue($localFilePath));
556 $sourceFile->expects($this->any())->method('getStorage')->will($this->returnValue($sourceStorage));
557 $sourceFile->expects($this->once())->method('getUpdatedProperties')->will($this->returnValue(array_keys($fileInfoDummy)));
558 $sourceFile->expects($this->once())->method('getProperties')->will($this->returnValue($fileInfoDummy));
559 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\LocalDriver|\PHPUnit_Framework_MockObject_MockObject */
560 $mockedDriver = $this->getMockBuilder(LocalDriver::class)
561 ->setConstructorArgs([['basePath' => $this->getMountRootUrl()]])
562 ->getMock();
563 $mockedDriver->expects($this->once())->method('getFileInfoByIdentifier')->will($this->returnValue($fileInfoDummy));
564 $mockedDriver->expects($this->once())->method('addFile')->with($localFilePath, '/targetFolder/', $this->equalTo('file.ext'))->will($this->returnValue('/targetFolder/file.ext'));
565 /** @var $subject ResourceStorage */
566 $subject = $this->getMockBuilder(ResourceStorage::class)
567 ->setMethods(['assureFileMovePermissions'])
568 ->setConstructorArgs([$mockedDriver, ['configuration' => $configuration]])
569 ->getMock();
570 $subject->moveFile($sourceFile, $targetFolder, 'file.ext');
571 }
572
573 /**
574 * @test
575 * @group integration
576 * @TODO: Rewrite or move to functional suite
577 */
578 public function storageUsesInjectedFilemountsToCheckForMountBoundaries()
579 {
580 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
581 $mockedFile = $this->getSimpleFileMock('/mountFolder/file');
582 $this->addToMount([
583 'mountFolder' => [
584 'file' => 'asdfg'
585 ]
586 ]);
587 $mockedDriver = $this->createDriverMock(['basePath' => $this->getMountRootUrl()], null, null);
588 $this->initializeVfs();
589 $this->prepareSubject([], null, $mockedDriver);
590 $this->subject->addFileMount('/mountFolder');
591 $this->assertEquals(1, count($this->subject->getFileMounts()));
592 $this->subject->isWithinFileMountBoundaries($mockedFile);
593 }
594
595 /**
596 * @test
597 * @TODO: Rewrite or move to functional suite
598 */
599 public function createFolderChecksIfParentFolderExistsBeforeCreatingFolder()
600 {
601 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
602 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
603 $mockedDriver = $this->createDriverMock([]);
604 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(true));
605 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'))->will($this->returnValue($mockedParentFolder));
606 $this->prepareSubject([], true);
607 $this->subject->setDriver($mockedDriver);
608 $this->subject->createFolder('newFolder', $mockedParentFolder);
609 }
610
611 /**
612 * @test
613 */
614 public function deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled()
615 {
616 $this->expectException(\RuntimeException::class);
617 $this->expectExceptionCode(1325952534);
618
619 /** @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject $folderMock */
620 $folderMock = $this->createMock(Folder::class);
621 /** @var $mockedDriver \TYPO3\CMS\Core\Resource\Driver\AbstractDriver|\PHPUnit_Framework_MockObject_MockObject */
622 $mockedDriver = $this->getMockForAbstractClass(AbstractDriver::class);
623 $mockedDriver->expects($this->once())->method('isFolderEmpty')->will($this->returnValue(false));
624 /** @var $subject ResourceStorage|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
625 $subject = $this->getAccessibleMock(ResourceStorage::class, ['checkFolderActionPermission'], [], '', false);
626 $subject->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(true));
627 $subject->_set('driver', $mockedDriver);
628 $subject->deleteFolder($folderMock, false);
629 }
630
631 /**
632 * @test
633 * @TODO: Rewrite or move to functional suite
634 */
635 public function createFolderCallsDriverForFolderCreation()
636 {
637 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
638 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
639 $this->prepareSubject([], true);
640 $mockedDriver = $this->createDriverMock([], $this->subject);
641 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('newFolder'), $this->equalTo('/someFolder/'))->will($this->returnValue(true));
642 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(true));
643 $this->subject->createFolder('newFolder', $mockedParentFolder);
644 }
645
646 /**
647 * @test
648 * @TODO: Rewrite or move to functional suite
649 */
650 public function createFolderCanRecursivelyCreateFolders()
651 {
652 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
653 $this->addToMount(['someFolder' => []]);
654 $mockedDriver = $this->createDriverMock(['basePath' => $this->getMountRootUrl()], null, null);
655 $this->prepareSubject([], true, $mockedDriver);
656 $parentFolder = $this->subject->getFolder('/someFolder/');
657 $newFolder = $this->subject->createFolder('subFolder/secondSubfolder', $parentFolder);
658 $this->assertEquals('secondSubfolder', $newFolder->getName());
659 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/'));
660 $this->assertFileExists($this->getUrlInMount('/someFolder/subFolder/secondSubfolder/'));
661 }
662
663 /**
664 * @test
665 * @TODO: Rewrite or move to functional suite
666 */
667 public function createFolderUsesRootFolderAsParentFolderIfNotGiven()
668 {
669 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
670 $this->prepareSubject([], true);
671 $mockedDriver = $this->createDriverMock([], $this->subject);
672 $mockedDriver->expects($this->once())->method('getRootLevelFolder')->with()->will($this->returnValue('/'));
673 $mockedDriver->expects($this->once())->method('createFolder')->with($this->equalTo('someFolder'));
674 $this->subject->createFolder('someFolder');
675 }
676
677 /**
678 * @test
679 * @TODO: Rewrite or move to functional suite
680 */
681 public function createFolderCreatesNestedStructureEvenIfPartsAlreadyExist()
682 {
683 $this->markTestSkipped('This test does way to much and is mocked incomplete. Skipped for now.');
684 $this->addToMount([
685 'existingFolder' => []
686 ]);
687 $this->initializeVfs();
688 $mockedDriver = $this->createDriverMock(['basePath' => $this->getMountRootUrl()], null, null);
689 $this->prepareSubject([], true, $mockedDriver);
690 $rootFolder = $this->subject->getFolder('/');
691 $newFolder = $this->subject->createFolder('existingFolder/someFolder', $rootFolder);
692 $this->assertEquals('someFolder', $newFolder->getName());
693 $this->assertFileExists($this->getUrlInMount('existingFolder/someFolder'));
694 }
695
696 /**
697 * @test
698 */
699 public function createFolderThrowsExceptionIfParentFolderDoesNotExist()
700 {
701 $this->expectException(\InvalidArgumentException::class);
702 $this->expectExceptionCode(1325689164);
703 $mockedParentFolder = $this->getSimpleFolderMock('/someFolder/');
704 $this->prepareSubject([], true);
705 $mockedDriver = $this->createDriverMock([], $this->subject);
706 $mockedDriver->expects($this->once())->method('folderExists')->with($this->equalTo('/someFolder/'))->will($this->returnValue(false));
707 $this->subject->createFolder('newFolder', $mockedParentFolder);
708 }
709 }