expire_options and formatter_options should not be filled up with

default values, resolves #52
This commit is contained in:
El RIDO 2015-10-24 08:44:17 +02:00
parent 176dff3b70
commit d42975580a
3 changed files with 84 additions and 41 deletions

1
cfg/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/conf.ini

View File

@ -95,11 +95,13 @@ class configuration
} }
} }
} }
$opts = '_options';
foreach ($this->_defaults as $section => $values) 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])) if (array_key_exists('dir', $this->_configuration[$section]))
{ {
$this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir']; $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
@ -117,6 +119,23 @@ class configuration
'opt' => array(PDO::ATTR_PERSISTENT => true), 'opt' => array(PDO::ATTR_PERSISTENT => true),
); );
} }
// "*_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 (is_int(current($values)))
{
$config[$section] = array_map('intval', $config[$section]);
}
$this->_configuration[$section] = $config[$section];
}
// check for missing keys and set defaults if necessary
else
{
foreach ($values as $key => $val) foreach ($values as $key => $val)
{ {
if ($key == 'dir') if ($key == 'dir')
@ -160,6 +179,13 @@ class configuration
} }
} }
// 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']);
}
}
/** /**
* get configuration as array * get configuration as array
* *

View File

@ -130,8 +130,9 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function testHandleWrongTypes() public function testHandleWrongTypes()
{ {
$this->_options['main']['syntaxhighlightingtheme'] = 'foo'; $original_options = $this->_options;
$options = $this->_options; $original_options['main']['syntaxhighlightingtheme'] = 'foo';
$options = $original_options;
$options['main']['discussion'] = 'true'; $options['main']['discussion'] = 'true';
$options['main']['opendiscussion'] = 0; $options['main']['opendiscussion'] = 0;
$options['main']['password'] = -1; // evaluates to TRUE $options['main']['password'] = -1; // evaluates to TRUE
@ -140,6 +141,21 @@ class configurationTest extends PHPUnit_Framework_TestCase
$options['formatter_options'][] = 'foo'; $options['formatter_options'][] = 'foo';
helper::createIniFile(CONF, $options); helper::createIniFile(CONF, $options);
$conf = new configuration; $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');
}
} }