[BUGFIX] Clean-up responsibilities of FAL classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Utility / FileExtensionFilterTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Oliver Hader <oliver.hader@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 /**
31 * Test suite for filtering files by their extensions.
32 *
33 * @author Oliver Hader <oliver.hader@typo3.org>
34 */
35 class FileExtensionFilterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
36
37 /**
38 * @var array A backup of registered singleton instances
39 */
40 protected $singletonInstances = array();
41
42 /**
43 * @var \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter
44 */
45 protected $filter;
46
47 /**
48 * @var array
49 */
50 protected $parameters;
51
52 /**
53 * @var \TYPO3\CMS\Core\DataHandling\DataHandler|PHPUnit_Framework_MockObject_MockObject
54 */
55 protected $tceMainMock;
56
57 /**
58 * @var \TYPO3\CMS\Core\Resource\ResourceFactory|PHPUnit_Framework_MockObject_MockObject
59 */
60 protected $fileFactoryMock;
61
62 /**
63 * Sets up this test suite.
64 */
65 protected function setUp() {
66 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
67 $this->filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
68 $this->tceMainMock = $this->getMock('TYPO3\\CMS\\Core\\DataHandling\\DataHandler', array('deleteAction'), array());
69 $this->fileFactoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array('getFileReferenceObject'), array());
70 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', $this->fileFactoryMock);
71 }
72
73 /**
74 * Cleans up this test suite.
75 */
76 protected function tearDown() {
77 unset($this->fileFactoryMock);
78 unset($this->tceMainMock);
79 unset($this->parameters);
80 unset($this->filter);
81 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
82 }
83
84 /**
85 * @return array
86 */
87 public function invalidInlineChildrenFilterParametersDataProvider() {
88 return array(
89 array(NULL, NULL, NULL),
90 array('', '', array(0, '', NULL, FALSE)),
91 array(NULL, NULL, array(0, '', NULL, FALSE))
92 );
93 }
94
95 /**
96 * @param array|string $allowed
97 * @param array|string $disallowed
98 * @param array|string $values
99 * @test
100 * @dataProvider invalidInlineChildrenFilterParametersDataProvider
101 */
102 public function areInlineChildrenFilteredWithInvalidParameters($allowed, $disallowed, $values) {
103 $this->parameters = array(
104 'allowedFileExtensions' => $allowed,
105 'disallowedFileExtensions' => $disallowed,
106 'values' => $values
107 );
108 $this->tceMainMock->expects($this->never())->method('deleteAction');
109 $this->fileFactoryMock->expects($this->never())->method('getFileReferenceObject');
110 $this->filter->filterInlineChildren($this->parameters, $this->tceMainMock);
111 }
112
113 /**
114 * @return array
115 */
116 public function extensionFilterIgnoresCaseInAllowedExtensionCheckDataProvider() {
117 return array(
118 'Allowed extensions' => array(
119 'ext1', 'EXT1', '', TRUE
120 ),
121 'Allowed extensions, lower and upper case mix' => array(
122 'ext1', 'ext2, ExT1, Ext3', '', TRUE
123 ),
124 'Disallowed extensions' => array(
125 'ext1', '', 'EXT1', FALSE
126 ),
127 'Disallowed extensions, lower and upper case mix' => array(
128 'ext1', '', 'ext2, ExT1, Ext3', FALSE
129 ),
130 'Combine allowed / disallowed extensions' => array(
131 'ext1', 'EXT1', 'EXT1', FALSE
132 ),
133 );
134 }
135
136 /**
137 * @param string $fileExtension
138 * @param array|string $allowedExtensions
139 * @param array|string $disallowedExtensions
140 * @param boolean $isAllowed
141 * @test
142 * @dataProvider extensionFilterIgnoresCaseInAllowedExtensionCheckDataProvider
143 */
144 public function extensionFilterIgnoresCaseInAllowedExtensionCheck($fileExtension, $allowedExtensions, $disallowedExtensions, $isAllowed) {
145
146 /** @var \TYPO3\CMS\Core\Resource\File $file */
147 $file = new \TYPO3\CMS\Core\Resource\File(array('name' => 'file.' . $fileExtension),
148 $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceStorage', array(), array(), '', FALSE)
149 );
150
151 /** @var \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter $filter */
152 $filter = $this->getAccessibleMock('\TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter', array('dummy'));
153 $filter->setAllowedFileExtensions($allowedExtensions);
154 $filter->setDisallowedFileExtensions($disallowedExtensions);
155 $result = $filter->_call('isAllowed', $file);
156 $this->assertEquals($isAllowed, $result);
157 }
158 }