1585c83dcca0e24b46c5760b17a3b2ac9dd4464e
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Resource / FolderTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Andreas Wolf <andreas.wolf@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Resource;
31
32 require_once 'vfsStream/vfsStream.php';
33
34 /**
35 * Functional test case for the FAL folder class.
36 *
37 * @author Andreas Wolf <andreas.wolf@typo3.org>
38 */
39 class FolderTest extends BaseTestCase {
40
41 /**
42 * Helper method for testing restore of filters in the storage
43 *
44 * @param $filterMode
45 * @param $listCallback
46 */
47 protected function _testFileAndFoldernameFilterRestoreAfterList($filterMode, $listCallback) {
48 $this->isType('callable')->evaluate($listCallback);
49 $storageFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
50 $storageFilter->setAllowedFileExtensions('jpg,png');
51 $folderFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
52 $folderFilter->setAllowedFileExtensions('png');
53
54 $storageObject = $this->getStorageObject();
55 $storageObject->setFileAndFolderNameFilters(array(array($storageFilter, 'filterFileList')));
56 $folder = $storageObject->getRootLevelFolder();
57 $folder->setFileAndFolderNameFilters(array(array($storageFilter, 'filterFileList')));
58
59 $filtersBackup = $storageObject->getFileAndFolderNameFilters();
60 $listCallback($folder, $filterMode);
61
62 $this->assertEquals($filtersBackup, $storageObject->getFileAndFolderNameFilters());
63 }
64
65
66 /***********************
67 * Tests for getFiles()
68 ***********************/
69
70 /**
71 * @test
72 */
73 public function getFilesRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFileListIfFilterModeIsUseOwnFilters() {
74 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS,
75 function(Resource\Folder $folder, $filterMode) {
76 $folder->getFiles(0, 0, $filterMode);
77 }
78 );
79 }
80
81 /**
82 * @test
83 */
84 public function getFilesRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFileListIfFilterModeIsUseOwnAndStorageFiltersAndFiltersHaveBeenSetInFolder() {
85 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS,
86 function(Resource\Folder $folder, $filterMode) {
87 $folder->getFiles(0, 0, $filterMode);
88 }
89 );
90 }
91
92 /**
93 * @test
94 */
95 public function getFilesRespectsSetFileAndFoldernameFiltersIfFilterModeIsUseOwnFilters() {
96 $filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
97 $filter->setAllowedFileExtensions('jpg,png');
98
99 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
100 $storageObject = $this->getStorageObject();
101 $folder = $storageObject->getRootLevelFolder();
102 $folder->setFileAndFolderNameFilters(array(array($filter, 'filterFileList')));
103
104 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS);
105
106 $this->assertArrayNotHasKey('somefile.exe', $fileList);
107 $this->assertCount(2, $fileList);
108 }
109
110 /**
111 * @test
112 */
113 public function getFilesMergesSetFileAndFoldernameFiltersIntoStoragesFiltersIfFilterModeIsUseOwnAndStorageFilters() {
114 $foldersFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
115 $foldersFilter->setAllowedFileExtensions('jpg,png');
116 $storagesFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
117 $storagesFilter->setDisallowedFileExtensions('png');
118
119 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
120 $storageObject = $this->getStorageObject();
121 $storageObject->setFileAndFolderNameFilters(array(array($storagesFilter, 'filterFileList')));
122 $folder = $storageObject->getRootLevelFolder();
123 $folder->setFileAndFolderNameFilters(array(array($foldersFilter, 'filterFileList')));
124
125 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS);
126
127 $this->assertArrayNotHasKey('somefile.exe', $fileList);
128 $this->assertArrayNotHasKey('somefile.png', $fileList);
129 $this->assertCount(1, $fileList);
130 }
131
132 /**
133 * @test
134 */
135 public function getFilesUsesOnlyFileAndFoldernameFiltersOfStorageIfNoFiltersAreSetAndFilterModeIsUseOwnAndStorageFilters() {
136 $filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
137 $filter->setAllowedFileExtensions('jpg,png');
138
139 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
140 $storageObject = $this->getStorageObject();
141 $folder = $storageObject->getRootLevelFolder();
142 $storageObject->setFileAndFolderNameFilters(array(array($filter, 'filterFileList')));
143
144 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS);
145
146 $this->assertArrayNotHasKey('somefile.exe', $fileList);
147 $this->assertCount(2, $fileList);
148 }
149
150 /**
151 * @test
152 */
153 public function getFilesIgnoresSetFileAndFoldernameFiltersIfFilterModeIsSetToUseStorageFilters() {
154 $filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
155 $filter->setAllowedFileExtensions('jpg,png');
156
157 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
158 $storageObject = $this->getStorageObject();
159 $folder = $storageObject->getRootLevelFolder();
160 $folder->setFileAndFolderNameFilters(array(array($filter, 'filterFileList')));
161
162 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_STORAGE_FILTERS);
163
164 $this->assertCount(3, $fileList);
165 }
166
167
168 /****************************
169 * Tests for getSubfolders()
170 ****************************/
171
172 /**
173 * @test
174 */
175 public function getSubfoldersRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFolderListIfFilterModeIsUseOwnFilters() {
176 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS,
177 function(Resource\Folder $folder, $filterMode) {
178 $folder->getSubfolders(0, 0, $filterMode);
179 }
180 );
181 }
182
183 /**
184 * @test
185 */
186 public function getSubfoldersRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFolderListIfFilterModeIsUseOwnAndStorageFiltersAndFiltersHaveBeenSetInFolder() {
187 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS,
188 function(Resource\Folder $folder, $filterMode) {
189 $folder->getSubfolders(0, 0, $filterMode);
190 }
191 );
192 }
193
194 /**
195 * @test
196 */
197 public function getSubfoldersRespectsSetFileAndFoldernameFiltersIfFilterModeIsUseOwnFilters() {
198 \TYPO3\CMS\Core\Resource\Filter\FileNameFilter::setShowHiddenFilesAndFolders(FALSE);
199
200 $this->addToMount(array('.hiddenFolder' => array(), 'someFolder' => array(), 'anotherFolder' => array()));
201 $storageObject = $this->getStorageObject();
202 $storageObject->setFileAndFolderNameFilters(array());
203 $folder = $storageObject->getRootLevelFolder();
204 $folder->setFileAndFolderNameFilters(array(array('TYPO3\\CMS\\Core\\Resource\\Filter\\FileNameFilter', 'filterHiddenFilesAndFolders')));
205
206 $folderList = $folder->getSubfolders(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS);
207
208 $this->assertArrayNotHasKey('.hiddenFolder', $folderList);
209 $this->assertCount(2, $folderList);
210 }
211
212 // TODO implement the other tests from getFiles
213 }
214
215 ?>