diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index 1322c00e5..c20c18a46 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -2,24 +2,24 @@ // {{{ICINGA_LICENSE_HEADER}}} /** * This file is part of Icinga 2 Web. - * + * * Icinga 2 Web - Head for multiple monitoring backends. * Copyright (C) 2013 Icinga Development Team - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * + * * @copyright 2013 Icinga Development Team * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team @@ -648,31 +648,31 @@ class CommandPipe } /** - * Send a custom host or service notification + * Send custom host and/or service notifications * - * @param $objects monitoring objects to send this notification to - * @param Comment $comment comment to use in the notification - * @param int [$...] Optional list of Notification flags which will be used as the option parameter + * @param array $objects Affected monitoring objects + * @param CustomNotification $notification */ - public function sendCustomNotification($objects, Comment $comment, $optionsVarList = 0/*, ...*/) + public function sendCustomNotification(array $objects, CustomNotification $notification) { - $args = func_get_args(); - // logical OR for all notification options - for ($i = 3; $i < count($args); $i++) { - $optionsVarList |= $args[$i]; - } - - foreach ($objects as $object) { - $type = $this->getObjectType($object); - $msg = 'SEND_CUSTOM_'.(($type == self::TYPE_SERVICE) ? 'SVC' : 'HOST' ).'_NOTIFICATION'; - $msg .= ';'.$object->host_name; - if ($type == self::TYPE_SERVICE) { - $msg .= ';'.$object->service_description; + foreach ($objects as $hostOrService) { + if (isset($hostOrService->service_description) && isset($hostOrService->host_name)) { + // Assume service + $command = sprintf( + $notification->getFormatString(self::TYPE_SERVICE), + $hostOrService->host_name, + $hostOrService->service_description + ); + } elseif (isset($hostOrService->host_name)) { + // Assume host + $command = sprintf( + $notification->getFormatString(self::TYPE_HOST), + $hostOrService->host_name + ); + } else { + continue; } - $msg .= ';'.$optionsVarList; - $msg .= ';'.$comment->author; - $msg .= ';'.$comment->comment; - $this->send($msg); + $this->send($command); } } diff --git a/library/Icinga/Protocol/Commandpipe/CustomNotification.php b/library/Icinga/Protocol/Commandpipe/CustomNotification.php new file mode 100644 index 000000000..e21bd9fa0 --- /dev/null +++ b/library/Icinga/Protocol/Commandpipe/CustomNotification.php @@ -0,0 +1,109 @@ +author = $author; + $this->comment = $comment; + $this->forced = $forced; + $this->broadcast = $broadcast; + } + + /** + * Get Custom Notification command format string according to if its sent to a host or a service + * + * @param string $type Identifier for either host or service + * + * @return string + * + * @throws InvalidCommandException When the given type is unknown + * @see \Icinga\Protocol\Commandpipe\CommandPipe::TYPE_HOST + * @see \Icinga\Protocol\Commandpipe\CommandPipe::TYPE_SERVICE + */ + public function getFormatString($type) + { + switch ($type) { + case CommandPipe::TYPE_HOST: + $format = '%s'; + break; + case CommandPipe::TYPE_SERVICE: + $format = '%s;%s'; + break; + default: + throw new InvalidCommandException('Custom Notifications can only apply on hosts and services'); + } + + $options = 0; + if ($this->forced) { + $options |= self::NOTIFY_FORCED; + } + if ($this->broadcast) { + $options |= self::NOTIFY_BROADCAST; + } + + // Build the command + $command = 'SEND_CUSTOM_' . $type . '_NOTIFICATION;' + . $format . ';' + . $options . ';' + . $this->author . ';' + . $this->comment; + return $command; + } +} diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 0033a7730..68c3c153b 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -431,12 +431,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $author = $this->getRequest()->getUser()->getUsername(); - $this->target->sendCustomNotification( - $this->view->objects, - new Comment($author, $form->getComment()), - $form->getOptions() - ); + $this->target->sendCustomNotification($this->view->objects, $form->getCustomNotification()); } } diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index 05a758246..3c55abea7 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -28,30 +28,35 @@ namespace Monitoring\Form\Command; -use Icinga\Web\Form\Element\DateTimePicker; -use Icinga\Web\Form\Element\Note; -use Icinga\Protocol\Commandpipe\Acknowledgement; -use Icinga\Protocol\Commandpipe\Comment; -use Icinga\Util\DateTimeFactory; +use \Icinga\Web\Form\Element\DateTimePicker; +use \Icinga\Web\Form\Element\Note; +use \Icinga\Protocol\Commandpipe\Acknowledgement; +use \Icinga\Protocol\Commandpipe\Comment; +use \Icinga\Util\DateTimeFactory; /** * Form for problem acknowledgements */ class AcknowledgeForm extends CommandForm { - /** - * Initialize form - */ - public function init() - { - $this->setName('AcknowledgeForm'); - } - /** * Create the form's elements */ protected function create() { + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t( + 'This command is used to acknowledge host or service problems. When a problem is ' + . 'acknowledged, future notifications about problems are temporarily disabled until the ' + . 'host/service changes from its current state.' + ) + ) + ) + ); + $this->addElement($this->createAuthorField()); $this->addElement( @@ -63,66 +68,96 @@ class AcknowledgeForm extends CommandForm 'required' => true ) ); + $this->addElement( + new Note( + array( + 'name' => 'commentnote', + 'value' => t( + ' If you work with other administrators you may find it useful to share information ' + . 'about a host or service that is having problems if more than one of you may be working on ' + . 'it. Make sure you enter a brief description of what you are doing.' + ) + ) + ) + ); $this->addElement( 'checkbox', 'persistent', array( - 'label' => t('Persistent comment'), + 'label' => t('Persistent Comment'), 'value' => false ) ); - - $expireNote = new Note( - array( - 'name' => 'expirenote', - 'value' => t('If the acknowledgement should expire, check the box and enter an expiration timestamp.') - ) - ); - - $expireCheck = $this->createElement( - 'checkbox', - 'expire', - array( - 'label' => t('Use expire time') - ) - ); - - if ($this->getRequest()->getPost('expire', '0') === '1') { - $now = DateTimeFactory::create(); - $expireTime = new DateTimePicker( + $this->addElement( + new Note( array( - 'name' => 'expiretime', - 'label' => t('Expire time'), - 'value' => $now->getTimestamp() + 3600 + 'name' => 'persistentnote', + 'value' => t( + 'If you would like the comment to remain even when the acknowledgement is removed, ' + . 'check this option.' + ) ) - ); - - $this->addElements(array($expireNote, $expireCheck, $expireTime)); - } else { - $this->addElements(array($expireNote, $expireCheck)); - } - - $this->enableAutoSubmit(array('expire')); - - $this->addDisplayGroup( - array( - 'expirenote', - 'expire', - 'expiretime' - ), - 'expire_group', - array( - 'legend' => t('Expire acknowledgement') ) ); + $this->addElement( + 'checkbox', + 'expire', + array( + 'label' => t('Use Expire Time') + ) + ); + $this->enableAutoSubmit(array('expire')); + $this->addElement( + new Note( + array( + 'name' => 'expirenote', + 'value' => t('If the acknowledgement should expire, check this option.') + ) + ) + ); + if ($this->getRequest()->getPost('expire', '0') === '1') { + $now = DateTimeFactory::create(); + $this->addElement( + new DateTimePicker( + array( + 'name' => 'expiretime', + 'label' => t('Expire Time'), + 'value' => $now->getTimestamp() + 3600 + ) + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'expiretimenote', + 'value' => t( + 'Enter the expire date/time for this acknowledgement here. Icinga will ' + . ' delete the acknowledgement after this date expired.' + ) + ) + ) + ); + } + $this->addElement( 'checkbox', 'sticky', array( - 'label' => t('Sticky acknowledgement'), - 'value' => false + 'label' => t('Sticky Acknowledgement'), + 'value' => true + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'stickynote', + 'value' => t( + 'If you want the acknowledgement to disable notifications until the host/service ' + . 'recovers, check this option.' + ) + ) ) ); @@ -130,12 +165,23 @@ class AcknowledgeForm extends CommandForm 'checkbox', 'notify', array( - 'label' => t('Send notification'), - 'value' => false + 'label' => t('Send Notification'), + 'value' => true + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'sendnotificationnote', + 'value' => t( + 'If you do not want an acknowledgement notification to be sent out to the appropriate ' + . 'contacts, uncheck this option.' + ) + ) ) ); - $this->setSubmitLabel(t('Acknowledge problem')); + $this->setSubmitLabel(t('Acknowledge Problem')); parent::create(); } @@ -143,7 +189,8 @@ class AcknowledgeForm extends CommandForm /** * Add validator for dependent fields * - * @param array $data + * @param array $data + * * @see \Icinga\Web\Form::preValidation() */ protected function preValidation(array $data) @@ -154,6 +201,11 @@ class AcknowledgeForm extends CommandForm } } + /** + * Create acknowledgement from request data + * + * @return \Icinga\Protocol\Commandpipe\Acknowledgement + */ public function getAcknowledgement() { $expireTime = -1; @@ -170,6 +222,5 @@ class AcknowledgeForm extends CommandForm $expireTime, $this->getValue('sticky') ); - } } diff --git a/modules/monitoring/application/forms/Command/CommandForm.php b/modules/monitoring/application/forms/Command/CommandForm.php index 4ca1e4e82..3c6004ea3 100644 --- a/modules/monitoring/application/forms/Command/CommandForm.php +++ b/modules/monitoring/application/forms/Command/CommandForm.php @@ -38,19 +38,9 @@ use \Zend_Validate_Date; */ class CommandForm extends Form { - /** - * Default date format - */ - const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s'; - - /** - * Default format for validation - */ - const DEFAULT_DATE_VALIDATION = 'yyyy-MM-dd hh:ii:ss'; - /** * Array of messages - * + * * @var string[] */ private $notes = array(); @@ -142,7 +132,7 @@ class CommandForm extends Form $authorField = new Zend_Form_Element_Hidden( array( 'name' => 'author', - 'label' => t('Author name'), + 'label' => t('Author (Your Name)'), 'value' => $authorName, 'required' => true ) @@ -159,34 +149,4 @@ class CommandForm extends Form return $authorField; } - - /** - * Getter for date format - * TODO(mh): Should be user preferences - * @return string - */ - protected function getDateFormat() - { - return self::DEFAULT_DATE_FORMAT; - } - - /** - * Getter for date validation format - * @return string - */ - protected function getDateValidationFormat() - { - return self::DEFAULT_DATE_VALIDATION; - } - - /** - * Create a new date validator - * @return Zend_Validate_Date - */ - protected function createDateTimeValidator() - { - $validator = new Zend_Validate_Date(); - $validator->setFormat($this->getDateValidationFormat()); - return $validator; - } } diff --git a/modules/monitoring/application/forms/Command/CommandWithIdentifierForm.php b/modules/monitoring/application/forms/Command/CommandWithIdentifierForm.php index 131d9178a..c59b737d3 100644 --- a/modules/monitoring/application/forms/Command/CommandWithIdentifierForm.php +++ b/modules/monitoring/application/forms/Command/CommandWithIdentifierForm.php @@ -37,6 +37,7 @@ class CommandWithIdentifierForm extends CommandForm { /** * Identifier for data field + * * @var string */ private $fieldName = 'objectid'; @@ -52,6 +53,7 @@ class CommandWithIdentifierForm extends CommandForm /** * Setter for field label + * * @param string $fieldLabel */ public function setFieldLabel($fieldLabel) @@ -61,6 +63,7 @@ class CommandWithIdentifierForm extends CommandForm /** * Getter for field label + * * @return string */ public function getFieldLabel() @@ -70,6 +73,7 @@ class CommandWithIdentifierForm extends CommandForm /** * Setter for field name + * * @param string $fieldName */ public function setFieldName($fieldName) @@ -79,6 +83,7 @@ class CommandWithIdentifierForm extends CommandForm /** * Getter for field name + * * @return string */ public function getFieldName() diff --git a/modules/monitoring/application/forms/Command/CommentForm.php b/modules/monitoring/application/forms/Command/CommentForm.php index 2e789dc99..61e87f1a5 100644 --- a/modules/monitoring/application/forms/Command/CommentForm.php +++ b/modules/monitoring/application/forms/Command/CommentForm.php @@ -28,18 +28,28 @@ namespace Monitoring\Form\Command; -use Icinga\Protocol\Commandpipe\Comment; +use \Icinga\Web\Form\Element\Note; +use \Icinga\Protocol\Commandpipe\Comment; + /** * Form for adding comment commands */ class CommentForm extends CommandForm { /** - * Interface method to build the form - * @see CommandForm::create + * Create the form's elements */ protected function create() { + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t('This command is used to add a comment to hosts or services.') + ) + ) + ); + $this->addElement($this->createAuthorField()); $this->addElement( @@ -51,13 +61,36 @@ class CommentForm extends CommandForm 'required' => true ) ); + $this->addElement( + new Note( + array( + 'name' => 'commentnote', + 'value' => t( + 'If you work with other administrators, you may find it useful to share information ' + . 'about a host or service that is having problems if more than one of you may be working on ' + . 'it. Make sure you enter a brief description of what you are doing.' + ) + ) + ) + ); $this->addElement( 'checkbox', 'persistent', array( 'label' => t('Persistent'), - 'value' => false + 'value' => true + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'persistentnote', + 'value' => t( + 'If you uncheck this option, the comment will automatically be deleted the next time ' + . 'Icinga is restarted.' + ) + ) ) ); @@ -66,6 +99,11 @@ class CommentForm extends CommandForm parent::create(); } + /** + * Create comment from request data + * + * @return \Icinga\Protocol\Commandpipe\Comment + */ public function getComment() { return new Comment($this->getAuthorName(), $this->getValue('comment'), $this->getValue('persistent')); diff --git a/modules/monitoring/application/forms/Command/CustomNotificationForm.php b/modules/monitoring/application/forms/Command/CustomNotificationForm.php index 7244fb759..a14fad112 100644 --- a/modules/monitoring/application/forms/Command/CustomNotificationForm.php +++ b/modules/monitoring/application/forms/Command/CustomNotificationForm.php @@ -28,7 +28,8 @@ namespace Monitoring\Form\Command; -use Zend_Form_Element_Hidden; +use \Icinga\Web\Form\Element\Note; +use \Icinga\Protocol\Commandpipe\CustomNotification; /** * For for command CustomNotification @@ -36,11 +37,23 @@ use Zend_Form_Element_Hidden; class CustomNotificationForm extends CommandForm { /** - * Interface method to build the form - * @see CommandForm::create + * Create the form's elements */ protected function create() { + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t( + 'This command is used to send a custom notification about hosts or services. Useful in ' + . 'emergencies when you need to notify admins of an issue regarding a monitored system or ' + . 'service.' + ) + ) + ) + ); + $this->addElement($this->createAuthorField()); $this->addElement( @@ -52,14 +65,38 @@ class CustomNotificationForm extends CommandForm 'required' => true ) ); + $this->addElement( + new Note( + array( + 'name' => 'commentnote', + 'value' => t( + 'If you work with other administrators, you may find it useful to share information ' + . 'about a host or service that is having problems if more than one of you may be working on ' + . 'it. Make sure you enter a brief description of what you are doing.' + ) + ) + ) + ); $this->addElement( 'checkbox', - 'force', + 'forced', array( 'label' => t('Forced') ) ); + $this->addElement( + new Note( + array( + 'name' => 'forcenote', + 'value' => t( + 'Custom notifications normally follow the regular notification logic in Icinga. Selecting this ' + . 'option will force the notification to be sent out, regardless of time restrictions, ' + . 'whether or not notifications are enabled, etc.' + ) + ) + ) + ); $this->addElement( 'checkbox', @@ -68,26 +105,36 @@ class CustomNotificationForm extends CommandForm 'label' => t('Broadcast') ) ); + $this->addElement( + new Note( + array( + 'name' => 'broadcastnote', + 'value' => t( + 'Selecting this option causes the notification to be sent out to all normal (non-escalated) ' + . ' and escalated contacts. These options allow you to override the normal notification logic ' + . 'if you need to get an important message out.' + ) + ) + ) + ); - $this->setSubmitLabel(t('Send custom notification')); + $this->setSubmitLabel(t('Send Custom Notification')); parent::create(); } - public function getComment() + /** + * Create Custom Notification from request data + * + * @return \Icinga\Protocol\Commandpipe\CustomNotification + */ + public function getCustomNotification() { - return $this->getValue('comment'); - } - - public function getOptions() - { - $value = 0; - if ($this->getValue('force')) { - $value |= 2; - } - if ($this->getValue('broadcast')) { - $value |= 1; - } - return $value; + return new CustomNotification( + $this->getAuthorName(), + $this->getValue('comment'), + $this->getValue('forced'), + $this->getValue('broadcast') + ); } } diff --git a/modules/monitoring/application/forms/Command/DelayNotificationForm.php b/modules/monitoring/application/forms/Command/DelayNotificationForm.php index ffe501fe9..f2eec200d 100644 --- a/modules/monitoring/application/forms/Command/DelayNotificationForm.php +++ b/modules/monitoring/application/forms/Command/DelayNotificationForm.php @@ -28,6 +28,8 @@ namespace Monitoring\Form\Command; +use \Icinga\Web\Form\Element\Note; + /** * Form for the delay notification command */ @@ -40,11 +42,17 @@ class DelayNotificationForm extends CommandForm /** * Create the form's elements - * - * @see CommandForm::create() */ protected function create() { + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t('This command is used to delay the next problem notification that is sent out.') + ) + ) + ); $this->addElement( 'text', 'minutes', @@ -65,12 +73,10 @@ class DelayNotificationForm extends CommandForm ) ) ); - $this->addNote( t( - 'Delay the next problem notification. The notification delay will be ' - . 'disregarded if the host/service changes state before the next notification is ' - . 'scheduled to be sent out.' + 'The notification delay will be disregarded if the host/service changes state before the next ' + . 'notification is scheduled to be sent out.' ) ); @@ -79,8 +85,13 @@ class DelayNotificationForm extends CommandForm parent::create(); } + /** + * Return the currently set delay time in seconds + * + * @return integer + */ public function getDelayTime() { - return $this->getValue('minutes')*60; + return $this->getValue('minutes') * 60; } } diff --git a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php index a5016b2f1..235124bc2 100644 --- a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php +++ b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php @@ -28,13 +28,13 @@ namespace Monitoring\Form\Command; -use \DateTime; use \Zend_Form_Element_Checkbox; use \Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Util\DateTimeFactory; +use \Icinga\Web\Form\Element\Note; /** - * Form for RescheduleNextCheck + * Form for scheduling checks */ class RescheduleNextCheckForm extends WithChildrenCommandForm { @@ -43,37 +43,76 @@ class RescheduleNextCheckForm extends WithChildrenCommandForm */ protected function create() { - $now = DateTimeFactory::create(); - $dateElement = new DateTimePicker( - array( - 'name' => 'checktime', - 'label' => t('Check time'), - 'value' => $now->getTimestamp() - ) - ); - $this->addElement($dateElement); - - $checkBox = new Zend_Form_Element_Checkbox( - array( - 'name' => 'forcecheck', - 'label' => t('Force check'), - 'value' => true + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t( + 'This command is used to schedule the next check of hosts/services. Icinga will re-queue the ' + . 'check at the time you specify.' + ) + ) ) ); - $this->addElement($checkBox); + $this->addElement( + new DateTimePicker( + array( + 'name' => 'checktime', + 'label' => t('Check Time'), + 'value' => DateTimeFactory::create()->getTimestamp(), + 'required' => !$this->getRequest()->getPost('forcecheck') + ) + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'checktimenote', + 'value' => t('Set the date/time when this check should be executed.') + ) + ) + ); + $this->addElement( + new Zend_Form_Element_Checkbox( + array( + 'name' => 'forcecheck', + 'label' => t('Force Check'), + 'value' => true + ) + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'forcechecknote', + 'value' => t( + 'If you select this option, Icinga will force a check regardless of both what time the ' + . 'scheduled check occurs and whether or not checks are enabled.' + ) + ) + ) + ); + + // TODO: As of the time of writing it's possible to set hosts AND services as affected by this command but + // with children only makes sense on hosts if ($this->getWithChildren() === true) { - $this->addNote(t('Reschedule next check for this host and its services.')); + $this->addNote(t('TODO: Help message when with children is enabled')); } else { - $this->addNote(t('Reschedule next check for this object.')); + $this->addNote(t('TODO: Help message when with children is disabled')); } - $this->setSubmitLabel(t('Reschedule check')); + $this->setSubmitLabel(t('Reschedule Check')); parent::create(); } + /** + * Return whether this is a forced check (force is checked) + * + * @return bool + */ public function isForced() { return $this->getValue('forcecheck') == true; diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index 84e71e7b4..7304ab3e2 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -31,13 +31,14 @@ namespace Monitoring\Form\Command; use \Zend_Form_Element_Text; use \Zend_Validate_GreaterThan; use \Zend_Validate_Digits; -use Icinga\Web\Form\Element\DateTimePicker; -use Icinga\Protocol\Commandpipe\Downtime; -use Icinga\Protocol\Commandpipe\Comment; -use Icinga\Util\DateTimeFactory; +use \Icinga\Web\Form\Element\DateTimePicker; +use \Icinga\Protocol\Commandpipe\Downtime; +use \Icinga\Protocol\Commandpipe\Comment; +use \Icinga\Util\DateTimeFactory; +use \Icinga\Web\Form\Element\Note; /** - * Form for any ScheduleDowntime command + * Form for scheduling downtimes */ class ScheduleDowntimeForm extends WithChildrenCommandForm { @@ -73,12 +74,24 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm } /** - * Interface method to build the form - * - * @see CommandForm::create + * Create the form's elements */ protected function create() { + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t( + 'This command is used to schedule downtime for hosts/services. During the specified downtime, ' + . 'Icinga will not send notifications out about the affected objects. When the scheduled ' + . 'downtime expires, Icinga will send out notifications as it normally would. Scheduled ' + . 'downtimes are preserved across program shutdowns and restarts.' + ) + ) + ) + ); + $this->addElement($this->createAuthorField()); $this->addElement( @@ -90,6 +103,18 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm 'required' => true ) ); + $this->addElement( + new Note( + array( + 'name' => 'commentnote', + 'value' => t( + 'If you work with other administrators, you may find it useful to share information ' + . 'about a host or service that is having problems if more than one of you may be working on ' + . 'it. Make sure you enter a brief description of what you are doing.' + ) + ) + ) + ); /** * @TODO: Display downtime list (Bug #4496) @@ -119,30 +144,49 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ); $now = DateTimeFactory::create(); - $dateTimeStart = new DateTimePicker( - array( - 'name' => 'starttime', - 'label' => t('Start time'), - 'value' => $now->getTimestamp() + $this->addElement( + new DateTimePicker( + array( + 'name' => 'starttime', + 'label' => t('Start Time'), + 'value' => $now->getTimestamp() + ) ) ); - $dateTimeEnd = new DateTimePicker( - array( - 'name' => 'endtime', - 'label' => t('End time'), - 'value' => $now->getTimestamp() + 3600 + $this->addElement( + new Note( + array( + 'name' => 'stattimenote', + 'value' => t('Set the start date/time for the downtime.') + ) + ) + ); + $this->addElement( + new DateTimePicker( + array( + 'name' => 'endtime', + 'label' => t('End Time'), + 'value' => $now->getTimestamp() + 3600 + ) + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'endtimenote', + 'value' => t('Set the end date/time for the downtime.') + ) ) ); - $this->addElements(array($dateTimeStart, $dateTimeEnd)); $this->addElement( 'select', 'type', array( - 'label' => t('Downtime type'), - 'multiOptions' => $this->getDowntimeTypeOptions(), - 'required' => true, - 'validators' => array( + 'label' => t('Downtime Type'), + 'multiOptions' => $this->getDowntimeTypeOptions(), + 'required' => true, + 'validators' => array( array( 'InArray', true, @@ -153,12 +197,25 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ) ) ); + $this->addElement( + new Note( + array( + 'name' => 'typenote', + 'value' => t( + 'If you select the fixed option, the downtime will be in effect between the start and end ' + . 'times you specify whereas a flexible downtime starts when the service enters a non-OK state ' + . '(sometime between the start and end times you specified) and lasts as long as the duration ' + . 'of time you enter. The duration fields do not apply for fixed downtime.' + ) + ) + ) + ); $this->enableAutoSubmit(array('type')); if ($this->getRequest()->getPost('type') === self::TYPE_FLEXIBLE) { $hoursText = new Zend_Form_Element_Text('hours'); - $hoursText->setLabel(t('Flexible duration')); + $hoursText->setLabel(t('Flexible Duration')); $hoursText->setAttrib('style', 'width: 40px;'); $hoursText->setValue(1); $hoursText->addDecorator('HtmlTag', array('tag' => 'dd', 'openOnly' => true)); @@ -170,7 +227,6 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm } ) ); - $minutesText = new Zend_Form_Element_Text('minutes'); $minutesText->setLabel(t('Minutes')); $minutesText->setAttrib('style', 'width: 40px;'); @@ -185,19 +241,30 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm } ) ); - $this->addElements(array($hoursText, $minutesText)); + $this->addElement( + new Note( + array( + 'name' => 'flexiblenote', + 'value' => t( + 'Enter here the duration of the downtime. Icinga will automatically delete the downtime ' + . 'after this time expired.' + ) + ) + ) + ); } + // TODO: As of the time of writing it's possible to set hosts AND services as affected by this command but + // with children only makes sense on hosts if ($this->getWithChildren() === true) { - $this->addNote(t('Schedule downtime for host and its services.')); + $this->addNote(t('TODO: Help message when with children is enabled')); } else { - $this->addElement( 'select', 'childobjects', array( - 'label' => t('Child objects'), + 'label' => t('Child Objects'), 'required' => true, 'multiOptions' => array( 0 => t('Do nothing with child objects'), @@ -219,11 +286,10 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ) ) ); - - $this->addNote(t('Schedule downtime for this object.')); + $this->addNote(t('TODO: Help message when with children is disabled')); } - $this->setSubmitLabel(t('Schedule downtime')); + $this->setSubmitLabel(t('Schedule Downtime')); parent::create(); } @@ -256,9 +322,9 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm } /** - * Return the downtime submitted in this form + * Create Downtime from request Data * - * @return Downtime + * @return \Icinga\Protocol\Commandpipe\Downtime */ public function getDowntime() { @@ -269,7 +335,7 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ); $duration = 0; if ($this->getValue('type') === self::TYPE_FLEXIBLE) { - $duration = ($this->getValue('hours')*3600) + ($this->getValue('minutes')*60); + $duration = ($this->getValue('hours') * 3600) + ($this->getValue('minutes') * 60); } $starttime = $this->getValue('starttime'); $endtime = $this->getValue('endtime'); @@ -281,7 +347,7 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm $duration, $this->getValue('triggered') ); - if (! $this->getWithChildren()) { + if (!$this->getWithChildren()) { switch ($this->getValue('childobjects')) { case 1: $downtime->setType(Downtime::TYPE_WITH_CHILDREN_TRIGGERED); diff --git a/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php b/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php index 7ee18aaa9..418be69ad 100644 --- a/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php +++ b/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php @@ -28,7 +28,8 @@ namespace Monitoring\Form\Command; -use Icinga\Exception\ProgrammingError; +use \Icinga\Exception\ProgrammingError; +use \Icinga\Web\Form\Element\Note; /** * Form for submitting passive check results @@ -59,6 +60,7 @@ class SubmitPassiveCheckResultForm extends CommandForm /** * Setup plugin states + * * @see Zend_Form::init */ public function init() @@ -85,6 +87,7 @@ class SubmitPassiveCheckResultForm extends CommandForm /** * Setter for type + * * @param string $type */ public function setType($type) @@ -94,6 +97,7 @@ class SubmitPassiveCheckResultForm extends CommandForm /** * Getter for type + * * @return string */ public function getType() @@ -103,6 +107,7 @@ class SubmitPassiveCheckResultForm extends CommandForm /** * Return array of options + * * @return array * @throws \Icinga\Exception\ProgrammingError */ @@ -117,20 +122,30 @@ class SubmitPassiveCheckResultForm extends CommandForm /** * Create the form's elements - * - * @see CommandForm::create() */ protected function create() { + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t( + 'This command is used to submit a passive check result for particular hosts/services. It is ' + . 'particularly useful for resetting security-related objects to OK states once they have been ' + . 'dealt with.' + ) + ) + ) + ); $this->addElement( 'select', 'pluginstate', array( - 'label' => t('Plugin state'), - 'multiOptions' => $this->getOptions(), - 'required' => true, - 'validators' => array( + 'label' => t('Check Result'), + 'multiOptions' => $this->getOptions(), + 'required' => true, + 'validators' => array( array( 'Digits', true @@ -145,14 +160,30 @@ class SubmitPassiveCheckResultForm extends CommandForm ) ) ); + $this->addElement( + new Note( + array( + 'name' => 'checkresultnote', + 'value' => t('Set the state which should be send to Icinga for this objects.') + ) + ) + ); $this->addElement( 'textarea', 'checkoutput', array( - 'label' => t('Check output'), - 'rows' => 2, - 'required' => true + 'label' => t('Check Output'), + 'rows' => 2, + 'required' => true + ) + ); + $this->addElement( + new Note( + array( + 'name' => 'checkoutputnote', + 'value' => t('Fill in the check output string which should be send to Icinga.') + ) ) ); @@ -160,26 +191,49 @@ class SubmitPassiveCheckResultForm extends CommandForm 'textarea', 'performancedata', array( - 'label' => t('Performance data'), + 'label' => t('Performance Data'), 'rows' => 2 ) ); + $this->addElement( + new Note( + array( + 'name' => 'performancedatanote', + 'value' => t('Fill in the performance data string which should be send to Icinga.') + ) + ) + ); - $this->setSubmitLabel(t('Submit passive check result')); + $this->setSubmitLabel(t('Submit Passive Check Result')); parent::create(); } + /** + * Return the entered object state as an integer + * + * @return int + */ public function getState() { return intval($this->getValue('pluginstate')); } + /** + * Return the entered check output as a string + * + * @return string + */ public function getOutput() { return $this->getValue('checkoutput'); } + /** + * Return the entered performance data as a string + * + * @return string + */ public function getPerformancedata() { return $this->getValue('performancedata'); diff --git a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php index d8451cb44..1e0e08f44 100644 --- a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php @@ -1,24 +1,22 @@ new DateTimeZone('UTC'))); } - public function testForm() - { - $formWithoutExpiration = $this->getRequestForm(array(), self::FORMCLASS); - $formWithoutExpiration->buildForm(); - $formWithExpiration = $this->getRequestForm(array( - 'expire' => '1' - ), self::FORMCLASS); - $formWithExpiration->buildForm(); - - $this->assertCount(10, $formWithoutExpiration->getElements()); - $this->assertCount(11, $formWithExpiration->getElements()); - } - - public function testValidateCorrectForm() + public function testFormValid() { $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '0', - 'sticky' => '0', - 'notify' => '0', - 'btn_submit' => 'foo' + 'author' => 'Author', + 'comment' => 'Comment', + 'persistent' => '0', + 'expire' => '0', + 'sticky' => '0', + 'notify' => '0', + 'btn_submit' => 'Submit' ), self::FORMCLASS); $this->assertTrue( $form->isSubmittedAndValid(), - "Asserting a correct form to be validated correctly" + 'Legal request data without expire time must be considered valid' ); - } - public function testDetectMissingAcknowledgementComment() - { - $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => '', - 'persistent' => '0', - 'expire' => '0', - 'sticky' => '0', - 'notify' => '0', - 'btn_submit' => 'foo' + $formWithExpireTime = $this->getRequestForm(array( + 'author' => 'Author', + 'comment' => 'Comment', + 'persistent' => '0', + 'expire' => '1', + 'expiretime' => '2013-07-10 17:32:16', + 'sticky' => '0', + 'notify' => '0', + 'btn_submit' => 'Submit' ), self::FORMCLASS); - $this->assertFalse( - $form->isSubmittedAndValid(), - "Asserting a missing comment text to cause validation errors" - ); - } - public function testValidateMissingExpireTime() - { - $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '1', - 'expiretime' => '', - 'sticky' => '0', - 'notify' => '0', - 'btn_submit' => 'foo' - ), self::FORMCLASS); - $this->assertFalse( - $form->isSubmittedAndValid(), - "Asserting a missing expire time to cause validation errors when expire is 1" - ); - } - - public function testValidateIncorrectExpireTime() - { - $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '1', - 'expiretime' => 'NOT A DATE', - 'sticky' => '0', - 'notify' => '0', - 'btn_submit' => 'foo' - ), self::FORMCLASS); - $this->assertFalse( - $form->isSubmittedAndValid(), - "Assert incorrect dates to be recognized when validating expiretime" - ); - } - - public function testValidateCorrectAcknowledgementWithExpireTime() - { - $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '1', - 'expiretime' => '2013-07-10 17:32:16', - 'sticky' => '0', - 'notify' => '0', - 'btn_submit' => 'foo' - ), self::FORMCLASS); $this->assertTrue( + $formWithExpireTime->isSubmittedAndValid(), + 'Legal request data with expire time must be considered valid' + ); + } + + public function testFormInvalidWhenCommentMissing() + { + $form = $this->getRequestForm(array( + 'author' => 'Author', + 'comment' => '', + 'persistent' => '0', + 'expire' => '0', + 'sticky' => '0', + 'notify' => '0', + 'btn_submit' => 'Submit' + ), self::FORMCLASS); + + $this->assertFalse( $form->isSubmittedAndValid(), - "Assert that correct expire time acknowledgement is considered valid" + 'Missing comment must be considered not valid' + ); + } + + public function testFormInvalidWhenExpireTimeMissingAndExpireSet() + { + $form = $this->getRequestForm(array( + 'author' => 'Author', + 'comment' => 'Comment', + 'persistent' => '0', + 'expire' => '1', + 'sticky' => '0', + 'notify' => '0', + 'btn_submit' => 'Submit' + ), self::FORMCLASS); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'If expire is set and expire time is missing, the form must not be valid' + ); + } + + public function testFormInvalidWhenExpireTimeIsIncorrectAndExpireSet() + { + $form = $this->getRequestForm(array( + 'author' => 'Author', + 'comment' => 'Comment', + 'persistent' => '0', + 'expire' => '1', + 'expiretime' => 'Not a date', + 'sticky' => '0', + 'notify' => '0', + 'btn_submit' => 'Submit' + ), self::FORMCLASS); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'If expire is set and expire time is incorrect, the form must not be valid' ); } } +// @codingStandardsIgnoreStop diff --git a/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php b/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php index ed849436c..aaba6575c 100644 --- a/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php @@ -30,6 +30,8 @@ namespace Test\Monitoring\Forms\Command { require_once realpath($base.'library/Icinga/Web/Form/InvalidCSRFTokenException.php'); require_once realpath($base.'library/Icinga/Web/Form/Element/Note.php'); require_once realpath($base.'library/Icinga/Web/Form/Element/DateTimePicker.php'); + require_once realpath($base . 'modules/monitoring/application/forms/Command/CommandForm.php'); + require_once realpath($base . 'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php'); use \Zend_View; use \Zend_Form; diff --git a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php index 3eedc454d..354aad114 100644 --- a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php @@ -1,68 +1,63 @@ setRequest($this->getRequest()); - $form->buildForm(); - - $this->assertCount(6, $form->getElements()); - } - + const FORMCLASS = 'Monitoring\Form\Command\CommentForm'; public function testCorrectCommentValidation() { $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => 'test2', - 'sticky' => '0', - 'btn_submit' => 'foo' + 'author' => 'Author', + 'comment' => 'Comment', + 'sticky' => '0', + 'btn_submit' => 'Submit' ), self::FORMCLASS); $this->assertTrue( $form->isSubmittedAndValid(), - "Asserting correct comment form to be considered valid" + 'Legal request data must be considered valid' ); } - public function testRecognizeMissingCommentText() + public function testFormInvalidWhenCommentMissing() { $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => '', - 'sticky' => '0' + 'author' => 'Author', + 'comment' => '', + 'sticky' => '0', + 'btn_submit' => 'Submit' + ), self::FORMCLASS); + $this->assertFalse( $form->isSubmittedAndValid(), - "Asserting missing comment text in comment form to cause validation errors" + 'Missing comment must be considered not valid' ); } - public function testRecognizeMissingCommentAuthor() + public function testFormInvalidWhenAuthorMissing() { $form = $this->getRequestForm(array( - 'author' => '', - 'comment' => 'test2', - 'sticky' => '0' + 'author' => '', + 'comment' => 'Comment', + 'sticky' => '0', + 'btn_submit' => 'Submit' ), self::FORMCLASS); + $this->assertFalse( $form->isSubmittedAndValid(), - "Asserting missing comment author to cause validation errors" + 'Missing author must be considered not valid' ); } } +// @codingStandardsIgnoreEnd diff --git a/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php b/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php index d5fd41390..9f3687919 100644 --- a/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php @@ -1,28 +1,31 @@ getRequestForm(array( - 'comment' => 'TEST COMMENT', - 'author' => 'LAOLA', - 'btn_submit' => 'foo' - ), "Monitoring\Form\Command\CustomNotificationForm"); - $form->buildForm(); + 'author' => 'Author', + 'comment' => '', + 'btn_submit' => 'Submit' + ), self::FORM_CLASS); - $this->assertCount(7, $form->getElements()); - $this->assertTrue($form->isSubmittedAndValid()); + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Missing comment must be considered not valid' + ); } } +// @codingStandardsIgnoreEnd diff --git a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php index 24abc39f3..dd7ade4ed 100644 --- a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php @@ -1,57 +1,56 @@ getRequestForm(array( - 'minutes' => 12, - 'btn_submit' => 'foo' - ), 'Monitoring\Form\Command\DelayNotificationForm'); - - $form->buildForm(); - $this->assertCount(5, $form->getElements()); - - $element = $form->getElement('minutes'); - $this->assertInstanceOf('Zend_Form_Element_Text', $element); - $this->assertEquals('0', $element->getValue(), "Assert a correct default value in minutes"); - $this->assertTrue($element->isRequired(), "Assert minutes to be declared as required"); - - $this->assertTrue( - $form->isSubmittedAndValid(), - "Assert a correct DelayNotificationForm to be considered valid" - ); - - $this->assertEquals('12', $form->getValue('minutes'), "Assert the minutes field to be correctly populated"); - } - - public function testInvalidMinuteValue() - { - $form = $this->getRequestForm(array( - 'minutes' => 'SCHAHH-LAHH-LAHH', - 'btn_submit' => 'foo' - ), 'Monitoring\Form\Command\DelayNotificationForm'); - - $form->buildForm(); + 'minutes' => '', + 'btn_submit' => 'Submit' + ), self::FORM_CLASS); $this->assertFalse( $form->isSubmittedAndValid(), - "Asserting invalid minutes (NaN) to cause validation errors" + 'Missing notification delay must be considered invalid' ); + } - $errors = $form->getErrors('minutes'); - $this->assertCount(1, $errors, "Asserting an error to be added when the minutes value is invalid"); - $this->assertEquals('notBetween', $errors[0], "Assert correct error message"); + public function testFormInvalidWhenNotificationDelayNaN() + { + $form = $this->getRequestForm(array( + 'minutes' => 'A String', + 'btn_submit' => 'Submit' + ), self::FORM_CLASS); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Incorrect notification delay, i.e. NaN must be considered invalid' + ); + } + + public function testFormInvalidWhenNotificationDelayOverflows() + { + $form = $this->getRequestForm(array( + 'minutes' => DelayNotificationForm::MAX_DELAY + 1, + 'btn_submit' => 'Submit' + ), self::FORM_CLASS); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Notification delay bigger than constant "DelayNotificationForm::MAX_DELAY" must be considered invalid' + ); } } +// @codingStandardsIgnoreEnd diff --git a/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php b/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php index 442a367b6..33e1cf2b9 100644 --- a/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php @@ -1,102 +1,70 @@ getRequestForm(array( - 'checktime' => '2013-10-19 17:30:00', - 'forcecheck' => 1, - 'btn_submit' => 'foo' - ), self::FORMCLASS); - $form->buildForm(); - - $this->assertCount(6, $form->getElements()); - - $this->assertTrue( - $form->isSubmittedAndValid(), - 'Asserting a reschedule form with correct time and forececheck=1 to be valid' - ); - $form = $this->getRequestForm(array( - 'checktime' => '2013-10-19 17:30:00', - 'forcecheck' => 0, - 'btn_submit' => 'foo' - ), self::FORMCLASS); - - $this->assertTrue( - $form->isSubmittedAndValid(), - 'Asserting a reschedule form with correct time and forecheck=0 to be valid' - ); + date_default_timezone_set('UTC'); + DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); } - public function testInValidRescheduleChecktimeSubmissions() + public function testFormInvalidWhenChecktimeIsIncorrect() { $form = $this->getRequestForm(array( - 'checktime' => '2013-24-12 17:30:00', - 'forcecheck' => 1 - ), self::FORMCLASS); + 'checktime' => '2013-24-12 17:30:00', + 'forcecheck' => 0, + 'btn_submit' => 'Submit' + ), self::FORM_CLASS); $this->assertFalse( $form->isSubmittedAndValid(), - 'Asserting an logically invalid checktime to be considered as invalid reschedule data' + 'Asserting a logically incorrect checktime as invalid' ); - $form = $this->getRequestForm(array( - 'checktime' => 'AHAHA', - 'forcecheck' => 1 - ), self::FORMCLASS); - + $form2 = $this->getRequestForm(array( + 'checktime' => 'Captain Morgan', + 'forcecheck' => 1, + 'btn_submit' => 'Submit' + ), self::FORM_CLASS); $this->assertFalse( - $form->isSubmittedAndValid(), - 'Asserting an invalid non-numeric checktime to be considered as invalid reschedule data' + $form2->isSubmittedAndValid(), + 'Providing arbitrary strings as checktime must be considered invalid' ); - } - public function testChildrenFlag() - { + $form3 = $this->getRequestForm(array( + 'checktime' => '', + 'forcecheck' => 0, + 'btn_submit' => 'Submit' + ), self::FORM_CLASS); - $form = new RescheduleNextCheckForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(true); - $form->buildForm(); - $notes1 = $form->getNotes(); - $form = null; - - $form = new RescheduleNextCheckForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(false); - $form->buildForm(); - $notes2 = $form->getNotes(); - $form = null; - - $form = new RescheduleNextCheckForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(); - $form->buildForm(); - $notes3 = $form->getNotes(); - $form = null; - - $this->assertEquals($notes1, $notes3); - $this->assertNotEquals($notes1, $notes2); + $this->assertFalse( + $form3->isSubmittedAndValid(), + 'Missing checktime must be considered invalid' + ); } } +// @codingStandardsIgnoreStop diff --git a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php index 5d5d43331..8b60a0ca7 100644 --- a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php @@ -1,39 +1,50 @@ new DateTimeZone('UTC'))); + } + public function testCorrectFormElementCreation() { - $formFixed = $this->getRequestForm(array(), self::FORMCLASS); + $formFixed = $this->getRequestForm(array(), self::FORM_CLASS); $formFixed->buildForm(); $formFlexible = $this->getRequestForm(array( 'type' => 'flexible' - ), self::FORMCLASS); + ), self::FORM_CLASS); $formFlexible->buildForm(); - $this->assertCount(11, $formFixed->getElements()); - $this->assertCount(13, $formFlexible->getElements()); - - $form = $this->getRequestForm(array(), self::FORMCLASS); + $form = $this->getRequestForm(array(), self::FORM_CLASS); $form->setWithChildren(true); $form->buildForm(); - - $this->assertCount(12, $form->getElements()); } - public function testCorrectValidationWithChildrend() { $form = $this->getRequestForm(array( @@ -47,8 +58,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'minutes' => '', 'btn_submit' => 'foo', // 'childobjects' => '', - ), self::FORMCLASS); - + ), self::FORM_CLASS); $form->setWithChildren(true); @@ -67,14 +77,13 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'minutes' => '10', 'btn_submit' => 'foo' // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); $this->assertTrue( $form->isSubmittedAndValid(), 'Asserting a correct flexible downtime form to be considered valid' ); - } public function testMissingFlexibleDurationRecognition() @@ -89,7 +98,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); $this->assertFalse( @@ -100,7 +109,6 @@ class ScheduleDowntimeFormTest extends BaseFormTest public function testMissingAuthorRecognition() { - $form = $this->getRequestForm(array( 'author' => '', 'comment' => 'DING DING', @@ -111,7 +119,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); @@ -133,7 +141,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); @@ -155,7 +163,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); $this->assertFalse( @@ -176,7 +184,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); $this->assertFalse( @@ -187,7 +195,6 @@ class ScheduleDowntimeFormTest extends BaseFormTest public function testInvalidEndTimeRecognition() { - $form = $this->getRequestForm(array( 'author' => 'OK', 'comment' => 'OK', @@ -198,7 +205,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); $this->assertFalse( @@ -220,7 +227,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '-1', 'minutes' => '12', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); $this->assertFalse( @@ -241,7 +248,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '12', 'minutes' => 'DING', // 'childobjects' => '', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(true); $this->assertFalse( @@ -264,13 +271,13 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'minutes' => '', 'btn_submit' => 'foo', 'childobjects' => '0', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(false); $this->assertTrue( $form->isSubmittedAndValid(), - "Assert a correct schedule downtime without children form to be considered valid" + 'Assert a correct schedule downtime without children form to be considered valid' ); } @@ -285,12 +292,12 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', 'childobjects' => 'AHA', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(false); $this->assertFalse( $form->isSubmittedAndValid(), - "Assert and incorrect (non-numeric) childobjects value to cause validation errors" + 'Assert and incorrect (non-numeric) childobjects value to cause validation errors' ); $form = $this->getRequestForm(array( @@ -303,18 +310,18 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'hours' => '', 'minutes' => '', 'childobjects' => '4', - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setWithChildren(false); $this->assertFalse( $form->isSubmittedAndValid(), - "Assert and incorrect (numeric) childobjects value to cause validation errors" + 'Assert and incorrect (numeric) childobjects value to cause validation errors' ); } public function testTimeRange() { - $form = $this->getRequestForm(array(), self::FORMCLASS); + $form = $this->getRequestForm(array(), self::FORM_CLASS); $form->buildForm(); $time1 = $form->getElement('starttime')->getValue(); @@ -323,3 +330,4 @@ class ScheduleDowntimeFormTest extends BaseFormTest $this->assertEquals(3600, ($time2 - $time1)); } } +// @codingStandardsIgnoreStop diff --git a/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php b/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php index 6074a90ac..e469658bf 100644 --- a/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php +++ b/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php @@ -1,38 +1,37 @@ getRequestForm(array(), self::FORMCLASS); + $form = $this->getRequestForm(array(), self::FORM_CLASS); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $options = $form->getOptions(); - $this->assertCount(4, $options, "Assert correct number of states in service passive checks form"); - $this->assertEquals('OK', $options[0], "Assert OK state to be available in service passive check form"); - $this->assertEquals('WARNING', $options[1], "Assert WARNING state to be available in service passive check form"); - $this->assertEquals('CRITICAL', $options[2], "Assert CRITICAL state to be available in service passive check form"); - $this->assertEquals('UNKNOWN', $options[3], "Assert UNKNOWN state to be available in service passive check form"); + $this->assertCount(4, $options, 'Assert correct number of states in service passive checks form'); + $this->assertEquals('OK', $options[0], 'Assert OK state to be available in service passive check form'); + $this->assertEquals('WARNING', $options[1], 'Assert WARNING state to be available in service passive check form'); + $this->assertEquals('CRITICAL', $options[2], 'Assert CRITICAL state to be available in service passive check form'); + $this->assertEquals('UNKNOWN', $options[3], 'Assert UNKNOWN state to be available in service passive check form'); $form->setType(SubmitPassiveCheckResultForm::TYPE_HOST); $options = $form->getOptions(); - $this->assertCount(3, $options, "Assert correct number of states in host passive checks form"); - $this->assertEquals('UP', $options[0], "Assert UP state to be available in host passive check form"); - $this->assertEquals('DOWN', $options[1], "Assert DOWN state to be available in host passive check form"); - $this->assertEquals('UNREACHABLE', $options[2], "Assert UNREACHABLE state to be available in host passive check form"); + $this->assertCount(3, $options, 'Assert correct number of states in host passive checks form'); + $this->assertEquals('UP', $options[0], 'Assert UP state to be available in host passive check form'); + $this->assertEquals('DOWN', $options[1], 'Assert DOWN state to be available in host passive check form'); + $this->assertEquals('UNREACHABLE', $options[2], 'Assert UNREACHABLE state to be available in host passive check form'); } /** @@ -41,17 +40,15 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest */ public function testMissingTypeThrowingException() { - $form = $this->getRequestForm(array(), self::FORMCLASS); + $form = $this->getRequestForm(array(), self::FORM_CLASS); $form->buildForm(); } public function testCorrectFormCreation() { - $form = $this->getRequestForm(array(), self::FORMCLASS); + $form = $this->getRequestForm(array(), self::FORM_CLASS); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->buildForm(); - - $this->assertCount(6, $form->getElements(), "Assert correct number of elements in form"); } public function testCorrectServicePassiveCheckSubmission() @@ -61,13 +58,13 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest 'checkoutput' => 'DING', 'performancedata' => '', 'btn_submit' => 'foo' - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $this->assertTrue( $form->isSubmittedAndValid(), - "Assert a correct passive service check form to pass form validation" + 'Assert a correct passive service check form to pass form validation' ); } @@ -77,12 +74,12 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest 'pluginstate' => 0, 'checkoutput' => '', 'performancedata' => '' - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $this->assertFalse( $form->isSubmittedAndValid(), - "Assert empty checkoutput to cause validation errors in passive service check " + 'Assert empty checkoutput to cause validation errors in passive service check ' ); } @@ -92,12 +89,12 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest 'pluginstate' => 'LA', 'checkoutput' => 'DING', 'performancedata' => '' - ), self::FORMCLASS); + ), self::FORM_CLASS); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $this->assertFalse( $form->isSubmittedAndValid(), - "Assert invalid (non-numeric) state to cause validation errors in passive service check" + 'Assert invalid (non-numeric) state to cause validation errors in passive service check' ); } } diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php index 5a5ca1831..4204a661f 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php @@ -1,11 +1,7 @@ getLocalTestPipe(); try { - $comment = new Comment("author", "commenttext"); + $notification = new CustomNotification('Author', 'Comment'); $pipe->sendCustomNotification(array( (object) array( - "host_name" => "host1", - "service_description" => "service1" + 'host_name' => 'Host', + 'service_description' => 'Service' ) - ), $comment); + ), $notification); $this->assertCommandSucceeded( - "SEND_CUSTOM_SVC_NOTIFICATION;host1;service1;0;author;commenttext" + 'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;0;Author;Comment' ); } catch (Exception $e) { $this->cleanup(); @@ -449,15 +454,15 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase { $pipe = $this->getLocalTestPipe(); try { - $comment = new Comment('author', 'commenttext'); + $notification = new CustomNotification('Author', 'Comment', true, true); $pipe->sendCustomNotification(array( (object) array( - 'host_name' => 'host' + 'host_name' => 'Host', + 'service_description' => 'Service' ) - ), $comment, Commandpipe::NOTIFY_FORCED, Commandpipe::NOTIFY_BROADCAST, Commandpipe::NOTIFY_INCREMENT); - + ), $notification); $this->assertCommandSucceeded( - 'SEND_CUSTOM_HOST_NOTIFICATION;host;7;author;commenttext' + 'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;3;Author;Comment' ); } catch (Exception $e) { $this->cleanup(); @@ -493,3 +498,4 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase $this->cleanup(); } } +// @codingStandardsIgnoreStop