diff --git a/library/Director/Objects/IcingaTemplateResolver.php b/library/Director/Objects/IcingaTemplateResolver.php index 10d3aca1..06f85b74 100644 --- a/library/Director/Objects/IcingaTemplateResolver.php +++ b/library/Director/Objects/IcingaTemplateResolver.php @@ -190,6 +190,51 @@ class IcingaTemplateResolver return $this->resolveParentNames($name); } + /** + * Gives a list of all object ids met when walking through ancestry + * + * Tree is walked in import order, duplicates are preserved, the given + * objectId is added last + * + * @param int $objectId + * + * @return array + */ + public function listFullInheritancePathIds($objectId = null) + { + $parentIds = $this->listParentIds($objectId); + $ids = array(); + + foreach ($parentIds as $parentId) { + foreach ($this->listFullInheritancePathIds($parentId) as $id) { + $ids[] = $id; + } + + $ids[] = $parentId; + } + + $object = $this->object; + if ($objectId === null && $object->hasBeenLoadedFromDb()) { + $ids[] = $object->id; + } + + return $ids; + } + + public function listInheritancePathIds($objectId = null) + { + $full = $this->listFullInheritancePathIds($objectId); + $single = array(); + foreach (array_reverse($full) as $id) { + if (array_key_exists($id, $single)) { + continue; + } + $single[$id] = $id; + } + + return array_reverse(array_keys($single)); + } + protected function resolveParentNames($name, &$list = array(), $path = array()) { $this->assertNotInList($name, $path); @@ -262,6 +307,7 @@ class IcingaTemplateResolver protected function getIdsForNames($names) { + $this->requireTemplates(); $ids = array(); foreach ($names as $name) { $ids[] = $this->getIdForName($name); diff --git a/test/php/library/Director/Objects/IcingaTemplateResolverTest.php b/test/php/library/Director/Objects/IcingaTemplateResolverTest.php new file mode 100644 index 00000000..e4caedbe --- /dev/null +++ b/test/php/library/Director/Objects/IcingaTemplateResolverTest.php @@ -0,0 +1,162 @@ +assertEquals( + array( + $this->prefixed('t6'), + $this->prefixed('t5'), + $this->prefixed('t2') + ), + $this->getHost('t1')->templateResolver()->listParentNames() + ); + } + + public function testFullInhertancePathIdsAreCorrect() + { + $this->assertEquals( + $this->getIds(array('t5', 't6', 't5', 't5', 't4', 't3', 't5', 't6', 't2', 't1')), + $this->getHost('t1')->templateResolver()->listFullInheritancePathIds() + ); + } + + public function testInhertancePathIdsAreCorrect() + { + $this->assertEquals( + $this->getIds(array('t4', 't3', 't5', 't6', 't2', 't1')), + $this->getHost('t1')->templateResolver()->listInheritancePathIds() + ); + } + + protected function getHost($name) + { + $hosts = $this->getScenario(); + return $hosts[$name]; + } + + protected function getId($name) + { + $hosts = $this->getScenario(); + return $hosts[$name]->id; + } + + protected function getIds($names) + { + $ids = array(); + foreach ($names as $name) { + $ids[] = $this->getId($name); + } + + return $ids; + } + + protected function prefixed($name) + { + return $this->prefix . $name; + } + + /** + * @return IcingaHost[] + */ + protected function getScenario() + { + if ($this->scenario === null) { + $this->scenario = $this->createScenario(); + } + + return $this->scenario; + } + + /** + * @return IcingaHost[] + */ + protected function createScenario() + { + // Defionition imports (object -> imported) + // t1 -> t6, t5, t2 + // t6 -> t5 + // t3 -> t4 + // t2 -> t3, t6 + // t4 -> t5 + + // 5, 6, 5, 5, 4, 3, 5, 6, 2, 1 + + $prefix = $this->prefix; + $t1 = $this->create($prefix . 't1'); + $t4 = $this->create($prefix . 't4'); + $t3 = $this->create($prefix . 't3'); + $t2 = $this->create($prefix . 't2'); + $t5 = $this->create($prefix . 't5'); + $t6 = $this->create($prefix . 't6'); + + $t1->set('imports', array($prefix . 't6', $prefix . 't5', $prefix . 't2')); + $t6->set('imports', array($t5)); + $t3->set('imports', array($t4)); + $t2->set('imports', array($t3, $t6)); + $t4->set('imports', array($t5)); + + $t1->store(); + $t2->store(); + $t3->store(); + $t4->store(); + $t5->store(); + $t6->store(); + + // TODO: Must work without this: + $t1->templateResolver()->clearCache(); + return array( + 't1' => $t1, + 't2' => $t2, + 't3' => $t3, + 't4' => $t4, + 't5' => $t5, + 't6' => $t6, + ); + } + + /** + * @param $name + * @return IcingaHost + */ + protected function create($name) + { + $host = IcingaHost::create( + array( + 'object_name' => $name, + 'object_type' => 'template' + ) + ); + + $host->store($this->getDb()); + return $host; + } + + protected function loadRendered($name) + { + return file_get_contents(__DIR__ . '/rendered/' . $name . '.out'); + } + + public function tearDown() + { + $db = $this->getDb(); + $kill = array('t1', 't2', 't6', 't3', 't4', 't5'); + foreach ($kill as $short) { + $name = $this->prefixed($short); + if (IcingaHost::exists($name, $db)) { + IcingaHost::load($name, $db)->delete(); + } + } + } +}