Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FormDisplayTest.php « Config « classes « test - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e89974f9940cc2df758682af12b811f529998247 (plain)
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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests\Config;

use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\Config\Form;
use PhpMyAdmin\Config\FormDisplay;
use PhpMyAdmin\Tests\AbstractTestCase;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;

use function function_exists;
use function gettype;

/**
 * @covers \PhpMyAdmin\Config\FormDisplay
 */
class FormDisplayTest extends AbstractTestCase
{
    /** @var FormDisplay */
    protected $object;

    /**
     * Configures global environment.
     */
    protected function setUp(): void
    {
        parent::setUp();
        parent::setTheme();
        parent::setGlobalConfig();
        $GLOBALS['server'] = 0;
        $this->object = new FormDisplay(new ConfigFile());
        Form::resetGroupCounter();
    }

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

    /**
     * Test for FormDisplay::registerForm
     *
     * @group medium
     */
    public function testRegisterForm(): void
    {
        $reflection = new ReflectionClass(FormDisplay::class);

        $attrForms = $reflection->getProperty('forms');
        $attrForms->setAccessible(true);

        $array = [
            'Servers' => [
                '1' => [
                    'test' => 1,
                    1 => ':group:end',
                ],
            ],
        ];

        $this->object->registerForm('pma_testform', $array, 2);
        $_forms = $attrForms->getValue($this->object);
        $this->assertInstanceOf(Form::class, $_forms['pma_testform']);

        $attrSystemPaths = $reflection->getProperty('systemPaths');
        $attrSystemPaths->setAccessible(true);

        $this->assertEquals(
            [
                'Servers/2/test' => 'Servers/1/test',
                'Servers/2/:group:end:0' => 'Servers/1/:group:end:0',
            ],
            $attrSystemPaths->getValue($this->object)
        );

        $attrTranslatedPaths = $reflection->getProperty('translatedPaths');
        $attrTranslatedPaths->setAccessible(true);

        $this->assertEquals(
            [
                'Servers/2/test' => 'Servers-2-test',
                'Servers/2/:group:end:0' => 'Servers-2-:group:end:0',
            ],
            $attrTranslatedPaths->getValue($this->object)
        );
    }

    /**
     * Test for FormDisplay::process
     *
     * @group medium
     */
    public function testProcess(): void
    {
        $this->assertFalse(
            $this->object->process(true, true)
        );

        $this->object = $this->getMockBuilder(FormDisplay::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['save'])
            ->getMock();

        $attrForms = new ReflectionProperty(FormDisplay::class, 'forms');
        $attrForms->setAccessible(true);
        $attrForms->setValue($this->object, [1, 2, 3]);

        $this->object->expects($this->once())
            ->method('save')
            ->with([0, 1, 2], false)
            ->will($this->returnValue(true));

        $this->assertTrue(
            $this->object->process(false, false)
        );

        $attrForms->setValue($this->object, []);

        $this->assertFalse(
            $this->object->process(false, false)
        );
    }

    /**
     * Test for FormDisplay::displayErrors
     */
    public function testDisplayErrors(): void
    {
        $reflection = new ReflectionClass(FormDisplay::class);

        $attrIsValidated = $reflection->getProperty('isValidated');
        $attrIsValidated->setAccessible(true);
        $attrIsValidated->setValue($this->object, true);

        $attrIsValidated = $reflection->getProperty('errors');
        $attrIsValidated->setAccessible(true);
        $attrIsValidated->setValue($this->object, []);

        $result = $this->object->displayErrors();

        $this->assertNull($result);

        $arr = [
            'Servers/1/test' => ['e1'],
            'foobar' => [
                'e2',
                'e3',
            ],
        ];

        $sysArr = ['Servers/1/test' => 'Servers/1/test2'];

        $attrSystemPaths = $reflection->getProperty('systemPaths');
        $attrSystemPaths->setAccessible(true);
        $attrSystemPaths->setValue($this->object, $sysArr);

        $attrIsValidated->setValue($this->object, $arr);

        $result = $this->object->displayErrors();

        $this->assertIsString($result);
        $this->assertStringContainsString('<dt>Servers/1/test2</dt>', $result);
        $this->assertStringContainsString('<dd>e1</dd>', $result);
        $this->assertStringContainsString('<dt>Form_foobar</dt>', $result);
        $this->assertStringContainsString('<dd>e2</dd>', $result);
        $this->assertStringContainsString('<dd>e3</dd>', $result);
    }

    /**
     * Test for FormDisplay::fixErrors
     */
    public function testFixErrors(): void
    {
        $reflection = new ReflectionClass(FormDisplay::class);

        $attrIsValidated = $reflection->getProperty('isValidated');
        $attrIsValidated->setAccessible(true);
        $attrIsValidated->setValue($this->object, true);

        $attrIsValidated = $reflection->getProperty('errors');
        $attrIsValidated->setAccessible(true);
        $attrIsValidated->setValue($this->object, []);

        $this->object->fixErrors();

        $arr = [
            'Servers/1/test' => ['e1'],
            'Servers/2/test' => [
                'e2',
                'e3',
            ],
            'Servers/3/test' => [],
        ];

        $sysArr = ['Servers/1/test' => 'Servers/1/host'];

        $attrSystemPaths = $reflection->getProperty('systemPaths');
        $attrSystemPaths->setAccessible(true);
        $attrSystemPaths->setValue($this->object, $sysArr);

        $attrIsValidated->setValue($this->object, $arr);

        $this->object->fixErrors();

        $this->assertEquals(
            [
                'Servers' => [
                    '1' => ['test' => 'localhost'],
                ],
            ],
            $_SESSION['ConfigFile0']
        );
    }

    /**
     * Test for FormDisplay::validateSelect
     */
    public function testValidateSelect(): void
    {
        $attrValidateSelect = new ReflectionMethod(FormDisplay::class, 'validateSelect');
        $attrValidateSelect->setAccessible(true);

        $arr = ['foo' => 'var'];
        $value = 'foo';
        $this->assertTrue(
            $attrValidateSelect->invokeArgs(
                $this->object,
                [
                    &$value,
                    $arr,
                ]
            )
        );

        $arr = ['' => 'foobar'];
        $value = null;
        $this->assertTrue(
            $attrValidateSelect->invokeArgs(
                $this->object,
                [
                    &$value,
                    $arr,
                ]
            )
        );
        $this->assertEquals(
            'string',
            gettype($value)
        );

        $arr = [0 => 'foobar'];
        $value = 0;
        $this->assertTrue(
            $attrValidateSelect->invokeArgs(
                $this->object,
                [
                    &$value,
                    $arr,
                ]
            )
        );

        $arr = ['1' => 'foobar'];
        $value = 0;
        $this->assertFalse(
            $attrValidateSelect->invokeArgs(
                $this->object,
                [
                    &$value,
                    $arr,
                ]
            )
        );
    }

    /**
     * Test for FormDisplay::hasErrors
     */
    public function testHasErrors(): void
    {
        $attrErrors = new ReflectionProperty(FormDisplay::class, 'errors');
        $attrErrors->setAccessible(true);

        $this->assertFalse(
            $this->object->hasErrors()
        );

        $attrErrors->setValue(
            $this->object,
            [
                1,
                2,
            ]
        );

        $this->assertTrue(
            $this->object->hasErrors()
        );
    }

    /**
     * Test for FormDisplay::getDocLink
     */
    public function testGetDocLink(): void
    {
        $this->assertEquals(
            'index.php?route=/url&url='
            . 'https%3A%2F%2Fdocs.phpmyadmin.net%2Fen%2Flatest%2Fconfig.html%23cfg_Servers_3_test_2_',
            $this->object->getDocLink('Servers/3/test/2/')
        );

        $this->assertEquals(
            '',
            $this->object->getDocLink('Import')
        );

        $this->assertEquals(
            '',
            $this->object->getDocLink('Export')
        );
    }

    /**
     * Test for FormDisplay::getOptName
     */
    public function testGetOptName(): void
    {
        $method = new ReflectionMethod(FormDisplay::class, 'getOptName');
        $method->setAccessible(true);

        $this->assertEquals(
            'Servers_',
            $method->invoke($this->object, 'Servers/1/')
        );

        $this->assertEquals(
            'Servers_23_',
            $method->invoke($this->object, 'Servers/1/23/')
        );
    }

    /**
     * Test for FormDisplay::loadUserprefsInfo
     */
    public function testLoadUserprefsInfo(): void
    {
        $method = new ReflectionMethod(FormDisplay::class, 'loadUserprefsInfo');
        $method->setAccessible(true);

        $attrUserprefs = new ReflectionProperty(FormDisplay::class, 'userprefsDisallow');

        $attrUserprefs->setAccessible(true);
        $method->invoke($this->object, null);
        $this->assertEquals(
            [],
            $attrUserprefs->getValue($this->object)
        );
    }

    /**
     * Test for FormDisplay::setComments
     */
    public function testSetComments(): void
    {
        $method = new ReflectionMethod(FormDisplay::class, 'setComments');
        $method->setAccessible(true);

        // recoding
        $opts = ['values' => []];
        $opts['values']['iconv'] = 'testIconv';
        $opts['values']['recode'] = 'testRecode';
        $opts['values']['mb'] = 'testMB';
        $opts['comment'] = null;
        $opts['comment_warning'] = null;

        $expect = $opts;

        $method->invokeArgs(
            $this->object,
            [
                'RecodingEngine',
                &$opts,
            ]
        );

        $expect['comment'] = '';
        if (! function_exists('iconv')) {
            $expect['values']['iconv'] .= ' (unavailable)';
            $expect['comment'] = '"iconv" requires iconv extension';
        }

        if (! function_exists('recode_string')) {
            $expect['values']['recode'] .= ' (unavailable)';
            $expect['comment'] .= ($expect['comment'] ? ', ' : '') .
                '"recode" requires recode extension';
        }

        $expect['comment_warning'] = 1;

        $this->assertEquals($expect, $opts);

        // ZipDump, GZipDump, BZipDump
        $method->invokeArgs(
            $this->object,
            [
                'ZipDump',
                &$opts,
            ]
        );

        $comment = '';
        if (! function_exists('zip_open')) {
            $comment = 'Compressed import will not work due to missing function zip_open.';
        }

        if (! function_exists('gzcompress')) {
            $comment .= ($comment ? '; ' : '') . 'Compressed export will not work ' .
            'due to missing function gzcompress.';
        }

        $this->assertEquals($comment, $opts['comment']);

        $this->assertTrue($opts['comment_warning']);

        $method->invokeArgs(
            $this->object,
            [
                'GZipDump',
                &$opts,
            ]
        );

        $comment = '';
        if (! function_exists('gzopen')) {
            $comment = 'Compressed import will not work due to missing function gzopen.';
        }

        if (! function_exists('gzencode')) {
            $comment .= ($comment ? '; ' : '') . 'Compressed export will not work ' .
            'due to missing function gzencode.';
        }

        $this->assertEquals($comment, $opts['comment']);

        $this->assertTrue($opts['comment_warning']);

        $method->invokeArgs(
            $this->object,
            [
                'BZipDump',
                &$opts,
            ]
        );

        $comment = '';
        if (! function_exists('bzopen')) {
            $comment = 'Compressed import will not work due to missing function bzopen.';
        }

        if (! function_exists('bzcompress')) {
            $comment .= ($comment ? '; ' : '') . 'Compressed export will not work ' .
            'due to missing function bzcompress.';
        }

        $this->assertEquals($comment, $opts['comment']);

        $this->assertTrue($opts['comment_warning']);

        $GLOBALS['config']->set('is_setup', false);

        $GLOBALS['cfg']['MaxDbList'] = 10;
        $GLOBALS['cfg']['MaxTableList'] = 10;
        $GLOBALS['cfg']['QueryHistoryMax'] = 10;

        $method->invokeArgs(
            $this->object,
            [
                'MaxDbList',
                &$opts,
            ]
        );

        $this->assertEquals('maximum 10', $opts['comment']);

        $method->invokeArgs(
            $this->object,
            [
                'MaxTableList',
                &$opts,
            ]
        );

        $this->assertEquals('maximum 10', $opts['comment']);

        $method->invokeArgs(
            $this->object,
            [
                'QueryHistoryMax',
                &$opts,
            ]
        );

        $this->assertEquals('maximum 10', $opts['comment']);
    }
}