Passed
Push — master ( d02918...9920b8 )
by Maurício
08:07
created

ConfigTest::testCheckServersWithInvalidServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Tests;
6
7
use PhpMyAdmin\Config;
8
use PhpMyAdmin\DatabaseInterface;
9
10
use function array_merge;
11
use function array_replace_recursive;
12
use function constant;
13
use function define;
14
use function defined;
15
use function file_exists;
16
use function file_put_contents;
17
use function filemtime;
18
use function fileperms;
19
use function function_exists;
20
use function gd_info;
21
use function mb_strstr;
22
use function ob_end_clean;
23
use function ob_get_contents;
24
use function ob_start;
25
use function phpinfo;
26
use function preg_match;
27
use function realpath;
28
use function strip_tags;
29
use function stristr;
30
use function sys_get_temp_dir;
31
use function tempnam;
32
use function unlink;
33
34
use const DIRECTORY_SEPARATOR;
35
use const INFO_MODULES;
36
use const PHP_EOL;
37
use const PHP_OS;
38
39
/**
40
 * @covers \PhpMyAdmin\Config
41
 */
42
class ConfigTest extends AbstractTestCase
43
{
44
    /** @var Config */
45
    protected $object;
46
47
    /** @var Config to test file permission */
48
    protected $permTestObj;
49
50
    /**
51
     * Sets up the fixture, for example, opens a network connection.
52
     * This method is called before a test is executed.
53
     */
54
    protected function setUp(): void
55
    {
56
        parent::setUp();
57
        parent::setTheme();
58
        $_SERVER['HTTP_USER_AGENT'] = '';
59
        $this->object = new Config();
60
        $GLOBALS['server'] = 0;
61
        $_SESSION['git_location'] = '.git';
62
        $_SESSION['is_git_revision'] = true;
63
        $GLOBALS['config'] = new Config(CONFIG_FILE);
64
        $GLOBALS['cfg']['ProxyUrl'] = '';
65
66
        //for testing file permissions
67
        $this->permTestObj = new Config(ROOT_PATH . 'config.sample.inc.php');
68
    }
69
70
    /**
71
     * Tears down the fixture, for example, closes a network connection.
72
     * This method is called after a test is executed.
73
     */
74
    protected function tearDown(): void
75
    {
76
        parent::tearDown();
77
        unset($this->object);
78
        unset($this->permTestObj);
79
    }
80
81
    /**
82
     * Test for load
83
     */
84
    public function testLoadConfigs(): void
85
    {
86
        $defaultConfig = new Config();
87
        $tmpConfig = tempnam('./', 'config.test.inc.php');
88
        if ($tmpConfig === false) {
89
            $this->markTestSkipped('Creating a temporary file does not work');
90
        }
91
92
        $this->assertFileExists($tmpConfig);
93
94
        // end of setup
95
96
        // Test loading an empty file does not change the default config
97
        $config = new Config($tmpConfig);
98
        $this->assertSame($defaultConfig->settings, $config->settings);
99
100
        $contents = '<?php' . PHP_EOL
101
                    . '$cfg[\'ProtectBinary\'] = true;';
102
        file_put_contents($tmpConfig, $contents);
103
104
        // Test loading a config changes the setup
105
        $config = new Config($tmpConfig);
106
        $defaultConfig->settings['ProtectBinary'] = true;
107
        $this->assertSame($defaultConfig->settings, $config->settings);
108
        $defaultConfig->settings['ProtectBinary'] = 'blob';
109
110
        // Teardown
111
        unlink($tmpConfig);
112
        $this->assertFalse(file_exists($tmpConfig));
113
    }
114
115
    /**
116
     * Test for load
117
     */
118
    public function testLoadInvalidConfigs(): void
119
    {
120
        $defaultConfig = new Config();
121
        $tmpConfig = tempnam('./', 'config.test.inc.php');
122
        if ($tmpConfig === false) {
123
            $this->markTestSkipped('Creating a temporary file does not work');
124
        }
125
126
        $this->assertFileExists($tmpConfig);
127
128
        // end of setup
129
130
        // Test loading an empty file does not change the default config
131
        $config = new Config($tmpConfig);
132
        $this->assertSame($defaultConfig->settings, $config->settings);
133
134
        $contents = '<?php' . PHP_EOL
135
                    . '$cfg[\'fooBar\'] = true;';
136
        file_put_contents($tmpConfig, $contents);
137
138
        // Test loading a custom key config changes the setup
139
        $config = new Config($tmpConfig);
140
        $defaultConfig->settings['fooBar'] = true;
141
        // Equals because of the key sorting
142
        $this->assertEquals($defaultConfig->settings, $config->settings);
143
        unset($defaultConfig->settings['fooBar']);
144
145
        $contents = '<?php' . PHP_EOL
146
                    . '$cfg[\'/InValidKey\'] = true;' . PHP_EOL
147
                    . '$cfg[\'In/ValidKey\'] = true;' . PHP_EOL
148
                    . '$cfg[\'/InValid/Key\'] = true;' . PHP_EOL
149
                    . '$cfg[\'In/Valid/Key\'] = true;' . PHP_EOL
150
                    . '$cfg[\'ValidKey\'] = true;';
151
        file_put_contents($tmpConfig, $contents);
152
153
        // Test loading a custom key config changes the setup
154
        $config = new Config($tmpConfig);
155
        $defaultConfig->settings['ValidKey'] = true;
156
        // Equals because of the key sorting
157
        $this->assertEquals($defaultConfig->settings, $config->settings);
158
        unset($defaultConfig->settings['ValidKey']);
159
160
        // Teardown
161
        unlink($tmpConfig);
162
        $this->assertFalse(file_exists($tmpConfig));
163
    }
164
165
    /**
166
     * Test for CheckSystem
167
     *
168
     * @group medium
169
     */
170
    public function testCheckSystem(): void
171
    {
172
        $this->object->checkSystem();
173
174
        $this->assertIsBool($this->object->get('PMA_IS_WINDOWS'));
175
    }
176
177
    /**
178
     * Test for checkOutputCompression
179
     */
180
    public function testCheckOutputCompression(): void
181
    {
182
        $this->object->set('OBGzip', 'auto');
183
184
        $this->object->set('PMA_USR_BROWSER_AGENT', 'IE');
185
        $this->object->set('PMA_USR_BROWSER_VER', 6);
186
        $this->object->checkOutputCompression();
187
        $this->assertTrue($this->object->get('OBGzip'));
188
189
        $this->object->set('OBGzip', 'auto');
190
        $this->object->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
191
        $this->object->set('PMA_USR_BROWSER_VER', 5);
192
        $this->object->checkOutputCompression();
193
        $this->assertTrue($this->object->get('OBGzip'));
194
    }
195
196
    /**
197
     * Tests client parsing code.
198
     *
199
     * @param string $agent   User agent string
200
     * @param string $os      Expected parsed OS (or null if none)
201
     * @param string $browser Expected parsed browser (or null if none)
202
     * @param string $version Expected browser version (or null if none)
203
     *
204
     * @dataProvider userAgentProvider
205
     */
206
    public function testCheckClient(string $agent, string $os, ?string $browser = null, ?string $version = null): void
207
    {
208
        $_SERVER['HTTP_USER_AGENT'] = $agent;
209
        $this->object->checkClient();
210
        $this->assertEquals($os, $this->object->get('PMA_USR_OS'));
211
        if ($os != null) {
212
            $this->assertEquals(
213
                $browser,
214
                $this->object->get('PMA_USR_BROWSER_AGENT')
215
            );
216
        }
217
218
        if ($version == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $version of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
219
            return;
220
        }
221
222
        $this->assertEquals(
223
            $version,
224
            $this->object->get('PMA_USR_BROWSER_VER')
225
        );
226
    }
227
228
    /**
229
     * user Agent Provider
230
     *
231
     * @return array
232
     */
233
    public function userAgentProvider(): array
234
    {
235
        return [
236
            [
237
                'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00',
238
                'Linux',
239
                'OPERA',
240
                '9.80',
241
            ],
242
            [
243
                'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US) AppleWebKit/'
244
                . '528.16 OmniWeb/622.8.0.112941',
245
                'Mac',
246
                'OMNIWEB',
247
                '622',
248
            ],
249
            [
250
                'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)',
251
                'Win',
252
                'IE',
253
                '8.0',
254
            ],
255
            [
256
                'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
257
                'Win',
258
                'IE',
259
                '9.0',
260
            ],
261
            [
262
                'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; '
263
                . 'Trident/6.0)',
264
                'Win',
265
                'IE',
266
                '10.0',
267
            ],
268
            [
269
                'Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; '
270
                . '.NET4.0C; rv:11.0) like Gecko',
271
                'Win',
272
                'IE',
273
                '11.0',
274
            ],
275
            [
276
                'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; '
277
                . '.NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; '
278
                . '.NET CLR 3.0.30729; InfoPath.3; rv:11.0) like Gecko',
279
                'Win',
280
                'IE',
281
                '11.0',
282
            ],
283
            [
284
                'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, '
285
                . 'like Gecko) Chrome/25.0.1364.172 Safari/537.22',
286
                'Win',
287
                'CHROME',
288
                '25.0.1364.172',
289
            ],
290
            [
291
                'Mozilla/5.0 (Unknown; U; Unix BSD/SYSV system; C -) '
292
                . 'AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.10.2',
293
                'Unix',
294
                'SAFARI',
295
                '5.0.419',
296
            ],
297
            [
298
                'Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.9b) Gecko/20031208',
299
                'Win',
300
                'GECKO',
301
                '1.9',
302
            ],
303
            [
304
                'Mozilla/5.0 (compatible; Konqueror/4.5; NetBSD 5.0.2; X11; '
305
                . 'amd64; en_US) KHTML/4.5.4 (like Gecko)',
306
                'Other',
307
                'KONQUEROR',
308
            ],
309
            [
310
                'Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0',
311
                'Linux',
312
                'FIREFOX',
313
                '5.0',
314
            ],
315
            [
316
                'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 '
317
                . 'Firefox/12.0',
318
                'Linux',
319
                'FIREFOX',
320
                '12.0',
321
            ],
322
            /**
323
             * @todo Is this version really expected?
324
             */
325
            [
326
                'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.4+ (KHTML, like G'
327
                . 'ecko) Version/5.0 Safari/535.4+ SUSE/12.1 (3.2.1) Epiphany/3.2.1',
328
                'Linux',
329
                'SAFARI',
330
                '5.0',
331
            ],
332
        ];
333
    }
334
335
    /**
336
     * test for CheckGd2
337
     */
338
    public function testCheckGd2(): void
339
    {
340
        $prevIsGb2Val = $this->object->get('PMA_IS_GD2');
0 ignored issues
show
Unused Code introduced by
The assignment to $prevIsGb2Val is dead and can be removed.
Loading history...
341
342
        $this->object->set('GD2Available', 'yes');
343
        $this->object->checkGd2();
344
        $this->assertEquals(1, $this->object->get('PMA_IS_GD2'));
345
346
        $this->object->set('GD2Available', 'no');
347
        $this->object->checkGd2();
348
        $this->assertEquals(0, $this->object->get('PMA_IS_GD2'));
349
350
        $this->object->set('GD2Available', 'auto');
351
352
        if (! function_exists('imagecreatetruecolor')) {
353
            $this->object->checkGd2();
354
            $this->assertEquals(
355
                0,
356
                $this->object->get('PMA_IS_GD2'),
357
                'imagecreatetruecolor does not exist, PMA_IS_GD2 should be 0'
358
            );
359
        }
360
361
        if (function_exists('gd_info')) {
362
            $this->object->checkGd2();
363
            $gd_nfo = gd_info();
364
            if (mb_strstr($gd_nfo['GD Version'], '2.')) {
365
                $this->assertEquals(
366
                    1,
367
                    $this->object->get('PMA_IS_GD2'),
368
                    'GD Version >= 2, PMA_IS_GD2 should be 1'
369
                );
370
            } else {
371
                $this->assertEquals(
372
                    0,
373
                    $this->object->get('PMA_IS_GD2'),
374
                    'GD Version < 2, PMA_IS_GD2 should be 0'
375
                );
376
            }
377
        }
378
379
        /* Get GD version string from phpinfo output */
380
        ob_start();
381
        phpinfo(INFO_MODULES); /* Only modules */
382
        $a = strip_tags((string) ob_get_contents());
383
        ob_end_clean();
384
385
        if (! preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
386
            return;
387
        }
388
389
        if (mb_strstr($v, '2.')) {
390
            $this->assertEquals(
391
                1,
392
                $this->object->get('PMA_IS_GD2'),
393
                'PMA_IS_GD2 should be 1'
394
            );
395
        } else {
396
            $this->assertEquals(
397
                0,
398
                $this->object->get('PMA_IS_GD2'),
399
                'PMA_IS_GD2 should be 0'
400
            );
401
        }
402
    }
403
404
    /**
405
     * Web server detection test
406
     *
407
     * @param string $server Server identification
408
     * @param int    $iis    Whether server should be detected as IIS
409
     *
410
     * @dataProvider serverNames
411
     */
412
    public function testCheckWebServer(string $server, int $iis): void
413
    {
414
        $_SERVER['SERVER_SOFTWARE'] = $server;
415
        $this->object->checkWebServer();
416
        $this->assertEquals($iis, $this->object->get('PMA_IS_IIS'));
417
        unset($_SERVER['SERVER_SOFTWARE']);
418
    }
419
420
    /**
421
     * return server names
422
     *
423
     * @return array
424
     */
425
    public function serverNames(): array
426
    {
427
        return [
428
            [
429
                'Microsoft-IIS 7.0',
430
                1,
431
            ],
432
            [
433
                'Apache/2.2.17',
434
                0,
435
            ],
436
        ];
437
    }
438
439
    /**
440
     * test for CheckWebServerOs
441
     */
442
    public function testCheckWebServerOs(): void
443
    {
444
        $this->object->checkWebServerOs();
445
446
        if (defined('PHP_OS')) {
447
            if (stristr(PHP_OS, 'darwin')) {
448
                $this->assertFalse($this->object->get('PMA_IS_WINDOWS'));
449
            } elseif (stristr(PHP_OS, 'win')) {
450
                $this->assertTrue($this->object->get('PMA_IS_WINDOWS'));
451
            } elseif (stristr(PHP_OS, 'OS/2')) {
452
                $this->assertTrue($this->object->get('PMA_IS_WINDOWS'));
453
            } elseif (stristr(PHP_OS, 'Linux')) {
454
                $this->assertFalse($this->object->get('PMA_IS_WINDOWS'));
455
            } else {
456
                $this->markTestIncomplete('Not known PHP_OS: ' . PHP_OS);
457
            }
458
        } else {
459
            $this->assertEquals(0, $this->object->get('PMA_IS_WINDOWS'));
460
461
            define('PHP_OS', 'Windows');
462
            $this->assertTrue($this->object->get('PMA_IS_WINDOWS'));
463
        }
464
    }
465
466
    /**
467
     * Tests loading of default values
468
     *
469
     * @group large
470
     */
471
    public function testLoadDefaults(): void
472
    {
473
        $prevDefaultSource = $this->object->defaultSource;
474
475
        $this->object->defaultSource = 'unexisted.file.php';
476
        $this->assertFalse($this->object->loadDefaults());
477
478
        $this->object->defaultSource = $prevDefaultSource;
479
480
        /** @var array<string,mixed> $cfg */
481
        $cfg = [];
482
        include $this->object->defaultSource;
483
        $loadedConf = $cfg;
484
        unset($cfg);
485
486
        $this->assertTrue($this->object->loadDefaults());
487
488
        $this->assertEquals(
489
            $this->object->defaultSourceMtime,
490
            filemtime($prevDefaultSource)
491
        );
492
        $this->assertEquals(
493
            $loadedConf['Servers'][1],
494
            $this->object->defaultServer
495
        );
496
497
        unset($loadedConf['Servers']);
498
499
        $this->assertEquals($loadedConf, $this->object->default);
500
501
        $expectedSettings = array_replace_recursive(
502
            $this->object->settings,
503
            $loadedConf
504
        );
505
506
        $this->assertEquals(
507
            $expectedSettings,
508
            $this->object->settings,
509
            'Settings loaded wrong'
510
        );
511
512
        $this->assertFalse($this->object->errorConfigDefaultFile);
513
    }
514
515
    /**
516
     * test for CheckConfigSource
517
     */
518
    public function testCheckConfigSource(): void
519
    {
520
        $this->object->setSource('unexisted.config.php');
521
        $this->assertFalse($this->object->checkConfigSource());
522
        $this->assertEquals(0, $this->object->sourceMtime);
523
524
        $this->object->setSource(ROOT_PATH . 'libraries/config.default.php');
525
526
        $this->assertNotEmpty($this->object->getSource());
527
        $this->assertTrue($this->object->checkConfigSource());
528
    }
529
530
    /**
531
     * Test getting and setting config values
532
     */
533
    public function testGetAndSet(): void
534
    {
535
        $this->assertNull($this->object->get('unresisting_setting'));
536
537
        $this->object->set('test_setting', 'test_value');
538
539
        $this->assertEquals('test_value', $this->object->get('test_setting'));
540
    }
541
542
    /**
543
     * Tests setting configuration source
544
     */
545
    public function testGetSetSource(): void
546
    {
547
        echo $this->object->getSource();
548
549
        $this->assertEmpty($this->object->getSource(), 'Source is null by default');
550
551
        $this->object->setSource(ROOT_PATH . 'config.sample.inc.php');
552
553
        $this->assertEquals(
554
            ROOT_PATH . 'config.sample.inc.php',
555
            $this->object->getSource(),
556
            'Cant set new source'
557
        );
558
    }
559
560
    /**
561
     * test for IsHttp
562
     *
563
     * @param string $scheme          http scheme
564
     * @param string $https           https
565
     * @param string $forwarded       forwarded header
566
     * @param string $uri             request uri
567
     * @param string $lb              http https from lb
568
     * @param string $front           http front end https
569
     * @param string $proto           http x forwarded proto
570
     * @param string $protoCloudFront http cloudfront forwarded proto
571
     * @param string $pmaAbsoluteUri  phpMyAdmin absolute URI
572
     * @param int    $port            server port
573
     * @param bool   $expected        expected result
574
     *
575
     * @dataProvider httpsParams
576
     */
577
    public function testIsHttps(
578
        string $scheme,
579
        string $https,
580
        string $forwarded,
581
        string $uri,
582
        string $lb,
583
        string $front,
584
        string $proto,
585
        string $protoCloudFront,
586
        string $pmaAbsoluteUri,
587
        int $port,
588
        bool $expected
589
    ): void {
590
        $_SERVER['HTTP_SCHEME'] = $scheme;
591
        $_SERVER['HTTPS'] = $https;
592
        $_SERVER['HTTP_FORWARDED'] = $forwarded;
593
        $_SERVER['REQUEST_URI'] = $uri;
594
        $_SERVER['HTTP_HTTPS_FROM_LB'] = $lb;
595
        $_SERVER['HTTP_FRONT_END_HTTPS'] = $front;
596
        $_SERVER['HTTP_X_FORWARDED_PROTO'] = $proto;
597
        $_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'] = $protoCloudFront;
598
        $_SERVER['SERVER_PORT'] = $port;
599
600
        $this->object->set('is_https', null);
601
        $this->object->set('PmaAbsoluteUri', $pmaAbsoluteUri);
602
        $this->assertEquals($expected, $this->object->isHttps());
603
    }
604
605
    /**
606
     * Data provider for https detection
607
     *
608
     * @return array
609
     */
610
    public function httpsParams(): array
611
    {
612
        return [
613
            [
614
                'http',
615
                '',
616
                '',
617
                '',
618
                '',
619
                '',
620
                'http',
621
                '',
622
                '',
623
                80,
624
                false,
625
            ],
626
            [
627
                'http',
628
                '',
629
                '',
630
                'http://',
631
                '',
632
                '',
633
                'http',
634
                '',
635
                '',
636
                80,
637
                false,
638
            ],
639
            [
640
                'http',
641
                '',
642
                '',
643
                '',
644
                '',
645
                '',
646
                'http',
647
                '',
648
                '',
649
                443,
650
                true,
651
            ],
652
            [
653
                'http',
654
                '',
655
                '',
656
                '',
657
                '',
658
                '',
659
                'https',
660
                '',
661
                '',
662
                80,
663
                true,
664
            ],
665
            [
666
                'http',
667
                '',
668
                '',
669
                '',
670
                '',
671
                'on',
672
                'http',
673
                '',
674
                '',
675
                80,
676
                true,
677
            ],
678
            [
679
                'http',
680
                '',
681
                '',
682
                '',
683
                'on',
684
                '',
685
                'http',
686
                '',
687
                '',
688
                80,
689
                true,
690
            ],
691
            [
692
                'http',
693
                '',
694
                '',
695
                'https://',
696
                '',
697
                '',
698
                'http',
699
                '',
700
                '',
701
                80,
702
                true,
703
            ],
704
            [
705
                'http',
706
                'on',
707
                '',
708
                '',
709
                '',
710
                '',
711
                'http',
712
                '',
713
                '',
714
                80,
715
                true,
716
            ],
717
            [
718
                'https',
719
                '',
720
                '',
721
                '',
722
                '',
723
                '',
724
                'http',
725
                '',
726
                '',
727
                80,
728
                true,
729
            ],
730
            [
731
                'http',
732
                '',
733
                '',
734
                '',
735
                '',
736
                '',
737
                '',
738
                'https',
739
                '',
740
                80,
741
                true,
742
            ],
743
            [
744
                'http',
745
                '',
746
                '',
747
                '',
748
                '',
749
                '',
750
                'https',
751
                'http',
752
                '',
753
                80,
754
                true,
755
            ],
756
            [
757
                'https',
758
                '',
759
                '',
760
                '',
761
                '',
762
                '',
763
                '',
764
                '',
765
                '',
766
                80,
767
                true,
768
            ],
769
            [
770
                'http',
771
                '',
772
                '',
773
                '',
774
                '',
775
                '',
776
                '',
777
                '',
778
                '',
779
                8080,
780
                false,
781
            ],
782
            [
783
                '',
784
                '',
785
                '',
786
                '',
787
                '',
788
                '',
789
                '',
790
                '',
791
                'https://127.0.0.1',
792
                80,
793
                true,
794
            ],
795
            [
796
                '',
797
                '',
798
                '',
799
                '',
800
                '',
801
                '',
802
                '',
803
                '',
804
                'http://127.0.0.1',
805
                80,
806
                false,
807
            ],
808
            [
809
                '',
810
                '',
811
                'for=12.34.56.78;host=example.com;proto=https, for=23.45.67.89',
812
                '',
813
                '',
814
                '',
815
                '',
816
                '',
817
                'http://127.0.0.1',
818
                80,
819
                true,
820
            ],
821
        ];
822
    }
823
824
    /**
825
     * Test for backward compatibility globals
826
     *
827
     * @depends testCheckSystem
828
     * @depends testCheckWebServer
829
     * @depends testLoadDefaults
830
     * @group large
831
     */
832
    public function testEnableBc(): void
833
    {
834
        $this->object->enableBc();
835
836
        $defines = [
837
            'PMA_IS_WINDOWS',
838
            'PMA_IS_GD2',
839
            'PMA_USR_OS',
840
            'PMA_USR_BROWSER_VER',
841
            'PMA_USR_BROWSER_AGENT',
842
        ];
843
844
        foreach ($defines as $define) {
845
            $this->assertTrue(defined($define));
846
            $this->assertEquals(constant($define), $this->object->get($define));
847
        }
848
    }
849
850
    /**
851
     * Test for getting root path
852
     *
853
     * @param string $request  The request URL used for phpMyAdmin
854
     * @param string $absolute The absolute URL used for phpMyAdmin
855
     * @param string $expected Expected root path
856
     *
857
     * @dataProvider rootUris
858
     */
859
    public function testGetRootPath(string $request, string $absolute, string $expected): void
860
    {
861
        $GLOBALS['PMA_PHP_SELF'] = $request;
862
        $this->object->set('PmaAbsoluteUri', $absolute);
863
        $this->assertEquals($expected, $this->object->getRootPath());
864
    }
865
866
    /**
867
     * Data provider for testGetRootPath
868
     *
869
     * @return array data for testGetRootPath
870
     */
871
    public function rootUris(): array
872
    {
873
        return [
874
            [
875
                '',
876
                '',
877
                '/',
878
            ],
879
            [
880
                '/',
881
                '',
882
                '/',
883
            ],
884
            [
885
                '/index.php',
886
                '',
887
                '/',
888
            ],
889
            [
890
                '\\index.php',
891
                '',
892
                '/',
893
            ],
894
            [
895
                '\\',
896
                '',
897
                '/',
898
            ],
899
            [
900
                '\\path\\to\\index.php',
901
                '',
902
                '/path/to/',
903
            ],
904
            [
905
                '/foo/bar/phpmyadmin/index.php',
906
                '',
907
                '/foo/bar/phpmyadmin/',
908
            ],
909
            [
910
                '/foo/bar/phpmyadmin/',
911
                '',
912
                '/foo/bar/phpmyadmin/',
913
            ],
914
            [
915
                'https://example.net/baz/phpmyadmin/',
916
                '',
917
                '/baz/phpmyadmin/',
918
            ],
919
            [
920
                'http://example.net/baz/phpmyadmin/',
921
                '',
922
                '/baz/phpmyadmin/',
923
            ],
924
            [
925
                'http://example.net/phpmyadmin/',
926
                '',
927
                '/phpmyadmin/',
928
            ],
929
            [
930
                'http://example.net/',
931
                '',
932
                '/',
933
            ],
934
            [
935
                'http://example.net/',
936
                'http://example.net/phpmyadmin/',
937
                '/phpmyadmin/',
938
            ],
939
            [
940
                'http://example.net/',
941
                'http://example.net/phpmyadmin',
942
                '/phpmyadmin/',
943
            ],
944
            [
945
                'http://example.net/',
946
                '/phpmyadmin2',
947
                '/phpmyadmin2/',
948
            ],
949
            [
950
                'http://example.net/',
951
                '/phpmyadmin3/',
952
                '/phpmyadmin3/',
953
            ],
954
        ];
955
    }
956
957
    /**
958
     * Tests loading of config file
959
     *
960
     * @param string $source File name of config to load
961
     * @param bool   $result Expected result of loading
962
     *
963
     * @dataProvider configPaths
964
     */
965
    public function testLoad(string $source, bool $result): void
966
    {
967
        if ($result) {
968
            $this->assertTrue($this->object->load($source));
969
        } else {
970
            $this->assertFalse($this->object->load($source));
971
        }
972
    }
973
974
    /**
975
     * return of config Paths
976
     *
977
     * @return array
978
     */
979
    public function configPaths(): array
980
    {
981
        return [
982
            [
983
                ROOT_PATH . 'test/test_data/config.inc.php',
984
                true,
985
            ],
986
            [
987
                ROOT_PATH . 'test/test_data/config-nonexisting.inc.php',
988
                false,
989
            ],
990
            [
991
                ROOT_PATH . 'libraries/config.default.php',
992
                true,
993
            ],
994
        ];
995
    }
996
997
    /**
998
     * Test for loading user preferences
999
     *
1000
     * @todo Test actually preferences loading
1001
     * @doesNotPerformAssertions
1002
     */
1003
    public function testLoadUserPreferences(): void
1004
    {
1005
        $this->object->loadUserPreferences();
1006
    }
1007
1008
    /**
1009
     * Test for setting user config value
1010
     */
1011
    public function testSetUserValue(): void
1012
    {
1013
        $this->object->setUserValue(null, 'lang', 'cs', 'en');
1014
        $this->object->setUserValue('TEST_COOKIE_USER_VAL', '', 'cfg_val_1');
1015
        $this->assertEquals(
1016
            $this->object->getUserValue('TEST_COOKIE_USER_VAL', 'fail'),
1017
            'cfg_val_1'
1018
        );
1019
    }
1020
1021
    /**
1022
     * Test for getting user config value
1023
     */
1024
    public function testGetUserValue(): void
1025
    {
1026
        $this->assertEquals($this->object->getUserValue('test_val', 'val'), 'val');
1027
    }
1028
1029
    /**
1030
     * Should test checking of config permissions
1031
     */
1032
    public function testCheckPermissions(): void
1033
    {
1034
        //load file permissions for the current permissions file
1035
        $perms = @fileperms($this->object->getSource());
1036
        //testing for permissions for no configuration file
1037
        $this->assertFalse(! ($perms === false) && ($perms & 2));
1038
1039
        //load file permissions for the current permissions file
1040
        $perms = @fileperms($this->permTestObj->getSource());
1041
1042
        if (! ($perms === false) && ($perms & 2)) {
1043
            $this->assertTrue((bool) $this->permTestObj->get('PMA_IS_WINDOWS'));
1044
        } else {
1045
            $this->assertFalse((bool) $this->permTestObj->get('PMA_IS_WINDOWS'));
1046
        }
1047
    }
1048
1049
    /**
1050
     * Test for setting cookies
1051
     */
1052
    public function testSetCookie(): void
1053
    {
1054
        $this->object->set('is_https', false);
1055
        $this->assertFalse(
1056
            $this->object->setCookie(
1057
                'TEST_DEF_COOKIE',
1058
                'test_def_123',
1059
                'test_def_123'
1060
            )
1061
        );
1062
1063
        $this->assertTrue(
1064
            $this->object->setCookie(
1065
                'TEST_CONFIG_COOKIE',
1066
                'test_val_123',
1067
                null,
1068
                3600
1069
            )
1070
        );
1071
1072
        $this->assertTrue(
1073
            $this->object->setCookie(
1074
                'TEST_CONFIG_COOKIE',
1075
                '',
1076
                'default_val'
1077
            )
1078
        );
1079
1080
        $_COOKIE['TEST_MANUAL_COOKIE'] = 'some_test_val';
1081
        $this->assertTrue(
1082
            $this->object->setCookie(
1083
                'TEST_MANUAL_COOKIE',
1084
                'other',
1085
                'other'
1086
            )
1087
        );
1088
    }
1089
1090
    /**
1091
     * Test for getTempDir
1092
     *
1093
     * @group file-system
1094
     */
1095
    public function testGetTempDir(): void
1096
    {
1097
        $this->object->set('TempDir', sys_get_temp_dir() . DIRECTORY_SEPARATOR);
1098
        // Check no double slash is here
1099
        $this->assertEquals(
1100
            sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'upload',
1101
            $this->object->getTempDir('upload')
1102
        );
1103
    }
1104
1105
    /**
1106
     * Test for getUploadTempDir
1107
     *
1108
     * @group file-system
1109
     */
1110
    public function testGetUploadTempDir(): void
1111
    {
1112
        $this->object->set('TempDir', realpath(sys_get_temp_dir()) . DIRECTORY_SEPARATOR);
1113
1114
        $this->assertEquals(
1115
            $this->object->getTempDir('upload'),
1116
            $this->object->getUploadTempDir()
1117
        );
1118
    }
1119
1120
    /**
1121
     * Test for checkServers
1122
     *
1123
     * @param array $settings settings array
1124
     * @param array $expected expected result
1125
     *
1126
     * @dataProvider serverSettingsProvider
1127
     */
1128
    public function testCheckServers(array $settings, array $expected): void
1129
    {
1130
        $this->object->settings['Servers'] = $settings;
1131
        $this->object->checkServers();
1132
        if ($expected === null) {
0 ignored issues
show
introduced by
The condition $expected === null is always false.
Loading history...
1133
            $expected = $this->object->defaultServer;
1134
        } else {
1135
            $expected = array_merge($this->object->defaultServer, $expected);
1136
        }
1137
1138
        $this->assertEquals($expected, $this->object->settings['Servers'][1]);
1139
    }
1140
1141
    /**
1142
     * Data provider for checkServers test
1143
     *
1144
     * @return array
1145
     */
1146
    public function serverSettingsProvider(): array
1147
    {
1148
        return [
1149
            'empty' => [
1150
                [],
1151
                [],
1152
            ],
1153
            'only_host' => [
1154
                [1 => ['host' => '127.0.0.1']],
1155
                ['host' => '127.0.0.1'],
1156
            ],
1157
            'empty_host' => [
1158
                [1 => ['host' => '']],
1159
                [
1160
                    'verbose' => 'Server 1',
1161
                    'host' => '',
1162
                ],
1163
            ],
1164
        ];
1165
    }
1166
1167
    /**
1168
     * @group with-trigger-error
1169
     */
1170
    public function testCheckServersWithInvalidServer(): void
1171
    {
1172
        $this->expectError();
1173
        $this->expectErrorMessage('Invalid server index: invalid');
1174
1175
        $this->object->settings['Servers'] = ['invalid' => ['host' => '127.0.0.1'], 1 => ['host' => '127.0.0.1']];
1176
        $this->object->checkServers();
1177
        $expected = array_merge($this->object->defaultServer, ['host' => '127.0.0.1']);
1178
1179
        $this->assertEquals($expected, $this->object->settings['Servers'][1]);
1180
    }
1181
1182
    /**
1183
     * Test for selectServer
1184
     *
1185
     * @param array  $settings settings array
1186
     * @param string $request  request
1187
     * @param int    $expected expected result
1188
     *
1189
     * @dataProvider selectServerProvider
1190
     * @depends testCheckServers
1191
     */
1192
    public function testSelectServer(array $settings, string $request, int $expected): void
1193
    {
1194
        $this->object->settings['Servers'] = $settings;
1195
        $this->object->checkServers();
1196
        $_REQUEST['server'] = $request;
1197
        $this->assertEquals($expected, $this->object->selectServer());
1198
    }
1199
1200
    /**
1201
     * Data provider for selectServer test
1202
     *
1203
     * @return array
1204
     */
1205
    public function selectServerProvider(): array
1206
    {
1207
        return [
1208
            'zero' => [
1209
                [],
1210
                '0',
1211
                1,
1212
            ],
1213
            'number' => [
1214
                [1 => []],
1215
                '1',
1216
                1,
1217
            ],
1218
            'host' => [
1219
                [2 => ['host' => '127.0.0.1']],
1220
                '127.0.0.1',
1221
                2,
1222
            ],
1223
            'verbose' => [
1224
                [
1225
                    1 => [
1226
                        'verbose' => 'Server 1',
1227
                        'host' => '',
1228
                    ],
1229
                ],
1230
                'Server 1',
1231
                1,
1232
            ],
1233
            'md5' => [
1234
                [
1235
                    66 => [
1236
                        'verbose' => 'Server 1',
1237
                        'host' => '',
1238
                    ],
1239
                ],
1240
                '753f173bd4ac8a45eae0fe9a4fbe0fc0',
1241
                66,
1242
            ],
1243
            'nonexisting_string' => [
1244
                [1 => []],
1245
                'invalid',
1246
                1,
1247
            ],
1248
            'nonexisting' => [
1249
                [1 => []],
1250
                '100',
1251
                1,
1252
            ],
1253
        ];
1254
    }
1255
1256
    /**
1257
     * Test for getConnectionParams
1258
     *
1259
     * @param array      $server_cfg Server configuration
1260
     * @param int        $mode       Mode to test
1261
     * @param array|null $server     Server array to test
1262
     * @param array      $expected   Expected result
1263
     *
1264
     * @dataProvider connectionParams
1265
     */
1266
    public function testGetConnectionParams(array $server_cfg, int $mode, ?array $server, array $expected): void
1267
    {
1268
        $GLOBALS['cfg']['Server'] = $server_cfg;
1269
        $result = Config::getConnectionParams($mode, $server);
1270
        $this->assertEquals($expected, $result);
1271
    }
1272
1273
    /**
1274
     * Data provider for getConnectionParams test
1275
     *
1276
     * @return array
1277
     */
1278
    public function connectionParams(): array
1279
    {
1280
        $cfg_basic = [
1281
            'user' => 'u',
1282
            'password' => 'pass',
1283
            'host' => '',
1284
            'controluser' => 'u2',
1285
            'controlpass' => 'p2',
1286
        ];
1287
        $cfg_ssl = [
1288
            'user' => 'u',
1289
            'password' => 'pass',
1290
            'host' => '',
1291
            'ssl' => true,
1292
            'controluser' => 'u2',
1293
            'controlpass' => 'p2',
1294
        ];
1295
        $cfg_control_ssl = [
1296
            'user' => 'u',
1297
            'password' => 'pass',
1298
            'host' => '',
1299
            'control_ssl' => true,
1300
            'controluser' => 'u2',
1301
            'controlpass' => 'p2',
1302
        ];
1303
1304
        return [
1305
            [
1306
                $cfg_basic,
1307
                DatabaseInterface::CONNECT_USER,
1308
                null,
1309
                [
1310
                    'u',
1311
                    'pass',
1312
                    [
1313
                        'user' => 'u',
1314
                        'password' => 'pass',
1315
                        'host' => 'localhost',
1316
                        'socket' => null,
1317
                        'port' => 0,
1318
                        'ssl' => false,
1319
                        'compress' => false,
1320
                        'controluser' => 'u2',
1321
                        'controlpass' => 'p2',
1322
                    ],
1323
                ],
1324
            ],
1325
            [
1326
                $cfg_basic,
1327
                DatabaseInterface::CONNECT_CONTROL,
1328
                null,
1329
                [
1330
                    'u2',
1331
                    'p2',
1332
                    [
1333
                        'host' => 'localhost',
1334
                        'socket' => null,
1335
                        'port' => 0,
1336
                        'ssl' => false,
1337
                        'compress' => false,
1338
                    ],
1339
                ],
1340
            ],
1341
            [
1342
                $cfg_ssl,
1343
                DatabaseInterface::CONNECT_USER,
1344
                null,
1345
                [
1346
                    'u',
1347
                    'pass',
1348
                    [
1349
                        'user' => 'u',
1350
                        'password' => 'pass',
1351
                        'host' => 'localhost',
1352
                        'socket' => null,
1353
                        'port' => 0,
1354
                        'ssl' => true,
1355
                        'compress' => false,
1356
                        'controluser' => 'u2',
1357
                        'controlpass' => 'p2',
1358
                    ],
1359
                ],
1360
            ],
1361
            [
1362
                $cfg_ssl,
1363
                DatabaseInterface::CONNECT_CONTROL,
1364
                null,
1365
                [
1366
                    'u2',
1367
                    'p2',
1368
                    [
1369
                        'host' => 'localhost',
1370
                        'socket' => null,
1371
                        'port' => 0,
1372
                        'ssl' => true,
1373
                        'compress' => false,
1374
                    ],
1375
                ],
1376
            ],
1377
            [
1378
                $cfg_control_ssl,
1379
                DatabaseInterface::CONNECT_USER,
1380
                null,
1381
                [
1382
                    'u',
1383
                    'pass',
1384
                    [
1385
                        'user' => 'u',
1386
                        'password' => 'pass',
1387
                        'host' => 'localhost',
1388
                        'socket' => null,
1389
                        'port' => 0,
1390
                        'ssl' => false,
1391
                        'compress' => false,
1392
                        'controluser' => 'u2',
1393
                        'controlpass' => 'p2',
1394
                        'control_ssl' => true,
1395
                    ],
1396
                ],
1397
            ],
1398
            [
1399
                $cfg_control_ssl,
1400
                DatabaseInterface::CONNECT_CONTROL,
1401
                null,
1402
                [
1403
                    'u2',
1404
                    'p2',
1405
                    [
1406
                        'host' => 'localhost',
1407
                        'socket' => null,
1408
                        'port' => 0,
1409
                        'ssl' => true,
1410
                        'compress' => false,
1411
                    ],
1412
                ],
1413
            ],
1414
        ];
1415
    }
1416
}
1417