‪TYPO3CMS  9.5
GeneralUtilityTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 Prophecy\Argument;
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\StreamInterface;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 class ‪GeneralUtilityTest extends UnitTestCase
29 {
30  public function ‪splitHeaderLinesDataProvider(): array
31  {
32  return [
33  'one-line, single header' => [
34  ['Content-Security-Policy:default-src \'self\'; img-src https://*; child-src \'none\';'],
35  ['Content-Security-Policy' => 'default-src \'self\'; img-src https://*; child-src \'none\';']
36  ],
37  'one-line, multiple headers' => [
38  [
39  'Content-Security-Policy:default-src \'self\'; img-src https://*; child-src \'none\';',
40  'Content-Security-Policy-Report-Only:default-src https:; report-uri /csp-violation-report-endpoint/'
41  ],
42  [
43  'Content-Security-Policy' => 'default-src \'self\'; img-src https://*; child-src \'none\';',
44  'Content-Security-Policy-Report-Only' => 'default-src https:; report-uri /csp-violation-report-endpoint/'
45  ]
46  ]
47  ];
48  }
49 
56  public function ‪splitHeaderLines(array $headers, array $expectedHeaders): void
57  {
58  $stream = $this->prophesize(StreamInterface::class);
59  $response = $this->prophesize(ResponseInterface::class);
60  $response->getBody()->willReturn($stream);
61  $requestFactory = $this->prophesize(RequestFactory::class);
62  $requestFactory->request(Argument::cetera())->willReturn($response);
63 
64  GeneralUtility::addInstance(RequestFactory::class, $requestFactory->reveal());
65  GeneralUtility::getUrl('http://example.com', 0, $headers);
66 
67  $requestFactory->request(Argument::any(), Argument::any(), ['headers' => $expectedHeaders])
68  ->shouldHaveBeenCalled();
69  }
70 
72  // Tests concerning unQuoteFilenames
74 
79  {
80  return [
81  // Some theoretical tests first
82  [
83  '',
84  [],
85  []
86  ],
87  [
88  'aa bb "cc" "dd"',
89  ['aa', 'bb', '"cc"', '"dd"'],
90  ['aa', 'bb', 'cc', 'dd']
91  ],
92  [
93  'aa bb "cc dd"',
94  ['aa', 'bb', '"cc dd"'],
95  ['aa', 'bb', 'cc dd']
96  ],
97  [
98  '\'aa bb\' "cc dd"',
99  ['\'aa bb\'', '"cc dd"'],
100  ['aa bb', 'cc dd']
101  ],
102  [
103  '\'aa bb\' cc "dd"',
104  ['\'aa bb\'', 'cc', '"dd"'],
105  ['aa bb', 'cc', 'dd']
106  ],
107  // Now test against some real world examples
108  [
109  '/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"',
110  [
111  '/opt/local/bin/gm.exe',
112  'convert',
113  '+profile',
114  '\'*\'',
115  '-geometry',
116  '170x136!',
117  '-negate',
118  '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
119  '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
120  ],
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  'C:/opt/local/bin/gm.exe',
148  'convert',
149  '+profile',
150  '*',
151  '-geometry',
152  '170x136!',
153  '-negate',
154  'C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
155  'C:/Program Files/Apache2/htdocs/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
156  ]
157  ],
158  [
159  '/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"',
160  [
161  '/usr/bin/gm',
162  'convert',
163  '+profile',
164  '\'*\'',
165  '-geometry',
166  '170x136!',
167  '-negate',
168  '"/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
169  '"/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
170  ],
171  [
172  '/usr/bin/gm',
173  'convert',
174  '+profile',
175  '*',
176  '-geometry',
177  '170x136!',
178  '-negate',
179  '/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
180  '/Shared Items/Data/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
181  ]
182  ],
183  [
184  '/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"',
185  [
186  '/usr/bin/gm',
187  'convert',
188  '+profile',
189  '\'*\'',
190  '-geometry',
191  '170x136!',
192  '-negate',
193  '"/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
194  '"/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
195  ],
196  [
197  '/usr/bin/gm',
198  'convert',
199  '+profile',
200  '*',
201  '-geometry',
202  '170x136!',
203  '-negate',
204  '/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
205  '/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
206  ]
207  ],
208  [
209  '/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\'',
210  [
211  '/usr/bin/gm',
212  'convert',
213  '+profile',
214  '\'*\'',
215  '-geometry',
216  '170x136!',
217  '-negate',
218  '\'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\'',
219  '\'/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\''
220  ],
221  [
222  '/usr/bin/gm',
223  'convert',
224  '+profile',
225  '*',
226  '-geometry',
227  '170x136!',
228  '-negate',
229  '/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
230  '/Network/Servers/server01.internal/Projects/typo3temp/var/transient/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
231  ]
232  ]
233  ];
234  }
235 
242  public function ‪unQuoteFilenamesUnquotesFileNames($source, $expectedQuoted, $expectedUnquoted)
243  {
244  $actualQuoted = GeneralUtility::unQuoteFilenames($source);
245  $actualUnquoted = GeneralUtility::unQuoteFilenames($source, true);
246  $this->assertEquals($expectedQuoted, $actualQuoted, 'The exploded command does not match the expected');
247  $this->assertEquals($expectedUnquoted, $actualUnquoted, 'The exploded and unquoted command does not match the expected');
248  }
249 
256  {
257  return [
258  'Empty input' => [[], ''],
259  'String parameters' => [['foo' => ['one' => '√', 'two' => 2]], '&foo[one]=%E2%88%9A&foo[two]=2'],
260  'Nested array parameters' => [['foo' => [['one' => '√', 'two' => 2]]], '&foo[0][one]=%E2%88%9A&foo[0][two]=2'],
261  'Keep blank parameters' => [['foo' => ['one' => '√', '']], '&foo[one]=%E2%88%9A&foo[0]=']
262  ];
263  }
264 
270  {
271  $this->assertEquals($expected, GeneralUtility::explodeUrl2Array($input, true));
272  }
273 
275  // Tests concerning _GETset()
277 
281  {
282  $_GET = [];
283  ‪$GLOBALS['HTTP_GET_VARS'] = [];
284  $getParameters = ['foo' => 'bar'];
285  GeneralUtility::_GETset($getParameters);
286  $this->assertSame($getParameters, $_GET);
287  }
288 
293  {
294  $_GET = [];
295  ‪$GLOBALS['HTTP_GET_VARS'] = [];
296  $getParameters = ['foo' => 'bar'];
297  GeneralUtility::_GETset($getParameters);
298  $this->assertSame($getParameters, ‪$GLOBALS['HTTP_GET_VARS']);
299  }
300 
305  {
306  $_GET = [];
307  ‪$GLOBALS['HTTP_GET_VARS'] = [];
308  GeneralUtility::_GETset(['foo' => 'bar']);
309  GeneralUtility::_GETset(['oneKey' => 'oneValue']);
310  $this->assertEquals(['oneKey' => 'oneValue'], ‪$GLOBALS['HTTP_GET_VARS']);
311  }
312 
317  {
318  $_GET = [];
319  ‪$GLOBALS['HTTP_GET_VARS'] = [];
320  GeneralUtility::_GETset('oneValue', 'oneKey');
321  $this->assertEquals('oneValue', ‪$GLOBALS['HTTP_GET_VARS']['oneKey']);
322  }
323 
328  {
329  $_GET = [];
330  ‪$GLOBALS['HTTP_GET_VARS'] = [];
331  GeneralUtility::_GETset(['foo' => 'bar']);
332  GeneralUtility::_GETset('oneValue', 'oneKey');
333  $this->assertEquals(['foo' => 'bar', 'oneKey' => 'oneValue'], ‪$GLOBALS['HTTP_GET_VARS']);
334  }
335 
340  {
341  $_GET = [];
342  ‪$GLOBALS['HTTP_GET_VARS'] = [];
343  GeneralUtility::_GETset(['childKey' => 'oneValue'], 'parentKey');
344  $this->assertEquals(['parentKey' => ['childKey' => 'oneValue']], ‪$GLOBALS['HTTP_GET_VARS']);
345  }
346 
351  {
352  $_GET = [];
353  ‪$GLOBALS['HTTP_GET_VARS'] = [];
354  GeneralUtility::_GETset('oneValue', 'parentKey|childKey');
355  $this->assertEquals(['parentKey' => ['childKey' => 'oneValue']], ‪$GLOBALS['HTTP_GET_VARS']);
356  }
357 
362  {
363  $_GET = [];
364  ‪$GLOBALS['HTTP_GET_VARS'] = [];
365  GeneralUtility::_GETset(['key1' => 'value1', 'key2' => 'value2'], 'parentKey|childKey');
366  $this->assertEquals([
367  'parentKey' => [
368  'childKey' => ['key1' => 'value1', 'key2' => 'value2']
369  ]
370  ], ‪$GLOBALS['HTTP_GET_VARS']);
371  }
372 
377  public function ‪canSetNewGetInputValues($input, $key, $expected, $getPreset = [])
378  {
379  $_GET = $getPreset;
380  GeneralUtility::_GETset($input, $key);
381  $this->assertSame($expected, $_GET);
382  }
383 
389  public function ‪getSetDataProvider()
390  {
391  return [
392  'No input data used without target key' => [null, null, []],
393  'No input data used with target key' => ['', 'cake', ['cake' => '']],
394  'No target key used with string input data' => ['data', null, []],
395  'No target key used with array input data' => [['cake' => 'lie'], null, ['cake' => 'lie']],
396  'Target key and string input data' => ['lie', 'cake', ['cake' => 'lie']],
397  'Replace existing GET data' => ['lie', 'cake', ['cake' => 'lie'], ['cake' => 'is a lie']],
398  'Target key pointing to sublevels and string input data' => ['lie', 'cake|is', ['cake' => ['is' => 'lie']]],
399  'Target key pointing to sublevels and array input data' => [['a' => 'lie'], 'cake|is', ['cake' => ['is' => ['a' => 'lie']]]]
400  ];
401  }
402 }
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\unQuoteFilenamesUnquotesFileNames
‪unQuoteFilenamesUnquotesFileNames($source, $expectedQuoted, $expectedUnquoted)
Definition: GeneralUtilityTest.php:242
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest
Definition: GeneralUtilityTest.php:29
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetCanAssignsAnArrayToASpecificArrayElement
‪getSetCanAssignsAnArrayToASpecificArrayElement()
Definition: GeneralUtilityTest.php:339
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetAssignsOneValueToOneKey
‪getSetAssignsOneValueToOneKey()
Definition: GeneralUtilityTest.php:316
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\explodeUrl2ArrayDataProvider
‪array explodeUrl2ArrayDataProvider()
Definition: GeneralUtilityTest.php:255
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetWritesArrayToGlobalsHttpGetVars
‪getSetWritesArrayToGlobalsHttpGetVars()
Definition: GeneralUtilityTest.php:292
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetCanAssignAnArrayToASpecificArrayChildElement
‪getSetCanAssignAnArrayToASpecificArrayChildElement()
Definition: GeneralUtilityTest.php:361
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetCanAssignAStringValueToASpecificArrayChildElement
‪getSetCanAssignAStringValueToASpecificArrayChildElement()
Definition: GeneralUtilityTest.php:350
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\unQuoteFilenamesUnquotesFileNamesDataProvider
‪unQuoteFilenamesUnquotesFileNamesDataProvider()
Definition: GeneralUtilityTest.php:78
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\splitHeaderLinesDataProvider
‪splitHeaderLinesDataProvider()
Definition: GeneralUtilityTest.php:30
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\splitHeaderLines
‪splitHeaderLines(array $headers, array $expectedHeaders)
Definition: GeneralUtilityTest.php:56
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetForArrayDropsExistingValues
‪getSetForArrayDropsExistingValues()
Definition: GeneralUtilityTest.php:304
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\explodeUrl2ArrayTransformsParameterStringToNestedArray
‪explodeUrl2ArrayTransformsParameterStringToNestedArray($expected, $input)
Definition: GeneralUtilityTest.php:269
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetDataProvider
‪array getSetDataProvider()
Definition: GeneralUtilityTest.php:389
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility
Definition: ClientUtilityTest.php:3
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:27
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetWritesArrayToGetSystemVariable
‪getSetWritesArrayToGetSystemVariable()
Definition: GeneralUtilityTest.php:280
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\getSetForOneValueDoesNotDropUnrelatedValues
‪getSetForOneValueDoesNotDropUnrelatedValues()
Definition: GeneralUtilityTest.php:327
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Utility\GeneralUtilityTest\canSetNewGetInputValues
‪canSetNewGetInputValues($input, $key, $expected, $getPreset=[])
Definition: GeneralUtilityTest.php:377