Monitoring/Tests: Replace BaseFormTest with BaseTestCase

refs #4586
This commit is contained in:
Eric Lippmann 2013-09-03 14:23:01 +02:00 committed by Jannis Moßhammer
parent 6f383abdd1
commit 3e4d1aa2fb
8 changed files with 290 additions and 307 deletions

View File

@ -1,27 +1,27 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/CommandForm.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/AcknowledgeForm.php'); use Icinga\Test\BaseTestCase;
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php'); require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/AcknowledgeForm.php';
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
use \DateTimeZone; use \DateTimeZone;
use \Icinga\Module\Monitoring\Form\Command\AcknowledgeForm; // Used by constant FORMCLASS use Icinga\Util\DateTimeFactory;
use \Icinga\Util\DateTimeFactory;
use \Test\Icinga\Web\Form\BaseFormTest;
class AcknowledgeFormTest extends BaseFormTest class AcknowledgeFormTest extends BaseTestCase
{ {
const FORMCLASS = '\Icinga\Module\Monitoring\Form\Command\AcknowledgeForm'; const FORM_CLASS = 'Icinga\Module\Monitoring\Form\Command\AcknowledgeForm';
/** /**
* Set up the default time zone * Set DateTimeFactory's time zone to UTC
* *
* Utilizes singleton DateTimeFactory * Utilizes singleton DateTimeFactory
* *
@ -29,38 +29,41 @@ class AcknowledgeFormTest extends BaseFormTest
*/ */
public function setUp() public function setUp()
{ {
date_default_timezone_set('UTC');
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
} }
public function testFormValid() public function testFormValid()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => 'Comment', array(
'persistent' => '0', 'author' => 'Author',
'expire' => '0', 'comment' => 'Comment',
'sticky' => '0', 'persistent' => '0',
'notify' => '0', 'expire' => '0',
'btn_submit' => 'Submit' 'sticky' => '0',
), self::FORMCLASS); 'notify' => '0',
'btn_submit' => 'Submit'
)
);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Legal request data without expire time must be considered valid' 'Legal request data without expire time must be considered valid'
); );
$formWithExpireTime = $this->getRequestForm(array( $formWithExpireTime = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => 'Comment', array(
'persistent' => '0', 'author' => 'Author',
'expire' => '1', 'comment' => 'Comment',
'expiretime' => '10/07/2013 5:32 PM', 'persistent' => '0',
'sticky' => '0', 'expire' => '1',
'notify' => '0', 'expiretime' => '10/07/2013 5:32 PM',
'btn_submit' => 'Submit' 'sticky' => '0',
), self::FORMCLASS); 'notify' => '0',
'btn_submit' => 'Submit'
)
);
$this->assertTrue( $this->assertTrue(
$formWithExpireTime->isSubmittedAndValid(), $formWithExpireTime->isSubmittedAndValid(),
'Legal request data with expire time must be considered valid' 'Legal request data with expire time must be considered valid'
@ -69,16 +72,18 @@ class AcknowledgeFormTest extends BaseFormTest
public function testFormInvalidWhenCommentMissing() public function testFormInvalidWhenCommentMissing()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => '', array(
'persistent' => '0', 'author' => 'Author',
'expire' => '0', 'comment' => '',
'sticky' => '0', 'persistent' => '0',
'notify' => '0', 'expire' => '0',
'btn_submit' => 'Submit' 'sticky' => '0',
), self::FORMCLASS); 'notify' => '0',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Missing comment must be considered not valid' 'Missing comment must be considered not valid'
@ -87,16 +92,18 @@ class AcknowledgeFormTest extends BaseFormTest
public function testFormInvalidWhenExpireTimeMissingAndExpireSet() public function testFormInvalidWhenExpireTimeMissingAndExpireSet()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => 'Comment', array(
'persistent' => '0', 'author' => 'Author',
'expire' => '1', 'comment' => 'Comment',
'sticky' => '0', 'persistent' => '0',
'notify' => '0', 'expire' => '1',
'btn_submit' => 'Submit' 'sticky' => '0',
), self::FORMCLASS); 'notify' => '0',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'If expire is set and expire time is missing, the form must not be valid' 'If expire is set and expire time is missing, the form must not be valid'
@ -105,21 +112,22 @@ class AcknowledgeFormTest extends BaseFormTest
public function testFormInvalidWhenExpireTimeIsIncorrectAndExpireSet() public function testFormInvalidWhenExpireTimeIsIncorrectAndExpireSet()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => 'Comment', array(
'persistent' => '0', 'author' => 'Author',
'expire' => '1', 'comment' => 'Comment',
'expiretime' => 'Not a date', 'persistent' => '0',
'sticky' => '0', 'expire' => '1',
'notify' => '0', 'expiretime' => 'Not a date',
'btn_submit' => 'Submit' 'sticky' => '0',
), self::FORMCLASS); 'notify' => '0',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'If expire is set and expire time is incorrect, the form must not be valid' 'If expire is set and expire time is incorrect, the form must not be valid'
); );
} }
} }
// @codingStandardsIgnoreStop

View File

@ -1,29 +1,31 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/CommentForm.php');
use \Icinga\Module\Monitoring\Form\Command\CommentForm; // Used by constant FORMCLASS use Icinga\Test\BaseTestCase;
use \Test\Icinga\Web\Form\BaseFormTest;
class CommentFormTest extends BaseFormTest require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommentForm.php';
class CommentFormTest extends BaseTestCase
{ {
const FORMCLASS = '\Icinga\Module\Monitoring\Form\Command\CommentForm'; const FORM_CLASS = 'Icinga\Module\Monitoring\Form\Command\CommentForm';
public function testCorrectCommentValidation() public function testCorrectCommentValidation()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => 'Comment', array(
'sticky' => '0', 'author' => 'Author',
'btn_submit' => 'Submit' 'comment' => 'Comment',
), self::FORMCLASS); 'sticky' => '0',
'btn_submit' => 'Submit'
)
);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Legal request data must be considered valid' 'Legal request data must be considered valid'
@ -32,14 +34,16 @@ class CommentFormTest extends BaseFormTest
public function testFormInvalidWhenCommentMissing() public function testFormInvalidWhenCommentMissing()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => '', array(
'sticky' => '0', 'author' => 'Author',
'btn_submit' => 'Submit' 'comment' => '',
'sticky' => '0',
), self::FORMCLASS); 'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Missing comment must be considered not valid' 'Missing comment must be considered not valid'
@ -48,17 +52,18 @@ class CommentFormTest extends BaseFormTest
public function testFormInvalidWhenAuthorMissing() public function testFormInvalidWhenAuthorMissing()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => '', self::FORM_CLASS,
'comment' => 'Comment', array(
'sticky' => '0', 'author' => '',
'btn_submit' => 'Submit' 'comment' => 'Comment',
), self::FORMCLASS); 'sticky' => '0',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Missing author must be considered not valid' 'Missing author must be considered not valid'
); );
} }
} }
// @codingStandardsIgnoreEnd

View File

@ -1,81 +1,63 @@
<?php <?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php');
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php';
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
require_once __DIR__. '/../../../../../application/forms/Command/CommandWithIdentifierForm.php';
use \Icinga\Module\Monitoring\Form\Command\CommandWithIdentifierForm; use Icinga\Test\BaseTestCase;
use \Zend_View;
use \Zend_Test_PHPUnit_ControllerTestCase;
use \Test\Icinga\Web\Form\BaseFormTest;
class CommandWithIdentifierFormTest extends BaseFormTest require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/WithChildrenCommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandWithIdentifierForm.php';
class CommandWithIdentifierFormTest extends BaseTestCase
{ {
const FORMCLASS = "\Icinga\Module\Monitoring\Form\Command\CommandWithIdentifierForm"; const FORM_CLASS = '\Icinga\Module\Monitoring\Form\Command\CommandWithIdentifierForm';
public function testForm()
public function testFormInvalidWhenObjectIdMissing()
{ {
$form = $this->getRequestForm(array(), self::FORMCLASS); $form = $this->createForm(
$form->setSubmitLabel('DING DING'); self::FORM_CLASS,
$form->buildForm(); array(
'object_id' => '',
$this->assertCount(4, $form->getElements()); 'btn_submit' => 'Submit'
} )
public function testCorrectFormValidation()
{
$form = $this->getRequestForm(array(
'testval' => 123,
'btn_submit' => 'foo'
), self::FORMCLASS);
$form->setFieldLabel('Test1');
$form->setFieldName('testval');
$form->setSubmitLabel('DING DING');
$this->assertTrue(
$form->isSubmittedAndValid(),
"Asserting correct confirmation with id to be valid"
); );
}
public function testInvalidValueValidationErrors()
{
$form = $this->getRequestForm(array(
'testval' => ''
), self::FORMCLASS);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
"Asserting an invalid (empty) value to cause validation errors" 'Missing object_id must be considered not valid'
); );
} }
public function testNonNumericValueValidationErrors() public function testFormInvalidWhenObjectIdNonDigit()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'testval' => 'NaN' self::FORM_CLASS,
), self::FORMCLASS); array(
'object_id' => 'A Service',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
"Asserting an non numeric value to cause validation errors" 'Non numeric input must be considered not valid'
); );
} }
public function testRequestBridge() public function testFormValidWhenObjectIdIsDigit()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'objectid' => 123123666 self::FORM_CLASS,
), self::FORMCLASS); array(
$form->buildForm(); 'object_id' => 1,
'btn_submit' => 'Submit'
$this->assertTrue($form->isSubmittedAndValid()); )
);
$this->assertEquals('123123666', $form->getElement('objectid')->getValue()); $this->assertFalse(
$form->isSubmittedAndValid(),
'Digits must be considered valid'
);
} }
} }

View File

@ -1,32 +1,33 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/CustomNotificationForm.php');
use \Test\Icinga\Web\Form\BaseFormTest; use Icinga\Test\BaseTestCase;
use \Icinga\Module\Monitoring\Form\Command\CustomNotificationForm; // Used by constant FORM_CLASS
class CustomNotificationFormTest extends BaseFormTest require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CustomNotificationForm.php';
class CustomNotificationFormTest extends BaseTestCase
{ {
const FORM_CLASS = '\Icinga\Module\Monitoring\Form\Command\CustomNotificationForm'; const FORM_CLASS = 'Icinga\Module\Monitoring\Form\Command\CustomNotificationForm';
public function testFormInvalidWhenCommentMissing() public function testFormInvalidWhenCommentMissing()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'author' => 'Author', self::FORM_CLASS,
'comment' => '', array(
'btn_submit' => 'Submit' 'author' => 'Author',
), self::FORM_CLASS); 'comment' => '',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Missing comment must be considered not valid' 'Missing comment must be considered not valid'
); );
} }
} }
// @codingStandardsIgnoreEnd

View File

@ -1,27 +1,31 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/DelayNotificationForm.php');
use \Test\Icinga\Web\Form\BaseFormTest; use Icinga\Test\BaseTestCase;
use \Icinga\Module\Monitoring\Form\Command\DelayNotificationForm; // Used by constant FORM_CLASS
class DelayNotificationFormTest extends BaseFormTest require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/DelayNotificationForm.php';
use Icinga\Module\Monitoring\Form\Command\DelayNotificationForm;
class DelayNotificationFormTest extends BaseTestCase
{ {
const FORM_CLASS = '\Icinga\Module\Monitoring\Form\Command\DelayNotificationForm'; const FORM_CLASS = 'Icinga\Module\Monitoring\Form\Command\DelayNotificationForm';
public function testFormInvalidWhenNotificationDelayMissing() public function testFormInvalidWhenNotificationDelayMissing()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'minutes' => '', self::FORM_CLASS,
'btn_submit' => 'Submit' array(
), self::FORM_CLASS); 'minutes' => '',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Missing notification delay must be considered invalid' 'Missing notification delay must be considered invalid'
@ -30,11 +34,13 @@ class DelayNotificationFormTest extends BaseFormTest
public function testFormInvalidWhenNotificationDelayNaN() public function testFormInvalidWhenNotificationDelayNaN()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'minutes' => 'A String', self::FORM_CLASS,
'btn_submit' => 'Submit' array(
), self::FORM_CLASS); 'minutes' => 'A String',
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Incorrect notification delay, i.e. NaN must be considered invalid' 'Incorrect notification delay, i.e. NaN must be considered invalid'
@ -43,15 +49,16 @@ class DelayNotificationFormTest extends BaseFormTest
public function testFormInvalidWhenNotificationDelayOverflows() public function testFormInvalidWhenNotificationDelayOverflows()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'minutes' => DelayNotificationForm::MAX_DELAY + 1, self::FORM_CLASS,
'btn_submit' => 'Submit' array(
), self::FORM_CLASS); 'minutes' => DelayNotificationForm::MAX_DELAY + 1,
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Notification delay bigger than constant "DelayNotificationForm::MAX_DELAY" must be considered invalid' 'Notification delay bigger than constant "DelayNotificationForm::MAX_DELAY" must be considered invalid'
); );
} }
} }
// @codingStandardsIgnoreEnd

View File

@ -1,26 +1,28 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/RescheduleNextCheckForm.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php'); use Icinga\Test\BaseTestCase;
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/WithChildrenCommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/RescheduleNextCheckForm.php';
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
use \Icinga\Module\Monitoring\Form\Command\RescheduleNextCheckForm; // Used by constant FORM_CLASS
use \DateTimeZone; use \DateTimeZone;
use \Icinga\Util\DateTimeFactory; use Icinga\Util\DateTimeFactory;
use \Test\Icinga\Web\Form\BaseFormTest;
class RescheduleNextCheckFormTest extends BaseFormTest class RescheduleNextCheckFormTest extends BaseTestCase
{ {
const FORM_CLASS = '\Icinga\Module\Monitoring\Form\Command\RescheduleNextCheckForm'; const FORM_CLASS = 'Icinga\Module\Monitoring\Form\Command\RescheduleNextCheckForm';
/** /**
* Set up the default time zone * Set DateTimeFactory's time zone to UTC
* *
* Utilizes singleton DateTimeFactory * Utilizes singleton DateTimeFactory
* *
@ -28,44 +30,48 @@ class RescheduleNextCheckFormTest extends BaseFormTest
*/ */
public function setUp() public function setUp()
{ {
date_default_timezone_set('UTC');
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
} }
public function testFormInvalidWhenChecktimeIsIncorrect() public function testFormInvalidWhenChecktimeIsIncorrect()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'checktime' => '2013-24-12 17:30:00', self::FORM_CLASS,
'forcecheck' => 0, array(
'btn_submit' => 'Submit' 'checktime' => '2013-24-12 17:30:00',
), self::FORM_CLASS); 'forcecheck' => 0,
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Asserting a logically incorrect checktime as invalid' 'Asserting a logically incorrect checktime as invalid'
); );
$form2 = $this->getRequestForm(array( $form2 = $this->createForm(
'checktime' => 'Captain Morgan', self::FORM_CLASS,
'forcecheck' => 1, array(
'btn_submit' => 'Submit' 'checktime' => 'Captain Morgan',
), self::FORM_CLASS); 'forcecheck' => 1,
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form2->isSubmittedAndValid(), $form2->isSubmittedAndValid(),
'Providing arbitrary strings as checktime must be considered invalid' 'Providing arbitrary strings as checktime must be considered invalid'
); );
$form3 = $this->getRequestForm(array( $form3 = $this->createForm(
'checktime' => '', self::FORM_CLASS,
'forcecheck' => 0, array(
'btn_submit' => 'Submit' 'checktime' => '',
), self::FORM_CLASS); 'forcecheck' => 0,
'btn_submit' => 'Submit'
)
);
$this->assertFalse( $this->assertFalse(
$form3->isSubmittedAndValid(), $form3->isSubmittedAndValid(),
'Missing checktime must be considered invalid' 'Missing checktime must be considered invalid'
); );
} }
} }
// @codingStandardsIgnoreStop

View File

@ -1,68 +1,87 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Test\Monitoring\Forms\Command; namespace Test\Monitoring\Forms\Command;
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php'); require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/SubmitPassiveCheckResultForm.php');
use \Test\Icinga\Web\Form\BaseFormTest; use Icinga\Test\BaseTestCase;
use \Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm; // Used by constant FORM_CLASS
class SubmitPassiveCheckResultFormTest extends BaseFormTest require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/CommandForm.php';
require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/SubmitPassiveCheckResultForm.php';
use Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm;
class SubmitPassiveCheckResultFormTest extends BaseTestCase
{ {
const FORM_CLASS = '\Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm'; const FORM_CLASS = 'Icinga\Module\Monitoring\Form\Command\SubmitPassiveCheckResultForm';
public function testStateTypes() public function testStateTypes()
{ {
$form = $this->getRequestForm(array(), self::FORM_CLASS); $form = $this->createForm(self::FORM_CLASS, array());
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$options = $form->getOptions(); $options = $form->getOptions();
$this->assertCount(4, $options, 'Assert correct number of states in service passive checks form'); $this->assertCount(4, $options, 'Assert correct number of states in service passive checks form');
$this->assertEquals('OK', $options[0], 'Assert OK state to be available in service passive check form'); $this->assertEquals('OK', $options[0], 'Assert OK state to be available in service passive check form');
$this->assertEquals('WARNING', $options[1], 'Assert WARNING state to be available in service passive check form'); $this->assertEquals(
$this->assertEquals('CRITICAL', $options[2], 'Assert CRITICAL state to be available in service passive check form'); 'WARNING',
$this->assertEquals('UNKNOWN', $options[3], 'Assert UNKNOWN state to be available in service passive check form'); $options[1],
'Assert WARNING state to be available in service passive check form'
);
$this->assertEquals(
'CRITICAL',
$options[2],
'Assert CRITICAL state to be available in service passive check form'
);
$this->assertEquals(
'UNKNOWN',
$options[3],
'Assert UNKNOWN state to be available in service passive check form'
);
$form->setType(SubmitPassiveCheckResultForm::TYPE_HOST); $form->setType(SubmitPassiveCheckResultForm::TYPE_HOST);
$options = $form->getOptions(); $options = $form->getOptions();
$this->assertCount(3, $options, 'Assert correct number of states in host passive checks form'); $this->assertCount(3, $options, 'Assert correct number of states in host passive checks form');
$this->assertEquals('UP', $options[0], 'Assert UP state to be available in host passive check form'); $this->assertEquals('UP', $options[0], 'Assert UP state to be available in host passive check form');
$this->assertEquals('DOWN', $options[1], 'Assert DOWN state to be available in host passive check form'); $this->assertEquals('DOWN', $options[1], 'Assert DOWN state to be available in host passive check form');
$this->assertEquals('UNREACHABLE', $options[2], 'Assert UNREACHABLE state to be available in host passive check form'); $this->assertEquals(
'UNREACHABLE',
$options[2],
'Assert UNREACHABLE state to be available in host passive check form'
);
} }
/** /**
* @expectedException Icinga\Exception\ProgrammingError * @expectedException Icinga\Exception\ProgrammingError
* @expectedExceptionMessage Type is not valid * @expectedExceptionMessage Type is not valid
*/ */
public function testMissingTypeThrowingException() public function testMissingTypeThrowingException()
{ {
$form = $this->getRequestForm(array(), self::FORM_CLASS); $form = $this->createForm(self::FORM_CLASS, array());
$form->buildForm(); $form->buildForm();
} }
public function testCorrectFormCreation() public function testCorrectFormCreation()
{ {
$form = $this->getRequestForm(array(), self::FORM_CLASS); $form = $this->createForm(self::FORM_CLASS, array());
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$form->buildForm(); $form->buildForm();
} }
public function testCorrectServicePassiveCheckSubmission() public function testCorrectServicePassiveCheckSubmission()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'pluginstate' => 0, self::FORM_CLASS,
'checkoutput' => 'DING', array(
'performancedata' => '', 'pluginstate' => 0,
'btn_submit' => 'foo' 'checkoutput' => 'DING',
), self::FORM_CLASS); 'performancedata' => '',
'btn_submit' => 'foo'
)
);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$this->assertTrue( $this->assertTrue(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Assert a correct passive service check form to pass form validation' 'Assert a correct passive service check form to pass form validation'
@ -71,13 +90,15 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest
public function testIncorrectCheckoutputRecognition() public function testIncorrectCheckoutputRecognition()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'pluginstate' => 0, self::FORM_CLASS,
'checkoutput' => '', array(
'performancedata' => '' 'pluginstate' => 0,
), self::FORM_CLASS); 'checkoutput' => '',
'performancedata' => ''
)
);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Assert empty checkoutput to cause validation errors in passive service check ' 'Assert empty checkoutput to cause validation errors in passive service check '
@ -86,13 +107,15 @@ class SubmitPassiveCheckResultFormTest extends BaseFormTest
public function testIncorrectStateRecognition() public function testIncorrectStateRecognition()
{ {
$form = $this->getRequestForm(array( $form = $this->createForm(
'pluginstate' => 'LA', self::FORM_CLASS,
'checkoutput' => 'DING', array(
'performancedata' => '' 'pluginstate' => 'LA',
), self::FORM_CLASS); 'checkoutput' => 'DING',
'performancedata' => ''
)
);
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
$this->assertFalse( $this->assertFalse(
$form->isSubmittedAndValid(), $form->isSubmittedAndValid(),
'Assert invalid (non-numeric) state to cause validation errors in passive service check' 'Assert invalid (non-numeric) state to cause validation errors in passive service check'

View File

@ -1,49 +0,0 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace {
if (!function_exists('t')) {
function t() {
return func_get_arg(0);
}
}
if (!function_exists('mt')) {
function mt() {
return func_get_arg(0);
}
}
}
namespace Test\Icinga\Web\Form {
require_once realpath('../../library/Icinga/Test/BaseTestCase.php');
use \Icinga\Test\BaseTestCase;
/**
* Base test to be extended for testing forms
*/
class BaseFormTest extends BaseTestCase
{
/**
* Temporary wrapper for BaseTestCase::createForm until this testcase is not used anymore
*/
public function getRequestForm(array $data, $formClass)
{
return $this->createForm($formClass, $data);
}
/**
* This is just a test to avoid warnings being submitted from the testrunner
*
*/
public function testForRemovingWarnings()
{
$this->assertTrue(true);
}
}
}