‪TYPO3CMS  9.5
ResourceStorageTest.php
Go to the documentation of this file.
1 <?php
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 
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
30 class ‪ResourceStorageTest extends FunctionalTestCase
31 {
32  protected function ‪tearDown()
33  {
34  // cleanup manually created folders
35  foreach (glob(‪Environment::getPublicPath() . '/fileadmin/*') as $folderToRemove) {
36  GeneralUtility::rmdir($folderToRemove, true);
37  }
38  }
39 
44  {
45  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
46  $this->setUpBackendUserFromFixture(1);
47  $subject = (new ‪StorageRepository())->findByUid(1);
48  $subject->setEvaluatePermissions(false);
49 
50  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/_processed_');
51  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/adirectory');
52  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/typo3temp/assets/_processed_/');
53  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/adirectory/bar.txt', 'myData');
54  clearstatcache();
55  $subject->addFileMount('/adirectory/', ['read_only' => false]);
57 
58  $rootProcessingFolder = $subject->getProcessingFolder();
59  $processingFolder = $subject->getProcessingFolder($file);
60 
61  $this->assertInstanceOf(Folder::class, $processingFolder);
62  $this->assertNotEquals($rootProcessingFolder, $processingFolder);
63 
64  for ($i = ‪ResourceStorage::PROCESSING_FOLDER_LEVELS; $i>0; $i--) {
65  $processingFolder = $processingFolder->getParentFolder();
66  }
67  $this->assertEquals($rootProcessingFolder, $processingFolder);
68  }
69 
79  public function ‪isWithinFileMountBoundariesRespectsReadOnlyFileMounts($targetDirectory, $fileMountFolder, $isFileMountReadOnly, $checkWriteAccess, $expectedResult)
80  {
81  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
82  $fileName = 'bar.txt';
83  $this->setUpBackendUserFromFixture(1);
84  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/_processed_');
85  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/' . $targetDirectory);
86  if ($fileMountFolder !== $targetDirectory) {
87  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/' . $fileMountFolder);
88  }
89  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/' . $targetDirectory . '/' . $fileName, 'myData');
90  clearstatcache();
91  $file = ‪ResourceFactory::getInstance()->‪getFileObjectFromCombinedIdentifier('1:/' . $targetDirectory . '/' . $fileName);
92 
93  $subject = (new ‪StorageRepository())->findByUid(1);
94  $subject->setEvaluatePermissions(true);
95 
96  // read_only = true -> no write access for user, so checking for second argument true should assert false
97  $subject->addFileMount('/' . $fileMountFolder . '/', ['read_only' => $isFileMountReadOnly]);
98  $this->assertSame($expectedResult, $subject->isWithinFileMountBoundaries($file, $checkWriteAccess));
99  }
100 
105  {
106  return [
107  'Access to file in ro file mount denied for write request' => [
108  '$targetDirectory' => 'fooBaz',
109  '$fileMountFolder' => 'fooBaz',
110  '$isFileMountReadOnly' => true,
111  '$checkWriteAccess' => true,
112  '$expectedResult' => false,
113  ],
114  'Access to file in ro file mount allowed for read request' => [
115  '$targetDirectory' => 'fooBaz',
116  '$fileMountFolder' => 'fooBaz',
117  '$isFileMountReadOnly' => true,
118  '$checkWriteAccess' => false,
119  '$expectedResult' => true,
120  ],
121  'Access to file in rw file mount allowed for write request' => [
122  '$targetDirectory' => 'fooBaz',
123  '$fileMountFolder' => 'fooBaz',
124  '$isFileMountReadOnly' => false,
125  '$checkWriteAccess' => true,
126  '$expectedResult' => true,
127  ],
128  'Access to file in rw file mount allowed for read request' => [
129  '$targetDirectory' => 'fooBaz',
130  '$fileMountFolder' => 'fooBaz',
131  '$isFileMountReadOnly' => false,
132  '$checkWriteAccess' => false,
133  '$expectedResult' => true,
134  ],
135  'Access to file not in file mount denied for write request' => [
136  '$targetDirectory' => 'fooBaz',
137  '$fileMountFolder' => 'barBaz',
138  '$isFileMountReadOnly' => false,
139  '$checkWriteAccess' => true,
140  '$expectedResult' => false,
141  ],
142  'Access to file not in file mount denied for read request' => [
143  '$targetDirectory' => 'fooBaz',
144  '$fileMountFolder' => 'barBaz',
145  '$isFileMountReadOnly' => false,
146  '$checkWriteAccess' => false,
147  '$expectedResult' => false,
148  ],
149  ];
150  }
151 
156  {
157  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
158  $this->setUpBackendUserFromFixture(1);
159 
160  $subject = (new ‪StorageRepository())->findByUid(1);
161  $processingFolder = $subject->getProcessingFolder();
162 
163  $this->assertInstanceOf(Folder::class, $processingFolder);
164  }
165 
170  {
171  $folderIdentifier = $this->getUniqueId();
172  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
173  $this->setUpBackendUserFromFixture(1);
174 
175  $subject = (new ‪StorageRepository())->findByUid(1);
176  $folder = new ‪Folder($subject, '/foo/' . $folderIdentifier . '/', $folderIdentifier);
177 
178  $role = $subject->getRole($folder);
179 
180  $this->assertSame(‪FolderInterface::ROLE_DEFAULT, $role);
181  }
182 
187  {
188  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
189  $this->setUpBackendUserFromFixture(1);
190  $subject = (new ‪StorageRepository())->findByUid(1);
191 
192  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/foo');
193  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/foo/bar.txt', 'myData');
194  clearstatcache();
196 
197  $this->expectException(\InvalidArgumentException::class);
198  $this->expectExceptionCode(1325842622);
199  $subject->replaceFile($file, ‪Environment::getPublicPath() . '/' . $this->getUniqueId());
200  }
201 
206  {
207  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
208  $this->setUpBackendUserFromFixture(1);
209  $subject = (new ‪StorageRepository())->findByUid(1);
210 
211  $this->expectException(\InvalidArgumentException::class);
212  $this->expectExceptionCode(1325689164);
213  $subject->createFolder('newFolder', new ‪Folder($subject, '/foo/', 'foo'));
214  }
215 
220  {
221  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
222  $this->setUpBackendUserFromFixture(1);
223  $subject = (new ‪StorageRepository())->findByUid(1);
224 
225  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/foo');
226  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/_recycler_');
227  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/foo/bar.txt', 'myData');
228  clearstatcache();
229 
231  $subject->deleteFile($file);
232 
233  $this->assertTrue(file_exists(‪Environment::getPublicPath() . '/fileadmin/_recycler_/bar.txt'));
234  $this->assertFalse(file_exists(‪Environment::getPublicPath() . '/fileadmin/foo/bar.txt'));
235  }
236 
241  {
242  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
243  $this->setUpBackendUserFromFixture(1);
244  $subject = (new ‪StorageRepository())->findByUid(1);
245 
246  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/foo');
247  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/foo/bar.txt', 'myData');
248  clearstatcache();
249 
251  $subject->deleteFile($file);
252 
253  $this->assertFalse(file_exists(‪Environment::getPublicPath() . '/fileadmin/foo/bar.txt'));
254  }
255 
257  {
258  return [
259  'Finds foo recursive by name' => [
260  'foo',
261  '/bar/',
262  true,
263  [],
264  [
265  '/bar/bla/foo.txt',
266  ],
267  ],
268  'Finds foo not recursive by name' => [
269  'foo',
270  '/bar/bla/',
271  false,
272  [],
273  [
274  '/bar/bla/foo.txt',
275  ],
276  ],
277  'Finds nothing when not recursive for top level folder' => [
278  'foo',
279  '/bar/',
280  false,
281  [],
282  [],
283  ],
284  'Finds foo by description' => [
285  'fodescrip',
286  '/bar/',
287  true,
288  [],
289  [
290  '/bar/bla/foo.txt',
291  ],
292  ],
293  'Finds foo by translated description' => [
294  'fotranslated',
295  '/bar/',
296  true,
297  [],
298  [
299  '/bar/bla/foo.txt',
300  ],
301  ],
302  'Finds blupp by name' => [
303  'blupp',
304  '/bar/',
305  false,
306  [],
307  [
308  '/bar/blupp.txt',
309  ],
310  ],
311  'Finds only blupp by title for non recursive' => [
312  'title',
313  '/bar/',
314  false,
315  [],
316  [
317  '/bar/blupp.txt',
318  ],
319  ],
320  'Finds foo and blupp by title for recursive' => [
321  'title',
322  '/bar/',
323  true,
324  [],
325  [
326  '/bar/blupp.txt',
327  '/bar/bla/foo.txt',
328  ],
329  ],
330  'Finds foo, baz and blupp with no folder' => [
331  'title',
332  null,
333  true,
334  [],
335  [
336  '/baz/bla/baz.txt',
337  '/bar/blupp.txt',
338  '/bar/bla/foo.txt',
339  ],
340  ],
341  'Finds nothing for not existing' => [
342  'baz',
343  '/bar/',
344  true,
345  [],
346  [],
347  ],
348  'Finds nothing in root, when not recursive' => [
349  'title',
350  '/',
351  false,
352  [],
353  [],
354  ],
355  'Finds nothing, when not recursive and no folder given' => [
356  'title',
357  null,
358  false,
359  [],
360  [],
361  ],
362  'Filter is applied to result' => [
363  'title',
364  null,
365  true,
366  [
367  function ($itemName) {
368  return strpos($itemName, 'blupp') !== false ? true : -1;
369  }
370  ],
371  [
372  '/bar/blupp.txt',
373  ],
374  ],
375  ];
376  }
377 
388  public function ‪searchFilesFindsFilesInFolder(string $searchTerm, ?string $searchFolder, bool $recursive, array $filters, array $expectedIdentifiers)
389  {
390  try {
391  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_file_storage.xml');
392  $this->importDataSet(__DIR__ . '/Fixtures/FileSearch.xml');
393  $this->setUpBackendUserFromFixture(1);
394  $subject = (new ‪StorageRepository())->findByUid(1);
395  $subject->setFileAndFolderNameFilters($filters);
396 
397  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/bar/bla');
398  GeneralUtility::mkdir_deep(‪Environment::getPublicPath() . '/fileadmin/baz/bla');
399  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/bar/bla/foo.txt', 'myData');
400  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/baz/bla/baz.txt', 'myData');
401  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/bar/blupp.txt', 'myData');
402  clearstatcache();
403 
404  $folder = $searchFolder ? ‪ResourceFactory::getInstance()->‪getFolderObjectFromCombinedIdentifier('1:' . $searchFolder) : null;
405  $search = ‪FileSearchDemand::createForSearchTerm($searchTerm);
406  if ($recursive) {
407  $search = $search->withRecursive();
408  }
409 
410  $result = $subject->searchFiles($search, $folder);
411  $expectedFiles = array_map([$subject, 'getFile'], $expectedIdentifiers);
412  $this->assertSame($expectedFiles, iterator_to_array($result));
413 
414  // Check if search also works for non hierarchical storages/drivers
415  $this->inject($subject, 'capabilities', $subject->getCapabilities() & 7);
416  $result = $subject->searchFiles($search, $folder);
417  $expectedFiles = array_map([$subject, 'getFile'], $expectedIdentifiers);
418  $this->assertSame($expectedFiles, iterator_to_array($result));
419  } finally {
420  GeneralUtility::rmdir(‪Environment::getPublicPath() . '/fileadmin/bar', true);
421  GeneralUtility::rmdir(‪Environment::getPublicPath() . '/fileadmin/baz', true);
422  }
423  }
424 }
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\deleteFileUnlinksFileIfNoRecyclerFolderAvailable
‪deleteFileUnlinksFileIfNoRecyclerFolderAvailable()
Definition: ResourceStorageTest.php:240
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Core\Resource\ResourceStorage\PROCESSING_FOLDER_LEVELS
‪const PROCESSING_FOLDER_LEVELS
Definition: ResourceStorage.php:159
‪TYPO3\CMS\Core\Tests\Functional\Resource
Definition: ResourceFactoryTest.php:18
‪TYPO3\CMS\Core\Resource\FolderInterface\ROLE_DEFAULT
‪const ROLE_DEFAULT
Definition: FolderInterface.php:25
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\createFolderThrowsExceptionIfParentFolderDoesNotExist
‪createFolderThrowsExceptionIfParentFolderDoesNotExist()
Definition: ResourceStorageTest.php:205
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFolderObjectFromCombinedIdentifier
‪Folder getFolderObjectFromCombinedIdentifier($identifier)
Definition: ResourceFactory.php:539
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\tearDown
‪tearDown()
Definition: ResourceStorageTest.php:32
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:24
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\searchFilesFindsFilesInFolder
‪searchFilesFindsFilesInFolder(string $searchTerm, ?string $searchFolder, bool $recursive, array $filters, array $expectedIdentifiers)
Definition: ResourceStorageTest.php:388
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:29
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\isWithinFileMountBoundariesDataProvider
‪array isWithinFileMountBoundariesDataProvider()
Definition: ResourceStorageTest.php:104
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\isWithinFileMountBoundariesRespectsReadOnlyFileMounts
‪isWithinFileMountBoundariesRespectsReadOnlyFileMounts($targetDirectory, $fileMountFolder, $isFileMountReadOnly, $checkWriteAccess, $expectedResult)
Definition: ResourceStorageTest.php:79
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\searchFilesFindsFilesInFolderDataProvider
‪searchFilesFindsFilesInFolderDataProvider()
Definition: ResourceStorageTest.php:256
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFileObjectFromCombinedIdentifier
‪File ProcessedFile null getFileObjectFromCombinedIdentifier($identifier)
Definition: ResourceFactory.php:424
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\getProcessingRootFolderTest
‪getProcessingRootFolderTest()
Definition: ResourceStorageTest.php:155
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\replaceFileFailsIfLocalFileDoesNotExist
‪replaceFileFailsIfLocalFileDoesNotExist()
Definition: ResourceStorageTest.php:186
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest
Definition: ResourceStorageTest.php:31
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:21
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\deleteFileMovesFileToRecyclerFolderIfAvailable
‪deleteFileMovesFileToRecyclerFolderIfAvailable()
Definition: ResourceStorageTest.php:219
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand\createForSearchTerm
‪static createForSearchTerm(string $searchTerm)
Definition: FileSearchDemand.php:68
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\getRoleReturnsDefaultForRegularFolders
‪getRoleReturnsDefaultForRegularFolders()
Definition: ResourceStorageTest.php:169
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceStorageTest\getNestedProcessingFolderTest
‪getNestedProcessingFolderTest()
Definition: ResourceStorageTest.php:43