fixing support for pre renaming configuration file format, resolves #37

pull/44/head
El RIDO 2016-07-13 09:41:45 +02:00
parent 8f2d43722b
commit 4d10fd9690
5 changed files with 85 additions and 12 deletions

View File

@ -90,7 +90,7 @@ class configuration
if (is_readable($configFile))
{
$config = parse_ini_file($configFile, true);
foreach (array('main', 'model') as $section) {
foreach (array('main', 'model', 'model_options') as $section) {
if (!array_key_exists($section, $config)) {
throw new Exception(i18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
}
@ -110,7 +110,12 @@ class configuration
continue;
}
// provide different defaults for database model
elseif ($section == 'model_options' && $this->_configuration['model']['class'] == 'privatebin_db')
elseif (
$section == 'model_options' && in_array(
$this->_configuration['model']['class'],
array('privatebin_db', 'zerobin_db')
)
)
{
$values = array(
'dsn' => 'sqlite:' . PATH . 'data/db.sq3',
@ -180,6 +185,12 @@ class configuration
}
}
// support for old config file format, before the fork was renamed
$this->_configuration['model']['class'] = str_replace(
'zerobin_', 'privatebin_',
$this->_configuration['model']['class']
);
// ensure a valid expire default key is set
if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options']))
{

View File

@ -61,13 +61,8 @@ class model
{
if ($this->_store === null)
{
// added option to support old config file format
$model = str_replace(
'zerobin_', 'privatebin_',
$this->_conf->getKey('class', 'model')
);
$this->_store = forward_static_call(
array($model, 'getInstance'),
array($this->_conf->getKey('class', 'model'), 'getInstance'),
$this->_conf->getSection('model_options')
);
}

View File

@ -134,6 +134,12 @@ class privatebin_db extends privatebin_abstract
self::_upgradeDatabase($db_version);
}
}
else
{
throw new Exception(
'Missing configuration for key dsn, usr, pwd or opt in the section model_options, please check your configuration file', 6
);
}
}
return self::$_instance;

View File

@ -3,6 +3,8 @@ class configurationTest extends PHPUnit_Framework_TestCase
{
private $_options;
private $_minimalConfig;
public function setUp()
{
/* Setup Routine */
@ -10,6 +12,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
$this->_options = configuration::getDefaults();
$this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
$this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
$this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
}
public function tearDown()
@ -51,7 +54,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
public function testHandleMinimalConfigFile()
{
file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
}
@ -62,7 +65,7 @@ class configurationTest extends PHPUnit_Framework_TestCase
*/
public function testHandleInvalidSection()
{
file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$conf->getKey('foo', 'bar');
}
@ -73,14 +76,14 @@ class configurationTest extends PHPUnit_Framework_TestCase
*/
public function testHandleInvalidKey()
{
file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$conf->getKey('foo');
}
public function testHandleGetKey()
{
file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
file_put_contents(CONF, $this->_minimalConfig);
$conf = new configuration;
$this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
}
@ -115,4 +118,18 @@ class configurationTest extends PHPUnit_Framework_TestCase
$this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
}
public function testHandlePreRenameConfig()
{
$options = $this->_options;
$options['model']['class'] = 'zerobin_data';
helper::createIniFile(CONF, $options);
$conf = new configuration;
$this->assertEquals('privatebin_data', $conf->getKey('class', 'model'), 'old data class gets renamed');
$options['model']['class'] = 'zerobin_db';
helper::createIniFile(CONF, $options);
$conf = new configuration;
$this->assertEquals('privatebin_db', $conf->getKey('class', 'model'), 'old db class gets renamed');
}
}

View File

@ -139,6 +139,50 @@ class privatebin_dbTest extends PHPUnit_Framework_TestCase
));
}
/**
* @expectedException Exception
* @expectedExceptionCode 6
*/
public function testMissingDsn()
{
$options = $this->_options;
unset($options['dsn']);
privatebin_db::getInstance($options);
}
/**
* @expectedException Exception
* @expectedExceptionCode 6
*/
public function testMissingUsr()
{
$options = $this->_options;
unset($options['usr']);
privatebin_db::getInstance($options);
}
/**
* @expectedException Exception
* @expectedExceptionCode 6
*/
public function testMissingPwd()
{
$options = $this->_options;
unset($options['pwd']);
privatebin_db::getInstance($options);
}
/**
* @expectedException Exception
* @expectedExceptionCode 6
*/
public function testMissingOpt()
{
$options = $this->_options;
unset($options['opt']);
privatebin_db::getInstance($options);
}
public function testTableUpgrade()
{
$path = PATH . 'data/db-test.sq3';