From 0ff6c8ee62040f91184af7cf487c5feaf37e7076 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:46:46 +0100 Subject: [PATCH] FormFieldSuggestionTest: Add tests for nullable command argument --- .../Form/IcingaObjectFieldFormTest.php | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 test/php/library/Director/Form/IcingaObjectFieldFormTest.php diff --git a/test/php/library/Director/Form/IcingaObjectFieldFormTest.php b/test/php/library/Director/Form/IcingaObjectFieldFormTest.php new file mode 100644 index 00000000..3f7672fb --- /dev/null +++ b/test/php/library/Director/Form/IcingaObjectFieldFormTest.php @@ -0,0 +1,150 @@ +hasDb()) { + $db = $this->getDb(); + $this->testDatafield = DirectorDatafield::create([ + 'varname' => self::DATAFIELD_NAME, + 'caption' => 'Blah', + 'description' => '', + 'datatype' => 'Icinga\Module\Director\DataType\DataTypeArray', + 'format' => 'json' + ]); + $this->testDatafield->store($db); + + $this->testIcingaCommand = IcingaCommand::create( + [ + 'object_name' => self::COMMAND_NAME, + 'object_type' => 'object' + ], + $db + ); + $this->testIcingaCommand->store($db); + + $this->testHost = IcingaHost::create([ + 'object_name' => self::HOST_NAME, + 'object_type' => 'object', + 'display_name' => 'Whatever' + ], $this->getDb()); + } + } + + public function testFieldSuggestionsWithoutCheckCommand() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $icingaHostFieldForm = (new IcingaObjectFieldForm()) + ->setDb($db) + ->setIcingaObject($this->testHost); + + $icingaHostFieldForm->setup(); + /** @var array $suggestions */ + $suggestions = $icingaHostFieldForm->getElement('datafield_id') + ->getAttrib('options'); + + array_shift($suggestions); + + $this->assertEquals( + [ + 'Other available fields' => [ + $this->testDatafield->get('id') => "Blah (dataFieldTest)" + ] + ], + $suggestions + ); + } + + public function testFieldSuggestionsWithCheckCommand() + { + if ($this->skipForMissingDb()) { + return; + } + + $db = $this->getDb(); + + $this->testHost->check_command = self::COMMAND_NAME; + $icingaHostFieldForm = (new IcingaObjectFieldForm()) + ->setDb($db) + ->setIcingaObject($this->testHost); + + $icingaHostFieldForm->setup(); + + /** @var array $suggestions */ + $suggestions = $icingaHostFieldForm->getElement('datafield_id') + ->getAttrib('options'); + + array_shift($suggestions); + $this->assertEquals( + [ + 'Other available fields' => [ + $this->testDatafield->get('id') => "Blah (dataFieldTest)" + ] + ], + $suggestions + ); + } + + public function tearDown(): void + { + if ($this->hasDb()) { + $db = $this->getDb(); + $this->deleteDatafields(); + + if (IcingaHost::exists(self::HOST_NAME, $db)) { + IcingaHost::load(self::HOST_NAME, $db)->delete(); + } + + if (IcingaCommand::exists(self::COMMAND_NAME, $db)) { + IcingaCommand::load(self::COMMAND_NAME, $db)->delete(); + } + } + + parent::tearDown(); + } + + protected function deleteDatafields() + { + $db = $this->getDb(); + $dbAdapter = $db->getDbAdapter(); + + $query = $dbAdapter->select() + ->from('director_datafield') + ->where('varname = ?', self::DATAFIELD_NAME); + foreach (DirectorDatafield::loadAll($db, $query, 'id') as $datafield) { + $datafield->delete(); + } + } +}