mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
StateFilterTest: a couple of tests for state filters
This commit is contained in:
parent
a9c63cf325
commit
a80aed49a1
171
test/php/library/Director/IcingaConfig/StateFilterTest.php
Normal file
171
test/php/library/Director/IcingaConfig/StateFilterTest.php
Normal file
@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Icinga\Module\Director\IcingaConfig;
|
||||
|
||||
use Icinga\Module\Director\IcingaConfig\StateFilterSet;
|
||||
use Icinga\Module\Director\Objects\IcingaUser;
|
||||
use Icinga\Module\Director\Test\BaseTestCase;
|
||||
|
||||
class StateFilterSetTest extends BaseTestCase
|
||||
{
|
||||
protected $testUserName1 = '__testuser2';
|
||||
|
||||
protected $testUserName2 = '__testuser2';
|
||||
|
||||
public function testIsEmptyForAnUnstoredUser()
|
||||
{
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
StateFilterSet::forIcingaObject(
|
||||
IcingaUser::create(),
|
||||
'states'
|
||||
)->getResolvedValues()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Icinga\Exception\InvalidPropertyException
|
||||
*/
|
||||
public function testFailsForInvalidProperties()
|
||||
{
|
||||
$set = new StateFilterSet('bla');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Icinga\Exception\ProgrammingError
|
||||
*/
|
||||
public function testCannotBeStoredForAnUnstoredUser()
|
||||
{
|
||||
StateFilterSet::forIcingaObject(
|
||||
$this->user1(),
|
||||
'states'
|
||||
)->override(
|
||||
array('OK', 'Down')
|
||||
)->store();
|
||||
}
|
||||
|
||||
public function testCanBeStored()
|
||||
{
|
||||
if ($this->skipForMissingDb()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$states = $this->simpleUnstoredSetForStoredUser();
|
||||
|
||||
$this->assertTrue($states->store());
|
||||
$states->getObject()->delete();
|
||||
}
|
||||
|
||||
public function testWillNotBeStoredTwice()
|
||||
{
|
||||
if ($this->skipForMissingDb()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$states = $this->simpleUnstoredSetForStoredUser();
|
||||
|
||||
$this->assertTrue($states->store());
|
||||
$this->assertFalse($states->store());
|
||||
$this->assertFalse($states->store());
|
||||
$states->getObject()->delete();
|
||||
}
|
||||
|
||||
public function testComplexDefinitionsCanBeStored()
|
||||
{
|
||||
if ($this->skipForMissingDb()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$states = $this->complexUnstoredSetForStoredUser();
|
||||
|
||||
$this->assertTrue($states->store());
|
||||
$states->getObject()->delete();
|
||||
}
|
||||
|
||||
public function testComplexDefinitionsCanBeLoadedAndRenderCorrectly()
|
||||
{
|
||||
if ($this->skipForMissingDb()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$states = $this->complexUnstoredSetForStoredUser();
|
||||
$user = $states->getObject();
|
||||
|
||||
$this->assertTrue($states->store());
|
||||
|
||||
$states = StateFilterSet::forIcingaObject($user, 'states');
|
||||
$expected = ' states = [ Down, OK, Up ]' . "\n"
|
||||
. ' states += [ Warning ]' . "\n"
|
||||
. ' states -= [ Up ]' . "\n";
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$states->renderAs('states')
|
||||
);
|
||||
|
||||
$states->getObject()->delete();
|
||||
}
|
||||
|
||||
protected function simpleUnstoredSetForStoredUser()
|
||||
{
|
||||
$user = $this->user1();
|
||||
$user->store($this->getDb());
|
||||
|
||||
$states = StateFilterSet::forIcingaObject(
|
||||
$user,
|
||||
'states'
|
||||
)->override(
|
||||
array('OK', 'Down')
|
||||
);
|
||||
|
||||
return $states;
|
||||
}
|
||||
|
||||
protected function complexUnstoredSetForStoredUser()
|
||||
{
|
||||
$user = $this->user2();
|
||||
$user->store($this->getDb());
|
||||
|
||||
$states = StateFilterSet::forIcingaObject(
|
||||
$user,
|
||||
'states'
|
||||
)->override(
|
||||
array('OK', 'Down', 'Up')
|
||||
)->blacklist('Up')->extend('Warning');
|
||||
|
||||
return $states;
|
||||
}
|
||||
|
||||
protected function user1()
|
||||
{
|
||||
return IcingaUser::create(array(
|
||||
'object_type' => 'object',
|
||||
'object_name' => $this->testUserName1
|
||||
));
|
||||
}
|
||||
|
||||
protected function user2()
|
||||
{
|
||||
return IcingaUser::create(array(
|
||||
'object_type' => 'object',
|
||||
'object_name' => $this->testUserName2
|
||||
));
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
if ($this->hasDb()) {
|
||||
$users = array(
|
||||
$this->testUserName1,
|
||||
$this->testUserName2
|
||||
);
|
||||
|
||||
$db = $this->getDb();
|
||||
foreach ($users as $user) {
|
||||
if (IcingaUser::exists($user, $db)) {
|
||||
IcingaUser::load($user, $db)->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user