Replace text_dir global var with LanguageManager::$textDir static
[phpmyadmin.git] / tests / classes / Setup / ConfigGeneratorTest.php
blob3352e6359e3d811b72eda6828189e22452c64add
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests\Setup;
7 use PhpMyAdmin\Config\ConfigFile;
8 use PhpMyAdmin\Current;
9 use PhpMyAdmin\Setup\ConfigGenerator;
10 use PhpMyAdmin\Tests\AbstractTestCase;
11 use PhpMyAdmin\Version;
12 use PHPUnit\Framework\Attributes\CoversClass;
13 use PHPUnit\Framework\Attributes\Group;
14 use ReflectionClass;
16 use function explode;
17 use function hex2bin;
18 use function mb_strlen;
19 use function str_repeat;
21 use const SODIUM_CRYPTO_SECRETBOX_KEYBYTES;
23 #[CoversClass(ConfigGenerator::class)]
24 class ConfigGeneratorTest extends AbstractTestCase
26 /**
27 * Test for ConfigGenerator::getConfigFile
29 #[Group('medium')]
30 public function testGetConfigFile(): void
32 unset($_SESSION['eol']);
34 parent::setGlobalConfig();
36 Current::$server = 2;
37 $cf = new ConfigFile();
38 $_SESSION['ConfigFile2'] = ['a', 'b', 'c'];
39 $_SESSION['ConfigFile2']['Servers'] = [[1, 2, 3]];
41 $cf->setPersistKeys(['1/', 2]);
43 $result = ConfigGenerator::getConfigFile($cf);
45 $this->assertStringContainsString(
46 "<?php\n" .
47 "/**\n" .
48 " * Generated configuration file\n" .
49 ' * Generated by: phpMyAdmin ' . Version::VERSION . " setup script\n",
50 $result,
53 $this->assertStringContainsString(
54 "/* Servers configuration */\n" .
55 '$i = 0;' . "\n\n" .
56 "/* Server: localhost [0] */\n" .
57 '$i++;' . "\n" .
58 '$cfg[\'Servers\'][$i][\'0\'] = 1;' . "\n" .
59 '$cfg[\'Servers\'][$i][\'1\'] = 2;' . "\n" .
60 '$cfg[\'Servers\'][$i][\'2\'] = 3;' . "\n\n" .
61 "/* End of servers configuration */\n\n",
62 $result,
66 /**
67 * Test for ConfigGenerator::getVarExport
69 public function testGetVarExport(): void
71 $reflection = new ReflectionClass(ConfigGenerator::class);
72 $method = $reflection->getMethod('getVarExport');
74 $this->assertEquals(
75 '$cfg[\'var_name\'] = 1;' . "\n",
76 $method->invoke(null, 'var_name', 1, "\n"),
79 $this->assertEquals(
80 '$cfg[\'var_name\'] = array (' .
81 "\n);\n",
82 $method->invoke(null, 'var_name', [], "\n"),
85 $this->assertEquals(
86 '$cfg[\'var_name\'] = [1, 2, 3];' . "\n",
87 $method->invoke(
88 null,
89 'var_name',
90 [1, 2, 3],
91 "\n",
95 $this->assertEquals(
96 '$cfg[\'var_name\'][\'1a\'] = \'foo\';' . "\n" .
97 '$cfg[\'var_name\'][\'b\'] = \'bar\';' . "\n",
98 $method->invoke(
99 null,
100 'var_name',
101 ['1a' => 'foo', 'b' => 'bar'],
102 "\n",
107 public function testGetVarExportForBlowfishSecret(): void
109 $reflection = new ReflectionClass(ConfigGenerator::class);
110 $method = $reflection->getMethod('getVarExport');
112 $this->assertEquals(
113 '$cfg[\'blowfish_secret\'] = \sodium_hex2bin(\''
114 . '6161616161616161616161616161616161616161616161616161616161616161\');' . "\n",
115 $method->invoke(null, 'blowfish_secret', str_repeat('a', SODIUM_CRYPTO_SECRETBOX_KEYBYTES), "\n"),
118 /** @var string $actual */
119 $actual = $method->invoke(null, 'blowfish_secret', 'invalid secret', "\n");
120 $this->assertStringStartsWith('$cfg[\'blowfish_secret\'] = \sodium_hex2bin(\'', $actual);
121 $this->assertStringEndsWith('\');' . "\n", $actual);
122 $pieces = explode('\'', $actual);
123 $this->assertCount(5, $pieces);
124 $binaryString = hex2bin($pieces[3]);
125 $this->assertIsString($binaryString);
126 $this->assertSame(SODIUM_CRYPTO_SECRETBOX_KEYBYTES, mb_strlen($binaryString, '8bit'));
130 * Test for ConfigGenerator::isZeroBasedArray
132 public function testIsZeroBasedArray(): void
134 $reflection = new ReflectionClass(ConfigGenerator::class);
135 $method = $reflection->getMethod('isZeroBasedArray');
137 $this->assertFalse(
138 $method->invoke(
139 null,
140 ['a' => 1, 'b' => 2],
144 $this->assertFalse(
145 $method->invoke(
146 null,
147 [0 => 1, 1 => 2, 3 => 3],
151 $this->assertTrue(
152 $method->invoke(
153 null,
158 $this->assertTrue(
159 $method->invoke(
160 null,
161 [1, 2, 3],
167 * Test for ConfigGenerator::exportZeroBasedArray
169 public function testExportZeroBasedArray(): void
171 $reflection = new ReflectionClass(ConfigGenerator::class);
172 $method = $reflection->getMethod('exportZeroBasedArray');
174 $arr = [1, 2, 3, 4];
176 $result = $method->invoke(null, $arr, "\n");
178 $this->assertEquals('[1, 2, 3, 4]', $result);
180 $arr = [1, 2, 3, 4, 7, 'foo'];
182 $result = $method->invoke(null, $arr, "\n");
184 $this->assertEquals(
185 '[' . "\n" .
186 ' 1,' . "\n" .
187 ' 2,' . "\n" .
188 ' 3,' . "\n" .
189 ' 4,' . "\n" .
190 ' 7,' . "\n" .
191 ' \'foo\']',
192 $result,