Skip to content

Commit

Permalink
Add test cases for config file loading and removing keys having a '/'…
Browse files Browse the repository at this point in the history
… in the name

Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Apr 20, 2021
1 parent 6d0e4b5 commit f8ebd7a
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions test/classes/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function constant;
use function define;
use function defined;
use function file_put_contents;
use function filemtime;
use function fileperms;
use function function_exists;
Expand All @@ -27,9 +28,12 @@
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
Expand Down Expand Up @@ -78,6 +82,90 @@ protected function tearDown(): void
unset($this->permTestObj);
}

/**
* 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
*
Expand Down

0 comments on commit f8ebd7a

Please sign in to comment.