[TASK] Add unit tests for CommandUtility::unQuoteFilenames
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / CommandUtilityTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Utility\CommandUtility;
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20
21 /**
22 * Test case
23 */
24 class CommandUtilityTest extends UnitTestCase
25 {
26 /**
27 * Data provider for getConfiguredApps
28 *
29 * @return array
30 */
31 public function getConfiguredAppsDataProvider()
32 {
33 $defaultExpected = [
34 'perl' => [
35 'app' => 'perl',
36 'path' => '/usr/bin/',
37 'valid' => true
38 ],
39 'unzip' => [
40 'app' => 'unzip',
41 'path' => '/usr/local/bin/',
42 'valid' => true
43 ],
44 ];
45 return [
46 'returns empty array for empty string' => [
47 '',
48 []
49 ],
50 'separated by comma' => [
51 'perl=/usr/bin/perl,unzip=/usr/local/bin/unzip',
52 $defaultExpected
53 ],
54 'separated by new line' => [
55 'perl=/usr/bin/perl ' . LF . ' unzip=/usr/local/bin/unzip',
56 $defaultExpected
57 ],
58 'separated by new line with spaces' => [
59 'perl = /usr/bin/perl ' . LF . ' unzip = /usr/local/bin/unzip',
60 $defaultExpected
61 ],
62 'separated by new line with spaces and empty rows' => [
63 LF . 'perl = /usr/bin/perl ' . LF . LF . ' unzip = /usr/local/bin/unzip' . LF,
64 $defaultExpected
65 ],
66 'separated by char(10)' => [
67 'perl=/usr/bin/perl' . '\'.chr(10).\'' . 'unzip=/usr/local/bin/unzip',
68 $defaultExpected
69 ],
70 'separated by LF as string' => [
71 'perl=/usr/bin/perl' . '\' . LF . \'' . 'unzip=/usr/local/bin/unzip',
72 $defaultExpected
73 ]
74 ];
75 }
76
77 /**
78 * @dataProvider getConfiguredAppsDataProvider
79 * @param array $globalsBinSetup
80 * @param array $expected
81 * @test
82 */
83 public function getConfiguredApps($globalsBinSetup, $expected)
84 {
85 $GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup'] = $globalsBinSetup;
86 $commandUtilityMock = $this->getAccessibleMock(CommandUtility::class, ['dummy']);
87 $result = $commandUtilityMock->_call('getConfiguredApps');
88 $this->assertSame($expected, $result);
89 }
90
91 /**
92 * Data provider unQuoteFilenameUnquotesCorrectly
93 */
94 public function unQuoteFilenameUnquotesCorrectlyDataProvider(): array
95 {
96 return [
97 // Some theoretical tests first
98 [
99 '',
100 []
101 ],
102 [
103 'aa bb "cc" "dd"',
104 ['aa', 'bb', '"cc"', '"dd"']
105 ],
106 [
107 'aa bb "cc dd"',
108 ['aa', 'bb', '"cc dd"']
109 ],
110 [
111 '\'aa bb\' "cc dd"',
112 ['\'aa bb\'', '"cc dd"']
113 ],
114 [
115 '\'aa bb\' cc "dd"',
116 ['\'aa bb\'', 'cc', '"dd"']
117 ],
118 // Now test against some real world examples
119 [
120 '/opt/local/bin/gm.exe convert +profile \'*\' -geometry 170x136! -negate "C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
121 [
122 '/opt/local/bin/gm.exe',
123 'convert',
124 '+profile',
125 '\'*\'',
126 '-geometry',
127 '170x136!',
128 '-negate',
129 '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
130 '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
131 ]
132 ],
133 [
134 'C:/opt/local/bin/gm.exe convert +profile \'*\' -geometry 170x136! -negate "C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
135 [
136 'C:/opt/local/bin/gm.exe',
137 'convert',
138 '+profile',
139 '\'*\'',
140 '-geometry',
141 '170x136!',
142 '-negate',
143 '"C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
144 '"C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
145 ]
146 ],
147 [
148 '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate "/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
149 [
150 '/usr/bin/gm',
151 'convert',
152 '+profile',
153 '\'*\'',
154 '-geometry',
155 '170x136!',
156 '-negate',
157 '"/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
158 '"/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
159 ]
160 ],
161 [
162 '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate "/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
163 [
164 '/usr/bin/gm',
165 'convert',
166 '+profile',
167 '\'*\'',
168 '-geometry',
169 '170x136!',
170 '-negate',
171 '"/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
172 '"/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
173 ]
174 ],
175 [
176 '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate \'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\' \'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\'',
177 [
178 '/usr/bin/gm',
179 'convert',
180 '+profile',
181 '\'*\'',
182 '-geometry',
183 '170x136!',
184 '-negate',
185 '\'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\'',
186 '\'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\''
187 ]
188 ]
189 ];
190 }
191
192 /**
193 * Tests if the commands are exploded and unquoted correctly
194 *
195 * @dataProvider unQuoteFilenameUnquotesCorrectlyDataProvider
196 * @test
197 * @param string $source
198 * @param array $expectedQuoted
199 */
200 public function unQuoteFilenameUnquotesCorrectly(string $source, array $expectedQuoted): void
201 {
202 $commandUtilityMock = $this->getAccessibleMock(CommandUtility::class, ['dummy']);
203 $actualQuoted = $commandUtilityMock->_call('unQuoteFilenames', $source);
204 $this->assertEquals($expectedQuoted, $actualQuoted);
205 }
206 }