Implement isPostAndValid, fix CSRF in forms, update tests
Form provides isPostAndValid for validation, using the request for fetching POST data. The tests are now updated to use the CSRF protection and are cleaner now refs #4355
This commit is contained in:
parent
849eee2cf2
commit
ddfbf915ed
|
@ -26,6 +26,7 @@
|
|||
namespace Icinga\Web;
|
||||
|
||||
use Icinga\Exception\ProgrammingError;
|
||||
use Icinga\Web\Form\InvalidCSRFTokenException;
|
||||
use Zend_Form_Exception;
|
||||
use Zend_View_Interface;
|
||||
|
||||
|
@ -68,16 +69,55 @@ abstract class Form extends \Zend_Form
|
|||
*/
|
||||
private $created = false;
|
||||
|
||||
/**
|
||||
* Session id required for CSRF token generation
|
||||
* @var numeric|bool
|
||||
*/
|
||||
private $sessionId = false;
|
||||
|
||||
/**
|
||||
* Returns the session ID stored in this form instance
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSessionId()
|
||||
{
|
||||
if (!$this->sessionId) {
|
||||
$this->sessionId = session_id();
|
||||
}
|
||||
return $this->sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrites the currently set session id to a user
|
||||
* provided one, helpful when testing
|
||||
*
|
||||
* @param $sessionId The session id to use for CSRF generation
|
||||
*/
|
||||
public function setSessionId($sessionId)
|
||||
{
|
||||
$this->sessionId = $sessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Zend_Form::init
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
if (!$this->tokenDisabled) {
|
||||
$this->initCsrfToken();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the html-element name of the CSRF token
|
||||
* field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTokenElementName()
|
||||
{
|
||||
return $this->tokenElementName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Render the form to html
|
||||
* @param Zend_View_Interface $view
|
||||
|
@ -86,7 +126,6 @@ abstract class Form extends \Zend_Form
|
|||
public function render(Zend_View_Interface $view = null)
|
||||
{
|
||||
// Elements must be there to render the form
|
||||
$this->buildForm();
|
||||
return parent::render($view);
|
||||
}
|
||||
|
||||
|
@ -126,6 +165,7 @@ abstract class Form extends \Zend_Form
|
|||
public function buildForm()
|
||||
{
|
||||
if ($this->created === false) {
|
||||
$this->initCsrfToken();
|
||||
$this->create();
|
||||
|
||||
// Empty action if not safe
|
||||
|
@ -137,17 +177,6 @@ abstract class Form extends \Zend_Form
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden to assert form creation
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($data)
|
||||
{
|
||||
$this->buildForm();
|
||||
$this->preValidation($data);
|
||||
return parent::isValid($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -163,24 +192,23 @@ abstract class Form extends \Zend_Form
|
|||
}
|
||||
|
||||
$checkData = $this->getRequest()->getParams();
|
||||
|
||||
$this->buildForm();
|
||||
$this->assertValidCsrfToken($checkData);
|
||||
$this->preValidation($checkData);
|
||||
return parent::isValid($checkData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable CSRF counter measure
|
||||
*/
|
||||
final public function enableCsrfToken()
|
||||
{
|
||||
$this->tokenDisabled = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Disable CSRF counter measure and remove its field if already added
|
||||
*/
|
||||
final public function disableCsrfToken()
|
||||
final public function setTokenDisabled($value)
|
||||
{
|
||||
$this->tokenDisabled = true;
|
||||
$this->removeElement($this->tokenElementName);
|
||||
$this->tokenDisabled = $value;
|
||||
if ($value == true)
|
||||
$this->removeElement($this->tokenElementName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -191,62 +219,89 @@ abstract class Form extends \Zend_Form
|
|||
if ($this->tokenDisabled || $this->getElement($this->tokenElementName)) {
|
||||
return;
|
||||
}
|
||||
list($seed, $token) = $this->generateCsrfToken($this->tokenTimeout);
|
||||
|
||||
$this->addElement(
|
||||
'hidden',
|
||||
$this->tokenElementName,
|
||||
array(
|
||||
'value' => sprintf('%s\|/%s', $seed, $token),
|
||||
'value' => $this->generateCsrfTokenAsString(),
|
||||
'decorators' => array('ViewHelper')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
final public function assertValidCsrfToken(array $checkData)
|
||||
{
|
||||
if ($this->tokenDisabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($checkData[$this->tokenElementName]) || !$this->hasValidCsrfToken($checkData[$this->tokenElementName])) {
|
||||
throw new InvalidCSRFTokenException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the form's CSRF token-field has a valid value
|
||||
*
|
||||
* @param int $maxAge Max allowed token age
|
||||
* @param string $sessionId A specific session id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final private function hasValidCsrfToken($maxAge, $sessionId = null)
|
||||
final private function hasValidCsrfToken($checkData)
|
||||
{
|
||||
if ($this->tokenDisabled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->getElement($this->tokenElementName) === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$elementValue = $this->getElement($this->tokenElementName)->getValue();
|
||||
list($seed, $token) = explode($elementValue, '\|/');
|
||||
$elementValue = $checkData;
|
||||
if (strpos($elementValue, '|') === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
list($seed, $token) = explode('|', $elementValue);
|
||||
|
||||
if (!is_numeric($seed)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$seed -= intval(time() / $maxAge) * $maxAge;
|
||||
$sessionId = $sessionId ? $sessionId : session_id();
|
||||
return $token === hash('sha256', $sessionId . $seed);
|
||||
$seed -= intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
|
||||
|
||||
return $token === hash('sha256', $this->getSessionId() . $seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new (seed, token) pair
|
||||
*
|
||||
* @param int $maxAge Max allowed token age
|
||||
* @param string $sessionId A specific session id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
final private function generateCsrfToken($maxAge, $sessionId = null)
|
||||
final public function generateCsrfToken()
|
||||
{
|
||||
$sessionId = $sessionId ? $sessionId : session_id();
|
||||
$seed = mt_rand();
|
||||
$hash = hash('sha256', $sessionId . $seed);
|
||||
$seed += intval(time() / $maxAge) * $maxAge;
|
||||
$hash = hash('sha256', $this->getSessionId() . $seed);
|
||||
$seed += intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
|
||||
|
||||
return array($seed, $hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation of the CSRF seed/token pair
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
final public function generateCsrfTokenAsString()
|
||||
{
|
||||
list ($seed, $token) = $this->generateCsrfToken($this->getSessionId());
|
||||
return sprintf('%s|%s', $seed, $token);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Web\Form;
|
||||
|
||||
|
||||
class InvalidCSRFTokenException extends \Exception {
|
||||
|
||||
}
|
|
@ -136,7 +136,8 @@ class AcknowledgeForm extends ConfirmationForm
|
|||
*/
|
||||
protected function preValidation(array $data)
|
||||
{
|
||||
if (isset($data['expire']) && $data['expire'] === '1') {
|
||||
|
||||
if (isset($data['expire']) && intval($data['expire']) === 1 ) {
|
||||
$expireTime = $this->getElement('expiretime');
|
||||
$expireTime->setRequired(true);
|
||||
$expireTime->addValidator($this->createDateTimeValidator(), true);
|
||||
|
|
|
@ -1,126 +1,116 @@
|
|||
<?php
|
||||
|
||||
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\Monitoring\Forms\Command {
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
require_once 'Zend/Form/Element/Checkbox.php';
|
||||
require_once 'Zend/Validate/Date.php';
|
||||
require_once __DIR__.'/BaseFormTest.php';
|
||||
$base = __DIR__.'/../../../../../../../';
|
||||
require_once $base.'modules/monitoring/application/forms/Command/ConfirmationForm.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 __DIR__. '/../../../../../../../library/Icinga/Exception/ProgrammingError.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/DateTime.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/AcknowledgeForm.php';
|
||||
use Monitoring\Form\Command\AcknowledgeForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
use Monitoring\Form\Command\AcknowledgeForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
class AcknowledgeFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class AcknowledgeFormTest extends BaseFormTest
|
||||
{
|
||||
const FORMCLASS = "Monitoring\Form\Command\AcknowledgeForm";
|
||||
public function testForm()
|
||||
{
|
||||
public function testForm()
|
||||
{
|
||||
$form = new AcknowledgeForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertCount(11, $form->getElements());
|
||||
}
|
||||
$this->assertCount(11, $form->getElements());
|
||||
}
|
||||
|
||||
public function testValidation1()
|
||||
{
|
||||
$form = new AcknowledgeForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test comment',
|
||||
'persistent' => '0',
|
||||
'expire' => '0',
|
||||
'expiretime' => '',
|
||||
'sticky' => '0',
|
||||
'notify' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'test1',
|
||||
'comment' => '',
|
||||
'persistent' => '0',
|
||||
'expire' => '0',
|
||||
'expiretime' => '',
|
||||
'sticky' => '0',
|
||||
'notify' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testValidateCorrectForm()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test comment',
|
||||
'persistent' => '0',
|
||||
'expire' => '0',
|
||||
'expiretime' => '',
|
||||
'sticky' => '0',
|
||||
'notify' => '0'
|
||||
), self::FORMCLASS);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test comment',
|
||||
'persistent' => '0',
|
||||
'expire' => '1',
|
||||
'expiretime' => '',
|
||||
'sticky' => '0',
|
||||
'notify' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting a correct form to be validated correctly"
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test comment',
|
||||
'persistent' => '0',
|
||||
'expire' => '1',
|
||||
'expiretime' => 'NOT A DATE',
|
||||
'sticky' => '0',
|
||||
'notify' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testDetectMissingAcknowledgementComment()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'test1',
|
||||
'comment' => '',
|
||||
'persistent' => '0',
|
||||
'expire' => '0',
|
||||
'expiretime' => '',
|
||||
'sticky' => '0',
|
||||
'notify' => '0',
|
||||
), self::FORMCLASS);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting a missing comment text to cause validation errors"
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test comment',
|
||||
'persistent' => '0',
|
||||
'expire' => '1',
|
||||
'expiretime' => '2013-07-10 17:32:16',
|
||||
'sticky' => '0',
|
||||
'notify' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
public function testValidateMissingExpireTime()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test comment',
|
||||
'persistent' => '0',
|
||||
'expire' => '1',
|
||||
'expiretime' => '',
|
||||
'sticky' => '0',
|
||||
'notify' => '0'
|
||||
), self::FORMCLASS);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"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'
|
||||
), self::FORMCLASS);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"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'
|
||||
), self::FORMCLASS);
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
"Assert that correct expire time acknowledgement is considered valid"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
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\Monitoring\Forms\Command {
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
require_once 'Zend/Form/Element/Checkbox.php';
|
||||
require_once 'Zend/Form/Element/Hidden.php';
|
||||
require_once 'Zend/Validate/Date.php';
|
||||
$base = __DIR__.'/../../../../../../../';
|
||||
require_once realpath($base.'library/Icinga/Exception/ProgrammingError.php');
|
||||
require_once realpath($base.'library/Icinga/Web/Form.php');
|
||||
require_once realpath($base.'library/Icinga/Web/Form/InvalidCSRFTokenException.php');
|
||||
require_once realpath($base.'library/Icinga/Web/Form/Element/Note.php');
|
||||
require_once realpath($base.'library/Icinga/Web/Form/Element/DateTime.php');
|
||||
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
|
||||
class BaseFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
{
|
||||
|
||||
public function getRequestForm(array $data, $formClass)
|
||||
{
|
||||
$form = new $formClass();
|
||||
$form->setSessionId("test");
|
||||
$form->initCsrfToken();
|
||||
$request = $this->getRequest();
|
||||
$data[$form->getTokenElementName()] = $form->getValue($form->getTokenElementName());
|
||||
|
||||
$request->setMethod("POST")->setPost($data);
|
||||
$form->setRequest($request);
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,86 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace {
|
||||
if (!function_exists('t')) {
|
||||
function t() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
if (!function_exists('mt')) {
|
||||
function mt() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Test\Monitoring\Forms\Command {
|
||||
require_once __DIR__.'/BaseFormTest.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/CommentForm.php';
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
require_once 'Zend/Form/Element/Checkbox.php';
|
||||
require_once 'Zend/Validate/Date.php';
|
||||
use Monitoring\Form\Command\CommentForm;
|
||||
use \Zend_View;
|
||||
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Exception/ProgrammingError.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/DateTime.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.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 \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
class CommentFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class CommentFormTest extends BaseFormTest
|
||||
{
|
||||
const FORMCLASS = "Monitoring\Form\Command\CommentForm";
|
||||
public function testForm()
|
||||
{
|
||||
public function testForm()
|
||||
{
|
||||
$form = new CommentForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
$form = new CommentForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertCount(6, $form->getElements());
|
||||
}
|
||||
$this->assertCount(6, $form->getElements());
|
||||
}
|
||||
|
||||
public function testValidation()
|
||||
{
|
||||
$form = new CommentForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test2',
|
||||
'sticky' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testCorrectCommentValidation()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'test1',
|
||||
'comment' => 'test2',
|
||||
'sticky' => '0'
|
||||
), self::FORMCLASS);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'test1',
|
||||
'comment' => '',
|
||||
'sticky' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting correct comment form to be considered valid"
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => '',
|
||||
'comment' => 'test2',
|
||||
'sticky' => '0'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
public function testRecognizeMissingCommentText()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'test1',
|
||||
'comment' => '',
|
||||
'sticky' => '0'
|
||||
), self::FORMCLASS);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting missing comment text in comment form to cause validation errors"
|
||||
);
|
||||
}
|
||||
|
||||
public function testRecognizeMissingCommentAuthor()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => '',
|
||||
'comment' => 'test2',
|
||||
'sticky' => '0'
|
||||
), self::FORMCLASS);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting missing comment author to cause validation errors"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
<?php
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
|
||||
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';
|
||||
|
@ -16,9 +11,9 @@ use \Zend_View;
|
|||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
use Monitoring\Form\Command\ConfirmationForm;
|
||||
|
||||
class ConfirmationFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class ConfirmationFormTest extends BaseFormTest
|
||||
{
|
||||
public function testForm1()
|
||||
public function testFormCreation()
|
||||
{
|
||||
$view = new Zend_View();
|
||||
$form = new ConfirmationForm();
|
||||
|
@ -31,7 +26,7 @@ class ConfirmationFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
|||
|
||||
$form->addNote('444 NOTE 1');
|
||||
$form->addNote('555 NOTE 2');
|
||||
|
||||
$form->buildForm();
|
||||
$content = $form->render($view);
|
||||
|
||||
$this->assertContains('<input type="submit" name="submit" id="submit" value="111TEST_SUBMIT" class="btn btn-primary pull-right">', $content);
|
||||
|
@ -42,7 +37,7 @@ class ConfirmationFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
|||
$this->assertContains('555 NOTE 2</dd>', $content);
|
||||
}
|
||||
|
||||
public function testNotes1()
|
||||
public function testFormNotes()
|
||||
{
|
||||
$form = new ConfirmationForm();
|
||||
$form->addNote('test1');
|
||||
|
|
|
@ -1,102 +1,79 @@
|
|||
<?php
|
||||
|
||||
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\Monitoring\Forms\Command;
|
||||
|
||||
namespace Test\Monitoring\Forms\Command {
|
||||
require_once __DIR__. '/BaseFormTest.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationWithIdentifierForm.php';
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
require_once 'Zend/Form/Element/Checkbox.php';
|
||||
require_once 'Zend/Validate/Date.php';
|
||||
use Monitoring\Form\Command\ConfirmationWithIdentifierForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Exception/ProgrammingError.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/DateTime.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationWithIdentifierForm.php';
|
||||
|
||||
use Monitoring\Form\Command\ConfirmationWithIdentifierForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
class ConfirmationWithIdentifierFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class ConfirmationWithIdentifierFormTest extends BaseFormTest
|
||||
{
|
||||
const FORMCLASS = "Monitoring\Form\Command\ConfirmationWithIdentifierForm";
|
||||
public function testForm()
|
||||
{
|
||||
public function testForm()
|
||||
{
|
||||
$form = new ConfirmationWithIdentifierForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setSubmitLabel('DING DING');
|
||||
$form->buildForm();
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
$form->setSubmitLabel('DING DING');
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertCount(4, $form->getElements());
|
||||
}
|
||||
$this->assertCount(4, $form->getElements());
|
||||
}
|
||||
|
||||
public function testValidation()
|
||||
{
|
||||
$form = new ConfirmationWithIdentifierForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setFieldLabel('Test1');
|
||||
$form->setFieldName('testval');
|
||||
$form->setSubmitLabel('DING DING');
|
||||
public function testCorrectFormValidation()
|
||||
{
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
array(
|
||||
'testval' => 123
|
||||
)
|
||||
)
|
||||
);
|
||||
$form = $this->getRequestForm(array(
|
||||
'testval' => 123
|
||||
), self::FORMCLASS);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'testval' => ''
|
||||
)
|
||||
)
|
||||
);
|
||||
$form->setFieldLabel('Test1');
|
||||
$form->setFieldName('testval');
|
||||
$form->setSubmitLabel('DING DING');
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'testval' => 'NaN'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting correct confirmation with id to be valid"
|
||||
);
|
||||
}
|
||||
|
||||
public function testRequestBridge()
|
||||
{
|
||||
$this->getRequest()->setMethod('POST');
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
'objectid' => 123123666
|
||||
)
|
||||
);
|
||||
public function testInvalidValueValidationErrors()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'testval' => ''
|
||||
), self::FORMCLASS);
|
||||
|
||||
$form = new ConfirmationWithIdentifierForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting an invalid (empty) value to cause validation errors"
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertTrue($form->isPostAndValid());
|
||||
public function testNonNumericValueValidationErrors()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'testval' => 'NaN'
|
||||
), self::FORMCLASS);
|
||||
|
||||
$this->assertEquals('123123666', $form->getElement('objectid')->getValue());
|
||||
}
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting an non numeric value to cause validation errors"
|
||||
);
|
||||
}
|
||||
|
||||
public function testRequestBridge()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'objectid' => 123123666
|
||||
), self::FORMCLASS);
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertTrue($form->isPostAndValid());
|
||||
|
||||
$this->assertEquals('123123666', $form->getElement('objectid')->getValue());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,55 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace {
|
||||
if (!function_exists('t')) {
|
||||
function t() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
if (!function_exists('mt')) {
|
||||
function mt() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Test\Monitoring\Forms\Command {
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.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/CustomNotificationForm.php';
|
||||
require_once __DIR__. '/BaseFormTest.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/CustomNotificationForm.php';
|
||||
|
||||
|
||||
use Monitoring\Form\Command\CustomNotificationForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
use Monitoring\Form\Command\CustomNotificationForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
class CustomNotificationFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class CustomNotificationFormTest extends BaseFormTest
|
||||
{
|
||||
public function testForm1()
|
||||
{
|
||||
public function testForm1()
|
||||
{
|
||||
$this->getRequest()->setMethod('POST');
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
'comment' => 'TEST COMMENT',
|
||||
'author' => 'LAOLA'
|
||||
)
|
||||
);
|
||||
$form = $this->getRequestForm(array(
|
||||
'comment' => 'TEST COMMENT',
|
||||
'author' => 'LAOLA'
|
||||
), "Monitoring\Form\Command\CustomNotificationForm");
|
||||
$form->buildForm();
|
||||
|
||||
$form = new CustomNotificationForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertCount(7, $form->getElements());
|
||||
$this->assertTrue($form->isPostAndValid());
|
||||
}
|
||||
$this->assertCount(7, $form->getElements());
|
||||
$this->assertTrue($form->isPostAndValid());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,84 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace {
|
||||
if (!function_exists('t')) {
|
||||
function t() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
if (!function_exists('mt')) {
|
||||
function mt() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Test\Monitoring\Forms\Command {
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.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/DelayNotificationForm.php';
|
||||
require_once __DIR__. '/BaseFormTest.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/DelayNotificationForm.php';
|
||||
|
||||
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
use Monitoring\Form\Command\DelayNotificationForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
use Monitoring\Form\Command\DelayNotificationForm;
|
||||
|
||||
class DelayNotificationFormFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class DelayNotificationFormFormTest extends BaseFormTest
|
||||
{
|
||||
public function testValidForm()
|
||||
{
|
||||
public function testForm1()
|
||||
{
|
||||
$this->getRequest()->setMethod('POST');
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
'minutes' => 12
|
||||
)
|
||||
);
|
||||
$form = $this->getRequestForm(array(
|
||||
'minutes' => 12
|
||||
), 'Monitoring\Form\Command\DelayNotificationForm');
|
||||
|
||||
$form = new DelayNotificationForm();
|
||||
$form->buildForm();
|
||||
$this->assertCount(5, $form->getElements());
|
||||
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
$element = $form->getElement('minutes');
|
||||
$this->assertInstanceOf('Zend_Form_Element_Text', $element);
|
||||
$this->assertEquals('0', $element->getValue(), "Assert a correct default value in minutes");
|
||||
$this->assertTrue($element->isRequired(), "Assert minutes to be declared as required");
|
||||
|
||||
$this->assertCount(5, $form->getElements());
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
"Assert a correct DelayNotificationForm to be considered valid"
|
||||
);
|
||||
|
||||
$element = $form->getElement('minutes');
|
||||
$this->assertInstanceOf('Zend_Form_Element_Text', $element);
|
||||
$this->assertEquals('0', $element->getValue());
|
||||
$this->assertTrue($element->isRequired());
|
||||
|
||||
$this->assertTrue($form->isPostAndValid());
|
||||
|
||||
$this->assertEquals('12', $form->getValue('minutes'));
|
||||
}
|
||||
|
||||
public function testValidation()
|
||||
{
|
||||
$this->getRequest()->setMethod('POST');
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
'minutes' => 'SCHAHH-LAHH-LAHH'
|
||||
)
|
||||
);
|
||||
|
||||
$form = new DelayNotificationForm();
|
||||
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertFalse($form->isPostAndValid());
|
||||
|
||||
$errors = $form->getErrors('minutes');
|
||||
$this->assertEquals('notBetween', $errors[0]);
|
||||
}
|
||||
$this->assertEquals('12', $form->getValue('minutes'), "Assert the minutes field to be correctly populated");
|
||||
}
|
||||
|
||||
public function testInvalidMinuteValue()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'minutes' => 'SCHAHH-LAHH-LAHH'
|
||||
), 'Monitoring\Form\Command\DelayNotificationForm');
|
||||
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Asserting invalid minutes (NaN) to cause validation errors"
|
||||
);
|
||||
|
||||
$errors = $form->getErrors('minutes');
|
||||
$this->assertEquals('notBetween', $errors[0], "Assert correct error message");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,120 +1,101 @@
|
|||
<?php
|
||||
|
||||
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\Monitoring\Forms\Command {
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
require_once 'Zend/Form/Element/Checkbox.php';
|
||||
require_once 'Zend/Validate/Date.php';
|
||||
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/DateTime.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/RescheduleNextCheckForm.php';
|
||||
|
||||
|
||||
use Monitoring\Form\Command\RescheduleNextCheckForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
class RescheduleNextCheckFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
|
||||
require_once __DIR__. '/BaseFormTest.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/RescheduleNextCheckForm.php';
|
||||
|
||||
|
||||
use Monitoring\Form\Command\RescheduleNextCheckForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
class RescheduleNextCheckFormTest extends BaseFormTest
|
||||
{
|
||||
|
||||
const FORMCLASS = 'Monitoring\Form\Command\RescheduleNextCheckForm';
|
||||
|
||||
public function testValidRescheduleSubmissions()
|
||||
{
|
||||
public function testForm1()
|
||||
{
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
|
||||
)
|
||||
);
|
||||
$form = $this->getRequestForm(array(
|
||||
'checktime' => '2013-10-19 17:30:00',
|
||||
'forcecheck' => 1
|
||||
), self::FORMCLASS);
|
||||
$form->buildForm();
|
||||
|
||||
$form = new RescheduleNextCheckForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
$this->assertCount(6, $form->getElements());
|
||||
|
||||
$this->assertCount(6, $form->getElements());
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
'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
|
||||
), self::FORMCLASS);
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
array(
|
||||
'checktime' => '2013-10-19 17:30:00',
|
||||
'forcecheck' => 1
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
'Asserting a reschedule form with correct time and forecheck=0 to be valid'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
array(
|
||||
'checktime' => '2013-10-19 17:30:00',
|
||||
'forcecheck' => 0
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testInValidRescheduleChecktimeSubmissions()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'checktime' => '2013-24-12 17:30:00',
|
||||
'forcecheck' => 1
|
||||
), self::FORMCLASS);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'checktime' => '2013-24-12 17:30:00',
|
||||
'forcecheck' => 1
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Asserting an logically invalid checktime to be considered as invalid reschedule data'
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'checktime' => 'AHAHA',
|
||||
'forcecheck' => 1
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
$form = $this->getRequestForm(array(
|
||||
'checktime' => 'AHAHA',
|
||||
'forcecheck' => 1
|
||||
), self::FORMCLASS);
|
||||
|
||||
public function testChildrenFlag()
|
||||
{
|
||||
|
||||
$form = new RescheduleNextCheckForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren(true);
|
||||
$form->buildForm();
|
||||
$notes1 = $form->getNotes();
|
||||
$form = null;
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Asserting an invalid non-numeric checktime to be considered as invalid reschedule data'
|
||||
);
|
||||
}
|
||||
|
||||
$form = new RescheduleNextCheckForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren(false);
|
||||
$form->buildForm();
|
||||
$notes2 = $form->getNotes();
|
||||
$form = null;
|
||||
public function testChildrenFlag()
|
||||
{
|
||||
|
||||
$form = new RescheduleNextCheckForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren();
|
||||
$form->buildForm();
|
||||
$notes3 = $form->getNotes();
|
||||
$form = null;
|
||||
$form = new RescheduleNextCheckForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren(true);
|
||||
$form->buildForm();
|
||||
$notes1 = $form->getNotes();
|
||||
$form = null;
|
||||
|
||||
$this->assertEquals($notes1, $notes3);
|
||||
$this->assertNotEquals($notes1, $notes2);
|
||||
}
|
||||
$form = new RescheduleNextCheckForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren(false);
|
||||
$form->buildForm();
|
||||
$notes2 = $form->getNotes();
|
||||
$form = null;
|
||||
|
||||
$form = new RescheduleNextCheckForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren();
|
||||
$form->buildForm();
|
||||
$notes3 = $form->getNotes();
|
||||
$form = null;
|
||||
|
||||
$this->assertEquals($notes1, $notes3);
|
||||
$this->assertNotEquals($notes1, $notes2);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,317 +1,318 @@
|
|||
<?php
|
||||
|
||||
namespace {
|
||||
if (!function_exists('t')) {
|
||||
function t() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
if (!function_exists('mt')) {
|
||||
function mt() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
require_once __DIR__. '/BaseFormTest.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ScheduleDowntimeForm.php';
|
||||
|
||||
namespace Test\Monitoring\Forms\Command {
|
||||
use Monitoring\Form\Command\ScheduleDowntimeForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
require_once 'Zend/Form/Element/Checkbox.php';
|
||||
require_once 'Zend/Validate/Date.php';
|
||||
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/DateTime.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ScheduleDowntimeForm.php';
|
||||
|
||||
use Monitoring\Form\Command\ScheduleDowntimeForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
class ScheduleDowntimeFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class ScheduleDowntimeFormTest extends BaseFormTest
|
||||
{
|
||||
const FORMCLASS = 'Monitoring\Form\Command\ScheduleDowntimeForm';
|
||||
public function testCorrectFormElementCreation()
|
||||
{
|
||||
public function testFormElements1()
|
||||
{
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
$form->buildForm();
|
||||
|
||||
)
|
||||
);
|
||||
$this->assertCount(13, $form->getElements());
|
||||
|
||||
$form = new ScheduleDowntimeForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertCount(13, $form->getElements());
|
||||
|
||||
$form = new ScheduleDowntimeForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren(true);
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertCount(12, $form->getElements());
|
||||
}
|
||||
$this->assertCount(12, $form->getElements());
|
||||
}
|
||||
|
||||
|
||||
public function testFormValidation1()
|
||||
{
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
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' => '',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
|
||||
)
|
||||
);
|
||||
|
||||
$form = new ScheduleDowntimeForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setWithChildren(true);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
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' => '',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
'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',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
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' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
'Asserting a correct flexible downtime form to be considered valid'
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
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',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'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' => '',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testMissingFlexibleDurationRecognition()
|
||||
{
|
||||
$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' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'OK',
|
||||
'comment' => '',
|
||||
'triggered' => '4',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 10:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert missing hours and minutes in downtime form to cause failing validation'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => 'HAHA',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 10:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testMissingAuthorRecognition()
|
||||
{
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17',
|
||||
'endtime' => '2013-07-17 10:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
$form = $this->getRequestForm(array(
|
||||
'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' => '',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => 'DING',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 09:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
|
||||
'hours' => '-1',
|
||||
'minutes' => '12',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert missing author to cause validation errors in fixed downtime'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 09:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
|
||||
'hours' => '12',
|
||||
'minutes' => 'DING',
|
||||
// 'childobjects' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testMissingCommentRecognition()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'OK',
|
||||
'comment' => '',
|
||||
'triggered' => '4',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 10:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
}
|
||||
|
||||
public function testFormValidation2()
|
||||
{
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert missing comment to cause validation errors in fixed downtime'
|
||||
);
|
||||
}
|
||||
|
||||
)
|
||||
);
|
||||
public function testInvalidTriggeredFieldValueRecognition()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => 'HAHA',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 10:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$form = new ScheduleDowntimeForm();
|
||||
$form->setWithChildren(false);
|
||||
$form->setRequest($this->getRequest());
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert invalid trigger field to cause validation to fail'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
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' => '',
|
||||
'childobjects' => '0',
|
||||
)
|
||||
)
|
||||
);
|
||||
public function testInvalidStartTimeRecognition()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17',
|
||||
'endtime' => '2013-07-17 10:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
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' => '',
|
||||
'childobjects' => 'AHA',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert incorrect start time to cause validation errors in fixed downtime'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
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' => '',
|
||||
'childobjects' => '4',
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
public function testInvalidEndTimeRecognition()
|
||||
{
|
||||
|
||||
public function testTimeRange()
|
||||
{
|
||||
$this->getRequest()->setPost(
|
||||
array(
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => 'DING',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FIXED,
|
||||
'hours' => '',
|
||||
'minutes' => '',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
)
|
||||
);
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert invalid endtime to cause validation errors in fixed downtime'
|
||||
);
|
||||
}
|
||||
|
||||
$form = new ScheduleDowntimeForm();
|
||||
$form->setWithChildren(false);
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
|
||||
$time1 = strtotime($form->getElement('starttime')->getValue());
|
||||
$time2 = strtotime($form->getElement('endtime')->getValue());
|
||||
public function testInvalidHoursValueRecognitionInFlexibleDowntime()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 09:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
|
||||
'hours' => '-1',
|
||||
'minutes' => '12',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$this->assertEquals(3600, ($time2 - $time1));
|
||||
}
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert negative hours to cause validation errors in flexible downtime'
|
||||
);
|
||||
}
|
||||
|
||||
public function testInvalidMinutesValueRecognitionInFlexibleDowntime()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'author' => 'OK',
|
||||
'comment' => 'OK',
|
||||
'triggered' => '123',
|
||||
'starttime' => '2013-07-17 10:30:00',
|
||||
'endtime' => '2013-07-17 09:30:00',
|
||||
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
|
||||
'hours' => '12',
|
||||
'minutes' => 'DING',
|
||||
// 'childobjects' => '',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(true);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
'Assert non numeric valud to cause validation errors in flexible downtime '
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
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' => '',
|
||||
'childobjects' => '0',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(false);
|
||||
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
"Assert a correct schedule downtime without children form to be considered valid"
|
||||
);
|
||||
}
|
||||
|
||||
public function testIncorrectChildObjectsRecognition() {
|
||||
$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' => '',
|
||||
'childobjects' => 'AHA',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(false);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Assert and incorrect (non-numeric) childobjects value to cause validation errors"
|
||||
);
|
||||
|
||||
$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' => '',
|
||||
'childobjects' => '4',
|
||||
), self::FORMCLASS);
|
||||
$form->setWithChildren(false);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Assert and incorrect (numeric) childobjects value to cause validation errors"
|
||||
);
|
||||
}
|
||||
|
||||
public function testTimeRange()
|
||||
{
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
$form->buildForm();
|
||||
|
||||
$time1 = strtotime($form->getElement('starttime')->getValue());
|
||||
$time2 = strtotime($form->getElement('endtime')->getValue());
|
||||
|
||||
$this->assertEquals(3600, ($time2 - $time1));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,120 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace {
|
||||
if (!function_exists('t')) {
|
||||
function t() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
if (!function_exists('mt')) {
|
||||
function mt() {
|
||||
return func_get_arg(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
require_once __DIR__. '/BaseFormTest.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/SubmitPassiveCheckResultForm.php';
|
||||
|
||||
namespace Test\Monitoring\Forms\Command {
|
||||
|
||||
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||
require_once 'Zend/Form.php';
|
||||
require_once 'Zend/View.php';
|
||||
require_once 'Zend/Form/Element/Submit.php';
|
||||
require_once 'Zend/Form/Element/Reset.php';
|
||||
require_once 'Zend/Form/Element/Checkbox.php';
|
||||
require_once 'Zend/Validate/Date.php';
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
use Monitoring\Form\Command\SubmitPassiveCheckResultForm;
|
||||
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Exception/ProgrammingError.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php';
|
||||
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/DateTime.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/ConfirmationForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
|
||||
require_once __DIR__. '/../../../../../application/forms/Command/SubmitPassiveCheckResultForm.php';
|
||||
|
||||
use Monitoring\Form\Command\SubmitPassiveCheckResultForm;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
|
||||
class SubmitPassiveCheckResultFormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||
class SubmitPassiveCheckResultFormTest extends BaseFormTest
|
||||
{
|
||||
const FORMCLASS = "Monitoring\Form\Command\SubmitPassiveCheckResultForm";
|
||||
public function testStateTypes()
|
||||
{
|
||||
public function testStateTypes()
|
||||
{
|
||||
$form = new SubmitPassiveCheckResultForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
$options = $form->getOptions();
|
||||
$this->assertCount(4, $options);
|
||||
$this->assertEquals('OK', $options[0]);
|
||||
$this->assertEquals('WARNING', $options[1]);
|
||||
$this->assertEquals('CRITICAL', $options[2]);
|
||||
$this->assertEquals('UNKNOWN', $options[3]);
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
$options = $form->getOptions();
|
||||
$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('WARNING', $options[1], "Assert WARNING state to be available in service passive check form");
|
||||
$this->assertEquals('CRITICAL', $options[2], "Assert CRITICAL state to be available in service passive check form");
|
||||
$this->assertEquals('UNKNOWN', $options[3], "Assert UNKNOWN state to be available in service passive check form");
|
||||
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_HOST);
|
||||
$options = $form->getOptions();
|
||||
$this->assertCount(3, $options);
|
||||
$this->assertEquals('UP', $options[0]);
|
||||
$this->assertEquals('DOWN', $options[1]);
|
||||
$this->assertEquals('UNREACHABLE', $options[2]);
|
||||
}
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_HOST);
|
||||
$options = $form->getOptions();
|
||||
$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('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");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Icinga\Exception\ProgrammingError
|
||||
* @expectedExceptionMessage Type is not valid
|
||||
*/
|
||||
public function testForm1()
|
||||
{
|
||||
$form = new SubmitPassiveCheckResultForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->buildForm();
|
||||
}
|
||||
/**
|
||||
* @expectedException Icinga\Exception\ProgrammingError
|
||||
* @expectedExceptionMessage Type is not valid
|
||||
*/
|
||||
public function testMissingTypeThrowingException()
|
||||
{
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
$form->buildForm();
|
||||
}
|
||||
|
||||
public function testForm2()
|
||||
{
|
||||
$form = new SubmitPassiveCheckResultForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
$form->buildForm();
|
||||
public function testCorrectFormCreation()
|
||||
{
|
||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
$form->buildForm();
|
||||
|
||||
$this->assertCount(6, $form->getElements());
|
||||
}
|
||||
$this->assertCount(6, $form->getElements(), "Assert correct number of elements in form");
|
||||
}
|
||||
|
||||
public function testValidation1()
|
||||
{
|
||||
$form = new SubmitPassiveCheckResultForm();
|
||||
$form->setRequest($this->getRequest());
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
public function testCorrectServicePassiveCheckSubmission()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'pluginstate' => 0,
|
||||
'checkoutput' => 'DING',
|
||||
'performancedata' => ''
|
||||
), self::FORMCLASS);
|
||||
|
||||
$this->assertTrue(
|
||||
$form->isValid(
|
||||
array(
|
||||
'pluginstate' => 0,
|
||||
'checkoutput' => 'DING',
|
||||
'performancedata' => ''
|
||||
)
|
||||
)
|
||||
);
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'pluginstate' => 0,
|
||||
'checkoutput' => '',
|
||||
'performancedata' => ''
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertTrue(
|
||||
$form->isPostAndValid(),
|
||||
"Assert a correct passive service check form to pass form validation"
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isValid(
|
||||
array(
|
||||
'pluginstate' => 'LA',
|
||||
'checkoutput' => 'DING',
|
||||
'performancedata' => ''
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
public function testIncorrectCheckoutputRecognition()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'pluginstate' => 0,
|
||||
'checkoutput' => '',
|
||||
'performancedata' => ''
|
||||
), self::FORMCLASS);
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Assert empty checkoutput to cause validation errors in passive service check "
|
||||
);
|
||||
}
|
||||
|
||||
public function testIncorrectStateRecognition()
|
||||
{
|
||||
$form = $this->getRequestForm(array(
|
||||
'pluginstate' => 'LA',
|
||||
'checkoutput' => 'DING',
|
||||
'performancedata' => ''
|
||||
), self::FORMCLASS);
|
||||
$form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE);
|
||||
|
||||
$this->assertFalse(
|
||||
$form->isPostAndValid(),
|
||||
"Assert invalid (non-numeric) state to cause validation errors in passive service check"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue