mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-24 22:34:24 +02:00
Monitoring/Forms: Replace PHP DateTime usage with DateTimeFactory
refs #4440
This commit is contained in:
parent
2c217d1d06
commit
7dea7fcad0
@ -28,12 +28,11 @@
|
|||||||
|
|
||||||
namespace Monitoring\Form\Command;
|
namespace Monitoring\Form\Command;
|
||||||
|
|
||||||
use \DateTime;
|
|
||||||
use Icinga\Web\Form\Element\DateTimePicker;
|
use Icinga\Web\Form\Element\DateTimePicker;
|
||||||
use \DateInterval;
|
|
||||||
use Icinga\Web\Form\Element\Note;
|
use Icinga\Web\Form\Element\Note;
|
||||||
use Icinga\Protocol\Commandpipe\Acknowledgement;
|
use Icinga\Protocol\Commandpipe\Acknowledgement;
|
||||||
use Icinga\Protocol\Commandpipe\Comment;
|
use Icinga\Protocol\Commandpipe\Comment;
|
||||||
|
use Icinga\Util\DateTimeFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Form for problem acknowledgements
|
* Form for problem acknowledgements
|
||||||
@ -59,9 +58,9 @@ class AcknowledgeForm extends CommandForm
|
|||||||
'textarea',
|
'textarea',
|
||||||
'comment',
|
'comment',
|
||||||
array(
|
array(
|
||||||
'label' => t('Comment'),
|
'label' => t('Comment'),
|
||||||
'rows' => 4,
|
'rows' => 4,
|
||||||
'required' => true
|
'required' => true
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -85,20 +84,17 @@ class AcknowledgeForm extends CommandForm
|
|||||||
'checkbox',
|
'checkbox',
|
||||||
'expire',
|
'expire',
|
||||||
array(
|
array(
|
||||||
'label' => t('Use expire time')
|
'label' => t('Use expire time')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($this->getRequest()->getPost('expire', '0') === '1') {
|
if ($this->getRequest()->getPost('expire', '0') === '1') {
|
||||||
$now = new DateTime();
|
$now = DateTimeFactory::create();
|
||||||
$interval = new DateInterval('PT1H'); // Add 3600 seconds
|
|
||||||
$now->add($interval);
|
|
||||||
|
|
||||||
$expireTime = new DateTimePicker(
|
$expireTime = new DateTimePicker(
|
||||||
array(
|
array(
|
||||||
'name' => 'expiretime',
|
'name' => 'expiretime',
|
||||||
'label' => t('Expire time'),
|
'label' => t('Expire time'),
|
||||||
'value' => $now->format($this->getDateFormat())
|
'value' => $now->getTimestamp() + 3600
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -146,15 +142,15 @@ class AcknowledgeForm extends CommandForm
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add validator for dependent fields
|
* Add validator for dependent fields
|
||||||
* @see Form::preValidation
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
|
* @see \Icinga\Web\Form::preValidation()
|
||||||
*/
|
*/
|
||||||
protected function preValidation(array $data)
|
protected function preValidation(array $data)
|
||||||
{
|
{
|
||||||
if (isset($data['expire']) && intval($data['expire']) === 1) {
|
if (isset($data['expire']) && intval($data['expire']) === 1) {
|
||||||
$expireTime = $this->getElement('expiretime');
|
$expireTime = $this->getElement('expiretime');
|
||||||
$expireTime->setRequired(true);
|
$expireTime->setRequired(true);
|
||||||
$expireTime->addValidator($this->createDateTimeValidator(), true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,8 +158,7 @@ class AcknowledgeForm extends CommandForm
|
|||||||
{
|
{
|
||||||
$expireTime = -1;
|
$expireTime = -1;
|
||||||
if ($this->getValue('expire')) {
|
if ($this->getValue('expire')) {
|
||||||
$time = new DateTime($this->getValue('expiretime'));
|
$expireTime = $this->getValue('expiretime');
|
||||||
$expireTime = $time->getTimestamp();
|
|
||||||
}
|
}
|
||||||
return new Acknowledgement(
|
return new Acknowledgement(
|
||||||
new Comment(
|
new Comment(
|
||||||
|
@ -29,8 +29,9 @@
|
|||||||
namespace Monitoring\Form\Command;
|
namespace Monitoring\Form\Command;
|
||||||
|
|
||||||
use \DateTime;
|
use \DateTime;
|
||||||
use Icinga\Web\Form\Element\DateTimePicker;
|
use \Zend_Form_Element_Checkbox;
|
||||||
use Zend_Form_Element_Checkbox;
|
use \Icinga\Web\Form\Element\DateTimePicker;
|
||||||
|
use \Icinga\Util\DateTimeFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Form for RescheduleNextCheck
|
* Form for RescheduleNextCheck
|
||||||
@ -38,24 +39,18 @@ use Zend_Form_Element_Checkbox;
|
|||||||
class RescheduleNextCheckForm extends WithChildrenCommandForm
|
class RescheduleNextCheckForm extends WithChildrenCommandForm
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Interface method to build the form
|
* Create the form's elements
|
||||||
* @see CommandForm::create
|
|
||||||
*/
|
*/
|
||||||
protected function create()
|
protected function create()
|
||||||
{
|
{
|
||||||
|
$now = DateTimeFactory::create();
|
||||||
$now = new DateTime();
|
|
||||||
$dateElement = new DateTimePicker(
|
$dateElement = new DateTimePicker(
|
||||||
array(
|
array(
|
||||||
'name' => 'checktime',
|
'name' => 'checktime',
|
||||||
'label' => t('Check time'),
|
'label' => t('Check time'),
|
||||||
'value' => $now->format($this->getDateFormat())
|
'value' => $now->getTimestamp()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$dateElement->setRequired(true);
|
|
||||||
$dateElement->addValidator($this->createDateTimeValidator(), true);
|
|
||||||
|
|
||||||
$this->addElement($dateElement);
|
$this->addElement($dateElement);
|
||||||
|
|
||||||
$checkBox = new Zend_Form_Element_Checkbox(
|
$checkBox = new Zend_Form_Element_Checkbox(
|
||||||
|
@ -28,26 +28,19 @@
|
|||||||
|
|
||||||
namespace Monitoring\Form\Command;
|
namespace Monitoring\Form\Command;
|
||||||
|
|
||||||
use \DateTime;
|
|
||||||
use Icinga\Web\Form\Element\DateTimePicker;
|
|
||||||
use Icinga\Protocol\Commandpipe\Downtime;
|
|
||||||
use Icinga\Protocol\Commandpipe\Comment;
|
|
||||||
use \DateInterval;
|
|
||||||
use \Zend_Form_Element_Text;
|
use \Zend_Form_Element_Text;
|
||||||
use \Zend_Validate_GreaterThan;
|
use \Zend_Validate_GreaterThan;
|
||||||
use \Zend_Validate_Digits;
|
use \Zend_Validate_Digits;
|
||||||
|
use Icinga\Web\Form\Element\DateTimePicker;
|
||||||
|
use Icinga\Protocol\Commandpipe\Downtime;
|
||||||
|
use Icinga\Protocol\Commandpipe\Comment;
|
||||||
|
use Icinga\Util\DateTimeFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Form for any ScheduleDowntime command
|
* Form for any ScheduleDowntime command
|
||||||
*/
|
*/
|
||||||
class ScheduleDowntimeForm extends WithChildrenCommandForm
|
class ScheduleDowntimeForm extends WithChildrenCommandForm
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Default endtime interval definition
|
|
||||||
* @see http://php.net/manual/de/class.dateinterval.php
|
|
||||||
*/
|
|
||||||
const DEFAULT_ENDTIME_INTERVAL = 'PT1H';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type constant for fixed downtimes
|
* Type constant for fixed downtimes
|
||||||
*/
|
*/
|
||||||
@ -66,25 +59,6 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
|
|||||||
$this->setName('ScheduleDowntimeForm');
|
$this->setName('ScheduleDowntimeForm');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Build an array of timestamps
|
|
||||||
*
|
|
||||||
* @return string[]
|
|
||||||
*/
|
|
||||||
private function generateDefaultTimestamps()
|
|
||||||
{
|
|
||||||
$out = array();
|
|
||||||
|
|
||||||
$dateTimeObject = new DateTime();
|
|
||||||
$out[] = $dateTimeObject->format($this->getDateFormat());
|
|
||||||
|
|
||||||
$dateInterval = new DateInterval(self::DEFAULT_ENDTIME_INTERVAL);
|
|
||||||
$dateTimeObject->add($dateInterval);
|
|
||||||
$out[] = $dateTimeObject->format($this->getDateFormat());
|
|
||||||
|
|
||||||
return $out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate translated multi options based on type constants
|
* Generate translated multi options based on type constants
|
||||||
*
|
*
|
||||||
@ -144,28 +118,21 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
list($timestampStart, $timestampEnd) = $this->generateDefaultTimestamps();
|
$now = DateTimeFactory::create();
|
||||||
|
|
||||||
$dateTimeStart = new DateTimePicker(
|
$dateTimeStart = new DateTimePicker(
|
||||||
array(
|
array(
|
||||||
'name' => 'starttime',
|
'name' => 'starttime',
|
||||||
'label' => t('Start time'),
|
'label' => t('Start time'),
|
||||||
'value' => $timestampStart
|
'value' => $now->getTimestamp()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$dateTimeStart->setRequired(true);
|
|
||||||
$dateTimeStart->addValidator($this->createDateTimeValidator(), true);
|
|
||||||
|
|
||||||
$dateTimeEnd = new DateTimePicker(
|
$dateTimeEnd = new DateTimePicker(
|
||||||
array(
|
array(
|
||||||
'name' => 'endtime',
|
'name' => 'endtime',
|
||||||
'label' => t('End time'),
|
'label' => t('End time'),
|
||||||
'value' => $timestampEnd
|
'value' => $now->getTimestamp() + 3600
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$dateTimeEnd->setRequired(true);
|
|
||||||
$dateTimeEnd->addValidator($this->createDateTimeValidator(), true);
|
|
||||||
|
|
||||||
$this->addElements(array($dateTimeStart, $dateTimeEnd));
|
$this->addElements(array($dateTimeStart, $dateTimeEnd));
|
||||||
|
|
||||||
$this->addElement(
|
$this->addElement(
|
||||||
@ -304,12 +271,12 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
|
|||||||
if ($this->getValue('type') === self::TYPE_FLEXIBLE) {
|
if ($this->getValue('type') === self::TYPE_FLEXIBLE) {
|
||||||
$duration = ($this->getValue('hours')*3600) + ($this->getValue('minutes')*60);
|
$duration = ($this->getValue('hours')*3600) + ($this->getValue('minutes')*60);
|
||||||
}
|
}
|
||||||
$starttime = new DateTime($this->getValue('starttime'));
|
$starttime = $this->getValue('starttime');
|
||||||
$endtime = new DateTime($this->getValue('endtime'));
|
$endtime = $this->getValue('endtime');
|
||||||
|
|
||||||
$downtime = new Downtime(
|
$downtime = new Downtime(
|
||||||
$starttime->getTimestamp(),
|
$starttime,
|
||||||
$endtime->getTimestamp(),
|
$endtime,
|
||||||
$comment,
|
$comment,
|
||||||
$duration,
|
$duration,
|
||||||
$this->getValue('triggered')
|
$this->getValue('triggered')
|
||||||
|
@ -1,23 +1,38 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace Test\Monitoring\Forms\Command;
|
namespace Test\Monitoring\Forms\Command;
|
||||||
|
|
||||||
require_once __DIR__.'/BaseFormTest.php';
|
require_once __DIR__.'/BaseFormTest.php';
|
||||||
$base = __DIR__.'/../../../../../../../';
|
$base = __DIR__.'/../../../../../../../';
|
||||||
require_once $base.'modules/monitoring/application/forms/Command/CommandForm.php';
|
require_once $base.'modules/monitoring/application/forms/Command/CommandForm.php';
|
||||||
|
require_once $base . 'library/Icinga/Util/ConfigAwareFactory.php';
|
||||||
|
require_once $base . 'library/Icinga/Util/DateTimeFactory.php';
|
||||||
require_once realpath($base.'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php');
|
require_once realpath($base.'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php');
|
||||||
require_once realpath($base.'modules/monitoring/application/forms/Command/AcknowledgeForm.php');
|
require_once realpath($base.'modules/monitoring/application/forms/Command/AcknowledgeForm.php');
|
||||||
|
|
||||||
use Monitoring\Form\Command\AcknowledgeForm;
|
use \DateTimeZone;
|
||||||
use \Zend_View;
|
use \Zend_View;
|
||||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||||
|
use Monitoring\Form\Command\AcknowledgeForm;
|
||||||
|
use Icinga\Util\DateTimeFactory;
|
||||||
|
|
||||||
class AcknowledgeFormTest extends BaseFormTest
|
class AcknowledgeFormTest extends BaseFormTest
|
||||||
{
|
{
|
||||||
const FORMCLASS = "Monitoring\Form\Command\AcknowledgeForm";
|
const FORMCLASS = "Monitoring\Form\Command\AcknowledgeForm";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the default time zone
|
||||||
|
*
|
||||||
|
* Utilizes singleton DateTimeFactory
|
||||||
|
*
|
||||||
|
* @backupStaticAttributes enabled
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
date_default_timezone_set('UTC');
|
||||||
|
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
|
||||||
|
}
|
||||||
|
|
||||||
public function testForm()
|
public function testForm()
|
||||||
{
|
{
|
||||||
$formWithoutExpiration = $this->getRequestForm(array(), self::FORMCLASS);
|
$formWithoutExpiration = $this->getRequestForm(array(), self::FORMCLASS);
|
||||||
|
@ -228,7 +228,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
|
|||||||
'Assert negative hours to cause validation errors in flexible downtime'
|
'Assert negative hours to cause validation errors in flexible downtime'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInvalidMinutesValueRecognitionInFlexibleDowntime()
|
public function testInvalidMinutesValueRecognitionInFlexibleDowntime()
|
||||||
{
|
{
|
||||||
$form = $this->getRequestForm(array(
|
$form = $this->getRequestForm(array(
|
||||||
@ -317,8 +317,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
|
|||||||
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
$form = $this->getRequestForm(array(), self::FORMCLASS);
|
||||||
$form->buildForm();
|
$form->buildForm();
|
||||||
|
|
||||||
$time1 = strtotime($form->getElement('starttime')->getValue());
|
$time1 = $form->getElement('starttime')->getValue();
|
||||||
$time2 = strtotime($form->getElement('endtime')->getValue());
|
$time2 = $form->getElement('endtime')->getValue();
|
||||||
|
|
||||||
$this->assertEquals(3600, ($time2 - $time1));
|
$this->assertEquals(3600, ($time2 - $time1));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user