[TASK] Remove ext:dbal from installation steps
[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
32 protected function tearDown()
33 {
34 // cleanup manually created folders
35 foreach (glob(PATH_site . 'fileadmin/*') as $folderToRemove) {
36 GeneralUtility::rmdir($folderToRemove, true);
37 }
38 }
39
40 /**
41 * @test
42 */
43 public function getNestedProcessingFolderTest()
44 {
45 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/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(PATH_site . 'fileadmin/_processed_');
51 GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/adirectory');
52 GeneralUtility::mkdir_deep(PATH_site . 'typo3temp/assets/_processed_/');
53 file_put_contents(PATH_site . 'fileadmin/adirectory/bar.txt', 'myData');
54 clearstatcache();
55 $subject->addFileMount('/adirectory/', ['read_only' => false]);
56 $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier('1:/adirectory/bar.txt');
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
70 /**
71 * @param string $targetDirectory
72 * @param string $fileMountFolder
73 * @param bool $isFileMountReadOnly
74 * @param bool $checkWriteAccess
75 * @param bool $expectedResult
76 * @test
77 * @dataProvider isWithinFileMountBoundariesDataProvider
78 */
79 public function isWithinFileMountBoundariesRespectsReadOnlyFileMounts($targetDirectory, $fileMountFolder, $isFileMountReadOnly, $checkWriteAccess, $expectedResult)
80 {
81 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
82 $fileName = 'bar.txt';
83 $this->setUpBackendUserFromFixture(1);
84 GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/_processed_');
85 GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/' . $targetDirectory);
86 if ($fileMountFolder !== $targetDirectory) {
87 GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/' . $fileMountFolder);
88 }
89 file_put_contents(PATH_site . '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 checkinf for second argument true should assert false
97 $subject->addFileMount('/' . $fileMountFolder . '/', ['read_only' => $isFileMountReadOnly]);
98 $this->assertSame($expectedResult, $subject->isWithinFileMountBoundaries($file, $checkWriteAccess));
99 }
100
101 /**
102 * @return array
103 */
104 public function isWithinFileMountBoundariesDataProvider()
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
152 /**
153 * @test
154 */
155 public function getProcessingRootFolderTest()
156 {
157 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/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
166 /**
167 * @test
168 */
169 public function getRoleReturnsDefaultForRegularFolders()
170 {
171 $folderIdentifier = $this->getUniqueId();
172 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/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
183 /**
184 * @test
185 */
186 public function replaceFileFailsIfLocalFileDoesNotExist()
187 {
188 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_file_storage.xml');
189 $this->setUpBackendUserFromFixture(1);
190 $subject = (new StorageRepository())->findByUid(1);
191
192 GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/foo');
193 file_put_contents(PATH_site . 'fileadmin/foo/bar.txt', 'myData');
194 clearstatcache();
195 $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier('1:/foo/bar.txt');
196
197 $this->expectException(\InvalidArgumentException::class);
198 $this->expectExceptionCode(1325842622);
199 $subject->replaceFile($file, PATH_site . $this->getUniqueId());
200 }
201
202 /**
203 * @test
204 */
205 public function createFolderThrowsExceptionIfParentFolderDoesNotExist()
206 {
207 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/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 }