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