‪TYPO3CMS  ‪main
ResourceStorageTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\CMS\Core\Resource\Driver\DriverInterface;
24 use TYPO3\CMS\Core\Resource\Driver\LocalDriver;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
33 final class ‪ResourceStorageTest extends FunctionalTestCase
34 {
35  protected function ‪setUp(): void
36  {
37  parent::setUp();
38  mkdir($this->instancePath . '/resource-storage-test');
39  }
40 
41  protected function ‪tearDown(): void
42  {
43  ‪GeneralUtility::rmdir($this->instancePath . '/resource-storage-test', true);
44  parent::tearDown();
45  }
46 
47  #[Test]
48  public function ‪addFileFailsIfFileDoesNotExist(): void
49  {
50  $this->expectException(\InvalidArgumentException::class);
51  $this->expectExceptionCode(1319552745);
52  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
53  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
54  $folder = new ‪Folder($subject, 'someName', 'someName');
55  $subject->addFile('/some/random/file', $folder);
56  }
57 
58  #[Test]
60  {
61  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
62  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
63  $file = new ‪File(
64  [
65  'identifier' => 'myIdentifier',
66  'name' => 'myName',
67  ],
68  $subject,
69  );
70  $subject->markAsPermanentlyOffline();
71  self::assertNull($subject->getPublicUrl($file));
72  }
73 
77  public static function checkFolderPermissionsFilesystemPermissionsDataProvider(): array
78  {
79  return [
80  'read action on readable/writable folder' => [
81  'read',
82  ['r' => true, 'w' => true],
83  true,
84  ],
85  'read action on unreadable folder' => [
86  'read',
87  ['r' => false, 'w' => true],
88  false,
89  ],
90  'write action on read-only folder' => [
91  'write',
92  ['r' => true, 'w' => false],
93  false,
94  ],
95  ];
96  }
97 
101  #[DataProvider('checkFolderPermissionsFilesystemPermissionsDataProvider')]
102  #[Test]
103  public function ‪checkFolderPermissionsRespectsFilesystemPermissions(string $action, array $permissionsFromDriver, bool $expectedResult): void
104  {
105  $localDriver = $this->getMockBuilder(LocalDriver::class)
106  ->setConstructorArgs([['basePath' => $this->instancePath . '/resource-storage-test']])
107  ->onlyMethods(['getPermissions'])
108  ->getMock();
109  $localDriver->method('getPermissions')->willReturn($permissionsFromDriver);
110  $mockedResourceFactory = $this->createMock(ResourceFactory::class);
111  $mockedFolder = $this->createMock(Folder::class);
112 
113  // Let all other checks pass
114  $subject = $this->getMockBuilder(ResourceStorage::class)
115  ->onlyMethods(['isWritable', 'isBrowsable', 'checkUserActionPermission', 'getResourceFactoryInstance'])
116  ->setConstructorArgs([$localDriver, ['uid' => 1], new ‪NoopEventDispatcher()])
117  ->getMock();
118  $subject->method('isWritable')->willReturn(true);
119  $subject->method('isBrowsable')->willReturn(true);
120  $subject->method('checkUserActionPermission')->willReturn(true);
121  $subject->method('getResourceFactoryInstance')->willReturn($mockedResourceFactory);
122  $subject->setDriver($localDriver);
123  self::assertSame($expectedResult, $subject->checkFolderActionPermission($action, $mockedFolder));
124  }
125 
126  #[Test]
128  {
129  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
130  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
131  self::assertTrue($subject->checkUserActionPermission('read', 'folder'));
132  }
133 
134  #[Test]
136  {
137  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
138  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
139  $subject->setUserPermissions(['readFolder' => true, 'writeFile' => true]);
140  self::assertTrue($subject->checkUserActionPermission('read', 'folder'));
141  }
142 
146  public static function checkUserActionPermission_arbitraryPermissionDataProvider(): array
147  {
148  return [
149  'all lower cased' => [
150  ['readFolder' => true],
151  'read',
152  'folder',
153  ],
154  'all upper case' => [
155  ['readFolder' => true],
156  'READ',
157  'FOLDER',
158  ],
159  'mixed case' => [
160  ['readFolder' => true],
161  'ReaD',
162  'FoLdEr',
163  ],
164  ];
165  }
166 
167  #[DataProvider('checkUserActionPermission_arbitraryPermissionDataProvider')]
168  #[Test]
169  public function ‪checkUserActionPermissionAcceptsArbitrarilyCasedArguments(array $permissions, string $action, string $type): void
170  {
171  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
172  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
173  $subject->setUserPermissions($permissions);
174  self::assertTrue($subject->checkUserActionPermission($action, $type));
175  }
176 
177  #[Test]
179  {
180  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
181  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
182  $subject->setEvaluatePermissions(true);
183  $subject->setUserPermissions(['readFolder' => false]);
184  self::assertFalse($subject->checkUserActionPermission('read', 'folder'));
185  }
186 
187  #[Test]
189  {
190  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
191  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
192  $subject->setEvaluatePermissions(true);
193  $subject->setUserPermissions(['readFolder' => true]);
194  self::assertFalse($subject->checkUserActionPermission('write', 'folder'));
195  }
196 
197  #[Test]
199  {
200  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
201  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
202  $subject->setEvaluatePermissions(true);
203  self::assertFalse($subject->checkFileActionPermission('editMeta', new ‪File(['identifier' => '/foo/bar.jpg'], $subject)));
204  }
205 
206  #[Test]
208  {
209  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
210  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
211  $subject->setEvaluatePermissions(false);
212  self::assertFalse($subject->getEvaluatePermissions());
213  }
214 
215  #[Test]
216  public function ‪getEvaluatePermissionsWhenSetTrue(): void
217  {
218  $localDriver = new LocalDriver(['basePath' => $this->instancePath . '/resource-storage-test']);
219  $subject = new ‪ResourceStorage($localDriver, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher());
220  $subject->setEvaluatePermissions(true);
221  self::assertTrue($subject->getEvaluatePermissions());
222  }
223 
224  #[Test]
226  {
227  $this->expectException(\RuntimeException::class);
228  $this->expectExceptionCode(1325952534);
229  $folderMock = $this->createMock(Folder::class);
230  $mockedDriver = $this->createMock(DriverInterface::class);
231  $mockedDriver->expects(self::once())->method('isFolderEmpty')->willReturn(false);
232  $subject = $this->getAccessibleMock(ResourceStorage::class, ['checkFolderActionPermission'], [], '', false);
233  $subject->method('checkFolderActionPermission')->willReturn(true);
234  $subject->_set('driver', $mockedDriver);
235  $subject->deleteFolder($folderMock);
236  }
237 
238  #[Test]
240  {
241  $driverMock = $this->getMockBuilder(LocalDriver::class)
242  ->onlyMethods(['renameFile', 'sanitizeFileName'])
243  ->getMock();
244  $driverMock->method('sanitizeFileName')
245  ->willReturn('a_b.jpg');
246  $driverMock->expects(self::once())
247  ->method('renameFile')
248  ->with('/a b.jpg', 'a_b.jpg');
249  $indexerMock = $this->getMockBuilder(Indexer::class)
250  ->onlyMethods(['updateIndexEntry'])
251  ->disableOriginalConstructor()
252  ->getMock();
253 
254  $subject = $this->getMockBuilder(ResourceStorage::class)
255  ->onlyMethods(['assureFileRenamePermissions', 'getIndexer'])
256  ->setConstructorArgs([$driverMock, ['uid' => 1, 'name' => 'testing'], new ‪NoopEventDispatcher()])
257  ->getMock();
258  $subject->method('getIndexer')
259  ->willReturn($indexerMock);
260  $file = new ‪File(
261  [
262  'identifier' => '/a b.jpg',
263  'name' => 'a b.jpg',
264  ],
265  $subject,
266  );
267  $subject->renameFile($file, 'a b.jpg');
268  }
269 
270 }
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\userActionIsDisallowedIfPermissionIsNotSet
‪userActionIsDisallowedIfPermissionIsNotSet()
Definition: ResourceStorageTest.php:188
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet
‪checkUserActionPermissionsAlwaysReturnsTrueIfNoUserPermissionsAreSet()
Definition: ResourceStorageTest.php:127
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\checkUserActionPermissionAcceptsArbitrarilyCasedArguments
‪static checkUserActionPermissionAcceptsArbitrarilyCasedArguments(array $permissions, string $action, string $type)
Definition: ResourceStorageTest.php:169
‪TYPO3\CMS\Core\Tests\Functional\Resource
Definition: DefaultUploadFolderResolverTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\userActionIsDisallowedIfPermissionIsSetToFalse
‪userActionIsDisallowedIfPermissionIsSetToFalse()
Definition: ResourceStorageTest.php:178
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled
‪deleteFolderThrowsExceptionIfFolderIsNotEmptyAndRecursiveDeleteIsDisabled()
Definition: ResourceStorageTest.php:225
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\metaDataEditIsNotAllowedWhenWhenNoFileMountsAreSet
‪metaDataEditIsNotAllowedWhenWhenNoFileMountsAreSet()
Definition: ResourceStorageTest.php:198
‪TYPO3\CMS\Core\Resource\Index\Indexer
Definition: Indexer.php:35
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\getPublicUrlReturnsNullIfStorageIsNotOnline
‪getPublicUrlReturnsNullIfStorageIsNotOnline()
Definition: ResourceStorageTest.php:59
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\setUp
‪setUp()
Definition: ResourceStorageTest.php:35
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\checkFolderPermissionsRespectsFilesystemPermissions
‪static checkFolderPermissionsRespectsFilesystemPermissions(string $action, array $permissionsFromDriver, bool $expectedResult)
Definition: ResourceStorageTest.php:103
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero
‪checkUserActionPermissionReturnsFalseIfPermissionIsSetToZero()
Definition: ResourceStorageTest.php:135
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\getEvaluatePermissionsWhenSetTrue
‪getEvaluatePermissionsWhenSetTrue()
Definition: ResourceStorageTest.php:216
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\tearDown
‪tearDown()
Definition: ResourceStorageTest.php:41
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\addFileFailsIfFileDoesNotExist
‪addFileFailsIfFileDoesNotExist()
Definition: ResourceStorageTest.php:48
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\getEvaluatePermissionsWhenSetFalse
‪getEvaluatePermissionsWhenSetFalse()
Definition: ResourceStorageTest.php:207
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir(string $path, bool $removeNonEmpty=false)
Definition: GeneralUtility.php:1702
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\renameFileWillCallRenameFileIfUnsanitizedAndNoChangeInTargetFilename
‪renameFileWillCallRenameFileIfUnsanitizedAndNoChangeInTargetFilename()
Definition: ResourceStorageTest.php:239
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:129
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest
Definition: ResourceStorageTest.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52