[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Resource / ResourceStorageTest.php
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
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Core\Resource\FolderInterface;
20 use TYPO3\CMS\Core\Resource\ResourceFactory;
21 use TYPO3\CMS\Core\Resource\ResourceStorage;
22 use TYPO3\CMS\Core\Resource\StorageRepository;
23 use TYPO3\CMS\Core\Tests\FunctionalTestCase;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Test case
28 */
29 class ResourceStorageTest extends FunctionalTestCase
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
39 /**
40 * @test
41 */
42 public function getNestedProcessingFolderTest()
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
69 /**
70 * @param string $targetDirectory
71 * @param string $fileMountFolder
72 * @param bool $isFileMountReadOnly
73 * @param bool $checkWriteAccess
74 * @param bool $expectedResult
75 * @test
76 * @dataProvider isWithinFileMountBoundariesDataProvider
77 */
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
100 /**
101 * @return array
102 */
103 public function isWithinFileMountBoundariesDataProvider()
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
151 /**
152 * @test
153 */
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
165 /**
166 * @test
167 */
168 public function getRoleReturnsDefaultForRegularFolders()
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
182 /**
183 * @test
184 */
185 public function replaceFileFailsIfLocalFileDoesNotExist()
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
201 /**
202 * @test
203 */
204 public function createFolderThrowsExceptionIfParentFolderDoesNotExist()
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 }