[BUGFIX] require_once of vfsStream triggers travis fail
[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 /**
33 * Functional test case for the FAL folder class.
34 *
35 * @author Andreas Wolf <andreas.wolf@typo3.org>
36 */
37 class FolderTest extends BaseTestCase {
38
39 /**
40 * Helper method for testing restore of filters in the storage
41 *
42 * @param $filterMode
43 * @param $listCallback
44 */
45 protected function _testFileAndFoldernameFilterRestoreAfterList($filterMode, $listCallback) {
46 $this->isType('callable')->evaluate($listCallback);
47 $storageFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
48 $storageFilter->setAllowedFileExtensions('jpg,png');
49 $folderFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
50 $folderFilter->setAllowedFileExtensions('png');
51
52 $storageObject = $this->getStorageObject();
53 $storageObject->setFileAndFolderNameFilters(array(array($storageFilter, 'filterFileList')));
54 $folder = $storageObject->getRootLevelFolder();
55 $folder->setFileAndFolderNameFilters(array(array($storageFilter, 'filterFileList')));
56
57 $filtersBackup = $storageObject->getFileAndFolderNameFilters();
58 $listCallback($folder, $filterMode);
59
60 $this->assertEquals($filtersBackup, $storageObject->getFileAndFolderNameFilters());
61 }
62
63
64 /***********************
65 * Tests for getFiles()
66 ***********************/
67
68 /**
69 * @test
70 */
71 public function getFilesRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFileListIfFilterModeIsUseOwnFilters() {
72 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS,
73 function(Resource\Folder $folder, $filterMode) {
74 $folder->getFiles(0, 0, $filterMode);
75 }
76 );
77 }
78
79 /**
80 * @test
81 */
82 public function getFilesRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFileListIfFilterModeIsUseOwnAndStorageFiltersAndFiltersHaveBeenSetInFolder() {
83 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS,
84 function(Resource\Folder $folder, $filterMode) {
85 $folder->getFiles(0, 0, $filterMode);
86 }
87 );
88 }
89
90 /**
91 * @test
92 */
93 public function getFilesRespectsSetFileAndFoldernameFiltersIfFilterModeIsUseOwnFilters() {
94 $filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
95 $filter->setAllowedFileExtensions('jpg,png');
96
97 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
98 $storageObject = $this->getStorageObject();
99 $folder = $storageObject->getRootLevelFolder();
100 $folder->setFileAndFolderNameFilters(array(array($filter, 'filterFileList')));
101
102 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS);
103
104 $this->assertArrayNotHasKey('somefile.exe', $fileList);
105 $this->assertCount(2, $fileList);
106 }
107
108 /**
109 * @test
110 */
111 public function getFilesMergesSetFileAndFoldernameFiltersIntoStoragesFiltersIfFilterModeIsUseOwnAndStorageFilters() {
112 $foldersFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
113 $foldersFilter->setAllowedFileExtensions('jpg,png');
114 $storagesFilter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
115 $storagesFilter->setDisallowedFileExtensions('png');
116
117 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
118 $storageObject = $this->getStorageObject();
119 $storageObject->setFileAndFolderNameFilters(array(array($storagesFilter, 'filterFileList')));
120 $folder = $storageObject->getRootLevelFolder();
121 $folder->setFileAndFolderNameFilters(array(array($foldersFilter, 'filterFileList')));
122
123 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS);
124
125 $this->assertArrayNotHasKey('somefile.exe', $fileList);
126 $this->assertArrayNotHasKey('somefile.png', $fileList);
127 $this->assertCount(1, $fileList);
128 }
129
130 /**
131 * @test
132 */
133 public function getFilesUsesOnlyFileAndFoldernameFiltersOfStorageIfNoFiltersAreSetAndFilterModeIsUseOwnAndStorageFilters() {
134 $filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
135 $filter->setAllowedFileExtensions('jpg,png');
136
137 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
138 $storageObject = $this->getStorageObject();
139 $folder = $storageObject->getRootLevelFolder();
140 $storageObject->setFileAndFolderNameFilters(array(array($filter, 'filterFileList')));
141
142 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS);
143
144 $this->assertArrayNotHasKey('somefile.exe', $fileList);
145 $this->assertCount(2, $fileList);
146 }
147
148 /**
149 * @test
150 */
151 public function getFilesIgnoresSetFileAndFoldernameFiltersIfFilterModeIsSetToUseStorageFilters() {
152 $filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
153 $filter->setAllowedFileExtensions('jpg,png');
154
155 $this->addToMount(array('somefile.png' => '', 'somefile.jpg' => '', 'somefile.exe' => ''));
156 $storageObject = $this->getStorageObject();
157 $folder = $storageObject->getRootLevelFolder();
158 $folder->setFileAndFolderNameFilters(array(array($filter, 'filterFileList')));
159
160 $fileList = $folder->getFiles(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_STORAGE_FILTERS);
161
162 $this->assertCount(3, $fileList);
163 }
164
165
166 /****************************
167 * Tests for getSubfolders()
168 ****************************/
169
170 /**
171 * @test
172 */
173 public function getSubfoldersRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFolderListIfFilterModeIsUseOwnFilters() {
174 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS,
175 function(Resource\Folder $folder, $filterMode) {
176 $folder->getSubfolders(0, 0, $filterMode);
177 }
178 );
179 }
180
181 /**
182 * @test
183 */
184 public function getSubfoldersRestoresFileAndFoldernameFiltersOfStorageAfterFetchingFolderListIfFilterModeIsUseOwnAndStorageFiltersAndFiltersHaveBeenSetInFolder() {
185 $this->_testFileAndFoldernameFilterRestoreAfterList(\TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS,
186 function(Resource\Folder $folder, $filterMode) {
187 $folder->getSubfolders(0, 0, $filterMode);
188 }
189 );
190 }
191
192 /**
193 * @test
194 */
195 public function getSubfoldersRespectsSetFileAndFoldernameFiltersIfFilterModeIsUseOwnFilters() {
196 \TYPO3\CMS\Core\Resource\Filter\FileNameFilter::setShowHiddenFilesAndFolders(FALSE);
197
198 $this->addToMount(array('.hiddenFolder' => array(), 'someFolder' => array(), 'anotherFolder' => array()));
199 $storageObject = $this->getStorageObject();
200 $storageObject->setFileAndFolderNameFilters(array());
201 $folder = $storageObject->getRootLevelFolder();
202 $folder->setFileAndFolderNameFilters(array(array('TYPO3\\CMS\\Core\\Resource\\Filter\\FileNameFilter', 'filterHiddenFilesAndFolders')));
203
204 $folderList = $folder->getSubfolders(0, 0, \TYPO3\CMS\Core\Resource\Folder::FILTER_MODE_USE_OWN_FILTERS);
205
206 $this->assertArrayNotHasKey('.hiddenFolder', $folderList);
207 $this->assertCount(2, $folderList);
208 }
209
210 // TODO implement the other tests from getFiles
211 }
212
213 ?>