-
Notifications
You must be signed in to change notification settings - Fork 638
/
GeneralUtilityTest.php
4823 lines (4503 loc) · 174 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
namespace TYPO3\CMS\Core\Tests\Unit\Utility;
/*
* 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!
*/
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Package\Package;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFilesystemFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFixture;
use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityMakeInstanceInjectLoggerFixture;
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\TwoParametersConstructorFixture;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\FileStreamWrapper;
/**
* Testcase for class \TYPO3\CMS\Core\Utility\GeneralUtility
*/
class GeneralUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
{
const NO_FIX_PERMISSIONS_ON_WINDOWS = 'fixPermissions() not available on Windows (method does nothing)';
/**
* Subject is not notice free, disable E_NOTICES
*/
protected static $suppressNotices = true;
/**
* @var array A backup of registered singleton instances
*/
protected $singletonInstances = [];
/**
* @var \TYPO3\CMS\Core\Package\PackageManager
*/
protected $backupPackageManager;
/**
* Set up
*/
protected function setUp()
{
GeneralUtilityFixture::flushInternalRuntimeCaches();
GeneralUtilityFixture::$isAllowedHostHeaderValueCallCount = 0;
GeneralUtilityFixture::setAllowHostHeaderValue(false);
$GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = GeneralUtility::ENV_TRUSTED_HOSTS_PATTERN_ALLOW_ALL;
$this->singletonInstances = GeneralUtility::getSingletonInstances();
$this->backupPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
}
/**
* Tear down
*/
protected function tearDown()
{
GeneralUtility::resetSingletonInstances($this->singletonInstances);
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()
{
$isConnected = false;
$connected = @fsockopen('typo3.org', 80);
if ($connected) {
$isConnected = true;
fclose($connected);
}
return $isConnected;
}
/**
* Helper method to create a random directory in the virtual file system
* and return the path.
*
* @param string $prefix
* @return string
*/
protected function getVirtualTestDir($prefix = 'root_')
{
$root = vfsStream::setup();
$path = $root->url() . '/typo3temp/var/tests/' . $this->getUniqueId($prefix);
GeneralUtility::mkdir_deep($path);
return $path;
}
///////////////////////////
// Tests concerning _GP
///////////////////////////
/**
* @test
* @dataProvider gpDataProvider
*/
public function canRetrieveValueWithGP($key, $get, $post, $expected)
{
$_GET = $get;
$_POST = $post;
$this->assertSame($expected, GeneralUtility::_GP($key));
}
/**
* Data provider for canRetrieveValueWithGP.
* All test values also check whether slashes are stripped properly.
*
* @return array
*/
public function gpDataProvider()
{
return [
'No key parameter' => [null, [], [], null],
'Key not found' => ['cake', [], [], null],
'Value only in GET' => ['cake', ['cake' => 'li\\e'], [], 'li\\e'],
'Value only in POST' => ['cake', [], ['cake' => 'l\\ie'], 'l\\ie'],
'Value from POST preferred over GET' => ['cake', ['cake' => 'is a'], ['cake' => '\\lie'], '\\lie'],
'Value can be an array' => [
'cake',
['cake' => ['is a' => 'l\\ie']],
[],
['is a' => 'l\\ie']
]
];
}
///////////////////////////
// Tests concerning _GPmerged
///////////////////////////
/**
* @test
* @dataProvider gpMergedDataProvider
*/
public function gpMergedWillMergeArraysFromGetAndPost($get, $post, $expected)
{
$_POST = $post;
$_GET = $get;
$this->assertEquals($expected, GeneralUtility::_GPmerged('cake'));
}
/**
* Data provider for gpMergedWillMergeArraysFromGetAndPost
*
* @return array
*/
public function gpMergedDataProvider()
{
$fullDataArray = ['cake' => ['a' => 'is a', 'b' => 'lie']];
$postPartData = ['cake' => ['b' => 'lie']];
$getPartData = ['cake' => ['a' => 'is a']];
$getPartDataModified = ['cake' => ['a' => 'is not a']];
return [
'Key doesn\' exist' => [['foo'], ['bar'], []],
'No POST data' => [$fullDataArray, [], $fullDataArray['cake']],
'No GET data' => [[], $fullDataArray, $fullDataArray['cake']],
'POST and GET are merged' => [$getPartData, $postPartData, $fullDataArray['cake']],
'POST is preferred over GET' => [$getPartDataModified, $fullDataArray, $fullDataArray['cake']]
];
}
///////////////////////////////
// Tests concerning _GET / _POST
///////////////////////////////
/**
* Data provider for canRetrieveGlobalInputsThroughGet
* and canRetrieveGlobalInputsThroughPost
*
* @return array
*/
public function getAndPostDataProvider()
{
return [
'Requested input data doesn\'t exist' => ['cake', [], null],
'No key will return entire input data' => [null, ['cake' => 'l\\ie'], ['cake' => 'l\\ie']],
'Can retrieve specific input' => ['cake', ['cake' => 'l\\ie', 'foo'], 'l\\ie'],
'Can retrieve nested input data' => ['cake', ['cake' => ['is a' => 'l\\ie']], ['is a' => 'l\\ie']]
];
}
/**
* @test
* @dataProvider getAndPostDataProvider
*/
public function canRetrieveGlobalInputsThroughGet($key, $get, $expected)
{
$_GET = $get;
$this->assertSame($expected, GeneralUtility::_GET($key));
}
/**
* @test
* @dataProvider getAndPostDataProvider
*/
public function canRetrieveGlobalInputsThroughPost($key, $post, $expected)
{
$_POST = $post;
$this->assertSame($expected, GeneralUtility::_POST($key));
}
///////////////////////////////
// Tests concerning _GETset
///////////////////////////////
/**
* @test
* @dataProvider getSetDataProvider
*/
public function canSetNewGetInputValues($input, $key, $expected, $getPreset = [])
{
$_GET = $getPreset;
GeneralUtility::_GETset($input, $key);
$this->assertSame($expected, $_GET);
}
/**
* Data provider for canSetNewGetInputValues
*
* @return array
*/
public function getSetDataProvider()
{
return [
'No input data used without target key' => [null, null, []],
'No input data used with target key' => ['', 'cake', ['cake' => '']],
'No target key used with string input data' => ['data', null, []],
'No target key used with array input data' => [['cake' => 'lie'], null, ['cake' => 'lie']],
'Target key and string input data' => ['lie', 'cake', ['cake' => 'lie']],
'Replace existing GET data' => ['lie', 'cake', ['cake' => 'lie'], ['cake' => 'is a lie']],
'Target key pointing to sublevels and string input data' => ['lie', 'cake|is', ['cake' => ['is' => 'lie']]],
'Target key pointing to sublevels and array input data' => [['a' => 'lie'], 'cake|is', ['cake' => ['is' => ['a' => 'lie']]]]
];
}
///////////////////////////
// Tests concerning cmpIPv4
///////////////////////////
/**
* Data provider for cmpIPv4ReturnsTrueForMatchingAddress
*
* @return array Data sets
*/
public static function cmpIPv4DataProviderMatching()
{
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)
{
$this->assertTrue(GeneralUtility::cmpIPv4($ip, $list));
}
/**
* Data provider for cmpIPv4ReturnsFalseForNotMatchingAddress
*
* @return array Data sets
*/
public static function cmpIPv4DataProviderNotMatching()
{
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)
{
$this->assertFalse(GeneralUtility::cmpIPv4($ip, $list));
}
///////////////////////////
// Tests concerning cmpIPv6
///////////////////////////
/**
* Data provider for cmpIPv6ReturnsTrueForMatchingAddress
*
* @return array Data sets
*/
public static function cmpIPv6DataProviderMatching()
{
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)
{
$this->assertTrue(GeneralUtility::cmpIPv6($ip, $list));
}
/**
* Data provider for cmpIPv6ReturnsFalseForNotMatchingAddress
*
* @return array Data sets
*/
public static function cmpIPv6DataProviderNotMatching()
{
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)
{
$this->assertFalse(GeneralUtility::cmpIPv6($ip, $list));
}
///////////////////////////////
// Tests concerning IPv6Hex2Bin
///////////////////////////////
/**
* Data provider for IPv6Hex2BinCorrect
*
* @return array Data sets
*/
public static function IPv6Hex2BinDataProviderCorrect()
{
return [
'empty 1' => ['::', str_pad('', 16, "\x00")],
'empty 2, already normalized' => ['0000:0000:0000:0000:0000:0000:0000:0000', str_pad('', 16, "\x00")],
'already normalized' => ['0102:0304:0000:0000:0000:0000:0506:0078', "\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78"],
'expansion in middle 1' => ['1::2', "\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02"],
'expansion in middle 2' => ['beef::fefa', "\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa"],
];
}
/**
* @test
* @dataProvider IPv6Hex2BinDataProviderCorrect
*/
public function IPv6Hex2BinCorrectlyConvertsAddresses($hex, $binary)
{
$this->assertTrue(GeneralUtility::IPv6Hex2Bin($hex) === $binary);
}
///////////////////////////////
// Tests concerning IPv6Bin2Hex
///////////////////////////////
/**
* Data provider for IPv6Bin2HexCorrect
*
* @return array Data sets
*/
public static function IPv6Bin2HexDataProviderCorrect()
{
return [
'empty' => [str_pad('', 16, "\x00"), '::'],
'non-empty front' => ["\x01" . str_pad('', 15, "\x00"), '100::'],
'non-empty back' => [str_pad('', 15, "\x00") . "\x01", '::1'],
'normalized' => ["\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78", '102:304::506:78'],
'expansion in middle 1' => ["\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02", '1::2'],
'expansion in middle 2' => ["\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa", 'beef::fefa'],
];
}
/**
* @test
* @dataProvider IPv6Bin2HexDataProviderCorrect
*/
public function IPv6Bin2HexCorrectlyConvertsAddresses($binary, $hex)
{
$this->assertEquals(GeneralUtility::IPv6Bin2Hex($binary), $hex);
}
////////////////////////////////////////////////
// Tests concerning normalizeIPv6 / compressIPv6
////////////////////////////////////////////////
/**
* Data provider for normalizeIPv6ReturnsCorrectlyNormalizedFormat
*
* @return array Data sets
*/
public static function normalizeCompressIPv6DataProviderCorrect()
{
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)
{
$this->assertEquals($normalized, GeneralUtility::normalizeIPv6($compressed));
}
/**
* @test
* @dataProvider normalizeCompressIPv6DataProviderCorrect
*/
public function compressIPv6CorrectlyCompressesAdresses($compressed, $normalized)
{
$this->assertEquals($compressed, GeneralUtility::compressIPv6($normalized));
}
/**
* @test
*/
public function compressIPv6CorrectlyCompressesAdressWithSomeAddressOnRightSide()
{
if (strtolower(PHP_OS) === 'darwin') {
$this->markTestSkipped('This test does not work on OSX / Darwin OS.');
}
$this->assertEquals('::f0f', GeneralUtility::compressIPv6('0000:0000:0000:0000:0000:0000:0000:0f0f'));
}
///////////////////////////////
// Tests concerning validIP
///////////////////////////////
/**
* Data provider for checkValidIpReturnsTrueForValidIp
*
* @return array Data sets
*/
public static function validIpDataProvider()
{
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)
{
$this->assertTrue(GeneralUtility::validIP($ip));
}
/**
* Data provider for checkValidIpReturnsFalseForInvalidIp
*
* @return array Data sets
*/
public static function invalidIpDataProvider()
{
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)
{
$this->assertFalse(GeneralUtility::validIP($ip));
}
///////////////////////////////
// Tests concerning cmpFQDN
///////////////////////////////
/**
* Data provider for cmpFqdnReturnsTrue
*
* @return array Data sets
*/
public static function cmpFqdnValidDataProvider()
{
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)
{
$this->assertTrue(GeneralUtility::cmpFQDN($baseHost, $list));
}
/**
* Data provider for cmpFqdnReturnsFalse
*
* @return array Data sets
*/
public static function cmpFqdnInvalidDataProvider()
{
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)
{
$this->assertFalse(GeneralUtility::cmpFQDN($baseHost, $list));
}
///////////////////////////////
// Tests concerning inList
///////////////////////////////
/**
* @test
* @param string $haystack
* @dataProvider inListForItemContainedReturnsTrueDataProvider
*/
public function inListForItemContainedReturnsTrue($haystack)
{
$this->assertTrue(GeneralUtility::inList($haystack, 'findme'));
}
/**
* Data provider for inListForItemContainedReturnsTrue.
*
* @return array
*/
public function inListForItemContainedReturnsTrueDataProvider()
{
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
* @param string $haystack
* @dataProvider inListForItemNotContainedReturnsFalseDataProvider
*/
public function inListForItemNotContainedReturnsFalse($haystack)
{
$this->assertFalse(GeneralUtility::inList($haystack, 'findme'));
}
/**
* Data provider for inListForItemNotContainedReturnsFalse.
*
* @return array
*/
public function inListForItemNotContainedReturnsFalseDataProvider()
{
return [
'Four item list' => ['one,two,three,four'],
'One item list' => ['one'],
'Empty list' => ['']
];
}
///////////////////////////////
// Tests concerning rmFromList
///////////////////////////////
/**
* @test
* @param string $initialList
* @param string $listWithElementRemoved
* @dataProvider rmFromListRemovesElementsFromCommaSeparatedListDataProvider
*/
public function rmFromListRemovesElementsFromCommaSeparatedList($initialList, $listWithElementRemoved)
{
$this->assertSame($listWithElementRemoved, GeneralUtility::rmFromList('removeme', $initialList));
}
/**
* Data provider for rmFromListRemovesElementsFromCommaSeparatedList
*
* @return array
*/
public function rmFromListRemovesElementsFromCommaSeparatedListDataProvider()
{
return [
'Element as second element of three' => ['one,removeme,two', 'one,two'],
'Element at beginning of list' => ['removeme,one,two', 'one,two'],
'Element at end of list' => ['one,two,removeme', 'one,two'],
'One item list' => ['removeme', ''],
'Element not contained in list' => ['one,two,three', 'one,two,three'],
'Empty element survives' => ['one,,three,,removeme', 'one,,three,'],
'Empty element survives at start' => [',removeme,three,removeme', ',three'],
'Empty element survives at end' => ['removeme,three,removeme,', 'three,'],
'Empty list' => ['', ''],
'List contains removeme multiple times' => ['removeme,notme,removeme,removeme', 'notme'],
'List contains removeme multiple times nothing else' => ['removeme,removeme,removeme', ''],
'List contains removeme multiple times nothing else 2x' => ['removeme,removeme', ''],
'List contains removeme multiple times nothing else 3x' => ['removeme,removeme,removeme', ''],
'List contains removeme multiple times nothing else 4x' => ['removeme,removeme,removeme,removeme', ''],
'List contains removeme multiple times nothing else 5x' => ['removeme,removeme,removeme,removeme,removeme', ''],
];
}
///////////////////////////////
// Tests concerning expandList
///////////////////////////////
/**
* @test
* @param string $list
* @param string $expectation
* @dataProvider expandListExpandsIntegerRangesDataProvider
*/
public function expandListExpandsIntegerRanges($list, $expectation)
{
$this->assertSame($expectation, GeneralUtility::expandList($list));
}
/**
* Data provider for expandListExpandsIntegerRangesDataProvider
*
* @return array
*/
public function expandListExpandsIntegerRangesDataProvider()
{
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()
{
$list = GeneralUtility::expandList('1-2000');
$this->assertSame(1000, count(explode(',', $list)));
}
///////////////////////////////
// Tests concerning uniqueList
///////////////////////////////
/**
* @test
* @param string $initialList
* @param string $unifiedList
* @dataProvider uniqueListUnifiesCommaSeparatedListDataProvider
*/
public function uniqueListUnifiesCommaSeparatedList($initialList, $unifiedList)
{
$this->assertSame($unifiedList, GeneralUtility::uniqueList($initialList));
}
/**
* Data provider for uniqueListUnifiesCommaSeparatedList
*
* @return array
*/
public function uniqueListUnifiesCommaSeparatedListDataProvider()
{
return [
'List without duplicates' => ['one,two,three', 'one,two,three'],
'List with two consecutive duplicates' => ['one,two,two,three,three', 'one,two,three'],
'List with non-consecutive duplicates' => ['one,two,three,two,three', 'one,two,three'],
'One item list' => ['one', 'one'],
'Empty list' => ['', '']
];
}
///////////////////////////////
// Tests concerning isFirstPartOfStr
///////////////////////////////
/**
* Data provider for isFirstPartOfStrReturnsTrueForMatchingFirstParts
*
* @return array
*/
public function isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider()
{
return [
'match first part of string' => ['hello world', 'hello'],
'match whole string' => ['hello', 'hello'],
'integer is part of string with same number' => ['24', 24],
'string is part of integer with same number' => [24, '24'],
'integer is part of string starting with same number' => ['24 beer please', 24]
];
}
/**
* @test
* @dataProvider isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider
*/
public function isFirstPartOfStrReturnsTrueForMatchingFirstPart($string, $part)
{
$this->assertTrue(GeneralUtility::isFirstPartOfStr($string, $part));
}
/**
* Data provider for checkIsFirstPartOfStrReturnsFalseForNotMatchingFirstParts
*
* @return array
*/
public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider()
{
return [
'no string match' => ['hello', 'bye'],
'no case sensitive string match' => ['hello world', 'Hello'],
'array is not part of string' => ['string', []],
'string is not part of array' => [[], 'string'],
'NULL is not part of string' => ['string', null],
'string is not part of NULL' => [null, 'string'],
'NULL is not part of array' => [[], null],
'array is not part of NULL' => [null, []],
'empty string is not part of empty string' => ['', ''],
'NULL is not part of empty string' => ['', null],
'false is not part of empty string' => ['', false],
'empty string is not part of NULL' => [null, ''],
'empty string is not part of false' => [false, ''],
'empty string is not part of zero integer' => [0, ''],
'zero integer is not part of NULL' => [null, 0],
'zero integer is not part of empty string' => ['', 0]
];
}
/**
* @test
* @dataProvider isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider
*/
public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPart($string, $part)
{
$this->assertFalse(GeneralUtility::isFirstPartOfStr($string, $part));
}
///////////////////////////////
// Tests concerning formatSize
///////////////////////////////
/**
* @test
* @dataProvider formatSizeDataProvider
*/
public function formatSizeTranslatesBytesToHigherOrderRepresentation($size, $labels, $base, $expected)
{
$this->assertEquals($expected, GeneralUtility::formatSize($size, $labels, $base));
}
/**
* Data provider for formatSizeTranslatesBytesToHigherOrderRepresentation
*
* @return array
*/
public function formatSizeDataProvider()
{
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 megabyes 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 megabyes 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 function splitCalcDataProvider()
{
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($expected, $expression)
{
$this->assertEquals($expected, GeneralUtility::splitCalc($expression, '+-*/'));
}
///////////////////////////////
// Tests concerning htmlspecialchars_decode
///////////////////////////////
/**
* @test
*/
public function htmlspecialcharsDecodeReturnsDecodedString()
{
$string = '<typo3 version="6.0"> </typo3>';
$encoded = htmlspecialchars($string);
$decoded = htmlspecialchars_decode($encoded);
$this->assertEquals($string, $decoded);
}
//////////////////////////////////
// Tests concerning validEmail
//////////////////////////////////
/**
* Data provider for valid validEmail's
*
* @return array Valid email addresses
*/
public function validEmailValidDataProvider()
{
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']
];
}
/**
* @test
* @dataProvider validEmailValidDataProvider
*/
public function validEmailReturnsTrueForValidMailAddress($address)
{
$this->assertTrue(GeneralUtility::validEmail($address));
}
/**
* Data provider for invalid validEmail's
*
* @return array Invalid email addresses
*/
public function validEmailInvalidDataProvider()
{
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'],
'top level domain only' => ['test@com'],
'dash as second level domain' => ['foo@-.com'],
'domain part starting with dash' => ['foo@-foo.com'],
'domain part ending with dash' => ['foo@foo-.com'],
'number as top level domain' => ['foo@bar.123'],
'dot at beginning of domain part' => ['test@.com'],
'local part ends with dot' => ['e.x.a.m.p.l.e.@example.com'],
'umlauts in local part' => ['äöüfoo@bar.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' . TAB],
'prohibited input characters' => ['“mailto:test@example.com”'],
];
}