From 17c1e5a4493747ec759def7d76a62c1f57498a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= Date: Thu, 15 Aug 2013 10:56:17 +0200 Subject: [PATCH] Add tests, move BaseFormTest away from modules/monitoring refs #3777 --- application/forms/Config/GeneralForm.php | 2 +- .../Backend/Livestatus/Query/StatusQuery.php | 34 +--- .../forms/Command/AcknowledgeFormTest.php | 3 +- .../forms/Command/CommentFormTest.php | 3 +- .../forms/Command/ConfirmationFormTest.php | 4 +- .../ConfirmationWithIdentifierFormTest.php | 3 +- .../Command/CustomNotificationFormTest.php | 3 +- .../Command/DelayNotificationFormTest.php | 3 +- .../Command/RescheduleNextCheckFormTest.php | 3 +- .../Command/ScheduleDowntimeFormTest.php | 3 +- .../Command/SubmitPassiveCheckResultTest.php | 3 +- .../forms/Config/GeneralFormTest.php | 161 ++++++++++++++++++ .../library/Icinga/Web/Form}/BaseFormTest.php | 26 ++- 13 files changed, 203 insertions(+), 48 deletions(-) create mode 100644 test/php/application/forms/Config/GeneralFormTest.php rename {modules/monitoring/test/php/application/forms/Command => test/php/library/Icinga/Web/Form}/BaseFormTest.php (66%) diff --git a/application/forms/Config/GeneralForm.php b/application/forms/Config/GeneralForm.php index bb9858b87..671d9694d 100644 --- a/application/forms/Config/GeneralForm.php +++ b/application/forms/Config/GeneralForm.php @@ -64,7 +64,7 @@ class GeneralForm extends Form * * @param IcingaConfig $cfg */ - public function setConfiguration(IcingaConfig $cfg) + public function setConfiguration($cfg) { $this->config = $cfg; } diff --git a/modules/monitoring/library/Monitoring/Backend/Livestatus/Query/StatusQuery.php b/modules/monitoring/library/Monitoring/Backend/Livestatus/Query/StatusQuery.php index ece3c02c0..cc1be7363 100644 --- a/modules/monitoring/library/Monitoring/Backend/Livestatus/Query/StatusQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Livestatus/Query/StatusQuery.php @@ -1,6 +1,6 @@ 'host_accept_passive_checks', 'host_last_state_change', - 'host_problems' => 'is_flapping', - 'service_in_downtime' => 'is_flapping', - 'service_handled' => 'is_flapping', - // Service config 'service_description' => 'description', 'service_display_name' => 'display_name', @@ -41,34 +37,12 @@ class StatusQuery extends AbstractQuery 'service_last_state_change' => 'last_state_change', // Service comments - //'comments_with_info', - //'downtimes_with_info', + 'comments_with_info', + 'downtimes_with_info', ); - public function init() - { - $this->query = $this->createQuery(); - //print_r($this->ds->getConnection()->fetchAll($this->query)); - //die('asdf'); - } - - public function count() - { - return $this->ds->getConnection()->count($this->query); - } - - public function fetchAll() - { - return $this->ds->getConnection()->fetchAll($this->query); - } - - public function fetchRow() - { - return array_shift($this->ds->getConnection()->fetchAll($this->query)); - } - protected function createQuery() { - return $this->ds->getConnection()->select()->from('services', $this->available_columns); + return $this->connection->getConnection()->select()->from('services', $this->available_columns); } } diff --git a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php index 1e0e08f44..981d900cb 100644 --- a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php @@ -5,7 +5,7 @@ namespace Test\Monitoring\Forms\Command; -require_once realpath(__DIR__ . '/BaseFormTest.php'); +require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/AcknowledgeForm.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php'); @@ -13,6 +13,7 @@ require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateT use \DateTimeZone; use \Monitoring\Form\Command\AcknowledgeForm; // Used by constant FORMCLASS use \Icinga\Util\DateTimeFactory; +use \Test\Icinga\Web\Form\BaseFormTest; class AcknowledgeFormTest extends BaseFormTest { diff --git a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php index 354aad114..e740b7101 100644 --- a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php @@ -5,10 +5,11 @@ namespace Test\Monitoring\Forms\Command; -require_once realpath(__DIR__ . '/BaseFormTest.php'); +require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/CommentForm.php'); use \Monitoring\Form\Command\CommentForm; // Used by constant FORMCLASS +use \Test\Icinga\Web\Form\BaseFormTest; class CommentFormTest extends BaseFormTest { diff --git a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php index 70e8b64c6..53434bb0a 100644 --- a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php @@ -1,7 +1,7 @@ + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Test\Icinga\Form\Config; + + +require_once('Zend/Config.php'); +require_once('Zend/Config/Ini.php'); +require_once(realpath('library/Icinga/Web/Form/BaseFormTest.php')); +require_once(realpath('../../application/forms/Config/GeneralForm.php')); + +use Test\Icinga\Web\Form\BaseFormTest; +use \Icinga\Web\Form; +use \DOMDocument; +use \Zend_Config; +use \Zend_View; + +class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest +{ + + private function isHiddenElement($value, $htmlString) + { + $html = new DOMDocument(); + $html->loadHTML($htmlString); + $hidden = $html->getElementsByTagName('noscript'); + + foreach($hidden as $node) { + foreach($node->childNodes as $child) { + if ($child->hasAttributes() === false) { + continue; + } + if (strpos($child->attributes->item(0)->value, $value) !== false) { + return true; + } + } + } + return false; + } + /** + * + */ + public function testCorrectFieldPopulation() + { + date_default_timezone_set('UTC'); + $form = $this->getRequestForm(array(), 'Icinga\Form\Config\GeneralForm'); + $form->setConfiguration( + new Zend_Config( + array( + 'global' => array( + 'environment' => 'development', + 'timezone' => 'Europe/Berlin', + 'indexModule' => 'monitoring', + 'indexController' => 'dashboard', + 'moduleFolder' => '/my/module/path', + 'dateFormat' => 'd-m/Y', + 'timeFormat' => 'A:i' + ), + 'preferences' => array( + 'type' => 'ini', + 'configPath' => './my/path' + ) + ) + ) + ); + + $form->setConfigDir('/tmp'); + $view = new Zend_View(); + + $form->create(); + $this->assertEquals(1, $form->getValue('environment'), 'Asserting the checkbox for devlopment being set to true'); + $this->assertEquals('Europe/Berlin', $form->getValue('timezone'), 'Asserting the correct timezone to be displayed'); + $this->assertEquals('/my/module/path', $form->getValue('module_folder'), 'Asserting the correct module folder to be set'); + $this->assertEquals('d-m/Y', $form->getValue('date_format'), 'Asserting the correct data format to be set'); + $this->assertEquals('A:i', $form->getValue('time_format'), 'Asserting the correct time to be set'); + $this->assertEquals('ini', $form->getValue('preferences_type'), 'Asserting the correct preference type to be set'); + $this->assertEquals('./my/path', $form->getValue('preferences_ini_path'), 'Asserting the correct ini path to be set'); + $this->assertEquals('', $form->getValue('preferences_db_resource'), 'Asserting the database resource not to be set'); + } + + public function testCorrectConditionalIniFieldRendering() + { + date_default_timezone_set('UTC'); + $form = $this->getRequestForm(array(), 'Icinga\Form\Config\GeneralForm'); + $form->setConfiguration( + new Zend_Config( + array( + 'preferences' => array( + 'type' => 'ini', + 'configPath' => './my/path' + ) + ) + ) + ); + $form->setConfigDir('/tmp'); + $form->create(); + $view = new Zend_View(); + + $this->assertFalse( + $this->isHiddenElement('preferences_ini_path', $form->render($view)), + "Asserting the ini path field to be displayed when an ini preference is set" + ); + $this->assertTrue( + $this->isHiddenElement('preferences_db_resource', $form->render($view)), + "Asserting the db resource to be hidden when an ini preference is set" + ); + } + + public function testCorrectConditionalDbFieldRendering() + { + date_default_timezone_set('UTC'); + $form = $this->getRequestForm(array(), 'Icinga\Form\Config\GeneralForm'); + $form->setConfiguration( + new Zend_Config( + array( + 'preferences' => array( + 'type' => 'db', + 'configPath' => './my/path', + 'resource' => 'my_resource' + ) + ) + ) + ); + $form->setConfigDir('/tmp'); + $form->create(); + $view = new Zend_View(); + + $this->assertTrue( + $this->isHiddenElement('preferences_ini_path', $form->render($view)), + "Asserting the ini path field to be hidden when db preference is set" + ); + $this->assertFalse( + $this->isHiddenElement('preferences_ini_resource', $form->render($view)), + "Asserting the db resource to be displayed when db preference is set" + ); + } +} \ No newline at end of file diff --git a/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php b/test/php/library/Icinga/Web/Form/BaseFormTest.php similarity index 66% rename from modules/monitoring/test/php/application/forms/Command/BaseFormTest.php rename to test/php/library/Icinga/Web/Form/BaseFormTest.php index aaba6575c..1cde68e66 100644 --- a/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php +++ b/test/php/library/Icinga/Web/Form/BaseFormTest.php @@ -1,4 +1,8 @@ assertTrue(true); + } } }