TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Functional/Resource/ResourceStorageTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\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 
25 
30 {
31  protected function tearDown()
32  {
33  // cleanup manually created folders
34  foreach (glob(PATH_site . 'fileadmin/*') as $folderToRemove) {
35  GeneralUtility::rmdir($folderToRemove, true);
36  }
37  }
38 
43  {
44  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
45  $this->setUpBackendUserFromFixture(1);
46  $subject = (new StorageRepository())->findByUid(1);
47  $subject->setEvaluatePermissions(false);
48 
49  GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/_processed_');
50  GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/adirectory');
51  GeneralUtility::mkdir_deep(PATH_site . 'typo3temp/assets/_processed_/');
52  file_put_contents(PATH_site . 'fileadmin/adirectory/bar.txt', 'myData');
53  clearstatcache();
54  $subject->addFileMount('/adirectory/', ['read_only' => false]);
55  $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier('1:/adirectory/bar.txt');
56 
57  $rootProcessingFolder = $subject->getProcessingFolder();
58  $processingFolder = $subject->getProcessingFolder($file);
59 
60  $this->assertInstanceOf(Folder::class, $processingFolder);
61  $this->assertNotEquals($rootProcessingFolder, $processingFolder);
62 
63  for ($i = ResourceStorage::PROCESSING_FOLDER_LEVELS; $i>0; $i--) {
64  $processingFolder = $processingFolder->getParentFolder();
65  }
66  $this->assertEquals($rootProcessingFolder, $processingFolder);
67  }
68 
78  public function isWithinFileMountBoundariesRespectsReadOnlyFileMounts($targetDirectory, $fileMountFolder, $isFileMountReadOnly, $checkWriteAccess, $expectedResult)
79  {
80  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
81  $fileName = 'bar.txt';
82  $this->setUpBackendUserFromFixture(1);
83  GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/_processed_');
84  GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/' . $targetDirectory);
85  if ($fileMountFolder !== $targetDirectory) {
86  GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/' . $fileMountFolder);
87  }
88  file_put_contents(PATH_site . 'fileadmin/' . $targetDirectory . '/' . $fileName, 'myData');
89  clearstatcache();
90  $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier('1:/' . $targetDirectory . '/' . $fileName);
91 
92  $subject = (new StorageRepository())->findByUid(1);
93  $subject->setEvaluatePermissions(true);
94 
95  // read_only = true -> no write access for user, so checkinf for second argument true should assert false
96  $subject->addFileMount('/' . $fileMountFolder . '/', ['read_only' => $isFileMountReadOnly]);
97  $this->assertSame($expectedResult, $subject->isWithinFileMountBoundaries($file, $checkWriteAccess));
98  }
99 
104  {
105  return [
106  'Access to file in ro file mount denied for write request' => [
107  '$targetDirectory' => 'fooBaz',
108  '$fileMountFolder' => 'fooBaz',
109  '$isFileMountReadOnly' => true,
110  '$checkWriteAccess' => true,
111  '$expectedResult' => false,
112  ],
113  'Access to file in ro file mount allowed for read request' => [
114  '$targetDirectory' => 'fooBaz',
115  '$fileMountFolder' => 'fooBaz',
116  '$isFileMountReadOnly' => true,
117  '$checkWriteAccess' => false,
118  '$expectedResult' => true,
119  ],
120  'Access to file in rw file mount allowed for write request' => [
121  '$targetDirectory' => 'fooBaz',
122  '$fileMountFolder' => 'fooBaz',
123  '$isFileMountReadOnly' => false,
124  '$checkWriteAccess' => true,
125  '$expectedResult' => true,
126  ],
127  'Access to file in rw file mount allowed for read request' => [
128  '$targetDirectory' => 'fooBaz',
129  '$fileMountFolder' => 'fooBaz',
130  '$isFileMountReadOnly' => false,
131  '$checkWriteAccess' => false,
132  '$expectedResult' => true,
133  ],
134  'Access to file not in file mount denied for write request' => [
135  '$targetDirectory' => 'fooBaz',
136  '$fileMountFolder' => 'barBaz',
137  '$isFileMountReadOnly' => false,
138  '$checkWriteAccess' => true,
139  '$expectedResult' => false,
140  ],
141  'Access to file not in file mount denied for read request' => [
142  '$targetDirectory' => 'fooBaz',
143  '$fileMountFolder' => 'barBaz',
144  '$isFileMountReadOnly' => false,
145  '$checkWriteAccess' => false,
146  '$expectedResult' => false,
147  ],
148  ];
149  }
150 
154  public function getProcessingRootFolderTest()
155  {
156  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
157  $this->setUpBackendUserFromFixture(1);
158 
159  $subject = (new StorageRepository())->findByUid(1);
160  $processingFolder = $subject->getProcessingFolder();
161 
162  $this->assertInstanceOf(Folder::class, $processingFolder);
163  }
164 
169  {
170  $folderIdentifier = $this->getUniqueId();
171  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
172  $this->setUpBackendUserFromFixture(1);
173 
174  $subject = (new StorageRepository())->findByUid(1);
175  $folder = new Folder($subject, '/foo/' . $folderIdentifier . '/', $folderIdentifier);
176 
177  $role = $subject->getRole($folder);
178 
179  $this->assertSame(FolderInterface::ROLE_DEFAULT, $role);
180  }
181 
186  {
187  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
188  $this->setUpBackendUserFromFixture(1);
189  $subject = (new StorageRepository())->findByUid(1);
190 
191  GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/foo');
192  file_put_contents(PATH_site . 'fileadmin/foo/bar.txt', 'myData');
193  clearstatcache();
194  $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier('1:/foo/bar.txt');
195 
196  $this->expectException(\InvalidArgumentException::class);
197  $this->expectExceptionCode(1325842622);
198  $subject->replaceFile($file, PATH_site . $this->getUniqueId());
199  }
200 
205  {
206  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
207  $this->setUpBackendUserFromFixture(1);
208  $subject = (new StorageRepository())->findByUid(1);
209 
210  $this->expectException(\InvalidArgumentException::class);
211  $this->expectExceptionCode(1325689164);
212  $subject->createFolder('newFolder', new Folder($subject, '/foo/', 'foo'));
213  }
214 }
static rmdir($path, $removeNonEmpty=false)
static mkdir_deep($directory, $deepDirectory= '')
isWithinFileMountBoundariesRespectsReadOnlyFileMounts($targetDirectory, $fileMountFolder, $isFileMountReadOnly, $checkWriteAccess, $expectedResult)