/
GeneralUtilityTest.php
3929 lines (3661 loc) · 144 KB
/
GeneralUtilityTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Tests\Unit\Utility;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Package\Package;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\ExtendedSingletonClassFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFilesystemFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityMakeInstanceInjectLoggerFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityTestClass;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OriginalClassFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OtherReplacementClassFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\ReplacementClassFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\SingletonClassFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\TwoParametersConstructorFixture;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
final class GeneralUtilityTest extends UnitTestCase
{
public const NO_FIX_PERMISSIONS_ON_WINDOWS = 'fixPermissions() not available on Windows (method does nothing)';
protected bool $resetSingletonInstances = true;
protected bool $backupEnvironment = true;
protected ?PackageManager $backupPackageManager;
protected function setUp(): void
{
parent::setUp();
$this->backupPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
}
protected function tearDown(): void
{
GeneralUtility::flushInternalRuntimeCaches();
if ($this->backupPackageManager) {
ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backupPackageManager);
}
parent::tearDown();
}
/**
* Helper method to test for an existing internet connection.
* Some tests are skipped if there is no working uplink.
*
* @return bool $isConnected
*/
public function isConnected(): bool
{
$isConnected = false;
$connected = @fsockopen('typo3.org', 80);
if ($connected) {
$isConnected = true;
fclose($connected);
}
return $isConnected;
}
/**
* Helper method to create a random directory and return the path.
* The path will be registered for deletion upon test ending
*/
protected function getTestDirectory(string $prefix = 'root_'): string
{
$path = Environment::getVarPath() . '/tests/' . StringUtility::getUniqueId($prefix);
GeneralUtility::mkdir_deep($path);
$this->testFilesToDelete[] = $path;
return $path;
}
public static function cmpIPv4DataProviderMatching(): array
{
return [
'host with full IP address' => ['127.0.0.1', '127.0.0.1'],
'host with two wildcards at the end' => ['127.0.0.1', '127.0.*.*'],
'host with wildcard at third octet' => ['127.0.0.1', '127.0.*.1'],
'host with wildcard at second octet' => ['127.0.0.1', '127.*.0.1'],
'/8 subnet' => ['127.0.0.1', '127.1.1.1/8'],
'/32 subnet (match only name)' => ['127.0.0.1', '127.0.0.1/32'],
'/30 subnet' => ['10.10.3.1', '10.10.3.3/30'],
'host with wildcard in list with IPv4/IPv6 addresses' => ['192.168.1.1', '127.0.0.1, 1234:5678::/126, 192.168.*'],
'host in list with IPv4/IPv6 addresses' => ['192.168.1.1', '::1, 1234:5678::/126, 192.168.1.1'],
];
}
/**
* @test
* @dataProvider cmpIPv4DataProviderMatching
*/
public function cmpIPv4ReturnsTrueForMatchingAddress($ip, $list): void
{
self::assertTrue(GeneralUtility::cmpIPv4($ip, $list));
}
/**
* Data provider for cmpIPv4ReturnsFalseForNotMatchingAddress
*
* @return array Data sets
*/
public static function cmpIPv4DataProviderNotMatching(): array
{
return [
'single host' => ['127.0.0.1', '127.0.0.2'],
'single host with wildcard' => ['127.0.0.1', '127.*.1.1'],
'single host with /32 subnet mask' => ['127.0.0.1', '127.0.0.2/32'],
'/31 subnet' => ['127.0.0.1', '127.0.0.2/31'],
'list with IPv4/IPv6 addresses' => ['127.0.0.1', '10.0.2.3, 192.168.1.1, ::1'],
'list with only IPv6 addresses' => ['10.20.30.40', '::1, 1234:5678::/127'],
];
}
/**
* @test
* @dataProvider cmpIPv4DataProviderNotMatching
*/
public function cmpIPv4ReturnsFalseForNotMatchingAddress($ip, $list): void
{
self::assertFalse(GeneralUtility::cmpIPv4($ip, $list));
}
///////////////////////////
// Tests concerning cmpIPv6
///////////////////////////
/**
* Data provider for cmpIPv6ReturnsTrueForMatchingAddress
*
* @return array Data sets
*/
public static function cmpIPv6DataProviderMatching(): array
{
return [
'empty address' => ['::', '::'],
'empty with netmask in list' => ['::', '::/0'],
'empty with netmask 0 and host-bits set in list' => ['::', '::123/0'],
'localhost' => ['::1', '::1'],
'localhost with leading zero blocks' => ['::1', '0:0::1'],
'host with submask /128' => ['::1', '0:0::1/128'],
'/16 subnet' => ['1234::1', '1234:5678::/16'],
'/126 subnet' => ['1234:5678::3', '1234:5678::/126'],
'/126 subnet with host-bits in list set' => ['1234:5678::3', '1234:5678::2/126'],
'list with IPv4/IPv6 addresses' => ['1234:5678::3', '::1, 127.0.0.1, 1234:5678::/126, 192.168.1.1'],
];
}
/**
* @test
* @dataProvider cmpIPv6DataProviderMatching
*/
public function cmpIPv6ReturnsTrueForMatchingAddress($ip, $list): void
{
self::assertTrue(GeneralUtility::cmpIPv6($ip, $list));
}
/**
* Data provider for cmpIPv6ReturnsFalseForNotMatchingAddress
*
* @return array Data sets
*/
public static function cmpIPv6DataProviderNotMatching(): array
{
return [
'empty against localhost' => ['::', '::1'],
'empty against localhost with /128 netmask' => ['::', '::1/128'],
'localhost against different host' => ['::1', '::2'],
'localhost against host with prior bits set' => ['::1', '::1:1'],
'host against different /17 subnet' => ['1234::1', '1234:f678::/17'],
'host against different /127 subnet' => ['1234:5678::3', '1234:5678::/127'],
'host against IPv4 address list' => ['1234:5678::3', '127.0.0.1, 192.168.1.1'],
'host against mixed list with IPv6 host in different subnet' => ['1234:5678::3', '::1, 1234:5678::/127'],
];
}
/**
* @test
* @dataProvider cmpIPv6DataProviderNotMatching
*/
public function cmpIPv6ReturnsFalseForNotMatchingAddress($ip, $list): void
{
self::assertFalse(GeneralUtility::cmpIPv6($ip, $list));
}
/////////////////////////////////
// Tests concerning normalizeIPv6
/////////////////////////////////
/**
* Data provider for normalizeIPv6ReturnsCorrectlyNormalizedFormat
*
* @return array Data sets
*/
public static function normalizeCompressIPv6DataProviderCorrect(): array
{
return [
'empty' => ['::', '0000:0000:0000:0000:0000:0000:0000:0000'],
'localhost' => ['::1', '0000:0000:0000:0000:0000:0000:0000:0001'],
'expansion in middle 1' => ['1::2', '0001:0000:0000:0000:0000:0000:0000:0002'],
'expansion in middle 2' => ['1:2::3', '0001:0002:0000:0000:0000:0000:0000:0003'],
'expansion in middle 3' => ['1::2:3', '0001:0000:0000:0000:0000:0000:0002:0003'],
'expansion in middle 4' => ['1:2::3:4:5', '0001:0002:0000:0000:0000:0003:0004:0005'],
];
}
/**
* @test
* @dataProvider normalizeCompressIPv6DataProviderCorrect
*/
public function normalizeIPv6CorrectlyNormalizesAddresses($compressed, $normalized): void
{
self::assertEquals($normalized, GeneralUtility::normalizeIPv6($compressed));
}
///////////////////////////////
// Tests concerning validIP
///////////////////////////////
/**
* Data provider for checkValidIpReturnsTrueForValidIp
*
* @return array Data sets
*/
public static function validIpDataProvider(): array
{
return [
'0.0.0.0' => ['0.0.0.0'],
'private IPv4 class C' => ['192.168.0.1'],
'private IPv4 class A' => ['10.0.13.1'],
'private IPv6' => ['fe80::daa2:5eff:fe8b:7dfb'],
];
}
/**
* @test
* @dataProvider validIpDataProvider
*/
public function validIpReturnsTrueForValidIp($ip): void
{
self::assertTrue(GeneralUtility::validIP($ip));
}
/**
* Data provider for checkValidIpReturnsFalseForInvalidIp
*
* @return array Data sets
*/
public static function invalidIpDataProvider(): array
{
return [
'null' => [null],
'zero' => [0],
'string' => ['test'],
'string empty' => [''],
'string NULL' => ['NULL'],
'out of bounds IPv4' => ['300.300.300.300'],
'dotted decimal notation with only two dots' => ['127.0.1'],
];
}
/**
* @test
* @dataProvider invalidIpDataProvider
*/
public function validIpReturnsFalseForInvalidIp($ip): void
{
self::assertFalse(GeneralUtility::validIP($ip));
}
///////////////////////////////
// Tests concerning cmpFQDN
///////////////////////////////
/**
* Data provider for cmpFqdnReturnsTrue
*
* @return array Data sets
*/
public static function cmpFqdnValidDataProvider(): array
{
return [
'localhost should usually resolve, IPv4' => ['127.0.0.1', '*'],
'localhost should usually resolve, IPv6' => ['::1', '*'],
// other testcases with resolving not possible since it would
// require a working IPv4/IPv6-connectivity
'aaa.bbb.ccc.ddd.eee, full' => ['aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.ddd.eee'],
'aaa.bbb.ccc.ddd.eee, wildcard first' => ['aaa.bbb.ccc.ddd.eee', '*.ccc.ddd.eee'],
'aaa.bbb.ccc.ddd.eee, wildcard last' => ['aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.*'],
'aaa.bbb.ccc.ddd.eee, wildcard middle' => ['aaa.bbb.ccc.ddd.eee', 'aaa.*.eee'],
'list-matches, 1' => ['aaa.bbb.ccc.ddd.eee', 'xxx, yyy, zzz, aaa.*.eee'],
'list-matches, 2' => ['aaa.bbb.ccc.ddd.eee', '127:0:0:1,,aaa.*.eee,::1'],
];
}
/**
* @test
* @dataProvider cmpFqdnValidDataProvider
*/
public function cmpFqdnReturnsTrue($baseHost, $list): void
{
self::assertTrue(GeneralUtility::cmpFQDN($baseHost, $list));
}
/**
* Data provider for cmpFqdnReturnsFalse
*
* @return array Data sets
*/
public static function cmpFqdnInvalidDataProvider(): array
{
return [
'num-parts of hostname to check can only be less or equal than hostname, 1' => ['aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.ddd.eee.fff'],
'num-parts of hostname to check can only be less or equal than hostname, 2' => ['aaa.bbb.ccc.ddd.eee', 'aaa.*.bbb.ccc.ddd.eee'],
];
}
/**
* @test
* @dataProvider cmpFqdnInvalidDataProvider
*/
public function cmpFqdnReturnsFalse($baseHost, $list): void
{
self::assertFalse(GeneralUtility::cmpFQDN($baseHost, $list));
}
///////////////////////////////
// Tests concerning inList
///////////////////////////////
/**
* @test
* @dataProvider inListForItemContainedReturnsTrueDataProvider
*/
public function inListForItemContainedReturnsTrue(string $haystack): void
{
self::assertTrue(GeneralUtility::inList($haystack, 'findme'));
}
/**
* Data provider for inListForItemContainedReturnsTrue.
*/
public static function inListForItemContainedReturnsTrueDataProvider(): array
{
return [
'Element as second element of four items' => ['one,findme,three,four'],
'Element at beginning of list' => ['findme,one,two'],
'Element at end of list' => ['one,two,findme'],
'One item list' => ['findme'],
];
}
/**
* @test
* @dataProvider inListForItemNotContainedReturnsFalseDataProvider
*/
public function inListForItemNotContainedReturnsFalse(string $haystack): void
{
self::assertFalse(GeneralUtility::inList($haystack, 'findme'));
}
/**
* Data provider for inListForItemNotContainedReturnsFalse.
*/
public static function inListForItemNotContainedReturnsFalseDataProvider(): array
{
return [
'Four item list' => ['one,two,three,four'],
'One item list' => ['one'],
'Empty list' => [''],
];
}
///////////////////////////////
// Tests concerning expandList
///////////////////////////////
/**
* @test
* @dataProvider expandListExpandsIntegerRangesDataProvider
*/
public function expandListExpandsIntegerRanges(string $list, string $expectation): void
{
self::assertSame($expectation, GeneralUtility::expandList($list));
}
/**
* Data provider for expandListExpandsIntegerRangesDataProvider
*/
public static function expandListExpandsIntegerRangesDataProvider(): array
{
return [
'Expand for the same number' => ['1,2-2,7', '1,2,7'],
'Small range expand with parameters reversed ignores reversed items' => ['1,5-3,7', '1,7'],
'Small range expand' => ['1,3-5,7', '1,3,4,5,7'],
'Expand at beginning' => ['3-5,1,7', '3,4,5,1,7'],
'Expand at end' => ['1,7,3-5', '1,7,3,4,5'],
'Multiple small range expands' => ['1,3-5,7-10,12', '1,3,4,5,7,8,9,10,12'],
'One item list' => ['1-5', '1,2,3,4,5'],
'Nothing to expand' => ['1,2,3,4', '1,2,3,4'],
'Empty list' => ['', ''],
];
}
/**
* @test
*/
public function expandListExpandsForTwoThousandElementsExpandsOnlyToThousandElementsMaximum(): void
{
$list = GeneralUtility::expandList('1-2000');
self::assertCount(1000, explode(',', $list));
}
///////////////////////////////
// Tests concerning formatSize
///////////////////////////////
/**
* @test
* @dataProvider formatSizeDataProvider
*/
public function formatSizeTranslatesBytesToHigherOrderRepresentation($size, $labels, $base, $expected): void
{
self::assertEquals($expected, GeneralUtility::formatSize($size, $labels, $base));
}
/**
* Data provider for formatSizeTranslatesBytesToHigherOrderRepresentation
*/
public static function formatSizeDataProvider(): array
{
return [
'IEC Bytes stay bytes (min)' => [1, '', 0, '1 '],
'IEC Bytes stay bytes (max)' => [921, '', 0, '921 '],
'IEC Kilobytes are used (min)' => [922, '', 0, '0.90 Ki'],
'IEC Kilobytes are used (max)' => [943718, '', 0, '922 Ki'],
'IEC Megabytes are used (min)' => [943719, '', 0, '0.90 Mi'],
'IEC Megabytes are used (max)' => [966367641, '', 0, '922 Mi'],
'IEC Gigabytes are used (min)' => [966367642, '', 0, '0.90 Gi'],
'IEC Gigabytes are used (max)' => [989560464998, '', 0, '922 Gi'],
'IEC Decimal is omitted for large kilobytes' => [31080, '', 0, '30 Ki'],
'IEC Decimal is omitted for large megabytes' => [31458000, '', 0, '30 Mi'],
'IEC Decimal is omitted for large gigabytes' => [32212254720, '', 0, '30 Gi'],
'SI Bytes stay bytes (min)' => [1, 'si', 0, '1 '],
'SI Bytes stay bytes (max)' => [899, 'si', 0, '899 '],
'SI Kilobytes are used (min)' => [901, 'si', 0, '0.90 k'],
'SI Kilobytes are used (max)' => [900000, 'si', 0, '900 k'],
'SI Megabytes are used (min)' => [900001, 'si', 0, '0.90 M'],
'SI Megabytes are used (max)' => [900000000, 'si', 0, '900 M'],
'SI Gigabytes are used (min)' => [900000001, 'si', 0, '0.90 G'],
'SI Gigabytes are used (max)' => [900000000000, 'si', 0, '900 G'],
'SI Decimal is omitted for large kilobytes' => [30000, 'si', 0, '30 k'],
'SI Decimal is omitted for large megabytes' => [30000000, 'si', 0, '30 M'],
'SI Decimal is omitted for large gigabytes' => [30000000000, 'si', 0, '30 G'],
'Label for bytes can be exchanged (binary unit)' => [1, ' Foo|||', 0, '1 Foo'],
'Label for kilobytes can be exchanged (binary unit)' => [1024, '| Foo||', 0, '1.00 Foo'],
'Label for megabytes can be exchanged (binary unit)' => [1048576, '|| Foo|', 0, '1.00 Foo'],
'Label for gigabytes can be exchanged (binary unit)' => [1073741824, '||| Foo', 0, '1.00 Foo'],
'Label for bytes can be exchanged (decimal unit)' => [1, ' Foo|||', 1000, '1 Foo'],
'Label for kilobytes can be exchanged (decimal unit)' => [1000, '| Foo||', 1000, '1.00 Foo'],
'Label for megabytes can be exchanged (decimal unit)' => [1000000, '|| Foo|', 1000, '1.00 Foo'],
'Label for gigabytes can be exchanged (decimal unit)' => [1000000000, '||| Foo', 1000, '1.00 Foo'],
'IEC Base is ignored' => [1024, 'iec', 1000, '1.00 Ki'],
'SI Base is ignored' => [1000, 'si', 1024, '1.00 k'],
'Use binary base for unexpected base' => [2048, '| Bar||', 512, '2.00 Bar'],
];
}
///////////////////////////////
// Tests concerning splitCalc
///////////////////////////////
/**
* Data provider for splitCalc
*
* @return array expected values, arithmetic expression
*/
public static function splitCalcDataProvider(): array
{
return [
'empty string returns empty array' => [
[],
'',
],
'number without operator returns array with plus and number' => [
[['+', '42']],
'42',
],
'two numbers with asterisk return first number with plus and second number with asterisk' => [
[['+', '42'], ['*', '31']],
'42 * 31',
],
];
}
/**
* @test
* @dataProvider splitCalcDataProvider
*/
public function splitCalcCorrectlySplitsExpression(array $expected, string $expression): void
{
self::assertSame($expected, GeneralUtility::splitCalc($expression, '+-*/'));
}
///////////////////////////////
// Tests concerning htmlspecialchars_decode
///////////////////////////////
/**
* @test
*/
public function htmlspecialcharsDecodeReturnsDecodedString(): void
{
$string = '<typo3 version="6.0"> </typo3>';
$encoded = htmlspecialchars($string);
$decoded = htmlspecialchars_decode($encoded);
self::assertEquals($string, $decoded);
}
//////////////////////////////////
// Tests concerning validEmail
//////////////////////////////////
/**
* Data provider for valid validEmail's
*
* @return array Valid email addresses
*/
public static function validEmailValidDataProvider(): array
{
return [
'short mail address' => ['a@b.c'],
'simple mail address' => ['test@example.com'],
'uppercase characters' => ['QWERTYUIOPASDFGHJKLZXCVBNM@QWERTYUIOPASDFGHJKLZXCVBNM.NET'],
'equal sign in local part' => ['test=mail@example.com'],
'dash in local part' => ['test-mail@example.com'],
'plus in local part' => ['test+mail@example.com'],
'question mark in local part' => ['test?mail@example.com'],
'slash in local part' => ['foo/bar@example.com'],
'hash in local part' => ['foo#bar@example.com'],
'dot in local part' => ['firstname.lastname@employee.2something.com'],
'dash as local part' => ['-@foo.com'],
'umlauts in domain part' => ['foo@äöüfoo.com'],
'number as top level domain' => ['foo@bar.123'],
'top level domain only' => ['test@localhost'],
'umlauts in local part' => ['äöüfoo@bar.com'],
'quoted @ char' => ['"Abc@def"@example.com'],
];
}
/**
* @test
* @dataProvider validEmailValidDataProvider
*/
public function validEmailReturnsTrueForValidMailAddress($address): void
{
self::assertTrue(GeneralUtility::validEmail($address));
}
/**
* Data provider for invalid validEmail's
*
* @return array Invalid email addresses
*/
public static function validEmailInvalidDataProvider(): array
{
return [
'empty string' => [''],
'empty array' => [[]],
'integer' => [42],
'float' => [42.23],
'array' => [['foo']],
'object' => [new \stdClass()],
'@ sign only' => ['@'],
'string longer than 320 characters' => [str_repeat('0123456789', 33)],
'duplicate @' => ['test@@example.com'],
'duplicate @ combined with further special characters in local part' => ['test!.!@#$%^&*@example.com'],
'opening parenthesis in local part' => ['foo(bar@example.com'],
'closing parenthesis in local part' => ['foo)bar@example.com'],
'opening square bracket in local part' => ['foo[bar@example.com'],
'closing square bracket as local part' => [']@example.com'],
'dash as second level domain' => ['foo@-.com'],
'domain part starting with dash' => ['foo@-foo.com'],
'domain part ending with dash' => ['foo@foo-.com'],
'dot at beginning of domain part' => ['test@.com'],
'local part ends with dot' => ['e.x.a.m.p.l.e.@example.com'],
'trailing whitespace' => ['test@example.com '],
'trailing carriage return' => ['test@example.com' . CR],
'trailing linefeed' => ['test@example.com' . LF],
'trailing carriage return linefeed' => ['test@example.com' . CRLF],
'trailing tab' => ['test@example.com' . "\t"],
'prohibited input characters' => ['“mailto:test@example.com”'],
'escaped @ char' => ['abc\@def@example.com'], // known bug, see https://github.com/egulias/EmailValidator/issues/181
];
}
/**
* @test
* @dataProvider validEmailInvalidDataProvider
*/
public function validEmailReturnsFalseForInvalidMailAddress($address): void
{
self::assertFalse(GeneralUtility::validEmail($address));
}
//////////////////////////////////
// Tests concerning intExplode
//////////////////////////////////
public static function intExplodeDataProvider(): array
{
return [
'convertStringToInteger' => [
'1,foo,2',
false,
[1, 0, 2],
],
'zerosAreKept' => [
'0,1, 0, 2,0',
false,
[0, 1, 0, 2, 0],
],
'emptyValuesAreKept' => [
'0,1,, 0, 2,,0',
false,
[0, 1, 0, 0, 2, 0, 0],
],
'emptyValuesAreRemoved' => [
'0,1,, 0, 2,,0',
true,
[0=>0, 1=>1, 3=>0, 4=>2, 6=>0], // note does not renumber keys!
],
];
}
/**
* @test
* @dataProvider intExplodeDataProvider
*/
public function intExplodeReturnsExplodedArray(string $input, bool $removeEmpty, array $expected): void
{
self::assertSame($expected, GeneralUtility::intExplode(',', $input, $removeEmpty));
}
//////////////////////////////////
// Tests concerning implodeArrayForUrl / explodeUrl2Array
//////////////////////////////////
/**
* Data provider for implodeArrayForUrlBuildsValidParameterString
*/
public static function implodeArrayForUrlDataProvider(): array
{
$valueArray = ['one' => '√', 'two' => 2];
return [
'Empty input' => ['foo', [], ''],
'String parameters' => ['foo', $valueArray, '&foo[one]=%E2%88%9A&foo[two]=2'],
'Nested array parameters' => ['foo', [$valueArray], '&foo[0][one]=%E2%88%9A&foo[0][two]=2'],
'Keep blank parameters' => ['foo', ['one' => '√', ''], '&foo[one]=%E2%88%9A&foo[0]='],
];
}
/**
* @test
* @dataProvider implodeArrayForUrlDataProvider
*/
public function implodeArrayForUrlBuildsValidParameterString($name, $input, $expected): void
{
self::assertSame($expected, GeneralUtility::implodeArrayForUrl($name, $input));
}
/**
* @test
*/
public function implodeArrayForUrlCanSkipEmptyParameters(): void
{
$input = ['one' => '√', ''];
$expected = '&foo[one]=%E2%88%9A';
self::assertSame($expected, GeneralUtility::implodeArrayForUrl('foo', $input, '', true));
}
/**
* @test
*/
public function implodeArrayForUrlCanUrlEncodeKeyNames(): void
{
$input = ['one' => '√', ''];
$expected = '&foo%5Bone%5D=%E2%88%9A&foo%5B0%5D=';
self::assertSame($expected, GeneralUtility::implodeArrayForUrl('foo', $input, '', false, true));
}
public static function explodeUrl2ArrayTransformsParameterStringToFlatArrayDataProvider(): array
{
return [
'Empty string' => ['', []],
'Simple parameter string' => ['&one=%E2%88%9A&two=2', ['one' => '√', 'two' => 2]],
'Nested parameter string' => ['&foo[one]=%E2%88%9A&two=2', ['foo[one]' => '√', 'two' => 2]],
'Parameter without value' => ['&one=&two=2', ['one' => '', 'two' => 2]],
'Nested parameter without value' => ['&foo[one]=&two=2', ['foo[one]' => '', 'two' => 2]],
'Parameter without equals sign' => ['&one&two=2', ['one' => '', 'two' => 2]],
'Nested parameter without equals sign' => ['&foo[one]&two=2', ['foo[one]' => '', 'two' => 2]],
];
}
/**
* @test
* @dataProvider explodeUrl2ArrayTransformsParameterStringToFlatArrayDataProvider
*/
public function explodeUrl2ArrayTransformsParameterStringToFlatArray(string $input, array $expected): void
{
self::assertEquals($expected, GeneralUtility::explodeUrl2Array($input));
}
public static function revExplodeDataProvider(): array
{
return [
'limit 0 should return unexploded string' => [
':',
'my:words:here',
0,
['my:words:here'],
],
'limit 1 should return unexploded string' => [
':',
'my:words:here',
1,
['my:words:here'],
],
'limit 2 should return two pieces' => [
':',
'my:words:here',
2,
['my:words', 'here'],
],
'limit 3 should return unexploded string' => [
':',
'my:words:here',
3,
['my', 'words', 'here'],
],
'limit 0 should return unexploded string if no delimiter is contained' => [
':',
'mywordshere',
0,
['mywordshere'],
],
'limit 1 should return unexploded string if no delimiter is contained' => [
':',
'mywordshere',
1,
['mywordshere'],
],
'limit 2 should return unexploded string if no delimiter is contained' => [
':',
'mywordshere',
2,
['mywordshere'],
],
'limit 3 should return unexploded string if no delimiter is contained' => [
':',
'mywordshere',
3,
['mywordshere'],
],
'multi character delimiter is handled properly with limit 2' => [
'[]',
'a[b][c][d]',
2,
['a[b][c', 'd]'],
],
'multi character delimiter is handled properly with limit 3' => [
'[]',
'a[b][c][d]',
3,
['a[b', 'c', 'd]'],
],
];
}
/**
* @test
* @dataProvider revExplodeDataProvider
*/
public function revExplodeCorrectlyExplodesStringForGivenPartsCount($delimiter, $testString, $count, $expectedArray): void
{
$actualArray = GeneralUtility::revExplode($delimiter, $testString, $count);
self::assertEquals($expectedArray, $actualArray);
}
/**
* @test
*/
public function revExplodeRespectsLimitThreeWhenExploding(): void
{
$testString = 'even:more:of:my:words:here';
$expectedArray = ['even:more:of:my', 'words', 'here'];
$actualArray = GeneralUtility::revExplode(':', $testString, 3);
self::assertEquals($expectedArray, $actualArray);
}
//////////////////////////////////
// Tests concerning trimExplode
//////////////////////////////////
/**
* @test
* @dataProvider trimExplodeReturnsCorrectResultDataProvider
*/
public function trimExplodeReturnsCorrectResult(string $delimiter, string $testString, bool $removeEmpty, int $limit, array $expectedResult): void
{
self::assertSame($expectedResult, GeneralUtility::trimExplode($delimiter, $testString, $removeEmpty, $limit));
}
public static function trimExplodeReturnsCorrectResultDataProvider(): array
{
return [
'spaces at element start and end' => [
',',
' a , b , c ,d ,, e,f,',
false,
0,
['a', 'b', 'c', 'd', '', 'e', 'f', ''],
],
'removes newline' => [
',',
' a , b , ' . LF . ' ,d ,, e,f,',
true,
0,
['a', 'b', 'd', 'e', 'f'],
],
'removes empty elements' => [
',',
'a , b , c , ,d ,, ,e,f,',
true,
0,
['a', 'b', 'c', 'd', 'e', 'f'],
],
'keeps remaining results with empty items after reaching limit with positive parameter' => [
',',
' a , b , c , , d,, ,e ',
false,
3,
['a', 'b', 'c , , d,, ,e'],
],
'keeps remaining results without empty items after reaching limit with positive parameter' => [
',',
' a , b , c , , d,, ,e ',
true,
3,
['a', 'b', 'c , d,e'],
],
'keeps remaining results with empty items after reaching limit with negative parameter' => [
',',
' a , b , c , d, ,e, f , , ',
false,
-3,
['a', 'b', 'c', 'd', '', 'e'],
],
'keeps remaining results without empty items after reaching limit with negative parameter' => [
',',
' a , b , c , d, ,e, f , , ',
true,
-3,
['a', 'b', 'c'],
],
'returns exact results without reaching limit with positive parameter' => [
',',
' a , b , , c , , , ',
true,
4,
['a', 'b', 'c'],
],
'keeps zero as string' => [
',',
'a , b , c , ,d ,, ,e,f, 0 ,',
true,
0,
['a', 'b', 'c', 'd', 'e', 'f', '0'],
],
'keeps whitespace inside elements' => [
',',
'a , b , c , ,d ,, ,e,f, g h ,',
true,
0,
['a', 'b', 'c', 'd', 'e', 'f', 'g h'],
],
'can use internal regex delimiter as explode delimiter' => [
'/',
'a / b / c / /d // /e/f/ g h /',
true,
0,
['a', 'b', 'c', 'd', 'e', 'f', 'g h'],
],
'can use whitespaces as delimiter' => [
' ',
'* * * * *',
true,
0,
['*', '*', '*', '*', '*'],
],
'can use words as delimiter' => [
'All',
'HelloAllTogether',
true,
0,
['Hello', 'Together'],
],
'can use word with appended and prepended spaces as delimiter' => [
' all ',
'Hello all together',
true,
0,
['Hello', 'together'],
],
'can use word with appended and prepended spaces as delimiter and do not remove empty' => [
' all ',
'Hello all together all there all all are all none',
false,
0,
['Hello', 'together', 'there', '', 'are', 'none'],
],
'can use word with appended and prepended spaces as delimiter, do not remove empty and limit' => [
' all ',
'Hello all together all there all all are all none',
false,
5,
['Hello', 'together', 'there', '', 'are all none'],
],
'can use word with appended and prepended spaces as delimiter, do not remove empty, limit and multiple delimiter in last' => [
' all ',
'Hello all together all there all all are all none',
false,
4,
['Hello', 'together', 'there', 'all are all none'],
],
'can use word with appended and prepended spaces as delimiter, remove empty and limit' => [
' all ',
'Hello all together all there all all are all none',
true,
4,
['Hello', 'together', 'there', 'are all none'],
],
'can use word with appended and prepended spaces as delimiter, remove empty and limit and multiple delimiter in last' => [
' all ',
'Hello all together all there all all are all none',
true,
5,
['Hello', 'together', 'there', 'are' , 'none'],
],
'can use words as delimiter and do not remove empty' => [
'all there',
'Helloall theretogether all there all there are all there none',
false,
0,
['Hello', 'together', '', 'are', 'none'],
],
'can use words as delimiter, do not remove empty and limit' => [
'all there',
'Helloall theretogether all there all there are all there none',
false,
4,
['Hello', 'together', '', 'are all there none'],
],
'can use words as delimiter, do not remove empty, limit and multiple delimiter in last' => [
'all there',
'Helloall theretogether all there all there are all there none',
false,
3,
['Hello', 'together', 'all there are all there none'],
],
'can use words as delimiter, remove empty' => [
'all there',
'Helloall theretogether all there all there are all there none',
true,
0,
['Hello', 'together', 'are', 'none'],
],
'can use words as delimiter, remove empty and limit' => [
'all there',
'Helloall theretogether all there all there are all there none',
true,
3,
['Hello', 'together', 'are all there none'],
],
'can use words as delimiter, remove empty and limit and multiple delimiter in last' => [
'all there',
'Helloall theretogether all there all there are all there none',