Adjust command forms and tests

Made those command forms using the DateTimePicker
element compatible with its new validation.

refs #4581
This commit is contained in:
Johannes Meyer 2013-08-30 10:37:32 +02:00
parent 368bd3c9c7
commit 23f0962da1
12 changed files with 97 additions and 116 deletions

View File

@ -156,10 +156,7 @@ class LoggingForm extends Form
'label' => 'Debug Log Path', 'label' => 'Debug Log Path',
'required' => $this->shouldDisplayDebugLog($debug), 'required' => $this->shouldDisplayDebugLog($debug),
'condition' => $this->shouldDisplayDebugLog($debug), 'condition' => $this->shouldDisplayDebugLog($debug),
'value' => $debug->get( 'value' => $debug->get('target', $this->getBaseDir() . '/var/log/icinga2.debug.log'),
'target',
$this->getBaseDir() . '/var/log/icinga2.debug.log'
),
'helptext' => 'Set the path to the debug log' 'helptext' => 'Set the path to the debug log'
) )
); );

View File

@ -44,6 +44,7 @@ use \Zend_Db_Adapter_Pdo_Mysql;
use \Zend_Db_Adapter_Pdo_Pgsql; use \Zend_Db_Adapter_Pdo_Pgsql;
use \Zend_Db_Adapter_Pdo_Oci; use \Zend_Db_Adapter_Pdo_Oci;
use \Icinga\Application\DbAdapterFactory; use \Icinga\Application\DbAdapterFactory;
use \Icinga\User\Preferences;
use \Icinga\Web\Form; use \Icinga\Web\Form;
/** /**
@ -327,12 +328,23 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
require_once $classFile; require_once $classFile;
$form = new $formClass(); $form = new $formClass();
$form->initCsrfToken();
$token = $form->getValue($form->getTokenElementName());
if ($token !== null) {
$requestData[$form->getTokenElementName()] = $token;
}
$request = $this->getRequest(); $request = $this->getRequest();
$request->setMethod('POST'); $request->setMethod('POST');
$request->setPost($requestData); $request->setPost($requestData);
$form->setRequest($request); $form->setRequest($request);
$form->setUserPreferences(
new Preferences(
array()
)
);
return $form; return $form;
} }
@ -366,6 +378,7 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
require_once self::$libDir . '/Web/Form.php'; require_once self::$libDir . '/Web/Form.php';
require_once self::$libDir . '/User/Preferences.php';
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
} }

View File

@ -28,7 +28,6 @@
namespace Icinga\Web; namespace Icinga\Web;
use Icinga\Exception\ProgrammingError;
use Zend_Controller_Request_Http; use Zend_Controller_Request_Http;
use Icinga\User; use Icinga\User;

View File

@ -276,6 +276,7 @@ class Monitoring_CommandController extends ActionController
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new RescheduleNextCheckForm(); $form = new RescheduleNextCheckForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(Config::app());
$this->setForm($form); $this->setForm($form);
@ -430,6 +431,7 @@ class Monitoring_CommandController extends ActionController
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new ScheduleDowntimeForm(); $form = new ScheduleDowntimeForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(Config::app());
$form->setWithChildren(false); $form->setWithChildren(false);
$this->setForm($form); $this->setForm($form);
@ -446,6 +448,7 @@ class Monitoring_CommandController extends ActionController
$this->setSupportedParameters(array('host')); $this->setSupportedParameters(array('host'));
$form = new ScheduleDowntimeForm(); $form = new ScheduleDowntimeForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(Config::app());
$form->setWithChildren(true); $form->setWithChildren(true);
$this->setForm($form); $this->setForm($form);
@ -515,6 +518,7 @@ class Monitoring_CommandController extends ActionController
$this->setSupportedParameters(array('host')); $this->setSupportedParameters(array('host'));
$form = new RescheduleNextCheckForm(); $form = new RescheduleNextCheckForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(Config::app());
$form->setWithChildren(true); $form->setWithChildren(true);
@ -676,6 +680,7 @@ class Monitoring_CommandController extends ActionController
$this->setSupportedParameters(array('host', 'service')); $this->setSupportedParameters(array('host', 'service'));
$form = new AcknowledgeForm(); $form = new AcknowledgeForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(Config::app());
$this->setForm($form); $this->setForm($form);

View File

@ -96,10 +96,11 @@ class AcknowledgeForm extends CommandForm
$this->addElement( $this->addElement(
new DateTimePicker( new DateTimePicker(
array( array(
'name' => 'expiretime', 'name' => 'expiretime',
'label' => t('Expire Time'), 'label' => t('Expire Time'),
'value' => $now->getTimestamp() + 3600, 'value' => $now->getTimestamp() + 3600,
'helptext' => t( 'patterns' => $this->getValidDateTimeFormats(),
'helptext' => t(
'Enter the expire date/time for this acknowledgement here. Icinga will ' 'Enter the expire date/time for this acknowledgement here. Icinga will '
. ' delete the acknowledgement after this date expired.' . ' delete the acknowledgement after this date expired.'
) )

View File

@ -28,7 +28,8 @@
namespace Monitoring\Form\Command; namespace Monitoring\Form\Command;
use Icinga\Web\Form; use \Zend_Config;
use \Icinga\Web\Form;
use \Zend_Form_Element_Hidden; use \Zend_Form_Element_Hidden;
/** /**
@ -101,4 +102,28 @@ class CommandForm extends Form
return $authorField; return $authorField;
} }
/**
* Get a list of valid datetime formats
*
* @return array
*/
public function getValidDateTimeFormats()
{
$config = $this->getConfiguration();
$global = $config->global;
if ($global === null) {
$global = new Zend_Config(array());
}
$preferences = $this->getUserPreferences();
return array(
implode(
' ',
array(
$preferences->get('app.dateFormat', $global->get('dateFormat', 'd/m/Y')),
$preferences->get('app.timeFormat', $global->get('timeFormat', 'g:i A'))
)
)
);
}
} }

View File

@ -52,11 +52,12 @@ class RescheduleNextCheckForm extends WithChildrenCommandForm
$this->addElement( $this->addElement(
new DateTimePicker( new DateTimePicker(
array( array(
'name' => 'checktime', 'name' => 'checktime',
'label' => t('Check Time'), 'label' => t('Check Time'),
'value' => DateTimeFactory::create()->getTimestamp(), 'patterns' => $this->getValidDateTimeFormats(),
'required' => !$this->getRequest()->getPost('forcecheck'), 'value' => DateTimeFactory::create()->getTimestamp(),
'helptext' => t('Set the date/time when this check should be executed.') 'required' => !$this->getRequest()->getPost('forcecheck'),
'helptext' => t('Set the date/time when this check should be executed.')
) )
) )
); );

View File

@ -134,20 +134,22 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
$this->addElement( $this->addElement(
new DateTimePicker( new DateTimePicker(
array( array(
'name' => 'starttime', 'name' => 'starttime',
'label' => t('Start Time'), 'label' => t('Start Time'),
'value' => $now->getTimestamp(), 'value' => $now->getTimestamp(),
'helptext' => t('Set the start date/time for the downtime.') 'patterns' => $this->getValidDateTimeFormats(),
'helptext' => t('Set the start date/time for the downtime.')
) )
) )
); );
$this->addElement( $this->addElement(
new DateTimePicker( new DateTimePicker(
array( array(
'name' => 'endtime', 'name' => 'endtime',
'label' => t('End Time'), 'label' => t('End Time'),
'value' => $now->getTimestamp() + 3600, 'value' => $now->getTimestamp() + 3600,
'helptext' => t('Set the end date/time for the downtime.') 'patterns' => $this->getValidDateTimeFormats(),
'helptext' => t('Set the end date/time for the downtime.')
) )
) )
); );

View File

@ -55,7 +55,7 @@ class AcknowledgeFormTest extends BaseFormTest
'comment' => 'Comment', 'comment' => 'Comment',
'persistent' => '0', 'persistent' => '0',
'expire' => '1', 'expire' => '1',
'expiretime' => '2013-07-10 17:32:16', 'expiretime' => '10/07/2013 5:32 PM',
'sticky' => '0', 'sticky' => '0',
'notify' => '0', 'notify' => '0',
'btn_submit' => 'Submit' 'btn_submit' => 'Submit'

View File

@ -52,8 +52,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'TEST_AUTHOR', 'author' => 'TEST_AUTHOR',
'comment' => 'DING DING', 'comment' => 'DING DING',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -71,8 +71,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'TEST_AUTHOR', 'author' => 'TEST_AUTHOR',
'comment' => 'DING DING', 'comment' => 'DING DING',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
'hours' => '10', 'hours' => '10',
'minutes' => '10', 'minutes' => '10',
@ -93,8 +93,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'TEST_AUTHOR', 'author' => 'TEST_AUTHOR',
'comment' => 'DING DING', 'comment' => 'DING DING',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -114,8 +114,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => '', 'author' => '',
'comment' => 'DING DING', 'comment' => 'DING DING',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -136,8 +136,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'OK', 'author' => 'OK',
'comment' => '', 'comment' => '',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -158,8 +158,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'OK', 'author' => 'OK',
'comment' => 'OK', 'comment' => 'OK',
'triggered' => 'HAHA', 'triggered' => 'HAHA',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -179,8 +179,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'OK', 'author' => 'OK',
'comment' => 'OK', 'comment' => 'OK',
'triggered' => '123', 'triggered' => '123',
'starttime' => '2013-07-17', 'starttime' => '17/07/2013',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -200,7 +200,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'OK', 'author' => 'OK',
'comment' => 'OK', 'comment' => 'OK',
'triggered' => '123', 'triggered' => '123',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => 'DING', 'endtime' => 'DING',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
@ -222,8 +222,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'OK', 'author' => 'OK',
'comment' => 'OK', 'comment' => 'OK',
'triggered' => '123', 'triggered' => '123',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 09:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
'hours' => '-1', 'hours' => '-1',
'minutes' => '12', 'minutes' => '12',
@ -243,8 +243,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'OK', 'author' => 'OK',
'comment' => 'OK', 'comment' => 'OK',
'triggered' => '123', 'triggered' => '123',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 09:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE,
'hours' => '12', 'hours' => '12',
'minutes' => 'DING', 'minutes' => 'DING',
@ -265,8 +265,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'TEST_AUTHOR', 'author' => 'TEST_AUTHOR',
'comment' => 'DING DING', 'comment' => 'DING DING',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -287,8 +287,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'TEST_AUTHOR', 'author' => 'TEST_AUTHOR',
'comment' => 'DING DING', 'comment' => 'DING DING',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',
@ -305,8 +305,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest
'author' => 'TEST_AUTHOR', 'author' => 'TEST_AUTHOR',
'comment' => 'DING DING', 'comment' => 'DING DING',
'triggered' => '4', 'triggered' => '4',
'starttime' => '2013-07-17 10:30:00', 'starttime' => '17/07/2013 10:30 AM',
'endtime' => '2013-07-17 10:30:00', 'endtime' => '18/07/2013 10:30 AM',
'type' => ScheduleDowntimeForm::TYPE_FIXED, 'type' => ScheduleDowntimeForm::TYPE_FIXED,
'hours' => '', 'hours' => '',
'minutes' => '', 'minutes' => '',

View File

@ -35,21 +35,17 @@ require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCas
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
// @codingStandardsIgnoreStart // @codingStandardsIgnoreStart
require_once 'Zend/Config.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Form/Element/Select.php'; require_once 'Zend/Form/Element/Select.php';
require_once 'Zend/View/Helper/Abstract.php'; require_once 'Zend/View/Helper/Abstract.php';
require_once BaseTestCase::$libDir . '/User/Preferences.php'; require_once BaseTestCase::$libDir . '/User/Preferences.php';
require_once BaseTestCase::$libDir . '/Web/Form.php'; require_once BaseTestCase::$libDir . '/Web/Form.php';
require_once BaseTestCase::$appDir . '/forms/Preference/GeneralForm.php'; require_once BaseTestCase::$appDir . '/forms/Preference/GeneralForm.php';
require_once BaseTestCase::$libDir . '/User/Preferences/ChangeSet.php';
require_once BaseTestCase::$appDir . '/views/helpers/DateFormat.php'; require_once BaseTestCase::$appDir . '/views/helpers/DateFormat.php';
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php'; require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php'; require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
use \DateTimeZone; use \DateTimeZone;
use \Zend_Config;
use \Icinga\User\Preferences; use \Icinga\User\Preferences;
use \Zend_View_Helper_DateFormat; use \Zend_View_Helper_DateFormat;
use \Icinga\Util\DateTimeFactory; use \Icinga\Util\DateTimeFactory;
@ -70,18 +66,6 @@ class GeneralFormTest extends BaseTestCase
$form = $this->createForm('Icinga\Form\Preference\GeneralForm'); $form = $this->createForm('Icinga\Form\Preference\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest())); $form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(
new Zend_Config(
array(
'timezone' => 'UTC'
)
)
);
$form->setUserPreferences(
new Preferences(
array()
)
);
$form->create(); $form->create();
$this->assertSame( $this->assertSame(
1, 1,
@ -101,13 +85,6 @@ class GeneralFormTest extends BaseTestCase
$form = $this->createForm('Icinga\Form\Preference\GeneralForm'); $form = $this->createForm('Icinga\Form\Preference\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest())); $form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setConfiguration(
new Zend_Config(
array(
'timezone' => 'UTC'
)
)
);
$form->setUserPreferences( $form->setUserPreferences(
new Preferences( new Preferences(
array( array(

View File

@ -20,59 +20,21 @@ namespace {
namespace Test\Icinga\Web\Form { namespace Test\Icinga\Web\Form {
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php'; require_once realpath('../../library/Icinga/Test/BaseTestCase.php');
require_once 'Zend/Form.php';
require_once 'Zend/View.php';
require_once 'Zend/Form/Element/Submit.php';
require_once 'Zend/Form/Element/Text.php';
require_once 'Zend/Form/Element/Password.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/Form/Decorator/Abstract.php';
require_once 'Zend/Validate/Date.php';
$base = '../../';
require_once realpath($base.'library/Icinga/Exception/ProgrammingError.php'); use \Icinga\Test\BaseTestCase;
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/DateTimePicker.php');
require_once realpath('../../library/Icinga/Web/Form/Decorator/ConditionalHidden.php');
require_once realpath('../../library/Icinga/Web/Form/Decorator/HelpText.php');
require_once realpath('../../library/Icinga/Web/Form/Validator/WritablePathValidator.php');
require_once realpath('../../library/Icinga/Web/Form/Validator/DateFormatValidator.php');
require_once realpath('../../library/Icinga/Web/Form/Validator/TimeFormatValidator.php');
use \Zend_Form;
use \Zend_Test_PHPUnit_ControllerTestCase;
/** /**
* Base test to be extended for testing forms * Base test to be extended for testing forms
*/ */
class BaseFormTest extends Zend_Test_PHPUnit_ControllerTestCase class BaseFormTest extends BaseTestCase
{ {
/** /**
* Returns a formclass with the given set of POST data applied * Temporary wrapper for BaseTestCase::createForm until this testcase is not used anymore
*
* @param array $data The POST parameters to ste
* @param string $formClass The class name (full namespace) to return
*
* @return Zend_Form $form A form of type $formClass
*/ */
public function getRequestForm(array $data, $formClass) public function getRequestForm(array $data, $formClass)
{ {
$form = new $formClass(); return $this->createForm($formClass, $data);
$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;
} }
/** /**
@ -84,5 +46,4 @@ namespace Test\Icinga\Web\Form {
$this->assertTrue(true); $this->assertTrue(true);
} }
} }
} }