File: ConfigFileTest.php

package info (click to toggle)
phpmyadmin 4%3A5.2.1%2Bdfsg-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 131,332 kB
  • sloc: javascript: 212,681; php: 168,094; xml: 18,098; sql: 504; sh: 274; makefile: 205; python: 199
file content (544 lines) | stat: -rw-r--r-- 15,403 bytes parent folder | download
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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Config;

use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\Config\Settings;
use PhpMyAdmin\Tests\AbstractTestCase;
use stdClass;

use function array_keys;
use function count;

/**
 * @covers \PhpMyAdmin\Config\ConfigFile
 */
class ConfigFileTest extends AbstractTestCase
{
    /**
     * Any valid key that exists in {@see \PhpMyAdmin\Config\Settings} and isn't empty
     */
    public const SIMPLE_KEY_WITH_DEFAULT_VALUE = 'DefaultQueryTable';

    /**
     * Object under test
     *
     * @var ConfigFile
     */
    protected $object;

    /**
     * Setup function for test cases
     */
    protected function setUp(): void
    {
        parent::setUp();
        $GLOBALS['server'] = 1;
        $this->object = new ConfigFile();
    }

    /**
     * TearDown function for test cases
     */
    protected function tearDown(): void
    {
        parent::tearDown();
        $this->object->setConfigData([]);
        unset($this->object);
    }

    /**
     * Test for new ConfigFile()
     */
    public function testNewObjectState(): void
    {
        // Check default dynamic values
        $this->assertEquals(
            [],
            $this->object->getConfig()
        );

        // Check environment state
        $this->assertEquals(
            [],
            $_SESSION['ConfigFile1']
        );

        // Validate default value used in tests
        $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE);
        $this->assertNotNull($default_value);
    }

    /**
     * Test for ConfigFile::setPersistKeys()
     */
    public function testPersistentKeys(): void
    {
        $default_simple_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE);
        $default_host = $this->object->getDefault('Servers/1/host');
        $default_config = [
            self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_simple_value,
            'Servers/1/host' => $default_host,
            'Servers/2/host' => $default_host,
        ];

        /**
         * Case 1: set default value, key should not be persisted
         */
        $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_simple_value);
        $this->object->set('Servers/1/host', $default_host);
        $this->object->set('Servers/2/host', $default_host);
        $this->assertEmpty($this->object->getConfig());

        /**
         * Case 2: persistent keys should be always present in flat array,
         * even if not explicitly set (unless they are Server entries)
         */
        $this->object->setPersistKeys(array_keys($default_config));
        $this->object->resetConfigData();
        $this->assertEmpty($this->object->getConfig());
        $this->assertEquals(
            $default_config,
            $this->object->getConfigArray()
        );

        /**
         * Case 3: persistent keys should be always saved,
         * even if set to default values
         */
        $this->object->set('Servers/2/host', $default_host);
        $this->assertEquals(
            ['Servers' => [2 => ['host' => $default_host]]],
            $this->object->getConfig()
        );
    }

    /**
     * Test for ConfigFile::setAllowedKeys
     */
    public function testAllowedKeys(): void
    {
        /**
         * Case 1: filter should not allow to set b
         */
        $this->object->setAllowedKeys(['a', 'c']);
        $this->object->set('a', 1);
        $this->object->set('b', 2);
        $this->object->set('c', 3);

        $this->assertEquals(
            [
                'a' => 1,
                'c' => 3,
            ],
            $this->object->getConfig()
        );

        /**
         * Case 2: disabling filter should allow to set b
         */
        $this->object->setAllowedKeys(null);
        $this->object->set('b', 2);

        $this->assertEquals(
            [
                'a' => 1,
                'b' => 2,
                'c' => 3,
            ],
            $this->object->getConfig()
        );
    }

    /**
     * Test for ConfigFile::setCfgUpdateReadMapping
     */
    public function testConfigReadMapping(): void
    {
        $this->object->setCfgUpdateReadMapping(
            [
                'Servers/value1' => 'Servers/1/value1',
                'Servers/value2' => 'Servers/1/value2',
            ]
        );
        $this->object->set('Servers/1/passthrough1', 1);
        $this->object->set('Servers/1/passthrough2', 2);
        $this->object->updateWithGlobalConfig(['Servers/value1' => 3]);

        $this->assertEquals(
            [
                'Servers' => [
                    1 => [
                        'passthrough1' => 1,
                        'passthrough2' => 2,
                        'value1' => 3,
                    ],
                ],
            ],
            $this->object->getConfig()
        );
        $this->assertEquals(
            3,
            $this->object->get('Servers/1/value1')
        );
    }

    /**
     * Test for ConfigFile::resetConfigData
     */
    public function testResetConfigData(): void
    {
        $this->object->set('key', 'value');

        $this->object->resetConfigData();

        $this->assertEmpty($this->object->getConfig());
        $this->assertEmpty($this->object->getConfigArray());
    }

    /**
     * Test for ConfigFile::setConfigData
     */
    public function testSetConfigData(): void
    {
        $this->object->set('abc', 'should be deleted by setConfigData');
        $this->object->setConfigData(['a' => 'b']);

        $this->assertEquals(
            ['a' => 'b'],
            $this->object->getConfig()
        );
        $this->assertEquals(
            ['a' => 'b'],
            $this->object->getConfigArray()
        );
    }

    /**
     * Test for ConfigFile::set and ConfigFile::get
     */
    public function testBasicSetUsage(): void
    {
        $default_host = $this->object->getDefault('Servers/1/host');
        $nondefault_host = $default_host . '.abc';

        $this->object->set('Servers/4/host', $nondefault_host);
        $this->object->set('Servers/5/host', $default_host);
        $this->object->set('Servers/6/host', $default_host, 'Servers/6/host');
        $this->assertEquals(
            $nondefault_host,
            $this->object->get('Servers/4/host')
        );
        $this->assertEquals(
            null,
            $this->object->get('Servers/5/host')
        );
        $this->assertEquals(
            $default_host,
            $this->object->get('Servers/6/host')
        );

        // return default value for nonexistent keys
        $this->assertNull(
            $this->object->get('key not excist')
        );
        $this->assertEquals(
            [1],
            $this->object->get('key not excist', [1])
        );
        $default = new stdClass();
        $this->assertInstanceOf(
            stdClass::class,
            $this->object->get('key not excist', $default)
        );
    }

    /**
     * Test for ConfigFile::set - in PMA Setup
     */
    public function testConfigFileSetInSetup(): void
    {
        $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE);

        // default values are not written
        $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_value);
        $this->assertEmpty($this->object->getConfig());
    }

    /**
     * Test for ConfigFile::set - in user preferences
     */
    public function testConfigFileSetInUserPreferences(): void
    {
        $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE);

        // values are not written when they are the same as in config.inc.php
        $this->object = new ConfigFile(
            [self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_value]
        );
        $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_value);
        $this->assertEmpty($this->object->getConfig());

        // but if config.inc.php differs from the default values,
        // allow to overwrite with value from the default values
        $config_inc_php_value = $default_value . 'suffix';
        $this->object = new ConfigFile(
            [self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $config_inc_php_value]
        );
        $this->object->set(self::SIMPLE_KEY_WITH_DEFAULT_VALUE, $default_value);
        $this->assertEquals(
            [self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_value],
            $this->object->getConfig()
        );
    }

    /**
     * Test for ConfigFile::getFlatDefaultConfig
     *
     * @group medium
     */
    public function testGetFlatDefaultConfig(): void
    {
        $flat_default_config = $this->object->getFlatDefaultConfig();

        $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE);
        $this->assertEquals($default_value, $flat_default_config[self::SIMPLE_KEY_WITH_DEFAULT_VALUE]);

        $localhost_value = $this->object->getDefault('Servers/1/host');
        $this->assertEquals($localhost_value, $flat_default_config['Servers/1/host']);

        $settings = new Settings([]);
        $cfg = $settings->toArray();

        $this->assertGreaterThanOrEqual(100, count($cfg));
        $this->assertGreaterThanOrEqual(count($cfg), count($flat_default_config));
    }

    /**
     * Test for ConfigFile::updateWithGlobalConfig
     */
    public function testUpdateWithGlobalConfig(): void
    {
        $this->object->set('key', 'value');
        $this->object->set('key2', 'value');
        $this->object->updateWithGlobalConfig(['key' => 'ABC']);

        $this->assertEquals(
            [
                'key' => 'ABC',
                'key2' => 'value',
            ],
            $this->object->getConfig()
        );
    }

    /**
     * Test for ConfigFile::getCanonicalPath
     */
    public function testGetCanonicalPath(): void
    {
        $this->assertEquals(
            'Servers/1/abcd',
            $this->object->getCanonicalPath('Servers/2/abcd')
        );

        $this->assertEquals(
            'Servers/foo/bar',
            $this->object->getCanonicalPath('Servers/foo/bar')
        );
    }

    /**
     * Test for ConfigFile::getDbEntry
     */
    public function testGetDbEntry(): void
    {
        $cfg_db = include ROOT_PATH . 'libraries/config.values.php';
        // verify that $cfg_db read from config.values.php is valid
        $this->assertGreaterThanOrEqual(20, count($cfg_db));

        $this->assertEquals(
            $cfg_db['Servers'][1]['port'],
            $this->object->getDbEntry('Servers/1/port')
        );
        $this->assertNull($this->object->getDbEntry('no such key'));
        $this->assertEquals(
            [1],
            $this->object->getDbEntry('no such key', [1])
        );
    }

    /**
     * Test for ConfigFile::getServerCount
     */
    public function testGetServerCount(): void
    {
        $this->object->set('Servers/1/x', 1);
        $this->object->set('Servers/2/x', 2);
        $this->object->set('Servers/3/x', 3);
        $this->object->set('Servers/4/x', 4);
        $this->object->set('ServerDefault', 3);

        $this->assertEquals(
            4,
            $this->object->getServerCount()
        );

        $this->object->removeServer(2);
        $this->object->removeServer(2);

        $this->assertEquals(
            2,
            $this->object->getServerCount()
        );

        $this->assertLessThanOrEqual(
            2,
            $this->object->get('ServerDefault')
        );
        $this->assertEquals(
            [
                'Servers' => [
                    1 => ['x' => 1],
                    2 => ['x' => 4],
                ],
            ],
            $this->object->getConfig()
        );
        $this->assertEquals(
            [
                'Servers/1/x' => 1,
                'Servers/2/x' => 4,
            ],
            $this->object->getConfigArray()
        );
    }

    /**
     * Test for ConfigFile::getServers
     */
    public function testGetServers(): void
    {
        $this->object->set('Servers/1/x', 'a');
        $this->object->set('Servers/2/x', 'b');

        $this->assertEquals(
            [
                1 => ['x' => 'a'],
                2 => ['x' => 'b'],
            ],
            $this->object->getServers()
        );
    }

    /**
     * Test for ConfigFile::getServerDSN
     */
    public function testGetServerDSN(): void
    {
        $this->assertEquals(
            '',
            $this->object->getServerDSN(1)
        );

        $this->object->updateWithGlobalConfig(
            [
                'Servers' => [
                    1 => [
                        'auth_type' => 'config',
                        'user' => 'testUser',
                        'host' => 'example.com',
                        'port' => '21',
                    ],
                ],
            ]
        );
        $this->assertEquals(
            'mysqli://testUser@example.com:21',
            $this->object->getServerDSN(1)
        );

        $this->object->updateWithGlobalConfig(
            [
                'Servers' => [
                    1 => [
                        'auth_type' => 'config',
                        'user' => 'testUser',
                        'host' => 'localhost',
                        'port' => '21',
                        'socket' => '123',
                        'password' => '',
                    ],
                ],
            ]
        );
        $this->assertEquals(
            'mysqli://testUser@123',
            $this->object->getServerDSN(1)
        );

        $this->object->updateWithGlobalConfig(
            [
                'Servers' => [
                    1 => [
                        'auth_type' => 'config',
                        'user' => 'testUser',
                        'host' => 'example.com',
                        'port' => '21',
                        'password' => 'testPass',
                    ],
                ],
            ]
        );
        $this->assertEquals(
            'mysqli://testUser:***@example.com:21',
            $this->object->getServerDSN(1)
        );
    }

    /**
     * Test for ConfigFile::getServerName
     */
    public function testGetServerName(): void
    {
        $this->assertEquals(
            '',
            $this->object->getServerName(1)
        );

        $this->object->set('Servers/1/host', 'example.com');
        $this->assertEquals(
            'example.com',
            $this->object->getServerName(1)
        );

        $this->object->set('Servers/1/verbose', 'testData');
        $this->assertEquals(
            'testData',
            $this->object->getServerName(1)
        );
    }

    /**
     * Test for ConfigFile::getConfigArray
     */
    public function testGetConfigArray(): void
    {
        $this->object->setPersistKeys([self::SIMPLE_KEY_WITH_DEFAULT_VALUE]);
        $this->object->set('Array/test', ['x', 'y']);
        $default_value = $this->object->getDefault(self::SIMPLE_KEY_WITH_DEFAULT_VALUE);

        $this->assertEquals(
            [
                self::SIMPLE_KEY_WITH_DEFAULT_VALUE => $default_value,
                'Array/test' => [
                    'x',
                    'y',
                ],
            ],
            $this->object->getConfigArray()
        );
    }
}