2013-06-03 15:34:57 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-07-10 11:40:48 +02:00
|
|
|
|
2013-06-03 15:34:57 +02:00
|
|
|
namespace Tests\Icinga\Application;
|
2013-07-10 11:40:48 +02:00
|
|
|
|
2014-04-10 10:32:50 +02:00
|
|
|
use Icinga\Test\BaseTestCase;
|
2014-11-06 15:41:31 +01:00
|
|
|
use Icinga\Application\Config;
|
2013-07-10 11:40:48 +02:00
|
|
|
|
2014-04-10 10:32:50 +02:00
|
|
|
class ConfigTest extends BaseTestCase
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
2013-08-12 15:02:25 +02:00
|
|
|
/**
|
|
|
|
* Set up config dir
|
|
|
|
*/
|
2013-07-10 11:40:48 +02:00
|
|
|
public function setUp()
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
2014-04-11 15:31:29 +02:00
|
|
|
parent::setUp();
|
2014-11-06 15:41:31 +01:00
|
|
|
$this->oldConfigDir = Config::$configDir;
|
|
|
|
Config::$configDir = dirname(__FILE__) . '/ConfigTest/files';
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 15:31:29 +02:00
|
|
|
/**
|
|
|
|
* Reset config dir
|
|
|
|
*/
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
2014-11-06 15:41:31 +01:00
|
|
|
Config::$configDir = $this->oldConfigDir;
|
2014-04-11 15:31:29 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherConfigIsCountable()
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => 'b', 'c' => array('d' => 'e')));
|
2014-11-06 15:41:31 +01:00
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertInstanceOf('Countable', $config, 'Config does not implement interface `Countable\'');
|
|
|
|
$this->assertEquals(2, count($config), 'Config does not count sections correctly');
|
2014-11-07 13:37:09 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherConfigIsTraversable()
|
2014-11-07 13:37:09 +01:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => array(), 'c' => array()));
|
|
|
|
$config->setSection('e');
|
2014-11-07 13:37:09 +01:00
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertInstanceOf('Iterator', $config, 'Config does not implement interface `Iterator\'');
|
2014-11-07 13:37:09 +01:00
|
|
|
|
|
|
|
$actual = array();
|
2014-11-18 13:02:56 +01:00
|
|
|
foreach ($config as $key => $_) {
|
|
|
|
$actual[] = $key;
|
2014-11-07 13:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-11-18 13:02:56 +01:00
|
|
|
array('a', 'c', 'e'),
|
2014-11-07 13:37:09 +01:00
|
|
|
$actual,
|
2014-11-18 13:02:56 +01:00
|
|
|
'Config does not iterate properly in the order its sections were inserted'
|
2014-11-07 13:37:09 +01:00
|
|
|
);
|
2014-11-06 15:41:31 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherOneCanCheckIfAConfigHasAnySections()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
|
|
|
$config = new Config();
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertTrue($config->isEmpty(), 'Config does not report that it is empty');
|
2014-11-06 15:41:31 +01:00
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$config->setSection('test');
|
|
|
|
$this->assertFalse($config->isEmpty(), 'Config does report that it is empty although it is not');
|
2014-11-06 15:41:31 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherItIsPossibleToRetrieveAllSectionNames()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => array('b' => 'c'), 'd' => array('e' => 'f')));
|
2014-11-06 15:41:31 +01:00
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
array('a', 'd'),
|
|
|
|
$config->keys(),
|
|
|
|
'Config::keys does not list section names correctly'
|
|
|
|
);
|
2014-11-06 15:41:31 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherConfigCanBeConvertedToAnArray()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => 'b', 'c' => array('d' => 'e')));
|
2014-11-06 15:41:31 +01:00
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
array('a' => 'b', 'c' => array('d' => 'e')),
|
|
|
|
$config->toArray(),
|
|
|
|
'Config::toArray does not return the correct array'
|
|
|
|
);
|
2014-11-06 15:41:31 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherItIsPossibleToDirectlyRetrieveASectionProperty()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => array('b' => 'c')));
|
2014-11-06 15:41:31 +01:00
|
|
|
|
2013-08-12 15:02:25 +02:00
|
|
|
$this->assertEquals(
|
2014-11-18 13:02:56 +01:00
|
|
|
'c',
|
|
|
|
$config->get('a', 'b'),
|
|
|
|
'Config::get does not return the actual value of a section\'s property'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
|
|
|
$this->assertNull(
|
2014-11-18 13:02:56 +01:00
|
|
|
$config->get('a', 'c'),
|
|
|
|
'Config::get does not return NULL as default for non-existent section properties'
|
|
|
|
);
|
|
|
|
$this->assertNull(
|
|
|
|
$config->get('b', 'c'),
|
|
|
|
'Config::get does not return NULL as default for non-existent sections'
|
2013-08-12 15:02:25 +02:00
|
|
|
);
|
2013-07-10 11:40:48 +02:00
|
|
|
$this->assertEquals(
|
2014-11-06 15:41:31 +01:00
|
|
|
'test',
|
2014-11-18 13:02:56 +01:00
|
|
|
$config->get('a', 'c', 'test'),
|
|
|
|
'Config::get does not return the given default value for non-existent section properties'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2014-11-18 13:02:56 +01:00
|
|
|
'c',
|
|
|
|
$config->get('a', 'b', 'test'),
|
|
|
|
'Config::get does not return the actual value of a section\'s property in case a default is given'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherConfigReturnsSingleSections()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => array('b' => 'c')));
|
2014-11-06 15:41:31 +01:00
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertInstanceOf(
|
|
|
|
'Icinga\Data\ConfigObject',
|
|
|
|
$config->getSection('a'),
|
|
|
|
'Config::getSection does not return a known section'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-18 13:02:56 +01:00
|
|
|
* @depends testWhetherConfigReturnsSingleSections
|
2014-11-06 15:41:31 +01:00
|
|
|
*/
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherConfigSetsSingleSections()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = new Config();
|
|
|
|
$config->setSection('a', array('b' => 'c'));
|
2014-11-06 15:41:31 +01:00
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertInstanceOf(
|
|
|
|
'Icinga\Data\ConfigObject',
|
|
|
|
$config->getSection('a'),
|
|
|
|
'Config::setSection does not set a new section'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
|
|
|
|
2014-11-18 13:02:56 +01:00
|
|
|
$config->setSection('a', array('bb' => 'cc'));
|
|
|
|
|
|
|
|
$this->assertNull(
|
|
|
|
$config->getSection('a')->b,
|
|
|
|
'Config::setSection does not overwrite existing sections'
|
|
|
|
);
|
2014-11-06 15:41:31 +01:00
|
|
|
$this->assertEquals(
|
2014-11-18 13:02:56 +01:00
|
|
|
'cc',
|
|
|
|
$config->getSection('a')->bb,
|
|
|
|
'Config::setSection does not overwrite existing sections'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-18 13:02:56 +01:00
|
|
|
* @depends testWhetherConfigIsCountable
|
2014-11-06 15:41:31 +01:00
|
|
|
*/
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherConfigRemovesSingleSections()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => array('b' => 'c'), 'd' => array('e' => 'f')));
|
|
|
|
$config->removeSection('a');
|
2014-11-06 15:41:31 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-11-18 13:02:56 +01:00
|
|
|
1,
|
|
|
|
$config->count(),
|
|
|
|
'Config::removeSection does not remove a known section'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
2014-11-18 13:02:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhetherConfigSetsSingleSections
|
|
|
|
*/
|
|
|
|
public function testWhetherConfigKnowsWhichSectionsItHas()
|
|
|
|
{
|
|
|
|
$config = new Config();
|
|
|
|
$config->setSection('a');
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$config->hasSection('a'),
|
|
|
|
'Config::hasSection does not know anything about its sections'
|
2014-11-06 15:41:31 +01:00
|
|
|
);
|
2014-11-18 13:02:56 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$config->hasSection('b'),
|
|
|
|
'Config::hasSection does not know anything about its sections'
|
2014-11-11 15:51:54 +01:00
|
|
|
);
|
2014-11-06 15:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException UnexpectedValueException
|
|
|
|
*/
|
|
|
|
public function testWhetherAnExceptionIsThrownWhenTryingToAccessASectionPropertyOnANonSection()
|
|
|
|
{
|
2014-11-18 13:02:56 +01:00
|
|
|
$config = Config::fromArray(array('a' => 'b'));
|
|
|
|
$config->get('a', 'b');
|
2014-11-06 15:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhetherConfigResolvePathReturnsValidAbsolutePaths()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
Config::$configDir . DIRECTORY_SEPARATOR . 'a' . DIRECTORY_SEPARATOR . 'b.ini',
|
|
|
|
Config::resolvePath(DIRECTORY_SEPARATOR . 'a' . DIRECTORY_SEPARATOR . 'b.ini'),
|
|
|
|
'Config::resolvePath does not produce valid absolute paths'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-18 13:02:56 +01:00
|
|
|
* @depends testWhetherConfigCanBeConvertedToAnArray
|
2014-11-06 15:41:31 +01:00
|
|
|
* @depends testWhetherConfigResolvePathReturnsValidAbsolutePaths
|
|
|
|
*/
|
2014-11-18 13:02:56 +01:00
|
|
|
public function testWhetherItIsPossibleToInitializeAConfigFromAIniFile()
|
2014-11-06 15:41:31 +01:00
|
|
|
{
|
|
|
|
$config = Config::fromIni(Config::resolvePath('config.ini'));
|
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2014-11-06 15:41:31 +01:00
|
|
|
'logging' => array(
|
|
|
|
'enable' => 1
|
|
|
|
),
|
|
|
|
'backend' => array(
|
|
|
|
'type' => 'db',
|
|
|
|
'user' => 'user',
|
|
|
|
'password' => 'password',
|
|
|
|
'disable' => 1
|
|
|
|
)
|
2013-07-10 11:40:48 +02:00
|
|
|
),
|
2014-11-06 15:41:31 +01:00
|
|
|
$config->toArray(),
|
|
|
|
'Config::fromIni does not load INI files correctly'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
get_class($config),
|
|
|
|
Config::fromIni('nichda'),
|
|
|
|
'Config::fromIni does not return empty configs for non-existent configuration files'
|
2013-07-10 11:40:48 +02:00
|
|
|
);
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-11-06 15:41:31 +01:00
|
|
|
/**
|
|
|
|
* @expectedException Icinga\Exception\NotReadableError
|
|
|
|
*/
|
|
|
|
public function testWhetherFromIniThrowsAnExceptionOnInsufficientPermission()
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
2014-11-06 15:41:31 +01:00
|
|
|
Config::fromIni('/etc/shadow');
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-11-06 15:41:31 +01:00
|
|
|
/**
|
2014-11-18 13:02:56 +01:00
|
|
|
* @depends testWhetherItIsPossibleToInitializeAConfigFromAIniFile
|
2014-11-06 15:41:31 +01:00
|
|
|
*/
|
|
|
|
public function testWhetherItIsPossibleToRetrieveApplicationConfiguration()
|
2013-06-03 15:34:57 +02:00
|
|
|
{
|
2014-11-06 15:41:31 +01:00
|
|
|
$config = Config::app();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'logging' => array(
|
|
|
|
'enable' => 1
|
|
|
|
),
|
|
|
|
'backend' => array(
|
|
|
|
'type' => 'db',
|
|
|
|
'user' => 'user',
|
|
|
|
'password' => 'password',
|
|
|
|
'disable' => 1
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$config->toArray(),
|
|
|
|
'Config::app does not load INI files correctly'
|
|
|
|
);
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-11-06 15:41:31 +01:00
|
|
|
/**
|
2014-11-18 13:02:56 +01:00
|
|
|
* @depends testWhetherItIsPossibleToInitializeAConfigFromAIniFile
|
2014-11-06 15:41:31 +01:00
|
|
|
*/
|
|
|
|
public function testWhetherItIsPossibleToRetrieveModuleConfiguration()
|
2013-07-10 11:40:48 +02:00
|
|
|
{
|
2014-11-06 15:41:31 +01:00
|
|
|
$config = Config::module('amodule');
|
|
|
|
|
2013-07-10 11:40:48 +02:00
|
|
|
$this->assertEquals(
|
2014-11-06 15:41:31 +01:00
|
|
|
array(
|
|
|
|
'menu' => array(
|
|
|
|
'breadcrumb' => 1
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$config->toArray(),
|
|
|
|
'Config::module does not load INI files correctly'
|
2013-07-10 11:40:48 +02:00
|
|
|
);
|
|
|
|
}
|
2013-06-03 15:34:57 +02:00
|
|
|
}
|