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,19 +38,9 @@ use \Zend_Validate_Date;
*/
class CommandForm extends Form
{
/**
* Default date format
*/
const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s';
/**
* Default format for validation
*/
const DEFAULT_DATE_VALIDATION = 'yyyy-MM-dd hh:ii:ss';
/**
* Array of messages
*
*
* @var string[]
*/
private $notes = array();
@ -142,7 +132,7 @@ class CommandForm extends Form
$authorField = new Zend_Form_Element_Hidden(
array(
'name' => 'author',
'label' => t('Author name'),
'label' => t('Author (Your Name)'),
'value' => $authorName,
'required' => true
)
@ -159,34 +149,4 @@ class CommandForm extends Form
return $authorField;
}
/**
* Getter for date format
* TODO(mh): Should be user preferences
* @return string
*/
protected function getDateFormat()
{
return self::DEFAULT_DATE_FORMAT;
}
/**
* Getter for date validation format
* @return string
*/
protected function getDateValidationFormat()
{
return self::DEFAULT_DATE_VALIDATION;
}
/**
* Create a new date validator
* @return Zend_Validate_Date
*/
protected function createDateTimeValidator()
{
$validator = new Zend_Validate_Date();
$validator->setFormat($this->getDateValidationFormat());
return $validator;
}
}

View File

@ -1,24 +1,24 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command;
require_once __DIR__.'/BaseFormTest.php';
$base = __DIR__.'/../../../../../../../';
require_once $base.'modules/monitoring/application/forms/Command/CommandForm.php';
require_once $base . 'library/Icinga/Util/ConfigAwareFactory.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');
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/CommandForm.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/WithChildrenCommandForm.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/AcknowledgeForm.php');
use \DateTimeZone;
use \Zend_View;
use \Zend_Test_PHPUnit_ControllerTestCase;
use Monitoring\Form\Command\AcknowledgeForm;
use Icinga\Util\DateTimeFactory;
use \Monitoring\Form\Command\AcknowledgeForm; // Used by constant FORMCLASS
use \Icinga\Util\DateTimeFactory;
class AcknowledgeFormTest extends BaseFormTest
{
const FORMCLASS = "Monitoring\Form\Command\AcknowledgeForm";
const FORMCLASS = 'Monitoring\Form\Command\AcknowledgeForm';
/**
* Set up the default time zone
@ -33,105 +33,93 @@ class AcknowledgeFormTest extends BaseFormTest
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
}
public function testForm()
{
$formWithoutExpiration = $this->getRequestForm(array(), self::FORMCLASS);
$formWithoutExpiration->buildForm();
$formWithExpiration = $this->getRequestForm(array(
'expire' => '1'
), self::FORMCLASS);
$formWithExpiration->buildForm();
$this->assertCount(10, $formWithoutExpiration->getElements());
$this->assertCount(11, $formWithExpiration->getElements());
}
public function testValidateCorrectForm()
public function testFormValid()
{
$form = $this->getRequestForm(array(
'author' => 'test1',
'comment' => 'test comment',
'persistent' => '0',
'expire' => '0',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'foo'
'author' => 'Author',
'comment' => 'Comment',
'persistent' => '0',
'expire' => '0',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertTrue(
$form->isSubmittedAndValid(),
"Asserting a correct form to be validated correctly"
'Legal request data without expire time must be considered valid'
);
}
public function testDetectMissingAcknowledgementComment()
{
$form = $this->getRequestForm(array(
'author' => 'test1',
'comment' => '',
'persistent' => '0',
'expire' => '0',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'foo'
$formWithExpireTime = $this->getRequestForm(array(
'author' => 'Author',
'comment' => 'Comment',
'persistent' => '0',
'expire' => '1',
'expiretime' => '2013-07-10 17:32:16',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
"Asserting a missing comment text to cause validation errors"
);
}
public function testValidateMissingExpireTime()
{
$form = $this->getRequestForm(array(
'author' => 'test1',
'comment' => 'test comment',
'persistent' => '0',
'expire' => '1',
'expiretime' => '',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'foo'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
"Asserting a missing expire time to cause validation errors when expire is 1"
);
}
public function testValidateIncorrectExpireTime()
{
$form = $this->getRequestForm(array(
'author' => 'test1',
'comment' => 'test comment',
'persistent' => '0',
'expire' => '1',
'expiretime' => 'NOT A DATE',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'foo'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
"Assert incorrect dates to be recognized when validating expiretime"
);
}
public function testValidateCorrectAcknowledgementWithExpireTime()
{
$form = $this->getRequestForm(array(
'author' => 'test1',
'comment' => 'test comment',
'persistent' => '0',
'expire' => '1',
'expiretime' => '2013-07-10 17:32:16',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'foo'
), self::FORMCLASS);
$this->assertTrue(
$formWithExpireTime->isSubmittedAndValid(),
'Legal request data with expire time must be considered valid'
);
}
public function testFormInvalidWhenCommentMissing()
{
$form = $this->getRequestForm(array(
'author' => 'Author',
'comment' => '',
'persistent' => '0',
'expire' => '0',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
"Assert that correct expire time acknowledgement is considered valid"
'Missing comment must be considered not valid'
);
}
public function testFormInvalidWhenExpireTimeMissingAndExpireSet()
{
$form = $this->getRequestForm(array(
'author' => 'Author',
'comment' => 'Comment',
'persistent' => '0',
'expire' => '1',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
'If expire is set and expire time is missing, the form must not be valid'
);
}
public function testFormInvalidWhenExpireTimeIsIncorrectAndExpireSet()
{
$form = $this->getRequestForm(array(
'author' => 'Author',
'comment' => 'Comment',
'persistent' => '0',
'expire' => '1',
'expiretime' => 'Not a date',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
'If expire is set and expire time is incorrect, the form must not be valid'
);
}
}
// @codingStandardsIgnoreStop

View File

@ -1,68 +1,65 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
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';
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;
use \Monitoring\Form\Command\CommentForm; // Used by constant FORMCLASS
class CommentFormTest extends BaseFormTest
{
const FORMCLASS = "Monitoring\Form\Command\CommentForm";
public function testForm()
{
$form = new CommentForm();
$form->setRequest($this->getRequest());
$form->buildForm();
$this->assertCount(6, $form->getElements());
}
const FORMCLASS = 'Monitoring\Form\Command\CommentForm';
public function testCorrectCommentValidation()
{
$form = $this->getRequestForm(array(
'author' => 'test1',
'comment' => 'test2',
'sticky' => '0',
'btn_submit' => 'foo'
'author' => 'Author',
'comment' => 'Comment',
'sticky' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertTrue(
$form->isSubmittedAndValid(),
"Asserting correct comment form to be considered valid"
'Legal request data must be considered valid'
);
}
public function testRecognizeMissingCommentText()
public function testFormInvalidWhenCommentMissing()
{
$form = $this->getRequestForm(array(
'author' => 'test1',
'comment' => '',
'sticky' => '0'
'author' => 'Author',
'comment' => '',
'sticky' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
"Asserting missing comment text in comment form to cause validation errors"
'Missing comment must be considered not valid'
);
}
public function testRecognizeMissingCommentAuthor()
public function testFormInvalidWhenAuthorMissing()
{
$form = $this->getRequestForm(array(
'author' => '',
'comment' => 'test2',
'sticky' => '0'
'author' => '',
'comment' => 'Comment',
'sticky' => '0',
'btn_submit' => 'Submit'
), self::FORMCLASS);
$this->assertFalse(
$form->isSubmittedAndValid(),
"Asserting missing comment author to cause validation errors"
'Missing author must be considered not valid'
);
}
}
// @codingStandardsIgnoreEnd

View File

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

View File

@ -1,11 +1,7 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: moja
* Date: 7/31/13
* Time: 1:29 PM
* To change this template use File | Settings | File Templates.
*/
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
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/SecureShell.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
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Protocol\Commandpipe;
require_once(__DIR__.'/CommandPipeLoader.php');
@ -6,6 +10,7 @@ CommandPipeLoader::requireLibrary();
use Icinga\Protocol\Commandpipe\Comment as Comment;
use Icinga\Protocol\Commandpipe\Acknowledgement as Acknowledgement;
use Icinga\Protocol\Commandpipe\CustomNotification;
use Icinga\Protocol\Commandpipe\Downtime as Downtime;
use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe;
use \Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG;
@ -423,15 +428,15 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
{
$pipe = $this->getLocalTestPipe();
try {
$comment = new Comment("author", "commenttext");
$notification = new CustomNotification('Author', 'Comment');
$pipe->sendCustomNotification(array(
(object) array(
"host_name" => "host1",
"service_description" => "service1"
'host_name' => 'Host',
'service_description' => 'Service'
)
), $comment);
), $notification);
$this->assertCommandSucceeded(
"SEND_CUSTOM_SVC_NOTIFICATION;host1;service1;0;author;commenttext"
'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;0;Author;Comment'
);
} catch (Exception $e) {
$this->cleanup();
@ -449,15 +454,15 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
{
$pipe = $this->getLocalTestPipe();
try {
$comment = new Comment('author', 'commenttext');
$notification = new CustomNotification('Author', 'Comment', true, true);
$pipe->sendCustomNotification(array(
(object) array(
'host_name' => 'host'
'host_name' => 'Host',
'service_description' => 'Service'
)
), $comment, Commandpipe::NOTIFY_FORCED, Commandpipe::NOTIFY_BROADCAST, Commandpipe::NOTIFY_INCREMENT);
), $notification);
$this->assertCommandSucceeded(
'SEND_CUSTOM_HOST_NOTIFICATION;host;7;author;commenttext'
'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;3;Author;Comment'
);
} catch (Exception $e) {
$this->cleanup();
@ -493,3 +498,4 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
$this->cleanup();
}
}
// @codingStandardsIgnoreStop