From 95d7a8a553debd0ada244ae7472040052890d07b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 3 Sep 2013 11:24:03 +0200 Subject: [PATCH] Fix triggered_by field in ScheduleDowntimeForm is not a select box refs #4496 --- .../forms/Command/ScheduleDowntimeForm.php | 73 +++- .../Command/ScheduleDowntimeFormTest.php | 409 ++++++++++++++++++ 2 files changed, 465 insertions(+), 17 deletions(-) create mode 100644 modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index 2944aef21..4848c2ba3 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -35,6 +35,7 @@ use \Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Protocol\Commandpipe\Downtime; use \Icinga\Protocol\Commandpipe\Comment; use \Icinga\Util\DateTimeFactory; +use \Monitoring\Backend; /** * Form for scheduling downtimes @@ -51,6 +52,8 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm */ const TYPE_FLEXIBLE = 'flexible'; + private $downtimes; + /** * Initialize form */ @@ -72,6 +75,55 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ); } + /** + * Fetch all available downtimes from the database + * + * @return array + */ + private function getCurrentDowntimes() + { + if (isset($this->downtimes)) { + return $this->downtimes; + } + + $cfg = $this->getConfiguration(); + $preferences = $this->getUserPreferences(); + $downtimes = Backend::getInstance($this->getRequest()->getParam('backend'))->select() + ->from('downtime', array( + 'host_name', + 'service_description', + 'downtime_scheduled_start_time', + 'downtime_internal_downtime_id', + ))->fetchAll(); + + $options = array(); + foreach ($downtimes as $downtime) + { + $dt = DateTimeFactory::create($downtime->downtime_scheduled_start_time); + $date_format = $preferences->get('app.dateFormat', $cfg->get('app.dateFormat', 'd/m/Y')); + $time_format = $preferences->get('app.timeFormat', $cfg->get('app.timeFormat', 'g:i A')); + $label = sprintf( + 'ID %s: %s%s starting @ %s', + $downtime->downtime_internal_downtime_id, + $downtime->host_name, + !empty($downtime->service_description) ? ' (' . $downtime->service_description . ')' : '', + $dt->format($date_format . ' ' . $time_format) + ); + $options[$downtime->downtime_internal_downtime_id] = $label; + } + return $options; + } + + /** + * Set the downtimes displayed by this form (used for testing) + * + * @param array $downtimes list of strings + */ + public function setCurrentDowntimes($downtimes) + { + $this->downtimes = $downtimes; + } + /** * Create the form's elements */ @@ -108,25 +160,12 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm * */ $this->addElement( - 'text', + 'select', 'triggered', array( - 'label' => t('Triggered by'), - 'value' => 0, - 'required' => true, - 'validators' => array( - array( - 'Digits', - true - ), - array( - 'GreaterThan', - true, - array( - 'min' => -1 - ) - ) - ) + 'label' => t('Triggered by'), + 'required' => true, + 'multiOptions' => $this->getCurrentDowntimes() ) ); diff --git a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php new file mode 100644 index 000000000..bcb8e67fb --- /dev/null +++ b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php @@ -0,0 +1,409 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Test\Monitoring\Forms\Command; + +// @codingStandardsIgnoreStart +require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php'); +// @codingStandardsIgnoreEnd + +use Icinga\Test\BaseTestCase; +// @codingStandardsIgnoreStart +require_once 'Zend/Validate/Digits.php'; +require_once 'Zend/Validate/GreaterThan.php'; +require_once BaseTestCase::$libDir . '/Web/Form.php'; +require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php'; +require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php'; +require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/ScheduleDowntimeForm.php'; +// @codingStandardsIgnoreEnd + +use \DateTimeZone; +use \Icinga\Util\DateTimeFactory; +use \Monitoring\Form\Command\ScheduleDowntimeForm; // Used by constant FORM_CLASS + +class ScheduleDowntimeFormTest extends BaseTestCase +{ + const FORM_CLASS = 'Monitoring\Form\Command\ScheduleDowntimeForm'; + + /** + * Set up the default time zone + * + * Utilizes singleton DateTimeFactory + * + * @backupStaticAttributes enabled + */ + public function setUp() + { + date_default_timezone_set('UTC'); + DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); + } + + public function testCorrectFormElementCreation() + { + $formFixed = $this->createForm(self::FORM_CLASS); + $formFixed->setCurrentDowntimes(array('foo')); + $formFixed->buildForm(); + $formFlexible = $this->createForm( + self::FORM_CLASS, + array( + 'type' => 'flexible' + ) + ); + $formFlexible->setCurrentDowntimes(array('foo')); + $formFlexible->buildForm(); + + $form = $this->createForm(self::FORM_CLASS); + $form->setCurrentDowntimes(array('foo')); + $form->setWithChildren(true); + $form->buildForm(); + } + + public function testCorrectValidationWithChildrend() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'btn_submit' => 'foo', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertTrue( + $form->isSubmittedAndValid(), + 'Asserting a correct fixed downtime form to be considered valid' + ); + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '10', + 'minutes' => '10', + 'btn_submit' => 'foo' + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertTrue( + $form->isSubmittedAndValid(), + 'Asserting a correct flexible downtime form to be considered valid' + ); + } + + public function testMissingFlexibleDurationRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert missing hours and minutes in downtime form to cause failing validation' + ); + } + + public function testMissingAuthorRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => '', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert missing author to cause validation errors in fixed downtime' + ); + } + + public function testMissingCommentRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => '', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert missing comment to cause validation errors in fixed downtime' + ); + } + + public function testInvalidTriggeredFieldValueRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => 'HAHA', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert invalid trigger field to cause validation to fail' + ); + } + + public function testInvalidStartTimeRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert incorrect start time to cause validation errors in fixed downtime' + ); + } + + public function testInvalidEndTimeRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => 'DING', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert invalid endtime to cause validation errors in fixed downtime' + ); + } + + + public function testInvalidHoursValueRecognitionInFlexibleDowntime() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '-1', + 'minutes' => '12', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert negative hours to cause validation errors in flexible downtime' + ); + } + + public function testInvalidMinutesValueRecognitionInFlexibleDowntime() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '12', + 'minutes' => 'DING', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert non numeric valud to cause validation errors in flexible downtime ' + ); + + } + + public function testCorrectScheduleDowntimeWithoutChildrenForm() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'btn_submit' => 'foo', + 'childobjects' => '0', + ) + ); + $form->setWithChildren(false); + $form->setCurrentDowntimes(array('foo')); + + $this->assertTrue( + $form->isSubmittedAndValid(), + 'Assert a correct schedule downtime without children form to be considered valid' + ); + } + + public function testIncorrectChildObjectsRecognition() { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'childobjects' => 'AHA', + ) + ); + $form->setWithChildren(false); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert and incorrect (non-numeric) childobjects value to cause validation errors' + ); + + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'childobjects' => '4', + ) + ); + $form->setWithChildren(false); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert and incorrect (numeric) childobjects value to cause validation errors' + ); + } + + public function testTimeRange() + { + $form = $this->createForm(self::FORM_CLASS); + $form->setCurrentDowntimes(array('foo')); + $form->buildForm(); + + $time1 = $form->getElement('starttime')->getValue(); + $time2 = $form->getElement('endtime')->getValue(); + + $this->assertEquals(3600, ($time2 - $time1)); + } +}