[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 * 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 /**
18 * Test suite for filtering files by their extensions.
19 *
20 * @author Oliver Hader <oliver.hader@typo3.org>
21 */
22 class FileExtensionFilterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @var array A backup of registered singleton instances
26 */
27 protected $singletonInstances = array();
28
29 /**
30 * @var \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter
31 */
32 protected $filter;
33
34 /**
35 * @var array
36 */
37 protected $parameters;
38
39 /**
40 * @var \TYPO3\CMS\Core\DataHandling\DataHandler|PHPUnit_Framework_MockObject_MockObject
41 */
42 protected $tceMainMock;
43
44 /**
45 * @var \TYPO3\CMS\Core\Resource\ResourceFactory|PHPUnit_Framework_MockObject_MockObject
46 */
47 protected $fileFactoryMock;
48
49 /**
50 * Sets up this test suite.
51 */
52 protected function setUp() {
53 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
54 $this->filter = new \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter();
55 $this->tceMainMock = $this->getMock('TYPO3\\CMS\\Core\\DataHandling\\DataHandler', array('deleteAction'), array());
56 $this->fileFactoryMock = $this->getMock('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', array('getFileReferenceObject'), array());
57 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory', $this->fileFactoryMock);
58 }
59
60 /**
61 * Cleans up this test suite.
62 */
63 protected function tearDown() {
64 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
65 parent::tearDown();
66 }
67
68 /**
69 * @return array
70 */
71 public function invalidInlineChildrenFilterParametersDataProvider() {
72 return array(
73 array(NULL, NULL, NULL),
74 array('', '', array(0, '', NULL, FALSE)),
75 array(NULL, NULL, array(0, '', NULL, FALSE))
76 );
77 }
78
79 /**
80 * @param array|string $allowed
81 * @param array|string $disallowed
82 * @param array|string $values
83 * @test
84 * @dataProvider invalidInlineChildrenFilterParametersDataProvider
85 */
86 public function areInlineChildrenFilteredWithInvalidParameters($allowed, $disallowed, $values) {
87 $this->parameters = array(
88 'allowedFileExtensions' => $allowed,
89 'disallowedFileExtensions' => $disallowed,
90 'values' => $values
91 );
92 $this->tceMainMock->expects($this->never())->method('deleteAction');
93 $this->fileFactoryMock->expects($this->never())->method('getFileReferenceObject');
94 $this->filter->filterInlineChildren($this->parameters, $this->tceMainMock);
95 }
96
97 /**
98 * @return array
99 */
100 public function extensionFilterIgnoresCaseInAllowedExtensionCheckDataProvider() {
101 return array(
102 'Allowed extensions' => array(
103 'ext1', 'EXT1', '', TRUE
104 ),
105 'Allowed extensions, lower and upper case mix' => array(
106 'ext1', 'ext2, ExT1, Ext3', '', TRUE
107 ),
108 'Disallowed extensions' => array(
109 'ext1', '', 'EXT1', FALSE
110 ),
111 'Disallowed extensions, lower and upper case mix' => array(
112 'ext1', '', 'ext2, ExT1, Ext3', FALSE
113 ),
114 'Combine allowed / disallowed extensions' => array(
115 'ext1', 'EXT1', 'EXT1', FALSE
116 ),
117 );
118 }
119
120 /**
121 * @param string $fileExtension
122 * @param array|string $allowedExtensions
123 * @param array|string $disallowedExtensions
124 * @param boolean $isAllowed
125 * @test
126 * @dataProvider extensionFilterIgnoresCaseInAllowedExtensionCheckDataProvider
127 */
128 public function extensionFilterIgnoresCaseInAllowedExtensionCheck($fileExtension, $allowedExtensions, $disallowedExtensions, $isAllowed) {
129 /** @var \TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter $filter */
130 $filter = $this->getAccessibleMock('\TYPO3\CMS\Core\Resource\Filter\FileExtensionFilter', array('dummy'));
131 $filter->setAllowedFileExtensions($allowedExtensions);
132 $filter->setDisallowedFileExtensions($disallowedExtensions);
133 $result = $filter->_call('isAllowed', 'file.' . $fileExtension);
134 $this->assertEquals($isAllowed, $result);
135 }
136 }