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