2015-10-22 20:51:01 +02:00
|
|
|
<?php
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2016-08-09 11:54:42 +02:00
|
|
|
use PrivateBin\Configuration;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2016-08-09 11:54:42 +02:00
|
|
|
class ConfigurationTest extends PHPUnit_Framework_TestCase
|
2015-10-22 20:51:01 +02:00
|
|
|
{
|
2016-07-11 14:15:20 +02:00
|
|
|
private $_options;
|
2015-10-22 20:51:01 +02:00
|
|
|
|
2016-07-13 09:41:45 +02:00
|
|
|
private $_minimalConfig;
|
|
|
|
|
2015-10-22 20:51:01 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::confBackup();
|
2016-07-11 14:15:20 +02:00
|
|
|
$this->_options = configuration::getDefaults();
|
|
|
|
$this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
|
|
|
|
$this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
|
2016-07-15 17:02:59 +02:00
|
|
|
$this->_options['purge']['dir'] = PATH . $this->_options['purge']['dir'];
|
2016-07-13 09:41:45 +02:00
|
|
|
$this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
|
2015-10-22 20:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::confRestore();
|
2015-10-22 20:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultConfigFile()
|
|
|
|
{
|
|
|
|
$this->assertTrue(copy(CONF . '.bak', CONF), 'copy default configuration file');
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-22 20:51:01 +02:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleFreshConfigFile()
|
|
|
|
{
|
2016-08-22 16:20:14 +02:00
|
|
|
Helper::createIniFile(CONF, $this->_options);
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-22 20:51:01 +02:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
|
|
|
|
}
|
|
|
|
|
2015-10-22 21:13:15 +02:00
|
|
|
public function testHandleMissingConfigFile()
|
|
|
|
{
|
|
|
|
@unlink(CONF);
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-22 21:13:15 +02:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
|
|
|
|
}
|
|
|
|
|
2015-10-22 20:51:01 +02:00
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 2
|
|
|
|
*/
|
|
|
|
public function testHandleBlankConfigFile()
|
|
|
|
{
|
|
|
|
file_put_contents(CONF, '');
|
2016-08-09 11:54:42 +02:00
|
|
|
new Configuration;
|
2015-10-22 20:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleMinimalConfigFile()
|
|
|
|
{
|
2016-07-13 09:41:45 +02:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-22 20:51:01 +02:00
|
|
|
$this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 3
|
|
|
|
*/
|
|
|
|
public function testHandleInvalidSection()
|
|
|
|
{
|
2016-07-13 09:41:45 +02:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-22 20:51:01 +02:00
|
|
|
$conf->getKey('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionCode 4
|
|
|
|
*/
|
|
|
|
public function testHandleInvalidKey()
|
|
|
|
{
|
2016-07-13 09:41:45 +02:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-22 20:51:01 +02:00
|
|
|
$conf->getKey('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleGetKey()
|
|
|
|
{
|
2016-07-13 09:41:45 +02:00
|
|
|
file_put_contents(CONF, $this->_minimalConfig);
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-22 20:51:01 +02:00
|
|
|
$this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHandleWrongTypes()
|
|
|
|
{
|
2015-10-24 08:44:17 +02:00
|
|
|
$original_options = $this->_options;
|
|
|
|
$original_options['main']['syntaxhighlightingtheme'] = 'foo';
|
|
|
|
$options = $original_options;
|
2015-10-22 20:51:01 +02:00
|
|
|
$options['main']['discussion'] = 'true';
|
|
|
|
$options['main']['opendiscussion'] = 0;
|
|
|
|
$options['main']['password'] = -1; // evaluates to TRUE
|
|
|
|
$options['main']['fileupload'] = 'false';
|
|
|
|
$options['expire_options']['foo'] = 'bar';
|
|
|
|
$options['formatter_options'][] = 'foo';
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$conf = new Configuration;
|
2015-10-24 08:44:17 +02:00
|
|
|
$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']);
|
2016-08-22 16:20:14 +02:00
|
|
|
Helper::createIniFile(CONF, $options);
|
2016-08-09 11:54:42 +02:00
|
|
|
$conf = new Configuration;
|
2015-10-24 08:44:17 +02:00
|
|
|
$options['expire']['default'] = '5min';
|
|
|
|
$this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
|
2015-10-22 20:51:01 +02:00
|
|
|
}
|
2015-10-24 08:44:17 +02:00
|
|
|
|
2016-07-13 09:41:45 +02:00
|
|
|
public function testHandlePreRenameConfig()
|
|
|
|
{
|
|
|
|
$options = $this->_options;
|
|
|
|
$options['model']['class'] = 'zerobin_data';
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$conf = new Configuration;
|
|
|
|
$this->assertEquals('Filesystem', $conf->getKey('class', 'model'), 'old data class gets renamed');
|
2016-07-13 09:41:45 +02:00
|
|
|
|
|
|
|
$options['model']['class'] = 'zerobin_db';
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$conf = new Configuration;
|
|
|
|
$this->assertEquals('Database', $conf->getKey('class', 'model'), 'old db class gets renamed');
|
2016-07-13 09:41:45 +02:00
|
|
|
}
|
2015-10-22 20:51:01 +02:00
|
|
|
}
|