diff --git a/application/controllers/AuthenticationController.php b/application/controllers/AuthenticationController.php index 8c4abc506..5be42b855 100644 --- a/application/controllers/AuthenticationController.php +++ b/application/controllers/AuthenticationController.php @@ -69,8 +69,7 @@ class AuthenticationController extends ActionController $this->redirectNow('index?_render=body'); } - if ($this->view->form->isPostAndValid()) { - + if ($this->view->form->isSubmittedAndValid()) { $credentials->setUsername($this->view->form->getValue('username')); $credentials->setPassword($this->view->form->getValue('password')); diff --git a/doc/form.md b/doc/form.md index 7f66dde5d..13845031c 100644 --- a/doc/form.md +++ b/doc/form.md @@ -24,11 +24,11 @@ In here you can add elements to your form, add validations and filters of your choice. The creation method is invoked lazy just before a form is rendered or *isValid()* is called. -#### Calling is *IsPostAndValid()* +#### Calling is *isSubmittedAndValid()* -*IsPostAndValid()* is used to test of all needed parameters there. It combines -testing for post request and pulls out the data from request object to handle -over an array for Zend native method *isValid()* +*isSubmittedAndValid()* is used to check whether the form is ready to be processed or not. +It ensures that the current request method is POST, that the form was manually submitted +and that the data provided in the request is valid and gets repopulated in case its invalid. #### Pre validation @@ -162,6 +162,32 @@ interface methods: } } +## Testing forms + +When testing forms it is a good idea to use Zend_Test_PHPUnit_ControllerTestCase +instead of others like PHPUnit_Framework_TestCase as this enables you to use a +request dummy which can be passed to your form. + +### Example: + + require_once 'Zend/Test/PHPUnit/ControllerTestCase.php'; + + class YourTestCase extends Zend_Test_PHPUnit_ControllerTestCase + { + function exampleTest() + { + $request = $this->getRequest(); + $request->setMethod('POST')->setPost(array( + 'key' => 'value' + ) + ); + $form = new SomeForm(); + $form->setRequest($request); + + ... + } + } + ## Additional resources * [API documentation](http://build.icinga.org/jenkins/view/icinga2-web/job/icinga2web-development/javadoc/?) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index fffd152ab..d79e304ac 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -27,19 +27,22 @@ namespace Icinga\Web; use Icinga\Exception\ProgrammingError; use Icinga\Web\Form\InvalidCSRFTokenException; -use Zend_Form_Exception; -use Zend_View_Interface; +use \Zend_Controller_Request_Abstract; +use \Zend_Form_Element_Submit; +use \Zend_Form_Element_Reset; +use \Zend_View_Interface; +use \Zend_Form; /** * Class Form * * How forms are used in Icinga 2 Web */ -abstract class Form extends \Zend_Form +abstract class Form extends Zend_Form { /** * The form's request object - * @var \Zend_Controller_Request_Abstract + * @var Zend_Controller_Request_Abstract */ private $request; @@ -75,6 +78,24 @@ abstract class Form extends \Zend_Form */ private $sessionId = false; + /** + * Label for submit button + * + * If omitted, no button will be shown. + * + * @var string + */ + private $submitLabel; + + /** + * Label for cancel button + * + * If omitted, no button will be shown. + * + * @var string + */ + private $cancelLabel; + /** * Returns the session ID stored in this form instance * @return mixed @@ -109,7 +130,6 @@ abstract class Form extends \Zend_Form return $this->tokenElementName; } - /** * Render the form to html * @param Zend_View_Interface $view @@ -136,16 +156,16 @@ abstract class Form extends \Zend_Form /** * Setter for request - * @param \Zend_Controller_Request_Abstract $request The request object of a session + * @param Zend_Controller_Request_Abstract $request The request object of a session */ - public function setRequest(\Zend_Controller_Request_Abstract $request) + public function setRequest(Zend_Controller_Request_Abstract $request) { $this->request = $request; } /** * Getter for request - * @return \Zend_Controller_Request_Abstract + * @return Zend_Controller_Request_Abstract */ public function getRequest() { @@ -161,6 +181,14 @@ abstract class Form extends \Zend_Form $this->initCsrfToken(); $this->create(); + if ($this->submitLabel) { + $this->addSubmitButton(); + } + + if ($this->cancelLabel) { + $this->addCancelButton(); + } + // Empty action if not safe if (!$this->getAction() && $this->getRequest()) { $this->setAction($this->getRequest()->getRequestUri()); @@ -171,23 +199,104 @@ abstract class Form extends \Zend_Form } /** - * Test if data from array or request is valid + * Setter for cancel label + * @param string $cancelLabel + */ + public function setCancelLabel($cancelLabel) + { + $this->cancelLabel = $cancelLabel; + } + + /** + * Add cancel button to form + */ + private function addCancelButton() + { + $cancelLabel = new Zend_Form_Element_Reset( + array( + 'name' => 'btn_reset', + 'label' => $this->cancelLabel, + 'class' => 'btn pull-right' + ) + ); + $this->addElement($cancelLabel); + } + + /** + * Setter for submit label + * @param string $submitLabel + */ + public function setSubmitLabel($submitLabel) + { + $this->submitLabel = $submitLabel; + } + + /** + * Add submit button to form + */ + private function addSubmitButton() + { + $submitButton = new Zend_Form_Element_Submit( + array( + 'name' => 'btn_submit', + 'label' => $this->submitLabel, + 'class' => 'btn btn-primary pull-right' + ) + ); + $this->addElement($submitButton); + } + + /** + * Enable automatic submission + * + * Enables automatic submission of this form once the user edits specific elements. + * + * @param array $triggerElements The element names which should auto-submit the form + * @throws ProgrammingError When an element is found which does not yet exist + */ + final public function enableAutoSubmit($triggerElements) + { + foreach ($triggerElements as $elementName) { + $element = $this->getElement($elementName); + if ($element !== null) { + $element->setAttrib('onchange', '$(this.form).submit();'); + } else { + throw new ProgrammingError( + 'You need to add the element "' . $elementName . '" to' . + ' the form before automatic submission can be enabled!' + ); + } + } + } + + /** + * Check whether the form was submitted with a valid request + * + * Ensures that the current request method is POST, that the + * form was manually submitted and that the data provided in + * the request is valid and gets repopulated in case its invalid. * - * If $data is null, internal request is selected to test validity * @return bool */ - public function isPostAndValid() + public function isSubmittedAndValid() { if ($this->getRequest()->isPost() === false) { return false; } - $checkData = $this->getRequest()->getParams(); - $this->buildForm(); + $checkData = $this->getRequest()->getParams(); $this->assertValidCsrfToken($checkData); - $this->preValidation($checkData); - return parent::isValid($checkData); + + $submitted = true; + if ($this->submitLabel) { + $submitted = isset($checkData['btn_submit']); + } + + if ($submitted) { + $this->preValidation($checkData); + } + return parent::isValid($checkData) && $submitted; } /** @@ -224,8 +333,8 @@ abstract class Form extends \Zend_Form /** * Tests the submitted data for a correct CSRF token, if needed * - * @param Array $checkData The POST data send by the user - * @throws Form\InvalidCSRFTokenException When CSRF Validation fails + * @param Array $checkData The POST data send by the user + * @throws InvalidCSRFTokenException When CSRF Validation fails */ final public function assertValidCsrfToken(array $checkData) { diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 0346bd7b6..16754c8f9 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -42,8 +42,8 @@ use Icinga\Exception\ConfigurationError; use Icinga\Exception\MissingParameterException; use Monitoring\Form\Command\AcknowledgeForm; use Monitoring\Form\Command\CommentForm; -use Monitoring\Form\Command\ConfirmationForm; -use Monitoring\Form\Command\ConfirmationWithIdentifierForm; +use Monitoring\Form\Command\CommandForm; +use Monitoring\Form\Command\CommandWithIdentifierForm; use Monitoring\Form\Command\CustomNotificationForm; use Monitoring\Form\Command\DelayNotificationForm; use Monitoring\Form\Command\RescheduleNextCheckForm; @@ -97,8 +97,7 @@ class Monitoring_CommandController extends ModuleActionController public function postDispatch() { if ($this->issetForm()) { - if ($this->form->isPostAndValid()) { - + if ($this->form->isSubmittedAndValid()) { $this->_helper->viewRenderer->setNoRender(true); $this->_helper->layout()->disableLayout(); } @@ -248,13 +247,13 @@ class Monitoring_CommandController extends ModuleActionController public function disableactivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable active checks')); $form->addNote(t('Disable active checks for this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->disableActiveChecks($this->view->objects); } } @@ -266,13 +265,13 @@ class Monitoring_CommandController extends ModuleActionController public function enableactivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable active checks')); $form->addNote(t('Enable active checks for this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->enableActiveChecks($this->view->objects); } } @@ -289,7 +288,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->scheduleCheck($this->view->objects); } } @@ -309,7 +308,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->submitCheckResult($this->view->objects, $form->getState(), $form->getOutput(), $form->getPerformancedata()); } } @@ -321,13 +320,13 @@ class Monitoring_CommandController extends ModuleActionController public function stopobsessingAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop obsessing')); $form->addNote(t('Stop obsessing over this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->stopObsessing($this->view->objects); } } @@ -339,13 +338,13 @@ class Monitoring_CommandController extends ModuleActionController public function startobsessingAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start obsessing')); $form->addNote(t('Start obsessing over this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->startObsessing($this->view->objects); } } @@ -357,13 +356,13 @@ class Monitoring_CommandController extends ModuleActionController public function stopacceptingpassivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Stop accepting passive checks')); $form->addNote(t('Passive checks for this object will be omitted.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->disablePassiveChecks($this->view->objects); } } @@ -375,13 +374,13 @@ class Monitoring_CommandController extends ModuleActionController public function startacceptingpassivechecksAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Start accepting passive checks')); $form->addNote(t('Passive checks for this object will be accepted.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->enableActiveChecks($this->view->objects); } } @@ -393,13 +392,13 @@ class Monitoring_CommandController extends ModuleActionController public function disablenotificationsAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable notifications')); $form->addNote(t('Notifications for this object will be disabled.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->disableNotifications($this->view->objects); } } @@ -410,13 +409,13 @@ class Monitoring_CommandController extends ModuleActionController */ public function enablenotificationsAction() { - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable notifications')); $form->addNote(t('Notifications for this object will be enabled.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->enableNotifications($this->view->objects); } } @@ -432,7 +431,7 @@ class Monitoring_CommandController extends ModuleActionController $form->setRequest($this->getRequest()); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $author = $this->getRequest()->getUser()->getUsername(); $this->target->sendCustomNotification( $this->view->objects, @@ -454,7 +453,7 @@ class Monitoring_CommandController extends ModuleActionController $form->setWithChildren(false); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->scheduleDowntime($this->view->objects, $form->getDowntime()); } } @@ -472,7 +471,7 @@ class Monitoring_CommandController extends ModuleActionController $form->setWithChildren(true); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->scheduleDowntime($this->view->objects, $form->getDowntime()); } } @@ -484,13 +483,13 @@ class Monitoring_CommandController extends ModuleActionController public function removedowntimeswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Remove downtime(s)')); $form->addNote(t('Remove downtime(s) from this host and its services.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->removeDowntime($this->view->objects); } } @@ -502,13 +501,13 @@ class Monitoring_CommandController extends ModuleActionController public function disablenotificationswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable notifications')); $form->addNote(t('Notifications for this host and its services will be disabled.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->disableNotifications($this->view->objects); $this->target->disableNotificationsForServices($this->view->objects); } @@ -521,13 +520,13 @@ class Monitoring_CommandController extends ModuleActionController public function enablenotificationswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable notifications')); $form->addNote(t('Notifications for this host and its services will be enabled.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->enableNotifications($this->view->objects); $this->target->enableNotificationsForServices($this->view->objects); } @@ -547,7 +546,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { if ($form->isForced()) { $this->target->scheduleForcedCheck($this->view->objects, time()); $this->target->scheduleForcedCheck($this->view->objects, time(), true); @@ -565,13 +564,13 @@ class Monitoring_CommandController extends ModuleActionController public function disableactivecheckswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable active checks')); $form->addNote(t('Disable active checks for this host and its services.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->disableActiveChecks($this->view->objects); $this->target->disableActiveChecksWithChildren($this->view->objects); } @@ -584,13 +583,13 @@ class Monitoring_CommandController extends ModuleActionController public function enableactivecheckswithchildrenAction() { $this->setSupportedParameters(array('host')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable active checks')); $form->addNote(t('Enable active checks for this host and its services.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->enableActiveChecks($this->view->objects); $this->target->enableActiveChecksWithChildren($this->view->objects); } @@ -603,13 +602,13 @@ class Monitoring_CommandController extends ModuleActionController public function disableeventhandlerAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable event handler')); $form->addNote(t('Disable event handler for this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->disableEventHandler($this->view->objects); } } @@ -621,13 +620,13 @@ class Monitoring_CommandController extends ModuleActionController public function enableeventhandlerAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable event handler')); $form->addNote(t('Enable event handler for this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->enableEventHandler($this->view->objects); } } @@ -639,13 +638,13 @@ class Monitoring_CommandController extends ModuleActionController public function disableflapdetectionAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Disable flapping detection')); $form->addNote(t('Disable flapping detection for this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->disableFlappingDetection($this->view->objects); } } @@ -657,13 +656,13 @@ class Monitoring_CommandController extends ModuleActionController public function enableflapdetectionAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Enable flapping detection')); $form->addNote(t('Enable flapping detection for this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->enableFlappingDetection($this->view->objects); } } @@ -680,7 +679,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->addComment($this->view->objects, $form->getComment()); } } @@ -692,13 +691,13 @@ class Monitoring_CommandController extends ModuleActionController public function resetattributesAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Reset attributes')); $form->addNote(t('Reset modified attributes to its default.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->resetAttributes($this->view->objects); } } @@ -715,7 +714,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->acknowledge($this->view->objects, $form->getAcknowledgement()); } } @@ -727,13 +726,13 @@ class Monitoring_CommandController extends ModuleActionController public function removeacknowledgementAction() { $this->setSupportedParameters(array('host', 'service')); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Remove problem acknowledgement')); $form->addNote(t('Remove problem acknowledgement for this object.')); $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->removeAcknowledge($this->view->objects); } } @@ -750,7 +749,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->delayNotification($this->view->objects, $form->getDelayTime()); } } @@ -762,7 +761,7 @@ class Monitoring_CommandController extends ModuleActionController public function removedowntimeAction() { $this->setSupportedParameters(array('downtimeid')); - $form = new ConfirmationWithIdentifierForm(); + $form = new CommandWithIdentifierForm(); $form->setRequest($this->getRequest()); $form->setSubmitLabel(t('Delete downtime')); @@ -772,7 +771,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); - if ($form->isPostAndValid() === true) { + if ($form->IsSubmittedAndValid() === true) { $this->target->removeDowntime($this->view->objects); } } diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index 61e9bb46e..0a4f93578 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -37,11 +37,11 @@ use Icinga\Protocol\Commandpipe\Comment; /** * Form for acknowledge commands */ -class AcknowledgeForm extends ConfirmationForm +class AcknowledgeForm extends CommandForm { /** * Interface method to build the form - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { @@ -66,6 +66,13 @@ class AcknowledgeForm extends ConfirmationForm ) ); + $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', @@ -74,26 +81,25 @@ class AcknowledgeForm extends ConfirmationForm ) ); - $now = new PhpDateTime(); - $interval = new DateInterval('PT1H'); // Add 3600 seconds - $now->add($interval); + if ($this->getRequest()->getPost('expire', '0') === '1') { + $now = new PhpDateTime(); + $interval = new DateInterval('PT1H'); // Add 3600 seconds + $now->add($interval); - $expireTime = new DateTime( - array( - 'name' => 'expiretime', - 'label' => t('Expire time'), - 'value' => $now->format($this->getDateFormat()) - ) - ); + $expireTime = new DateTime( + array( + 'name' => 'expiretime', + 'label' => t('Expire time'), + 'value' => $now->format($this->getDateFormat()) + ) + ); + + $this->addElements(array($expireNote, $expireCheck, $expireTime)); + } else { + $this->addElements(array($expireNote, $expireCheck)); + } - $expireNote = new Note( - array( - 'name' => 'expirenote', - 'value' => t('If the acknowledgement should expire, check the box and enter an expiration timestamp.') - ) - ); - - $this->addElements(array($expireNote, $expireCheck, $expireTime)); + $this->enableAutoSubmit(array('expire')); $this->addDisplayGroup( array( diff --git a/modules/monitoring/application/forms/Command/ConfirmationForm.php b/modules/monitoring/application/forms/Command/CommandForm.php similarity index 72% rename from modules/monitoring/application/forms/Command/ConfirmationForm.php rename to modules/monitoring/application/forms/Command/CommandForm.php index de24650d3..4ca1e4e82 100644 --- a/modules/monitoring/application/forms/Command/ConfirmationForm.php +++ b/modules/monitoring/application/forms/Command/CommandForm.php @@ -36,7 +36,7 @@ use \Zend_Validate_Date; /** * Simple confirmation command */ -class ConfirmationForm extends Form +class CommandForm extends Form { /** * Default date format @@ -48,24 +48,6 @@ class ConfirmationForm extends Form */ const DEFAULT_DATE_VALIDATION = 'yyyy-MM-dd hh:ii:ss'; - /** - * Label for submit button - * - * If omitted, no button will be shown. - * - * @var string - */ - private $submitLabel; - - /** - * Label for cancel button - * - * If omitted, no button will be shown. - * - * @var string - */ - private $cancelLabel; - /** * Array of messages * @@ -73,46 +55,6 @@ class ConfirmationForm extends Form */ private $notes = array(); - /** - * Setter for cancel label - * - * @param string $cancelLabel - */ - public function setCancelLabel($cancelLabel) - { - $this->cancelLabel = $cancelLabel; - } - - /** - * Getter for cancel label - * - * @return string - */ - public function getCancelLabel() - { - return $this->cancelLabel; - } - - /** - * Setter for submit label - * - * @param string $submitLabel - */ - public function setSubmitLabel($submitLabel) - { - $this->submitLabel = $submitLabel; - } - - /** - * Getter for submit label - * - * @return string - */ - public function getSubmitLabel() - { - return $this->submitLabel; - } - /** * Add message to stack * @@ -173,28 +115,6 @@ class ConfirmationForm extends Form } } - if ($this->getCancelLabel()) { - $cancelLabel = new \Zend_Form_Element_Reset( - array( - 'name' => 'reset', - 'label' => $this->getCancelLabel(), - 'class' => 'btn pull-right' - ) - ); - $this->addElement($cancelLabel); - } - - if ($this->getSubmitLabel()) { - $submitButton = new \Zend_Form_Element_Submit( - array( - 'name' => 'submit', - 'label' => $this->getSubmitLabel(), - 'class' => 'btn btn-primary pull-right' - ) - ); - $this->addElement($submitButton); - } - $this->addElement($this->createInstanceHiddenField()); } diff --git a/modules/monitoring/application/forms/Command/ConfirmationWithIdentifierForm.php b/modules/monitoring/application/forms/Command/CommandWithIdentifierForm.php similarity index 97% rename from modules/monitoring/application/forms/Command/ConfirmationWithIdentifierForm.php rename to modules/monitoring/application/forms/Command/CommandWithIdentifierForm.php index 5d3838fee..131d9178a 100644 --- a/modules/monitoring/application/forms/Command/ConfirmationWithIdentifierForm.php +++ b/modules/monitoring/application/forms/Command/CommandWithIdentifierForm.php @@ -33,7 +33,7 @@ use \Zend_Form_Element_Hidden; /** * Form to handle confirmations with a single value processed */ -class ConfirmationWithIdentifierForm extends ConfirmationForm +class CommandWithIdentifierForm extends CommandForm { /** * Identifier for data field @@ -124,7 +124,7 @@ class ConfirmationWithIdentifierForm extends ConfirmationForm /** * Interface method to build the form - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { diff --git a/modules/monitoring/application/forms/Command/CommentForm.php b/modules/monitoring/application/forms/Command/CommentForm.php index 51d16f840..2e789dc99 100644 --- a/modules/monitoring/application/forms/Command/CommentForm.php +++ b/modules/monitoring/application/forms/Command/CommentForm.php @@ -32,11 +32,11 @@ use Icinga\Protocol\Commandpipe\Comment; /** * Form for adding comment commands */ -class CommentForm extends ConfirmationForm +class CommentForm extends CommandForm { /** * Interface method to build the form - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { diff --git a/modules/monitoring/application/forms/Command/CustomNotificationForm.php b/modules/monitoring/application/forms/Command/CustomNotificationForm.php index f3217a50e..7244fb759 100644 --- a/modules/monitoring/application/forms/Command/CustomNotificationForm.php +++ b/modules/monitoring/application/forms/Command/CustomNotificationForm.php @@ -33,11 +33,11 @@ use Zend_Form_Element_Hidden; /** * For for command CustomNotification */ -class CustomNotificationForm extends ConfirmationForm +class CustomNotificationForm extends CommandForm { /** * Interface method to build the form - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { diff --git a/modules/monitoring/application/forms/Command/DelayNotificationForm.php b/modules/monitoring/application/forms/Command/DelayNotificationForm.php index 59ec290da..13fde8a25 100644 --- a/modules/monitoring/application/forms/Command/DelayNotificationForm.php +++ b/modules/monitoring/application/forms/Command/DelayNotificationForm.php @@ -31,7 +31,7 @@ namespace Monitoring\Form\Command; /** * Form to handle DelayNotification command */ -class DelayNotificationForm extends ConfirmationForm +class DelayNotificationForm extends CommandForm { /** * Biggest value for minutes @@ -39,7 +39,7 @@ class DelayNotificationForm extends ConfirmationForm const MAX_VALUE = 1440; /** * Interface method to build the form - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { diff --git a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php index 3d5ea4730..80b1e913c 100644 --- a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php +++ b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php @@ -39,7 +39,7 @@ class RescheduleNextCheckForm extends WithChildrenCommandForm { /** * Interface method to build the form - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index cab8fae0e..5bb1e8e49 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -93,7 +93,7 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm /** * Interface method to build the form * - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { @@ -178,37 +178,41 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ) ) ); + $this->enableAutoSubmit(array('type')); - $hoursText = new Zend_Form_Element_Text('hours'); - $hoursText->setLabel(t('Flexible duration')); - $hoursText->setAttrib('style', 'width: 40px;'); - $hoursText->setValue(1); - $hoursText->addDecorator('HtmlTag', array('tag' => 'dd', 'openOnly' => true)); - $hoursText->addDecorator( - 'Callback', - array( - 'callback' => function () { - return t('Hours'); - } - ) - ); + + if ($this->getRequest()->getPost('type') === self::TYPE_FLEXIBLE) { + $hoursText = new Zend_Form_Element_Text('hours'); + $hoursText->setLabel(t('Flexible duration')); + $hoursText->setAttrib('style', 'width: 40px;'); + $hoursText->setValue(1); + $hoursText->addDecorator('HtmlTag', array('tag' => 'dd', 'openOnly' => true)); + $hoursText->addDecorator( + 'Callback', + array( + 'callback' => function () { + return t('Hours'); + } + ) + ); - $minutesText = new Zend_Form_Element_Text('minutes'); - $minutesText->setLabel(t('Minutes')); - $minutesText->setAttrib('style', 'width: 40px;'); - $minutesText->setValue(0); - $minutesText->removeDecorator('HtmlTag'); - $minutesText->removeDecorator('Label'); - $minutesText->addDecorator( - 'Callback', - array( - 'callback' => function () { - return t('Minutes'); - } - ) - ); + $minutesText = new Zend_Form_Element_Text('minutes'); + $minutesText->setLabel(t('Minutes')); + $minutesText->setAttrib('style', 'width: 40px;'); + $minutesText->setValue(0); + $minutesText->removeDecorator('HtmlTag'); + $minutesText->removeDecorator('Label'); + $minutesText->addDecorator( + 'Callback', + array( + 'callback' => function () { + return t('Minutes'); + } + ) + ); - $this->addElements(array($hoursText, $minutesText)); + $this->addElements(array($hoursText, $minutesText)); + } if ($this->getWithChildren() === true) { $this->addNote(t('Schedule downtime for host and its services.')); diff --git a/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php b/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php index f1e8861ea..56fe6a394 100644 --- a/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php +++ b/modules/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php @@ -33,7 +33,7 @@ use Icinga\Exception\ProgrammingError; /** * Form for command SubmitPassiveCheckResult */ -class SubmitPassiveCheckResultForm extends ConfirmationForm +class SubmitPassiveCheckResultForm extends CommandForm { /** * Type constant for host form @@ -117,7 +117,7 @@ class SubmitPassiveCheckResultForm extends ConfirmationForm /** * Interface method to build the form - * @see ConfirmationForm::create + * @see CommandForm::create */ protected function create() { diff --git a/modules/monitoring/application/forms/Command/WithChildrenCommandForm.php b/modules/monitoring/application/forms/Command/WithChildrenCommandForm.php index b9dd6760b..2982b136e 100644 --- a/modules/monitoring/application/forms/Command/WithChildrenCommandForm.php +++ b/modules/monitoring/application/forms/Command/WithChildrenCommandForm.php @@ -31,7 +31,7 @@ namespace Monitoring\Form\Command; /** * Class handle specific command flags */ -abstract class WithChildrenCommandForm extends ConfirmationForm +abstract class WithChildrenCommandForm extends CommandForm { /** * Flag if we handle child objects as well diff --git a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php index 1c03717b2..b39af8e77 100644 --- a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php @@ -6,7 +6,7 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__.'/BaseFormTest.php'; $base = __DIR__.'/../../../../../../../'; -require_once $base.'modules/monitoring/application/forms/Command/ConfirmationForm.php'; +require_once $base.'modules/monitoring/application/forms/Command/CommandForm.php'; require_once realpath($base.'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php'); require_once realpath($base.'modules/monitoring/application/forms/Command/AcknowledgeForm.php'); @@ -17,16 +17,20 @@ use \Zend_Test_PHPUnit_ControllerTestCase; class AcknowledgeFormTest extends BaseFormTest { const FORMCLASS = "Monitoring\Form\Command\AcknowledgeForm"; + public function testForm() { - $form = $this->getRequestForm(array(), self::FORMCLASS); - $form->buildForm(); + $formWithoutExpiration = $this->getRequestForm(array(), self::FORMCLASS); + $formWithoutExpiration->buildForm(); + $formWithExpiration = $this->getRequestForm(array( + 'expire' => '1' + ), self::FORMCLASS); + $formWithExpiration->buildForm(); - $this->assertCount(11, $form->getElements()); + $this->assertCount(10, $formWithoutExpiration->getElements()); + $this->assertCount(11, $formWithExpiration->getElements()); } - - public function testValidateCorrectForm() { $form = $this->getRequestForm(array( @@ -34,13 +38,13 @@ class AcknowledgeFormTest extends BaseFormTest 'comment' => 'test comment', 'persistent' => '0', 'expire' => '0', - 'expiretime' => '', 'sticky' => '0', - 'notify' => '0' + 'notify' => '0', + 'btn_submit' => 'foo' ), self::FORMCLASS); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting a correct form to be validated correctly" ); } @@ -52,12 +56,12 @@ class AcknowledgeFormTest extends BaseFormTest 'comment' => '', 'persistent' => '0', 'expire' => '0', - 'expiretime' => '', 'sticky' => '0', 'notify' => '0', + 'btn_submit' => 'foo' ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting a missing comment text to cause validation errors" ); } @@ -71,10 +75,11 @@ class AcknowledgeFormTest extends BaseFormTest 'expire' => '1', 'expiretime' => '', 'sticky' => '0', - 'notify' => '0' + 'notify' => '0', + 'btn_submit' => 'foo' ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting a missing expire time to cause validation errors when expire is 1" ); } @@ -88,10 +93,11 @@ class AcknowledgeFormTest extends BaseFormTest 'expire' => '1', 'expiretime' => 'NOT A DATE', 'sticky' => '0', - 'notify' => '0' + 'notify' => '0', + 'btn_submit' => 'foo' ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert incorrect dates to be recognized when validating expiretime" ); } @@ -105,10 +111,11 @@ class AcknowledgeFormTest extends BaseFormTest 'expire' => '1', 'expiretime' => '2013-07-10 17:32:16', 'sticky' => '0', - 'notify' => '0' + 'notify' => '0', + 'btn_submit' => 'foo' ), self::FORMCLASS); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert that correct expire time acknowledgement is considered valid" ); } diff --git a/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php b/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php index ce79c7a11..2312607d6 100644 --- a/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php @@ -32,6 +32,11 @@ namespace Test\Monitoring\Forms\Command { require_once realpath($base.'library/Icinga/Web/Form/Element/DateTime.php'); use \Zend_View; + use \Zend_Form; + use \Zend_View_Interface; + use \Zend_Form_Element_Reset; + use \Zend_Form_Element_Submit; + use \Zend_Controller_Request_Abstract; use \Zend_Test_PHPUnit_ControllerTestCase; diff --git a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php index e5e242fa5..8447d6528 100644 --- a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php @@ -4,7 +4,7 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__.'/BaseFormTest.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/CommentForm.php'; @@ -28,13 +28,14 @@ class CommentFormTest extends BaseFormTest public function testCorrectCommentValidation() { $form = $this->getRequestForm(array( - 'author' => 'test1', - 'comment' => 'test2', - 'sticky' => '0' + 'author' => 'test1', + 'comment' => 'test2', + 'sticky' => '0', + 'btn_submit' => 'foo' ), self::FORMCLASS); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting correct comment form to be considered valid" ); } @@ -47,7 +48,7 @@ class CommentFormTest extends BaseFormTest 'sticky' => '0' ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting missing comment text in comment form to cause validation errors" ); } @@ -60,7 +61,7 @@ class CommentFormTest extends BaseFormTest 'sticky' => '0' ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting missing comment author to cause validation errors" ); } diff --git a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php index 3a35d48ba..70e8b64c6 100644 --- a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php @@ -4,33 +4,27 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__.'/BaseFormTest.php'; require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php'; require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; use \Zend_View; use \Zend_Test_PHPUnit_ControllerTestCase; -use Monitoring\Form\Command\ConfirmationForm; +use Monitoring\Form\Command\CommandForm; -class ConfirmationFormTest extends BaseFormTest +class CommandFormTest extends BaseFormTest { public function testFormCreation() { $view = new Zend_View(); - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->setRequest($this->getRequest()); - $form->setSubmitLabel('111TEST_SUBMIT'); - - $form->setCancelLabel('888TEST_RESET'); - $form->addNote('444 NOTE 1'); $form->addNote('555 NOTE 2'); $form->buildForm(); $content = $form->render($view); - $this->assertContains('', $content); - $this->assertContains('', $content); $this->assertContains('
', $content); $this->assertContains('
', $content); $this->assertContains('444 NOTE 1
', $content); @@ -39,7 +33,7 @@ class ConfirmationFormTest extends BaseFormTest public function testFormNotes() { - $form = new ConfirmationForm(); + $form = new CommandForm(); $form->addNote('test1'); $form->addNote('test2'); diff --git a/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php b/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php index 8ba7f0602..bce0ab5c9 100644 --- a/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php @@ -4,17 +4,17 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__. '/BaseFormTest.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationWithIdentifierForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandWithIdentifierForm.php'; -use Monitoring\Form\Command\ConfirmationWithIdentifierForm; +use Monitoring\Form\Command\CommandWithIdentifierForm; use \Zend_View; use \Zend_Test_PHPUnit_ControllerTestCase; -class ConfirmationWithIdentifierFormTest extends BaseFormTest +class CommandWithIdentifierFormTest extends BaseFormTest { - const FORMCLASS = "Monitoring\Form\Command\ConfirmationWithIdentifierForm"; + const FORMCLASS = "Monitoring\Form\Command\CommandWithIdentifierForm"; public function testForm() { $form = $this->getRequestForm(array(), self::FORMCLASS); @@ -28,7 +28,8 @@ class ConfirmationWithIdentifierFormTest extends BaseFormTest { $form = $this->getRequestForm(array( - 'testval' => 123 + 'testval' => 123, + 'btn_submit' => 'foo' ), self::FORMCLASS); $form->setFieldLabel('Test1'); @@ -36,7 +37,7 @@ class ConfirmationWithIdentifierFormTest extends BaseFormTest $form->setSubmitLabel('DING DING'); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting correct confirmation with id to be valid" ); } @@ -48,7 +49,7 @@ class ConfirmationWithIdentifierFormTest extends BaseFormTest ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting an invalid (empty) value to cause validation errors" ); } @@ -60,7 +61,7 @@ class ConfirmationWithIdentifierFormTest extends BaseFormTest ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting an non numeric value to cause validation errors" ); } @@ -72,7 +73,7 @@ class ConfirmationWithIdentifierFormTest extends BaseFormTest ), self::FORMCLASS); $form->buildForm(); - $this->assertTrue($form->isPostAndValid()); + $this->assertTrue($form->isSubmittedAndValid()); $this->assertEquals('123123666', $form->getElement('objectid')->getValue()); } diff --git a/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php b/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php index cdeb1a886..ba366e2f6 100644 --- a/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php @@ -3,7 +3,7 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__. '/BaseFormTest.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/CustomNotificationForm.php'; @@ -16,13 +16,14 @@ class CustomNotificationFormTest extends BaseFormTest public function testForm1() { $form = $this->getRequestForm(array( - 'comment' => 'TEST COMMENT', - 'author' => 'LAOLA' + 'comment' => 'TEST COMMENT', + 'author' => 'LAOLA', + 'btn_submit' => 'foo' ), "Monitoring\Form\Command\CustomNotificationForm"); $form->buildForm(); $this->assertCount(7, $form->getElements()); - $this->assertTrue($form->isPostAndValid()); + $this->assertTrue($form->isSubmittedAndValid()); } } diff --git a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php index 856b4bd31..3e8536b2f 100644 --- a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php @@ -3,7 +3,7 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__. '/BaseFormTest.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/DelayNotificationForm.php'; @@ -16,7 +16,8 @@ class DelayNotificationFormFormTest extends BaseFormTest public function testValidForm() { $form = $this->getRequestForm(array( - 'minutes' => 12 + 'minutes' => 12, + 'btn_submit' => 'foo' ), 'Monitoring\Form\Command\DelayNotificationForm'); $form->buildForm(); @@ -28,7 +29,7 @@ class DelayNotificationFormFormTest extends BaseFormTest $this->assertTrue($element->isRequired(), "Assert minutes to be declared as required"); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert a correct DelayNotificationForm to be considered valid" ); @@ -44,7 +45,7 @@ class DelayNotificationFormFormTest extends BaseFormTest $form->buildForm(); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Asserting invalid minutes (NaN) to cause validation errors" ); diff --git a/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php b/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php index ff52d69ca..6403f8d8f 100644 --- a/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php @@ -6,7 +6,7 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__. '/BaseFormTest.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/RescheduleNextCheckForm.php'; @@ -25,23 +25,25 @@ class RescheduleNextCheckFormTest extends BaseFormTest $form = $this->getRequestForm(array( 'checktime' => '2013-10-19 17:30:00', - 'forcecheck' => 1 + 'forcecheck' => 1, + 'btn_submit' => 'foo' ), self::FORMCLASS); $form->buildForm(); $this->assertCount(6, $form->getElements()); $this->assertTrue( - $form->isPostAndValid(), + $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 + 'forcecheck' => 0, + 'btn_submit' => 'foo' ), self::FORMCLASS); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Asserting a reschedule form with correct time and forecheck=0 to be valid' ); } @@ -54,7 +56,7 @@ class RescheduleNextCheckFormTest extends BaseFormTest ), self::FORMCLASS); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Asserting an logically invalid checktime to be considered as invalid reschedule data' ); @@ -65,7 +67,7 @@ class RescheduleNextCheckFormTest extends BaseFormTest $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Asserting an invalid non-numeric checktime to be considered as invalid reschedule data' ); } diff --git a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php index 7ae842296..6e277a0d3 100644 --- a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php @@ -3,7 +3,7 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__. '/BaseFormTest.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/ScheduleDowntimeForm.php'; @@ -16,10 +16,15 @@ class ScheduleDowntimeFormTest extends BaseFormTest const FORMCLASS = 'Monitoring\Form\Command\ScheduleDowntimeForm'; public function testCorrectFormElementCreation() { - $form = $this->getRequestForm(array(), self::FORMCLASS); - $form->buildForm(); + $formFixed = $this->getRequestForm(array(), self::FORMCLASS); + $formFixed->buildForm(); + $formFlexible = $this->getRequestForm(array( + 'type' => 'flexible' + ), self::FORMCLASS); + $formFlexible->buildForm(); - $this->assertCount(13, $form->getElements()); + $this->assertCount(11, $formFixed->getElements()); + $this->assertCount(13, $formFlexible->getElements()); $form = $this->getRequestForm(array(), self::FORMCLASS); $form->setWithChildren(true); @@ -32,14 +37,15 @@ class ScheduleDowntimeFormTest extends BaseFormTest public function testCorrectValidationWithChildrend() { $form = $this->getRequestForm(array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'btn_submit' => 'foo', // 'childobjects' => '', ), self::FORMCLASS); @@ -47,24 +53,25 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(true); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Asserting a correct fixed downtime form to be considered valid' ); $form = $this->getRequestForm(array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, - 'hours' => '10', - 'minutes' => '10', + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '10', + 'minutes' => '10', + 'btn_submit' => 'foo' // 'childobjects' => '', ), self::FORMCLASS); $form->setWithChildren(true); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Asserting a correct flexible downtime form to be considered valid' ); @@ -86,7 +93,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(true); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert missing hours and minutes in downtime form to cause failing validation' ); } @@ -109,7 +116,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert missing author to cause validation errors in fixed downtime' ); } @@ -131,7 +138,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert missing comment to cause validation errors in fixed downtime' ); } @@ -152,7 +159,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(true); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert invalid trigger field to cause validation to fail' ); } @@ -173,7 +180,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(true); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert incorrect start time to cause validation errors in fixed downtime' ); } @@ -195,7 +202,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(true); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert invalid endtime to cause validation errors in fixed downtime' ); } @@ -217,7 +224,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(true); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert negative hours to cause validation errors in flexible downtime' ); } @@ -238,7 +245,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(true); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), 'Assert non numeric valud to cause validation errors in flexible downtime ' ); @@ -247,21 +254,22 @@ class ScheduleDowntimeFormTest extends BaseFormTest public function testCorrectScheduleDowntimeWithoutChildrenForm() { $form = $this->getRequestForm(array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'btn_submit' => 'foo', 'childobjects' => '0', ), self::FORMCLASS); $form->setWithChildren(false); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert a correct schedule downtime without children form to be considered valid" ); } @@ -281,7 +289,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(false); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert and incorrect (non-numeric) childobjects value to cause validation errors" ); @@ -299,7 +307,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form->setWithChildren(false); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert and incorrect (numeric) childobjects value to cause validation errors" ); } diff --git a/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php b/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php index 019e9768a..0bf710936 100644 --- a/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php +++ b/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php @@ -3,7 +3,7 @@ namespace Test\Monitoring\Forms\Command; require_once __DIR__. '/BaseFormTest.php'; -require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php'; +require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php'; require_once __DIR__. '/../../../../../application/forms/Command/SubmitPassiveCheckResultForm.php'; @@ -59,13 +59,14 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest $form = $this->getRequestForm(array( 'pluginstate' => 0, 'checkoutput' => 'DING', - 'performancedata' => '' + 'performancedata' => '', + 'btn_submit' => 'foo' ), self::FORMCLASS); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $this->assertTrue( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert a correct passive service check form to pass form validation" ); } @@ -80,7 +81,7 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert empty checkoutput to cause validation errors in passive service check " ); } @@ -95,7 +96,7 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $this->assertFalse( - $form->isPostAndValid(), + $form->isSubmittedAndValid(), "Assert invalid (non-numeric) state to cause validation errors in passive service check" ); } diff --git a/test/php/library/Icinga/Web/FormTest.php b/test/php/library/Icinga/Web/FormTest.php new file mode 100644 index 000000000..09c09a86a --- /dev/null +++ b/test/php/library/Icinga/Web/FormTest.php @@ -0,0 +1,109 @@ +setCancelLabel('Cancel'); + $form->buildForm(); + $this->assertCount(2, $form->getElements(), 'Asserting that the cancel label is present'); + } + + /** + * Tests whether the submit button will be added to the form + */ + function testSubmitButton() + { + $form = new TestForm(); + $form->setSubmitLabel('Submit'); + $form->buildForm(); + $this->assertCount(2, $form->getElements(), 'Asserting that the submit button is present'); + } + + /** + * Tests whether automatic form submission will be enabled for a single field + */ + function testEnableAutoSubmitSingle() + { + $form = new TestForm(); + $form->addElement('checkbox', 'example1', array()); + $form->enableAutoSubmit(array('example1')); + $this->assertArrayHasKey('onchange', $form->getElement('example1')->getAttribs(), + 'Asserting that auto-submit got enabled for one element'); + } + + /** + * Tests whether automatic form submission will be enabled for multiple fields + */ + function testEnableAutoSubmitMultiple() + { + $form = new TestForm(); + $form->addElement('checkbox', 'example1', array()); + $form->addElement('checkbox', 'example2', array()); + $form->enableAutoSubmit(array('example1', 'example2')); + $this->assertArrayHasKey('onchange', $form->getElement('example1')->getAttribs(), + 'Asserting that auto-submit got enabled for multiple elements'); + $this->assertArrayHasKey('onchange', $form->getElement('example2')->getAttribs(), + 'Asserting that auto-submit got enabled for multiple elements'); + } + + /** + * Tests whether automatic form submission can only be enabled for existing elements + * + * @expectedException Icinga\Exception\ProgrammingError + */ + function testEnableAutoSubmitExisting() + { + $form = new TestForm(); + $form->enableAutoSubmit(array('not_existing')); + } + + /** + * Tests whether a form will be detected as properly submitted + */ + function testFormSubmission() + { + $form = new TestForm(); + $form->setTokenDisabled(); + $form->setSubmitLabel('foo'); + $request = $this->getRequest(); + $form->setRequest($request->setMethod('GET')); + $this->assertFalse($form->isSubmittedAndValid(), + 'Asserting that it is not possible to submit a form not using POST'); + $request->setMethod('POST')->setPost(array('btn_submit' => 'foo')); + $this->assertTrue($form->isSubmittedAndValid(), + 'Asserting that it is possible to detect a form as submitted'); + } +} \ No newline at end of file diff --git a/test/php/regression/LoginMaskBroken4459Test.php b/test/php/regression/LoginMaskBroken4459Test.php index fee3736cd..bb35bbdb3 100644 --- a/test/php/regression/LoginMaskBroken4459Test.php +++ b/test/php/regression/LoginMaskBroken4459Test.php @@ -56,7 +56,7 @@ namespace Tests\Icinga\Regression $form = new LoginForm(); $form->setRequest($request); $form->buildForm(); - $this->assertTrue($form->isPostAndValid()); + $this->assertTrue($form->isSubmittedAndValid()); } } diff --git a/test/php/runtests b/test/php/runtests index 6fd0d18c2..13f53db1f 100755 --- a/test/php/runtests +++ b/test/php/runtests @@ -9,7 +9,7 @@ from optparse import OptionParser, BadOptionError, AmbiguousOptionError APPLICATION = 'phpunit' -DEFAULT_ARGS = ['--strict'] +DEFAULT_ARGS = [] VAGRANT_SCRIPT = '/vagrant/test/php/runtests' REPORT_DIRECTORY = '../../build/log'