diff options
author | William Desportes <williamdes@wdes.fr> | 2021-04-20 22:47:38 +0300 |
---|---|---|
committer | William Desportes <williamdes@wdes.fr> | 2021-04-20 22:50:33 +0300 |
commit | f8ebd7ad7b3759a7a44ec0dbb313f1b8d47b4238 (patch) | |
tree | b588ee97ac7ec65a3cea87d319ec42c619573c2d /test | |
parent | 6d0e4b546ebc190667be4bc4d5a46af851cffc46 (diff) |
Add test cases for config file loading and removing keys having a '/' in the name
Signed-off-by: William Desportes <williamdes@wdes.fr>
Diffstat (limited to 'test')
-rw-r--r-- | test/classes/ConfigTest.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/test/classes/ConfigTest.php b/test/classes/ConfigTest.php index fa3e012b47..17c4424e81 100644 --- a/test/classes/ConfigTest.php +++ b/test/classes/ConfigTest.php @@ -13,6 +13,7 @@ use function array_replace_recursive; use function constant; use function define; use function defined; +use function file_put_contents; use function filemtime; use function fileperms; use function function_exists; @@ -27,9 +28,12 @@ use function realpath; use function strip_tags; use function stristr; use function sys_get_temp_dir; +use function tempnam; +use function unlink; use const DIRECTORY_SEPARATOR; use const INFO_MODULES; +use const PHP_EOL; use const PHP_OS; class ConfigTest extends AbstractTestCase @@ -79,6 +83,90 @@ class ConfigTest extends AbstractTestCase } /** + * Test for load + */ + public function testLoadConfigs(): void + { + $defaultConfig = new Config(); + $tmpConfig = tempnam('./', 'config.test.inc.php'); + if ($tmpConfig === false) { + $this->markTestSkipped('Creating a temporary file does not work'); + } + + $this->assertFileExists($tmpConfig); + + // end of setup + + // Test loading an empty file does not change the default config + $config = new Config($tmpConfig); + $this->assertSame($defaultConfig->settings, $config->settings); + + $contents = '<?php' . PHP_EOL + . '$cfg[\'ProtectBinary\'] = true;'; + file_put_contents($tmpConfig, $contents); + + // Test loading a config changes the setup + $config = new Config($tmpConfig); + $defaultConfig->settings['ProtectBinary'] = true; + $this->assertSame($defaultConfig->settings, $config->settings); + $defaultConfig->settings['ProtectBinary'] = 'blob'; + + // Teardown + unlink($tmpConfig); + $this->assertFileDoesNotExist($tmpConfig); + } + + /** + * Test for load + */ + public function testLoadInvalidConfigs(): void + { + $defaultConfig = new Config(); + $tmpConfig = tempnam('./', 'config.test.inc.php'); + if ($tmpConfig === false) { + $this->markTestSkipped('Creating a temporary file does not work'); + } + + $this->assertFileExists($tmpConfig); + + // end of setup + + // Test loading an empty file does not change the default config + $config = new Config($tmpConfig); + $this->assertSame($defaultConfig->settings, $config->settings); + + $contents = '<?php' . PHP_EOL + . '$cfg[\'fooBar\'] = true;'; + file_put_contents($tmpConfig, $contents); + + // Test loading a custom key config changes the setup + $config = new Config($tmpConfig); + $defaultConfig->settings['fooBar'] = true; + // Equals because of the key sorting + $this->assertEquals($defaultConfig->settings, $config->settings); + unset($defaultConfig->settings['fooBar']); + + $contents = '<?php' . PHP_EOL + . '$cfg[\'/InValidKey\'] = true;' . PHP_EOL + . '$cfg[\'In/ValidKey\'] = true;' . PHP_EOL + . '$cfg[\'/InValid/Key\'] = true;' . PHP_EOL + . '$cfg[\'In/Valid/Key\'] = true;' . PHP_EOL + . '$cfg[\'ValidKey\'] = true;'; + file_put_contents($tmpConfig, $contents); + + // Test loading a custom key config changes the setup + $config = new Config($tmpConfig); + $defaultConfig->settings['ValidKey'] = true; + // Equals because of the key sorting + $this->assertEquals($defaultConfig->settings, $config->settings); + unset($defaultConfig->settings['ValidKey']); + + // Teardown + unlink($tmpConfig); + $this->assertFileDoesNotExist($tmpConfig); + } + + /** * Test for CheckSystem * * @group medium |