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
;
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
27 * Test for ConfigGenerator::getConfigFile
30 public function testGetConfigFile(): void
32 unset($_SESSION['eol']);
34 parent
::setGlobalConfig();
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(
48 " * Generated configuration file\n" .
49 ' * Generated by: phpMyAdmin ' . Version
::VERSION
. " setup script\n",
53 $this->assertStringContainsString(
54 "/* Servers configuration */\n" .
56 "/* Server: localhost [0] */\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",
67 * Test for ConfigGenerator::getVarExport
69 public function testGetVarExport(): void
71 $reflection = new ReflectionClass(ConfigGenerator
::class);
72 $method = $reflection->getMethod('getVarExport');
75 '$cfg[\'var_name\'] = 1;' . "\n",
76 $method->invoke(null, 'var_name', 1, "\n"),
80 '$cfg[\'var_name\'] = array (' .
82 $method->invoke(null, 'var_name', [], "\n"),
86 '$cfg[\'var_name\'] = [1, 2, 3];' . "\n",
96 '$cfg[\'var_name\'][\'1a\'] = \'foo\';' . "\n" .
97 '$cfg[\'var_name\'][\'b\'] = \'bar\';' . "\n",
101 ['1a' => 'foo', 'b' => 'bar'],
107 public function testGetVarExportForBlowfishSecret(): void
109 $reflection = new ReflectionClass(ConfigGenerator
::class);
110 $method = $reflection->getMethod('getVarExport');
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');
140 ['a' => 1, 'b' => 2],
147 [0 => 1, 1 => 2, 3 => 3],
167 * Test for ConfigGenerator::exportZeroBasedArray
169 public function testExportZeroBasedArray(): void
171 $reflection = new ReflectionClass(ConfigGenerator
::class);
172 $method = $reflection->getMethod('exportZeroBasedArray');
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");