Monitoring/Commands: Fix tests

refs #4524
This commit is contained in:
Eric Lippmann 2013-08-15 15:35:17 +02:00 committed by Jannis Moßhammer
parent 3d3fbb123d
commit 329d63839b
6 changed files with 159 additions and 207 deletions

View File

@ -38,16 +38,6 @@ 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
* *
@ -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

@ -1,24 +1,24 @@
<?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/CommandForm.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(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/WithChildrenCommandForm.php');
require_once realpath($base.'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php'); require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/AcknowledgeForm.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 +33,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',
'comment' => '',
'persistent' => '0',
'expire' => '0',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'foo'
), 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', 'persistent' => '0',
'expire' => '1', 'expire' => '1',
'expiretime' => '2013-07-10 17:32:16', 'expiretime' => '2013-07-10 17:32:16',
'sticky' => '0', 'sticky' => '0',
'notify' => '0', 'notify' => '0',
'btn_submit' => 'foo' 'btn_submit' => 'Submit'
), self::FORMCLASS); ), 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

@ -1,68 +1,65 @@
<?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 __DIR__ . '/../../../../../application/forms/Command/CommandForm.php';
require_once __DIR__ . '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
require_once __DIR__ . '/../../../../../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 FORMCLASS
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 FORMCLASS = '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::FORMCLASS);
$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,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