Merge branch 'bugfix/command-forms-lack-help-messages-4524'

fixes #4524
This commit is contained in:
Jannis Moßhammer 2013-08-16 17:42:29 +02:00
commit 6928a806ab
22 changed files with 920 additions and 583 deletions

View File

@ -2,24 +2,24 @@
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
/** /**
* This file is part of Icinga 2 Web. * This file is part of Icinga 2 Web.
* *
* Icinga 2 Web - Head for multiple monitoring backends. * Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team * Copyright (C) 2013 Icinga Development Team
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* *
* @copyright 2013 Icinga Development Team <info@icinga.org> * @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org> * @author Icinga Development Team <info@icinga.org>
@ -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 array $objects Affected monitoring objects
* @param Comment $comment comment to use in the notification * @param CustomNotification $notification
* @param int [$...] Optional list of Notification flags which will be used as the option parameter
*/ */
public function sendCustomNotification($objects, Comment $comment, $optionsVarList = 0/*, ...*/) public function sendCustomNotification(array $objects, CustomNotification $notification)
{ {
$args = func_get_args(); foreach ($objects as $hostOrService) {
// logical OR for all notification options if (isset($hostOrService->service_description) && isset($hostOrService->host_name)) {
for ($i = 3; $i < count($args); $i++) { // Assume service
$optionsVarList |= $args[$i]; $command = sprintf(
} $notification->getFormatString(self::TYPE_SERVICE),
$hostOrService->host_name,
foreach ($objects as $object) { $hostOrService->service_description
$type = $this->getObjectType($object); );
$msg = 'SEND_CUSTOM_'.(($type == self::TYPE_SERVICE) ? 'SVC' : 'HOST' ).'_NOTIFICATION'; } elseif (isset($hostOrService->host_name)) {
$msg .= ';'.$object->host_name; // Assume host
if ($type == self::TYPE_SERVICE) { $command = sprintf(
$msg .= ';'.$object->service_description; $notification->getFormatString(self::TYPE_HOST),
$hostOrService->host_name
);
} else {
continue;
} }
$msg .= ';'.$optionsVarList; $this->send($command);
$msg .= ';'.$comment->author;
$msg .= ';'.$comment->comment;
$this->send($msg);
} }
} }

View File

@ -0,0 +1,109 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Protocol\Commandpipe;
/**
* Custom notification about hosts or services sent to Icinga's command pipe
*/
class CustomNotification
{
/**
* Notification comment
*
* @var string
*/
private $comment;
/**
* Notification author
*
* @var string
*/
private $author;
/**
* Whether to force the notification to be sent out, regardless of the time restrictions, whether or not
* notifications are enabled, etc.
*
* @var bool
*/
private $forced;
/**
* Whether the notification is sent out to all normal (non-escalated) and escalated contacts
*
* @var bool
*/
private $broadcast;
/**
* 1 = Broadcast (send notification to all normal and all escalated contacts for the host)
*/
const NOTIFY_BROADCAST = 1;
/**
* 2 = Forced (notification is sent out regardless of current time, whether or not notifications are enabled, etc.)
*/
const NOTIFY_FORCED = 2;
/**
* Constructor
*
* @param string $author Notification author
* @param string $comment Notification comment
* @param bool $forced Whether to force the notification to be sent out, regardless of the time
* restrictions, whether or not notifications are enabled, etc.
* @param bool $broadcast Whether the notification is sent out to all normal (non-escalated) and escalated
* contacts
*/
public function __construct($author, $comment, $forced = false, $broadcast = false)
{
$this->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;
}
}

View File

@ -431,12 +431,7 @@ class Monitoring_CommandController extends ModuleActionController
$this->setForm($form); $this->setForm($form);
if ($form->IsSubmittedAndValid() === true) { if ($form->IsSubmittedAndValid() === true) {
$author = $this->getRequest()->getUser()->getUsername(); $this->target->sendCustomNotification($this->view->objects, $form->getCustomNotification());
$this->target->sendCustomNotification(
$this->view->objects,
new Comment($author, $form->getComment()),
$form->getOptions()
);
} }
} }

View File

@ -28,30 +28,35 @@
namespace Monitoring\Form\Command; namespace Monitoring\Form\Command;
use Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Web\Form\Element\DateTimePicker;
use Icinga\Web\Form\Element\Note; use \Icinga\Web\Form\Element\Note;
use Icinga\Protocol\Commandpipe\Acknowledgement; use \Icinga\Protocol\Commandpipe\Acknowledgement;
use Icinga\Protocol\Commandpipe\Comment; use \Icinga\Protocol\Commandpipe\Comment;
use Icinga\Util\DateTimeFactory; use \Icinga\Util\DateTimeFactory;
/** /**
* Form for problem acknowledgements * Form for problem acknowledgements
*/ */
class AcknowledgeForm extends CommandForm class AcknowledgeForm extends CommandForm
{ {
/**
* Initialize form
*/
public function init()
{
$this->setName('AcknowledgeForm');
}
/** /**
* Create the form's elements * Create the form's elements
*/ */
protected function create() 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($this->createAuthorField());
$this->addElement( $this->addElement(
@ -63,66 +68,96 @@ class AcknowledgeForm extends CommandForm
'required' => true '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( $this->addElement(
'checkbox', 'checkbox',
'persistent', 'persistent',
array( array(
'label' => t('Persistent comment'), 'label' => t('Persistent Comment'),
'value' => false 'value' => false
) )
); );
$this->addElement(
$expireNote = new Note( 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(
array( array(
'name' => 'expiretime', 'name' => 'persistentnote',
'label' => t('Expire time'), 'value' => t(
'value' => $now->getTimestamp() + 3600 '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( $this->addElement(
'checkbox', 'checkbox',
'sticky', 'sticky',
array( array(
'label' => t('Sticky acknowledgement'), 'label' => t('Sticky Acknowledgement'),
'value' => false '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', 'checkbox',
'notify', 'notify',
array( array(
'label' => t('Send notification'), 'label' => t('Send Notification'),
'value' => false '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(); parent::create();
} }
@ -143,7 +189,8 @@ class AcknowledgeForm extends CommandForm
/** /**
* Add validator for dependent fields * Add validator for dependent fields
* *
* @param array $data * @param array $data
*
* @see \Icinga\Web\Form::preValidation() * @see \Icinga\Web\Form::preValidation()
*/ */
protected function preValidation(array $data) 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() public function getAcknowledgement()
{ {
$expireTime = -1; $expireTime = -1;
@ -170,6 +222,5 @@ class AcknowledgeForm extends CommandForm
$expireTime, $expireTime,
$this->getValue('sticky') $this->getValue('sticky')
); );
} }
} }

View File

@ -38,19 +38,9 @@ use \Zend_Validate_Date;
*/ */
class CommandForm extends Form 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 * Array of messages
* *
* @var string[] * @var string[]
*/ */
private $notes = array(); private $notes = array();
@ -142,7 +132,7 @@ class CommandForm extends Form
$authorField = new Zend_Form_Element_Hidden( $authorField = new Zend_Form_Element_Hidden(
array( array(
'name' => 'author', 'name' => 'author',
'label' => t('Author name'), 'label' => t('Author (Your Name)'),
'value' => $authorName, 'value' => $authorName,
'required' => true 'required' => true
) )
@ -159,34 +149,4 @@ class CommandForm extends Form
return $authorField; 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;
}
} }

View File

@ -37,6 +37,7 @@ class CommandWithIdentifierForm extends CommandForm
{ {
/** /**
* Identifier for data field * Identifier for data field
*
* @var string * @var string
*/ */
private $fieldName = 'objectid'; private $fieldName = 'objectid';
@ -52,6 +53,7 @@ class CommandWithIdentifierForm extends CommandForm
/** /**
* Setter for field label * Setter for field label
*
* @param string $fieldLabel * @param string $fieldLabel
*/ */
public function setFieldLabel($fieldLabel) public function setFieldLabel($fieldLabel)
@ -61,6 +63,7 @@ class CommandWithIdentifierForm extends CommandForm
/** /**
* Getter for field label * Getter for field label
*
* @return string * @return string
*/ */
public function getFieldLabel() public function getFieldLabel()
@ -70,6 +73,7 @@ class CommandWithIdentifierForm extends CommandForm
/** /**
* Setter for field name * Setter for field name
*
* @param string $fieldName * @param string $fieldName
*/ */
public function setFieldName($fieldName) public function setFieldName($fieldName)
@ -79,6 +83,7 @@ class CommandWithIdentifierForm extends CommandForm
/** /**
* Getter for field name * Getter for field name
*
* @return string * @return string
*/ */
public function getFieldName() public function getFieldName()

View File

@ -28,18 +28,28 @@
namespace Monitoring\Form\Command; 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 * Form for adding comment commands
*/ */
class CommentForm extends CommandForm class CommentForm extends CommandForm
{ {
/** /**
* Interface method to build the form * Create the form's elements
* @see CommandForm::create
*/ */
protected function create() 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($this->createAuthorField());
$this->addElement( $this->addElement(
@ -51,13 +61,36 @@ class CommentForm extends CommandForm
'required' => true '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( $this->addElement(
'checkbox', 'checkbox',
'persistent', 'persistent',
array( array(
'label' => t('Persistent'), '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(); parent::create();
} }
/**
* Create comment from request data
*
* @return \Icinga\Protocol\Commandpipe\Comment
*/
public function getComment() public function getComment()
{ {
return new Comment($this->getAuthorName(), $this->getValue('comment'), $this->getValue('persistent')); return new Comment($this->getAuthorName(), $this->getValue('comment'), $this->getValue('persistent'));

View File

@ -28,7 +28,8 @@
namespace Monitoring\Form\Command; namespace Monitoring\Form\Command;
use Zend_Form_Element_Hidden; use \Icinga\Web\Form\Element\Note;
use \Icinga\Protocol\Commandpipe\CustomNotification;
/** /**
* For for command CustomNotification * For for command CustomNotification
@ -36,11 +37,23 @@ use Zend_Form_Element_Hidden;
class CustomNotificationForm extends CommandForm class CustomNotificationForm extends CommandForm
{ {
/** /**
* Interface method to build the form * Create the form's elements
* @see CommandForm::create
*/ */
protected function create() 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($this->createAuthorField());
$this->addElement( $this->addElement(
@ -52,14 +65,38 @@ class CustomNotificationForm extends CommandForm
'required' => true '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( $this->addElement(
'checkbox', 'checkbox',
'force', 'forced',
array( array(
'label' => t('Forced') '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( $this->addElement(
'checkbox', 'checkbox',
@ -68,26 +105,36 @@ class CustomNotificationForm extends CommandForm
'label' => t('Broadcast') '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(); parent::create();
} }
public function getComment() /**
* Create Custom Notification from request data
*
* @return \Icinga\Protocol\Commandpipe\CustomNotification
*/
public function getCustomNotification()
{ {
return $this->getValue('comment'); return new CustomNotification(
} $this->getAuthorName(),
$this->getValue('comment'),
public function getOptions() $this->getValue('forced'),
{ $this->getValue('broadcast')
$value = 0; );
if ($this->getValue('force')) {
$value |= 2;
}
if ($this->getValue('broadcast')) {
$value |= 1;
}
return $value;
} }
} }

View File

@ -28,6 +28,8 @@
namespace Monitoring\Form\Command; namespace Monitoring\Form\Command;
use \Icinga\Web\Form\Element\Note;
/** /**
* Form for the delay notification command * Form for the delay notification command
*/ */
@ -40,11 +42,17 @@ class DelayNotificationForm extends CommandForm
/** /**
* Create the form's elements * Create the form's elements
*
* @see CommandForm::create()
*/ */
protected function 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( $this->addElement(
'text', 'text',
'minutes', 'minutes',
@ -65,12 +73,10 @@ class DelayNotificationForm extends CommandForm
) )
) )
); );
$this->addNote( $this->addNote(
t( t(
'Delay the next problem notification. The notification delay will be ' 'The notification delay will be disregarded if the host/service changes state before the next '
. 'disregarded if the host/service changes state before the next notification is ' . 'notification is scheduled to be sent out.'
. 'scheduled to be sent out.'
) )
); );
@ -79,8 +85,13 @@ class DelayNotificationForm extends CommandForm
parent::create(); parent::create();
} }
/**
* Return the currently set delay time in seconds
*
* @return integer
*/
public function getDelayTime() public function getDelayTime()
{ {
return $this->getValue('minutes')*60; return $this->getValue('minutes') * 60;
} }
} }

View File

@ -28,13 +28,13 @@
namespace Monitoring\Form\Command; namespace Monitoring\Form\Command;
use \DateTime;
use \Zend_Form_Element_Checkbox; use \Zend_Form_Element_Checkbox;
use \Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Web\Form\Element\DateTimePicker;
use \Icinga\Util\DateTimeFactory; use \Icinga\Util\DateTimeFactory;
use \Icinga\Web\Form\Element\Note;
/** /**
* Form for RescheduleNextCheck * Form for scheduling checks
*/ */
class RescheduleNextCheckForm extends WithChildrenCommandForm class RescheduleNextCheckForm extends WithChildrenCommandForm
{ {
@ -43,37 +43,76 @@ class RescheduleNextCheckForm extends WithChildrenCommandForm
*/ */
protected function create() protected function create()
{ {
$now = DateTimeFactory::create(); $this->addElement(
$dateElement = new DateTimePicker( new Note(
array( array(
'name' => 'checktime', 'name' => 'commanddescription',
'label' => t('Check time'), 'value' => t(
'value' => $now->getTimestamp() '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($dateElement); )
$checkBox = new Zend_Form_Element_Checkbox(
array(
'name' => 'forcecheck',
'label' => t('Force check'),
'value' => true
) )
); );
$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) { 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 { } 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(); parent::create();
} }
/**
* Return whether this is a forced check (force is checked)
*
* @return bool
*/
public function isForced() public function isForced()
{ {
return $this->getValue('forcecheck') == true; return $this->getValue('forcecheck') == true;

View File

@ -31,13 +31,14 @@ namespace Monitoring\Form\Command;
use \Zend_Form_Element_Text; use \Zend_Form_Element_Text;
use \Zend_Validate_GreaterThan; use \Zend_Validate_GreaterThan;
use \Zend_Validate_Digits; use \Zend_Validate_Digits;
use Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Web\Form\Element\DateTimePicker;
use Icinga\Protocol\Commandpipe\Downtime; use \Icinga\Protocol\Commandpipe\Downtime;
use Icinga\Protocol\Commandpipe\Comment; use \Icinga\Protocol\Commandpipe\Comment;
use Icinga\Util\DateTimeFactory; use \Icinga\Util\DateTimeFactory;
use \Icinga\Web\Form\Element\Note;
/** /**
* Form for any ScheduleDowntime command * Form for scheduling downtimes
*/ */
class ScheduleDowntimeForm extends WithChildrenCommandForm class ScheduleDowntimeForm extends WithChildrenCommandForm
{ {
@ -73,12 +74,24 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
} }
/** /**
* Interface method to build the form * Create the form's elements
*
* @see CommandForm::create
*/ */
protected function create() 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($this->createAuthorField());
$this->addElement( $this->addElement(
@ -90,6 +103,18 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
'required' => true '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) * @TODO: Display downtime list (Bug #4496)
@ -119,30 +144,49 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
); );
$now = DateTimeFactory::create(); $now = DateTimeFactory::create();
$dateTimeStart = new DateTimePicker( $this->addElement(
array( new DateTimePicker(
'name' => 'starttime', array(
'label' => t('Start time'), 'name' => 'starttime',
'value' => $now->getTimestamp() 'label' => t('Start Time'),
'value' => $now->getTimestamp()
)
) )
); );
$dateTimeEnd = new DateTimePicker( $this->addElement(
array( new Note(
'name' => 'endtime', array(
'label' => t('End time'), 'name' => 'stattimenote',
'value' => $now->getTimestamp() + 3600 '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( $this->addElement(
'select', 'select',
'type', 'type',
array( array(
'label' => t('Downtime type'), 'label' => t('Downtime Type'),
'multiOptions' => $this->getDowntimeTypeOptions(), 'multiOptions' => $this->getDowntimeTypeOptions(),
'required' => true, 'required' => true,
'validators' => array( 'validators' => array(
array( array(
'InArray', 'InArray',
true, 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')); $this->enableAutoSubmit(array('type'));
if ($this->getRequest()->getPost('type') === self::TYPE_FLEXIBLE) { if ($this->getRequest()->getPost('type') === self::TYPE_FLEXIBLE) {
$hoursText = new Zend_Form_Element_Text('hours'); $hoursText = new Zend_Form_Element_Text('hours');
$hoursText->setLabel(t('Flexible duration')); $hoursText->setLabel(t('Flexible Duration'));
$hoursText->setAttrib('style', 'width: 40px;'); $hoursText->setAttrib('style', 'width: 40px;');
$hoursText->setValue(1); $hoursText->setValue(1);
$hoursText->addDecorator('HtmlTag', array('tag' => 'dd', 'openOnly' => true)); $hoursText->addDecorator('HtmlTag', array('tag' => 'dd', 'openOnly' => true));
@ -170,7 +227,6 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
} }
) )
); );
$minutesText = new Zend_Form_Element_Text('minutes'); $minutesText = new Zend_Form_Element_Text('minutes');
$minutesText->setLabel(t('Minutes')); $minutesText->setLabel(t('Minutes'));
$minutesText->setAttrib('style', 'width: 40px;'); $minutesText->setAttrib('style', 'width: 40px;');
@ -185,19 +241,30 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
} }
) )
); );
$this->addElements(array($hoursText, $minutesText)); $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) { 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 { } else {
$this->addElement( $this->addElement(
'select', 'select',
'childobjects', 'childobjects',
array( array(
'label' => t('Child objects'), 'label' => t('Child Objects'),
'required' => true, 'required' => true,
'multiOptions' => array( 'multiOptions' => array(
0 => t('Do nothing with child objects'), 0 => t('Do nothing with child objects'),
@ -219,11 +286,10 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
) )
) )
); );
$this->addNote(t('TODO: Help message when with children is disabled'));
$this->addNote(t('Schedule downtime for this object.'));
} }
$this->setSubmitLabel(t('Schedule downtime')); $this->setSubmitLabel(t('Schedule Downtime'));
parent::create(); 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() public function getDowntime()
{ {
@ -269,7 +335,7 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
); );
$duration = 0; $duration = 0;
if ($this->getValue('type') === self::TYPE_FLEXIBLE) { 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'); $starttime = $this->getValue('starttime');
$endtime = $this->getValue('endtime'); $endtime = $this->getValue('endtime');
@ -281,7 +347,7 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
$duration, $duration,
$this->getValue('triggered') $this->getValue('triggered')
); );
if (! $this->getWithChildren()) { if (!$this->getWithChildren()) {
switch ($this->getValue('childobjects')) { switch ($this->getValue('childobjects')) {
case 1: case 1:
$downtime->setType(Downtime::TYPE_WITH_CHILDREN_TRIGGERED); $downtime->setType(Downtime::TYPE_WITH_CHILDREN_TRIGGERED);

View File

@ -28,7 +28,8 @@
namespace Monitoring\Form\Command; namespace Monitoring\Form\Command;
use Icinga\Exception\ProgrammingError; use \Icinga\Exception\ProgrammingError;
use \Icinga\Web\Form\Element\Note;
/** /**
* Form for submitting passive check results * Form for submitting passive check results
@ -59,6 +60,7 @@ class SubmitPassiveCheckResultForm extends CommandForm
/** /**
* Setup plugin states * Setup plugin states
*
* @see Zend_Form::init * @see Zend_Form::init
*/ */
public function init() public function init()
@ -85,6 +87,7 @@ class SubmitPassiveCheckResultForm extends CommandForm
/** /**
* Setter for type * Setter for type
*
* @param string $type * @param string $type
*/ */
public function setType($type) public function setType($type)
@ -94,6 +97,7 @@ class SubmitPassiveCheckResultForm extends CommandForm
/** /**
* Getter for type * Getter for type
*
* @return string * @return string
*/ */
public function getType() public function getType()
@ -103,6 +107,7 @@ class SubmitPassiveCheckResultForm extends CommandForm
/** /**
* Return array of options * Return array of options
*
* @return array * @return array
* @throws \Icinga\Exception\ProgrammingError * @throws \Icinga\Exception\ProgrammingError
*/ */
@ -117,20 +122,30 @@ class SubmitPassiveCheckResultForm extends CommandForm
/** /**
* Create the form's elements * Create the form's elements
*
* @see CommandForm::create()
*/ */
protected function 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( $this->addElement(
'select', 'select',
'pluginstate', 'pluginstate',
array( array(
'label' => t('Plugin state'), 'label' => t('Check Result'),
'multiOptions' => $this->getOptions(), 'multiOptions' => $this->getOptions(),
'required' => true, 'required' => true,
'validators' => array( 'validators' => array(
array( array(
'Digits', 'Digits',
true 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( $this->addElement(
'textarea', 'textarea',
'checkoutput', 'checkoutput',
array( array(
'label' => t('Check output'), 'label' => t('Check Output'),
'rows' => 2, 'rows' => 2,
'required' => true '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', 'textarea',
'performancedata', 'performancedata',
array( array(
'label' => t('Performance data'), 'label' => t('Performance Data'),
'rows' => 2 '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(); parent::create();
} }
/**
* Return the entered object state as an integer
*
* @return int
*/
public function getState() public function getState()
{ {
return intval($this->getValue('pluginstate')); return intval($this->getValue('pluginstate'));
} }
/**
* Return the entered check output as a string
*
* @return string
*/
public function getOutput() public function getOutput()
{ {
return $this->getValue('checkoutput'); return $this->getValue('checkoutput');
} }
/**
* Return the entered performance data as a string
*
* @return string
*/
public function getPerformancedata() public function getPerformancedata()
{ {
return $this->getValue('performancedata'); return $this->getValue('performancedata');

View File

@ -1,24 +1,22 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once __DIR__.'/BaseFormTest.php'; require_once realpath(__DIR__ . '/BaseFormTest.php');
$base = __DIR__.'/../../../../../../../'; require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/AcknowledgeForm.php');
require_once $base.'modules/monitoring/application/forms/Command/CommandForm.php'; require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once $base . 'library/Icinga/Util/ConfigAwareFactory.php'; require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
require_once $base . 'library/Icinga/Util/DateTimeFactory.php';
require_once realpath($base.'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php');
require_once realpath($base.'modules/monitoring/application/forms/Command/AcknowledgeForm.php');
use \DateTimeZone; use \DateTimeZone;
use \Zend_View; use \Monitoring\Form\Command\AcknowledgeForm; // Used by constant FORMCLASS
use \Zend_Test_PHPUnit_ControllerTestCase; use \Icinga\Util\DateTimeFactory;
use Monitoring\Form\Command\AcknowledgeForm;
use Icinga\Util\DateTimeFactory;
class AcknowledgeFormTest extends BaseFormTest class AcknowledgeFormTest extends BaseFormTest
{ {
const FORMCLASS = "Monitoring\Form\Command\AcknowledgeForm"; const FORMCLASS = 'Monitoring\Form\Command\AcknowledgeForm';
/** /**
* Set up the default time zone * Set up the default time zone
@ -33,105 +31,93 @@ class AcknowledgeFormTest extends BaseFormTest
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
} }
public function testForm() public function testFormValid()
{
$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()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
'author' => 'test1', 'author' => 'Author',
'comment' => 'test comment', 'comment' => 'Comment',
'persistent' => '0', 'persistent' => '0',
'expire' => '0', 'expire' => '0',
'sticky' => '0', 'sticky' => '0',
'notify' => '0', 'notify' => '0',
'btn_submit' => 'foo' 'btn_submit' => 'Submit'
), self::FORMCLASS); ), self::FORMCLASS);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
"Asserting a correct form to be validated correctly" 'Legal request data without expire time must be considered valid'
); );
}
public function testDetectMissingAcknowledgementComment() $formWithExpireTime = $this->getRequestForm(array(
{ 'author' => 'Author',
$form = $this->getRequestForm(array( 'comment' => 'Comment',
'author' => 'test1', 'persistent' => '0',
'comment' => '', 'expire' => '1',
'persistent' => '0', 'expiretime' => '2013-07-10 17:32:16',
'expire' => '0', 'sticky' => '0',
'sticky' => '0', 'notify' => '0',
'notify' => '0', 'btn_submit' => 'Submit'
'btn_submit' => 'foo'
), self::FORMCLASS); ), 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( $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(), $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

View File

@ -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/InvalidCSRFTokenException.php');
require_once realpath($base.'library/Icinga/Web/Form/Element/Note.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.'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_View;
use \Zend_Form; use \Zend_Form;

View File

@ -1,68 +1,63 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/CommentForm.php');
require_once __DIR__.'/BaseFormTest.php'; use \Monitoring\Form\Command\CommentForm; // Used by constant FORMCLASS
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php';
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
require_once __DIR__. '/../../../../../application/forms/Command/CommentForm.php';
use Monitoring\Form\Command\CommentForm;
use \Zend_View;
class CommentFormTest extends BaseFormTest class CommentFormTest extends BaseFormTest
{ {
const FORMCLASS = "Monitoring\Form\Command\CommentForm"; const FORMCLASS = 'Monitoring\Form\Command\CommentForm';
public function testForm()
{
$form = new CommentForm();
$form->setRequest($this->getRequest());
$form->buildForm();
$this->assertCount(6, $form->getElements());
}
public function testCorrectCommentValidation() public function testCorrectCommentValidation()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
'author' => 'test1', 'author' => 'Author',
'comment' => 'test2', 'comment' => 'Comment',
'sticky' => '0', 'sticky' => '0',
'btn_submit' => 'foo' 'btn_submit' => 'Submit'
), self::FORMCLASS); ), self::FORMCLASS);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $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( $form = $this->getRequestForm(array(
'author' => 'test1', 'author' => 'Author',
'comment' => '', 'comment' => '',
'sticky' => '0' 'sticky' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS); ), self::FORMCLASS);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $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( $form = $this->getRequestForm(array(
'author' => '', 'author' => '',
'comment' => 'test2', 'comment' => 'Comment',
'sticky' => '0' 'sticky' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS); ), self::FORMCLASS);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
"Asserting missing comment author to cause validation errors" 'Missing author must be considered not valid'
); );
} }
} }
// @codingStandardsIgnoreEnd

View File

@ -1,28 +1,31 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once __DIR__. '/BaseFormTest.php'; require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once realpath(__DIR__ . '/../../../../../application/forms/Command/CustomNotificationForm.php');
require_once __DIR__. '/../../../../../application/forms/Command/CustomNotificationForm.php';
use \Monitoring\Form\Command\CustomNotificationForm; // Used by constant FORM_CLASS
use Monitoring\Form\Command\CustomNotificationForm;
use \Zend_View;
use \Zend_Test_PHPUnit_ControllerTestCase;
class CustomNotificationFormTest extends BaseFormTest class CustomNotificationFormTest extends BaseFormTest
{ {
public function testForm1() const FORM_CLASS = 'Monitoring\Form\Command\CustomNotificationForm';
public function testFormInvalidWhenCommentMissing()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
'comment' => 'TEST COMMENT', 'author' => 'Author',
'author' => 'LAOLA', 'comment' => '',
'btn_submit' => 'foo' 'btn_submit' => 'Submit'
), "Monitoring\Form\Command\CustomNotificationForm"); ), self::FORM_CLASS);
$form->buildForm();
$this->assertCount(7, $form->getElements()); $this->assertFalse(
$this->assertTrue($form->isSubmittedAndValid()); $form->isSubmittedAndValid(),
'Missing comment must be considered not valid'
);
} }
} }
// @codingStandardsIgnoreEnd

View File

@ -1,57 +1,56 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once __DIR__. '/BaseFormTest.php'; require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once realpath(__DIR__ . '/../../../../../application/forms/Command/DelayNotificationForm.php');
require_once __DIR__. '/../../../../../application/forms/Command/DelayNotificationForm.php';
use \Monitoring\Form\Command\DelayNotificationForm; // Used by constant FORM_CLASS
use \Zend_View; class DelayNotificationFormTest extends BaseFormTest
use \Zend_Test_PHPUnit_ControllerTestCase;
use Monitoring\Form\Command\DelayNotificationForm;
class DelayNotificationFormFormTest extends BaseFormTest
{ {
public function testValidForm() const FORM_CLASS = 'Monitoring\Form\Command\DelayNotificationForm';
public function testFormInvalidWhenNotificationDelayMissing()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
'minutes' => 12, 'minutes' => '',
'btn_submit' => 'foo' 'btn_submit' => 'Submit'
), 'Monitoring\Form\Command\DelayNotificationForm'); ), self::FORM_CLASS);
$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();
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
"Asserting invalid minutes (NaN) to cause validation errors" 'Missing notification delay must be considered invalid'
); );
}
$errors = $form->getErrors('minutes'); public function testFormInvalidWhenNotificationDelayNaN()
$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"); $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

View File

@ -1,102 +1,70 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/RescheduleNextCheckForm.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
require_once __DIR__. '/BaseFormTest.php'; use \Monitoring\Form\Command\RescheduleNextCheckForm; // Used by constant FORM_CLASS
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; use \DateTimeZone;
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php'; use \Icinga\Util\DateTimeFactory;
require_once __DIR__. '/../../../../../application/forms/Command/RescheduleNextCheckForm.php';
use Monitoring\Form\Command\RescheduleNextCheckForm;
use \Zend_View;
use \Zend_Test_PHPUnit_ControllerTestCase;
class RescheduleNextCheckFormTest extends BaseFormTest class RescheduleNextCheckFormTest extends BaseFormTest
{ {
const FORM_CLASS = 'Monitoring\Form\Command\RescheduleNextCheckForm';
const FORMCLASS = 'Monitoring\Form\Command\RescheduleNextCheckForm'; /**
* Set up the default time zone
public function testValidRescheduleSubmissions() *
* Utilizes singleton DateTimeFactory
*
* @backupStaticAttributes enabled
*/
public function setUp()
{ {
date_default_timezone_set('UTC');
$form = $this->getRequestForm(array( DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
'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'
);
} }
public function testInValidRescheduleChecktimeSubmissions() public function testFormInvalidWhenChecktimeIsIncorrect()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
'checktime' => '2013-24-12 17:30:00', 'checktime' => '2013-24-12 17:30:00',
'forcecheck' => 1 'forcecheck' => 0,
), self::FORMCLASS); 'btn_submit' => 'Submit'
), self::FORM_CLASS);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $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( $form2 = $this->getRequestForm(array(
'checktime' => 'AHAHA', 'checktime' => 'Captain Morgan',
'forcecheck' => 1 'forcecheck' => 1,
), self::FORMCLASS); 'btn_submit' => 'Submit'
), self::FORM_CLASS);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form2->isSubmittedAndValid(),
'Asserting an invalid non-numeric checktime to be considered as invalid reschedule data' '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(); $this->assertFalse(
$form->setRequest($this->getRequest()); $form3->isSubmittedAndValid(),
$form->setWithChildren(true); 'Missing checktime must be considered invalid'
$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);
} }
} }
// @codingStandardsIgnoreStop

View File

@ -1,39 +1,50 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once __DIR__. '/BaseFormTest.php'; require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php');
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php'; require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once __DIR__. '/../../../../../application/forms/Command/ScheduleDowntimeForm.php'; require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
use Monitoring\Form\Command\ScheduleDowntimeForm; use \Monitoring\Form\Command\ScheduleDowntimeForm; // Used by constant FORM_CLASS
use \Zend_View; use \DateTimeZone;
use \Zend_Test_PHPUnit_ControllerTestCase; use \Icinga\Util\DateTimeFactory;
class ScheduleDowntimeFormTest extends BaseFormTest class ScheduleDowntimeFormTest extends BaseFormTest
{ {
const FORMCLASS = 'Monitoring\Form\Command\ScheduleDowntimeForm'; 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() public function testCorrectFormElementCreation()
{ {
$formFixed = $this->getRequestForm(array(), self::FORMCLASS); $formFixed = $this->getRequestForm(array(), self::FORM_CLASS);
$formFixed->buildForm(); $formFixed->buildForm();
$formFlexible = $this->getRequestForm(array( $formFlexible = $this->getRequestForm(array(
'type' => 'flexible' 'type' => 'flexible'
), self::FORMCLASS); ), self::FORM_CLASS);
$formFlexible->buildForm(); $formFlexible->buildForm();
$this->assertCount(11, $formFixed->getElements()); $form = $this->getRequestForm(array(), self::FORM_CLASS);
$this->assertCount(13, $formFlexible->getElements());
$form = $this->getRequestForm(array(), self::FORMCLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$form->buildForm(); $form->buildForm();
$this->assertCount(12, $form->getElements());
} }
public function testCorrectValidationWithChildrend() public function testCorrectValidationWithChildrend()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
@ -47,8 +58,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'minutes' => '', 'minutes' => '',
'btn_submit' => 'foo', 'btn_submit' => 'foo',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
@ -67,14 +77,13 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'minutes' => '10', 'minutes' => '10',
'btn_submit' => 'foo' 'btn_submit' => 'foo'
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Asserting a correct flexible downtime form to be considered valid' 'Asserting a correct flexible downtime form to be considered valid'
); );
} }
public function testMissingFlexibleDurationRecognition() public function testMissingFlexibleDurationRecognition()
@ -89,7 +98,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$this->assertFalse( $this->assertFalse(
@ -100,7 +109,6 @@ class ScheduleDowntimeFormTest extends BaseFormTest
public function testMissingAuthorRecognition() public function testMissingAuthorRecognition()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
'author' => '', 'author' => '',
'comment' => 'DING DING', 'comment' => 'DING DING',
@ -111,7 +119,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
@ -133,7 +141,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
@ -155,7 +163,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$this->assertFalse( $this->assertFalse(
@ -176,7 +184,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$this->assertFalse( $this->assertFalse(
@ -187,7 +195,6 @@ class ScheduleDowntimeFormTest extends BaseFormTest
public function testInvalidEndTimeRecognition() public function testInvalidEndTimeRecognition()
{ {
$form = $this->getRequestForm(array( $form = $this->getRequestForm(array(
'author' => 'OK', 'author' => 'OK',
'comment' => 'OK', 'comment' => 'OK',
@ -198,7 +205,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$this->assertFalse( $this->assertFalse(
@ -220,7 +227,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '-1', 'hours' => '-1',
'minutes' => '12', 'minutes' => '12',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$this->assertFalse( $this->assertFalse(
@ -241,7 +248,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '12', 'hours' => '12',
'minutes' => 'DING', 'minutes' => 'DING',
// 'childobjects' => '', // 'childobjects' => '',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(true); $form->setWithChildren(true);
$this->assertFalse( $this->assertFalse(
@ -264,13 +271,13 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'minutes' => '', 'minutes' => '',
'btn_submit' => 'foo', 'btn_submit' => 'foo',
'childobjects' => '0', 'childobjects' => '0',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(false); $form->setWithChildren(false);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $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' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
'childobjects' => 'AHA', 'childobjects' => 'AHA',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(false); $form->setWithChildren(false);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $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( $form = $this->getRequestForm(array(
@ -303,18 +310,18 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
'childobjects' => '4', 'childobjects' => '4',
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setWithChildren(false); $form->setWithChildren(false);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $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() public function testTimeRange()
{ {
$form = $this->getRequestForm(array(), self::FORMCLASS); $form = $this->getRequestForm(array(), self::FORM_CLASS);
$form->buildForm(); $form->buildForm();
$time1 = $form->getElement('starttime')->getValue(); $time1 = $form->getElement('starttime')->getValue();
@ -323,3 +330,4 @@ class ScheduleDowntimeFormTest extends BaseFormTest
$this->assertEquals(3600, ($time2 - $time1)); $this->assertEquals(3600, ($time2 - $time1));
} }
} }
// @codingStandardsIgnoreStop

View File

@ -1,38 +1,37 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once __DIR__. '/BaseFormTest.php'; require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once realpath(__DIR__ . '/../../../../../application/forms/Command/SubmitPassiveCheckResultForm.php');
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
require_once __DIR__. '/../../../../../application/forms/Command/SubmitPassiveCheckResultForm.php';
use \Monitoring\Form\Command\SubmitPassiveCheckResultForm; // Used by constant FORM_CLASS
use \Zend_View;
use \Zend_Test_PHPUnit_ControllerTestCase;
use Monitoring\Form\Command\SubmitPassiveCheckResultForm;
class SubmitPassiveCheckResultFormTest extends BaseFormTest class SubmitPassiveCheckResultFormTest extends BaseFormTest
{ {
const FORMCLASS = "Monitoring\Form\Command\SubmitPassiveCheckResultForm"; const FORM_CLASS = 'Monitoring\Form\Command\SubmitPassiveCheckResultForm';
public function testStateTypes() public function testStateTypes()
{ {
$form = $this->getRequestForm(array(), self::FORMCLASS); $form = $this->getRequestForm(array(), self::FORM_CLASS);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$options = $form->getOptions(); $options = $form->getOptions();
$this->assertCount(4, $options, "Assert correct number of states in service passive checks 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('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('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('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->assertEquals('UNKNOWN', $options[3], 'Assert UNKNOWN state to be available in service passive check form');
$form->setType(SubmitPassiveCheckResultForm::TYPE_HOST); $form->setType(SubmitPassiveCheckResultForm::TYPE_HOST);
$options = $form->getOptions(); $options = $form->getOptions();
$this->assertCount(3, $options, "Assert correct number of states in host passive checks 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('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('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->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() public function testMissingTypeThrowingException()
{ {
$form = $this->getRequestForm(array(), self::FORMCLASS); $form = $this->getRequestForm(array(), self::FORM_CLASS);
$form->buildForm(); $form->buildForm();
} }
public function testCorrectFormCreation() public function testCorrectFormCreation()
{ {
$form = $this->getRequestForm(array(), self::FORMCLASS); $form = $this->getRequestForm(array(), self::FORM_CLASS);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$form->buildForm(); $form->buildForm();
$this->assertCount(6, $form->getElements(), "Assert correct number of elements in form");
} }
public function testCorrectServicePassiveCheckSubmission() public function testCorrectServicePassiveCheckSubmission()
@ -61,13 +58,13 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest
'checkoutput' => 'DING', 'checkoutput' => 'DING',
'performancedata' => '', 'performancedata' => '',
'btn_submit' => 'foo' 'btn_submit' => 'foo'
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $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, 'pluginstate' => 0,
'checkoutput' => '', 'checkoutput' => '',
'performancedata' => '' 'performancedata' => ''
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $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', 'pluginstate' => 'LA',
'checkoutput' => 'DING', 'checkoutput' => 'DING',
'performancedata' => '' 'performancedata' => ''
), self::FORMCLASS); ), self::FORM_CLASS);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $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'
); );
} }
} }

View File

@ -1,11 +1,7 @@
<?php <?php
/** // @codingStandardsIgnoreStart
* Created by JetBrains PhpStorm. // {{{ICINGA_LICENSE_HEADER}}}
* User: moja // {{{ICINGA_LICENSE_HEADER}}}
* Date: 7/31/13
* Time: 1:29 PM
* To change this template use File | Settings | File Templates.
*/
namespace Tests\Icinga\Protocol\Commandpipe; namespace Tests\Icinga\Protocol\Commandpipe;
@ -30,5 +26,7 @@ class CommandPipeLoader extends LibraryLoader {
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/Transport.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Transport/Transport.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/SecureShell.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Transport/SecureShell.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php");
require_once('../../library/Icinga/Protocol/Commandpipe/CustomNotification.php');
} }
} }
// @codingStandardsIgnoreEnd

View File

@ -1,4 +1,8 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Protocol\Commandpipe; namespace Tests\Icinga\Protocol\Commandpipe;
require_once(__DIR__.'/CommandPipeLoader.php'); require_once(__DIR__.'/CommandPipeLoader.php');
@ -6,6 +10,7 @@ CommandPipeLoader::requireLibrary();
use Icinga\Protocol\Commandpipe\Comment as Comment; use Icinga\Protocol\Commandpipe\Comment as Comment;
use Icinga\Protocol\Commandpipe\Acknowledgement as Acknowledgement; use Icinga\Protocol\Commandpipe\Acknowledgement as Acknowledgement;
use Icinga\Protocol\Commandpipe\CustomNotification;
use Icinga\Protocol\Commandpipe\Downtime as Downtime; use Icinga\Protocol\Commandpipe\Downtime as Downtime;
use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe; use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe;
use \Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG; use \Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG;
@ -423,15 +428,15 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
{ {
$pipe = $this->getLocalTestPipe(); $pipe = $this->getLocalTestPipe();
try { try {
$comment = new Comment("author", "commenttext"); $notification = new CustomNotification('Author', 'Comment');
$pipe->sendCustomNotification(array( $pipe->sendCustomNotification(array(
(object) array( (object) array(
"host_name" => "host1", 'host_name' => 'Host',
"service_description" => "service1" 'service_description' => 'Service'
) )
), $comment); ), $notification);
$this->assertCommandSucceeded( $this->assertCommandSucceeded(
"SEND_CUSTOM_SVC_NOTIFICATION;host1;service1;0;author;commenttext" 'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;0;Author;Comment'
); );
} catch (Exception $e) { } catch (Exception $e) {
$this->cleanup(); $this->cleanup();
@ -449,15 +454,15 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
{ {
$pipe = $this->getLocalTestPipe(); $pipe = $this->getLocalTestPipe();
try { try {
$comment = new Comment('author', 'commenttext'); $notification = new CustomNotification('Author', 'Comment', true, true);
$pipe->sendCustomNotification(array( $pipe->sendCustomNotification(array(
(object) 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( $this->assertCommandSucceeded(
'SEND_CUSTOM_HOST_NOTIFICATION;host;7;author;commenttext' 'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;3;Author;Comment'
); );
} catch (Exception $e) { } catch (Exception $e) {
$this->cleanup(); $this->cleanup();
@ -493,3 +498,4 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
$this->cleanup(); $this->cleanup();
} }
} }
// @codingStandardsIgnoreStop