‪TYPO3CMS  11.5
StringUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
26 class ‪StringUtilityTest extends UnitTestCase
27 {
31  public static function stringCastableValuesDataProvider(): \Generator
32  {
33  ‪yield 'empty string' => [''];
34  ‪yield 'string' => ['value'];
35  ‪yield 'int' => [1];
36  ‪yield 'float' => [1.2345];
37  ‪yield 'bool' => [true];
38  }
39 
47  {
48  $expected = (string)$value;
49 
50  self::assertSame($expected, ‪StringUtility::cast($value, 'default'));
51  }
52 
56  public static function nonStringCastableValuesDataProvider(): \Generator
57  {
58  ‪yield 'array' => [['1']];
59  ‪yield 'null' => [null];
60  ‪yield 'object' => [new \stdClass()];
61  ‪yield 'closure' => [static fn(): string => 'fn'];
62  // PHP interprets it as `lim(x→0) log(x) = -∞`
63  ‪yield 'infinite' => [log(0)];
64  // acos only supports values in range [-1; +1]
65  ‪yield 'NaN' => [acos(2)];
66  }
67 
74  public function ‪castWithWithNonStringCastableReturnsDefault($value): void
75  {
76  $default = 'default';
77 
78  self::assertSame($default, ‪StringUtility::cast($value, $default));
79  }
80 
88  {
89  self::assertNull(‪StringUtility::cast($value));
90  }
91 
95  public static function nonStringValueToFilterDataProvider(): \Generator
96  {
97  ‪yield 'int' => [1];
98  ‪yield 'float' => [1.2345];
99  ‪yield 'bool' => [true];
100  ‪yield 'array' => [['1']];
101  ‪yield 'null' => [null];
102  ‪yield 'object' => [new \stdClass()];
103  ‪yield 'closure' => [static fn(): string => 'fn'];
104  // PHP interprets it as `lim(x→0) log(x) = -∞`
105  ‪yield 'infinite' => [log(0)];
106  // acos only supports values in range [-1; +1]
107  ‪yield 'NaN' => [acos(2)];
108  }
109 
117  {
118  $default = 'default';
119 
120  self::assertSame($default, ‪StringUtility::filter($value, $default));
121  }
122 
130  {
131  self::assertNull(‪StringUtility::filter($value));
132  }
133 
137  public static function stringValueToFilterDataProvider(): \Generator
138  {
139  ‪yield 'empty string' => [''];
140  ‪yield 'non-empty string' => ['value'];
141  }
146  public function ‪filterForStringValuesReturnsProvidedValue(string $value): void
147  {
148  self::assertSame($value, ‪StringUtility::filter($value, 'some default'));
149  }
150 
154  public function ‪getUniqueIdReturnsIdWithPrefix(): void
155  {
156  $id = ‪StringUtility::getUniqueId('NEW');
157  self::assertEquals('NEW', substr($id, 0, 3));
158  }
159 
163  public function ‪getUniqueIdReturnsIdWithoutDot(): void
164  {
165  self::assertStringNotContainsString('.', ‪StringUtility::getUniqueId());
166  }
167 
174  public function ‪escapeCssSelector(string $selector, string $expectedValue): void
175  {
176  self::assertEquals($expectedValue, ‪StringUtility::escapeCssSelector($selector));
177  }
178 
182  public function ‪escapeCssSelectorDataProvider(): array
183  {
184  return [
185  ['data.field', 'data\\.field'],
186  ['#theId', '\\#theId'],
187  ['.theId:hover', '\\.theId\\:hover'],
188  ['.theId:hover', '\\.theId\\:hover'],
189  ['input[name=foo]', 'input\\[name\\=foo\\]'],
190  ];
191  }
192 
199  public function ‪removeByteOrderMark(string $input, string $expectedValue): void
200  {
201  // assertContains is necessary as one test contains non-string characters
202  self::assertSame($expectedValue, ‪StringUtility::removeByteOrderMark(hex2bin($input)));
203  }
204 
208  public function ‪removeByteOrderMarkDataProvider(): array
209  {
210  return [
211  'BOM gets removed' => [
212  'efbbbf424f4d2061742074686520626567696e6e696e6720676574732072656d6f766564',
213  'BOM at the beginning gets removed',
214  ],
215  'No BOM available' => [
216  '4e6f20424f4d20617661696c61626c65',
217  'No BOM available',
218  ],
219  ];
220  }
221 
226  public function ‪searchStringWildcard(string $haystack, string $needle, bool $result): void
227  {
228  self::assertSame($result, ‪StringUtility::searchStringWildcard($haystack, $needle));
229  }
230 
234  public function ‪searchStringWildcardDataProvider(): array
235  {
236  return [
237  'Simple wildcard single character with *' => [
238  'TYPO3',
239  'TY*O3',
240  true,
241  ],
242  'Simple wildcard multiple character with *' => [
243  'TYPO3',
244  'T*P*3',
245  true,
246  ],
247  'Simple wildcard multiple character for one placeholder with *' => [
248  'TYPO3',
249  'T*3',
250  true,
251  ],
252  'Simple wildcard single character with ?' => [
253  'TYPO3',
254  'TY?O3',
255  true,
256  ],
257  'Simple wildcard multiple character with ?' => [
258  'TYPO3',
259  'T?P?3',
260  true,
261  ],
262  'Simple wildcard multiple character for one placeholder with ?' => [
263  'TYPO3',
264  'T?3',
265  false,
266  ],
267  'RegExp' => [
268  'TYPO3',
269  '/^TYPO(\d)$/',
270  true,
271  ],
272  ];
273  }
274 
281  {
282  ‪yield 'List without duplicates' => ['one,two,three', 'one,two,three'];
283  ‪yield 'List with two consecutive duplicates' => ['one,two,two,three,three', 'one,two,three'];
284  ‪yield 'List with non-consecutive duplicates' => ['one,two,three,two,three', 'one,two,three'];
285  ‪yield 'One item list' => ['one', 'one'];
286  ‪yield 'Empty list' => ['', ''];
287  ‪yield 'No list, just a comma' => [',', ''];
288  ‪yield 'List with leading comma' => [',one,two', 'one,two'];
289  ‪yield 'List with trailing comma' => ['one,two,', 'one,two'];
290  ‪yield 'List with multiple consecutive commas' => ['one,,two', 'one,two'];
291  }
292 
301  public function ‪uniqueListUnifiesCommaSeparatedList(string $initialList, string $unifiedList): void
302  {
303  self::assertSame($unifiedList, ‪StringUtility::uniqueList($initialList));
304  }
305 
312  {
313  ‪yield 'Pad right to 10 with string with uneven length' => ['ABC', 10, ' ', STR_PAD_RIGHT];
314  ‪yield 'Pad left to 10 with string with uneven length' => ['ABC', 10, ' ', STR_PAD_LEFT];
315  ‪yield 'Pad both to 10 with string with uneven length' => ['ABC', 10, ' ', STR_PAD_BOTH];
316  ‪yield 'Pad right to 10 with string with uneven length and 2 character padding' => ['ABC', 10, '12', STR_PAD_RIGHT];
317  ‪yield 'Pad left to 10 with string with uneven length and 2 character padding' => ['ABC', 10, '12', STR_PAD_LEFT];
318  ‪yield 'Pad both to 10 with string with uneven length and 2 character padding' => ['ABC', 10, '12', STR_PAD_BOTH];
319 
320  ‪yield 'Pad right to 10 with string with even length' => ['AB', 10, ' ', STR_PAD_RIGHT];
321  ‪yield 'Pad left to 10 with string with even length' => ['AB', 10, ' ', STR_PAD_LEFT];
322  ‪yield 'Pad both to 10 with string with even length' => ['AB', 10, ' ', STR_PAD_BOTH];
323  ‪yield 'Pad right to 10 with string with even length and 2 character padding' => ['AB', 10, '12', STR_PAD_RIGHT];
324  ‪yield 'Pad left to 10 with string with even length and 2 character padding' => ['AB', 10, '12', STR_PAD_LEFT];
325  ‪yield 'Pad both to 10 with string with even length and 2 character padding' => ['AB', 10, '12', STR_PAD_BOTH];
326  }
327 
341  public function ‪multibyteStringPadReturnsSameValueAsStrPadForAsciiStrings(string $string, int $length, string $pad_string, int $pad_type): void
342  {
343  self::assertEquals(
344  str_pad($string, $length, $pad_string, $pad_type),
345  ‪StringUtility::multibyteStringPad($string, $length, $pad_string, $pad_type)
346  );
347  }
348 
350  {
351  ‪yield 'Pad right to 8 with string with uneven length' => ['häh ', 'häh', 8, ' ', STR_PAD_RIGHT];
352  ‪yield 'Pad left to 8 with string with uneven length' => [' häh', 'häh', 8, ' ', STR_PAD_LEFT];
353  ‪yield 'Pad both to 8 with string with uneven length' => [' häh ', 'häh', 8, ' ', STR_PAD_BOTH];
354  ‪yield 'Pad right to 8 with string with uneven length and 2 character padding' => ['hühäöäöä', 'hüh', 8, 'äö', STR_PAD_RIGHT];
355  ‪yield 'Pad left to 8 with string with uneven length and 2 character padding' => ['äöäöähüh', 'hüh', 8, 'äö', STR_PAD_LEFT];
356  ‪yield 'Pad both to 8 with string with uneven length and 2 character padding' => ['äöhühäöä', 'hüh', 8, 'äö', STR_PAD_BOTH];
357 
358  ‪yield 'Pad right to 8 with string with even length' => ['hä ', 'hä', 8, ' ', STR_PAD_RIGHT];
359  ‪yield 'Pad left to 8 with string with even length' => [' hä', 'hä', 8, ' ', STR_PAD_LEFT];
360  ‪yield 'Pad both to 8 with string with even length' => [' hä ', 'hä', 8, ' ', STR_PAD_BOTH];
361  ‪yield 'Pad right to 8 with string with even length and 2 character padding with MB char' => ['hüäöäöäö', 'hü', 8, 'äö', STR_PAD_RIGHT];
362  ‪yield 'Pad left to 8 with string with even length and 2 character padding with MB char' => ['äöäöäöhü', 'hü', 8, 'äö', STR_PAD_LEFT];
363  ‪yield 'Pad both to 8 with string with even length and 2 character padding with MB char' => ['äöähüäöä', 'hü', 8, 'äö', STR_PAD_BOTH];
364  }
365 
377  public function ‪multibyteStringPadReturnsCorrectResultsMultibyte(string $expectedResult, string $string, int $length, string $pad_string, int $pad_type): void
378  {
379  self::assertEquals(
380  $expectedResult,
381  ‪StringUtility::multibyteStringPad($string, $length, $pad_string, $pad_type)
382  );
383  }
384 
385  public static function ‪base64urlRoundTripWorksDataProvider(): \Generator
386  {
387  ‪yield ['a'];
388  ‪yield ['aa'];
389  ‪yield ['aaa'];
390  ‪yield ['aaaa'];
391  ‪yield [random_bytes(31)];
392  ‪yield [random_bytes(32)];
393  ‪yield [random_bytes(33)];
394  }
395 
400  public function ‪base64urlRoundTripWorks(string $rawValue): void
401  {
402  $encoded = ‪StringUtility::base64urlEncode($rawValue);
403  $decoded = ‪StringUtility::base64urlDecode($encoded);
404  self::assertSame($rawValue, $decoded);
405  }
406 
407  public static function ‪base64urlDataProvider(): \Generator
408  {
409  ‪yield ['', ''];
410  ‪yield ['a', 'YQ'];
411  ‪yield ['aa', 'YWE'];
412  ‪yield ['aa>', 'YWE-'];
413  ‪yield ['aa?', 'YWE_'];
414  ‪yield ['aaa', 'YWFh'];
415  ‪yield ['aaaa', 'YWFhYQ'];
416  }
417 
422  public function ‪base64urlEncodeWorks(string $rawValue, string $encodedValue): void
423  {
424  self::assertSame($encodedValue, ‪StringUtility::base64urlEncode($rawValue));
425  }
426 
431  public function ‪base64urlDecodeWorks(string $rawValue, string $encodedValue): void
432  {
433  self::assertSame($rawValue, ‪StringUtility::base64urlDecode($encodedValue));
434  }
435 }
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard($haystack, $needle)
Definition: StringUtility.php:170
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\base64urlRoundTripWorks
‪base64urlRoundTripWorks(string $rawValue)
Definition: StringUtilityTest.php:400
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\castWithWithNonStringCastableAndNoDefaultProvidedReturnsNull
‪castWithWithNonStringCastableAndNoDefaultProvidedReturnsNull($value)
Definition: StringUtilityTest.php:87
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlEncode
‪static string base64urlEncode(string $value)
Definition: StringUtility.php:258
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\escapeCssSelector
‪escapeCssSelector(string $selector, string $expectedValue)
Definition: StringUtilityTest.php:174
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\searchStringWildcard
‪searchStringWildcard(string $haystack, string $needle, bool $result)
Definition: StringUtilityTest.php:226
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\yield
‪yield
Definition: StringUtilityTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\filterForNonStringValueAndNoDefaultProvidedReturnsNull
‪filterForNonStringValueAndNoDefaultProvidedReturnsNull($value)
Definition: StringUtilityTest.php:129
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlDecode
‪static string base64urlDecode(string $value)
Definition: StringUtility.php:276
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\filterForNonStringValueAndDefaultProvidedReturnsDefault
‪filterForNonStringValueAndDefaultProvidedReturnsDefault($value)
Definition: StringUtilityTest.php:116
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\castWithWithNonStringCastableReturnsDefault
‪castWithWithNonStringCastableReturnsDefault($value)
Definition: StringUtilityTest.php:74
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\uniqueListUnifiesCommaSeparatedListDataProvider
‪Generator uniqueListUnifiesCommaSeparatedListDataProvider()
Definition: StringUtilityTest.php:280
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\getUniqueIdReturnsIdWithPrefix
‪getUniqueIdReturnsIdWithPrefix()
Definition: StringUtilityTest.php:154
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Utility\StringUtility\uniqueList
‪static string uniqueList(string $list)
Definition: StringUtility.php:197
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\removeByteOrderMark
‪removeByteOrderMark(string $input, string $expectedValue)
Definition: StringUtilityTest.php:199
‪TYPO3\CMS\Core\Utility\StringUtility\escapeCssSelector
‪static string escapeCssSelector(string $selector)
Definition: StringUtility.php:143
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\base64urlRoundTripWorksDataProvider
‪static base64urlRoundTripWorksDataProvider()
Definition: StringUtilityTest.php:385
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\filterForStringValuesReturnsProvidedValue
‪filterForStringValuesReturnsProvidedValue(string $value)
Definition: StringUtilityTest.php:146
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest
Definition: StringUtilityTest.php:27
‪TYPO3\CMS\Core\Utility\StringUtility\removeByteOrderMark
‪static string removeByteOrderMark(string $input)
Definition: StringUtility.php:154
‪TYPO3\CMS\Core\Utility\StringUtility\filter
‪static filter($value, ?string $default=null)
Definition: StringUtility.php:50
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\escapeCssSelectorDataProvider
‪array escapeCssSelectorDataProvider()
Definition: StringUtilityTest.php:182
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsCorrectResultsMultibyteDataProvider
‪multibyteStringPadReturnsCorrectResultsMultibyteDataProvider()
Definition: StringUtilityTest.php:349
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\castWithStringCastableReturnsValueCastToString
‪castWithStringCastableReturnsValueCastToString($value)
Definition: StringUtilityTest.php:46
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsSameValueAsStrPadForAsciiStringsDataProvider
‪Generator multibyteStringPadReturnsSameValueAsStrPadForAsciiStringsDataProvider()
Definition: StringUtilityTest.php:311
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsCorrectResultsMultibyte
‪multibyteStringPadReturnsCorrectResultsMultibyte(string $expectedResult, string $string, int $length, string $pad_string, int $pad_type)
Definition: StringUtilityTest.php:377
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\searchStringWildcardDataProvider
‪array searchStringWildcardDataProvider()
Definition: StringUtilityTest.php:234
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\base64urlEncodeWorks
‪base64urlEncodeWorks(string $rawValue, string $encodedValue)
Definition: StringUtilityTest.php:422
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\getUniqueIdReturnsIdWithoutDot
‪getUniqueIdReturnsIdWithoutDot()
Definition: StringUtilityTest.php:163
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\removeByteOrderMarkDataProvider
‪array removeByteOrderMarkDataProvider()
Definition: StringUtilityTest.php:208
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\multibyteStringPadReturnsSameValueAsStrPadForAsciiStrings
‪multibyteStringPadReturnsSameValueAsStrPadForAsciiStrings(string $string, int $length, string $pad_string, int $pad_type)
Definition: StringUtilityTest.php:341
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\uniqueListUnifiesCommaSeparatedList
‪uniqueListUnifiesCommaSeparatedList(string $initialList, string $unifiedList)
Definition: StringUtilityTest.php:301
‪TYPO3\CMS\Core\Utility\StringUtility\cast
‪static cast($value, ?string $default=null)
Definition: StringUtility.php:30
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\base64urlDecodeWorks
‪base64urlDecodeWorks(string $rawValue, string $encodedValue)
Definition: StringUtilityTest.php:431
‪TYPO3\CMS\Core\Utility\StringUtility\multibyteStringPad
‪static string multibyteStringPad(string $string, int $length, string $pad_string=' ', int $pad_type=STR_PAD_RIGHT, string $encoding='UTF-8')
Definition: StringUtility.php:213
‪TYPO3\CMS\Core\Tests\Unit\Utility\StringUtilityTest\base64urlDataProvider
‪static base64urlDataProvider()
Definition: StringUtilityTest.php:407