From d42975580af549ae6b3d233d0e8818c4dc06cb00 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sat, 24 Oct 2015 08:44:17 +0200 Subject: [PATCH] expire_options and formatter_options should not be filled up with default values, resolves #52 --- cfg/.gitignore | 1 + lib/configuration.php | 102 ++++++++++++++++++++++++++---------------- tst/configuration.php | 22 +++++++-- 3 files changed, 84 insertions(+), 41 deletions(-) create mode 100644 cfg/.gitignore diff --git a/cfg/.gitignore b/cfg/.gitignore new file mode 100644 index 0000000..69db020 --- /dev/null +++ b/cfg/.gitignore @@ -0,0 +1 @@ +/conf.ini diff --git a/lib/configuration.php b/lib/configuration.php index da2dd4b..91fbb30 100644 --- a/lib/configuration.php +++ b/lib/configuration.php @@ -95,11 +95,13 @@ class configuration } } } + $opts = '_options'; foreach ($this->_defaults as $section => $values) { - if (!array_key_exists($section, $config)) + // fill missing sections with default values + if (!array_key_exists($section, $config) || count($config[$section]) == 0) { - $this->_configuration[$section] = $this->_defaults[$section]; + $this->_configuration[$section] = $values; if (array_key_exists('dir', $this->_configuration[$section])) { $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir']; @@ -117,46 +119,70 @@ class configuration 'opt' => array(PDO::ATTR_PERSISTENT => true), ); } - foreach ($values as $key => $val) + + // "*_options" sections don't require all defaults to be set + if ( + $section !== 'model_options' && + ($from = strlen($section) - strlen($opts)) >= 0 && + strpos($section, $opts, $from) !== false + ) { - if ($key == 'dir') + if (is_int(current($values))) { - $val = PATH . $val; + $config[$section] = array_map('intval', $config[$section]); } - $result = $val; - if (array_key_exists($key, $config[$section])) - { - if ($val === null) - { - $result = $config[$section][$key]; - } - elseif (is_bool($val)) - { - $val = strtolower($config[$section][$key]); - if (in_array($val, array('true', 'yes', 'on'))) - { - $result = true; - } - elseif (in_array($val, array('false', 'no', 'off'))) - { - $result = false; - } - else - { - $result = (bool) $config[$section][$key]; - } - } - elseif (is_int($val)) - { - $result = (int) $config[$section][$key]; - } - elseif (is_string($val) && !empty($config[$section][$key])) - { - $result = (string) $config[$section][$key]; - } - } - $this->_configuration[$section][$key] = $result; + $this->_configuration[$section] = $config[$section]; } + // check for missing keys and set defaults if necessary + else + { + foreach ($values as $key => $val) + { + if ($key == 'dir') + { + $val = PATH . $val; + } + $result = $val; + if (array_key_exists($key, $config[$section])) + { + if ($val === null) + { + $result = $config[$section][$key]; + } + elseif (is_bool($val)) + { + $val = strtolower($config[$section][$key]); + if (in_array($val, array('true', 'yes', 'on'))) + { + $result = true; + } + elseif (in_array($val, array('false', 'no', 'off'))) + { + $result = false; + } + else + { + $result = (bool) $config[$section][$key]; + } + } + elseif (is_int($val)) + { + $result = (int) $config[$section][$key]; + } + elseif (is_string($val) && !empty($config[$section][$key])) + { + $result = (string) $config[$section][$key]; + } + } + $this->_configuration[$section][$key] = $result; + } + } + } + + // ensure a valid expire default key is set + if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) + { + $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']); } } diff --git a/tst/configuration.php b/tst/configuration.php index 13e3b0b..93cb957 100644 --- a/tst/configuration.php +++ b/tst/configuration.php @@ -130,8 +130,9 @@ class configurationTest extends PHPUnit_Framework_TestCase public function testHandleWrongTypes() { - $this->_options['main']['syntaxhighlightingtheme'] = 'foo'; - $options = $this->_options; + $original_options = $this->_options; + $original_options['main']['syntaxhighlightingtheme'] = 'foo'; + $options = $original_options; $options['main']['discussion'] = 'true'; $options['main']['opendiscussion'] = 0; $options['main']['password'] = -1; // evaluates to TRUE @@ -140,6 +141,21 @@ class configurationTest extends PHPUnit_Framework_TestCase $options['formatter_options'][] = 'foo'; helper::createIniFile(CONF, $options); $conf = new configuration; - $this->assertEquals($this->_options, $conf->get(), 'incorrect types are corrected'); + $original_options['expire_options']['foo'] = intval('bar'); + $original_options['formatter_options'][0] = 'foo'; + $this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected'); } + + public function testHandleMissingSubKeys() + { + $options = $this->_options; + unset($options['expire_options']['1week']); + unset($options['expire_options']['1year']); + unset($options['expire_options']['never']); + helper::createIniFile(CONF, $options); + $conf = new configuration; + $options['expire']['default'] = '5min'; + $this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys'); + } + }