Merge branch 'bugfix/proper-datetime-validation-4581'

fixes #4581
fixes #4632
This commit is contained in:
Johannes Meyer 2013-09-03 09:27:33 +02:00
commit 86d721c317
21 changed files with 306 additions and 299 deletions

View File

@ -46,13 +46,6 @@ use \Icinga\Web\Form\Decorator\ConditionalHidden;
*/ */
class GeneralForm extends Form class GeneralForm extends Form
{ {
/**
* The configuration to use for populating this form
*
* @var IcingaConfig
*/
private $config = null;
/** /**
* The base directory of the icingaweb configuration * The base directory of the icingaweb configuration
* *
@ -60,7 +53,6 @@ class GeneralForm extends Form
*/ */
private $configDir = null; private $configDir = null;
/** /**
* The resources to use instead of the factory provided ones (use for testing) * The resources to use instead of the factory provided ones (use for testing)
* *
@ -75,16 +67,6 @@ class GeneralForm extends Form
*/ */
private $dateHelper; private $dateHelper;
/**
* Set the configuration to be used for this form
*
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
$this->config = $cfg;
}
/** /**
* Set a specific configuration directory to use for configuration specific default paths * Set a specific configuration directory to use for configuration specific default paths
* *
@ -349,15 +331,12 @@ class GeneralForm extends Form
*/ */
public function create() public function create()
{ {
if ($this->config === null) { $config = $this->getConfiguration();
$this->config = new Zend_Config(array()); $global = $config->global;
}
$global = $this->config->global;
if ($global === null) { if ($global === null) {
$global = new Zend_Config(array()); $global = new Zend_Config(array());
} }
$preferences = $this->config->preferences; $preferences = $config->preferences;
if ($preferences === null) { if ($preferences === null) {
$preferences = new Zend_Config(array()); $preferences = new Zend_Config(array());
} }
@ -378,18 +357,16 @@ class GeneralForm extends Form
*/ */
public function getConfig() public function getConfig()
{ {
if ($this->config === null) { $config = $this->getConfiguration();
$this->config = new Zend_Config(array()); if ($config->global === null) {
$config->global = new Zend_Config(array());
} }
if ($this->config->global === null) { if ($config->preferences === null) {
$this->config->global = new Zend_Config(array()); $config->preferences = new Zend_Config(array());
}
if ($this->config->preferences === null) {
$this->config->preferences = new Zend_Config(array());
} }
$values = $this->getValues(); $values = $this->getValues();
$cfg = clone $this->config; $cfg = clone $config;
$cfg->global->environment = ($values['environment'] == 1) ? 'development' : 'production'; $cfg->global->environment = ($values['environment'] == 1) ? 'development' : 'production';
$cfg->global->timezone = $values['timezone']; $cfg->global->timezone = $values['timezone'];
$cfg->global->moduleFolder = $values['module_folder']; $cfg->global->moduleFolder = $values['module_folder'];

View File

@ -41,13 +41,6 @@ use \Icinga\Web\Form\Decorator\ConditionalHidden;
*/ */
class LoggingForm extends Form class LoggingForm extends Form
{ {
/**
* The configuration to use for this form
*
* @var Zend_Config
*/
private $config = null;
/** /**
* Base directory to use instead of the one provided by Icinga::app (used for testing) * Base directory to use instead of the one provided by Icinga::app (used for testing)
* *
@ -55,18 +48,6 @@ class LoggingForm extends Form
*/ */
private $baseDir = null; private $baseDir = null;
/**
* Set the configuration of this form
*
* If not called, default values are used instead
*
* @param Zend_Config $cfg The config.ini to set with this form
*/
public function setConfiguration(Zend_Config $cfg)
{
$this->config = $cfg;
}
/** /**
* Set a different base directory to use for default paths instead of the one provided by Icinga::app() * Set a different base directory to use for default paths instead of the one provided by Icinga::app()
* *
@ -123,15 +104,13 @@ class LoggingForm extends Form
public function create() public function create()
{ {
$this->setName('form_config_logging'); $this->setName('form_config_logging');
if ($this->config === null) {
$this->config = new Zend_Config(array()); $config = $this->getConfiguration();
} $logging = $config->logging;
$logging = $this->config->logging;
if ($logging === null) { if ($logging === null) {
$logging = new IcingaConfig(array()); $logging = new IcingaConfig(array());
} }
$debug = $config->logging->debug;
$debug = $logging->debug;
if ($debug === null) { if ($debug === null) {
$debug = new IcingaConfig(array()); $debug = new IcingaConfig(array());
} }
@ -200,19 +179,16 @@ class LoggingForm extends Form
*/ */
public function getConfig() public function getConfig()
{ {
if ($this->config === null) { $config = $this->getConfiguration();
$this->config = new Zend_Config(array()); if ($config->logging === null) {
$config->logging = new IcingaConfig(array());
} }
if ($this->config->logging === null) { if ($config->logging->debug === null) {
$this->config->logging = new Zend_Config(array()); $config->logging->debug = new IcingaConfig(array());
}
if ($this->config->logging->debug === null) {
$this->config->logging->debug = new Zend_Config(array());
} }
$values = $this->getValues(); $values = $this->getValues();
$cfg = $this->config->toArray(); $cfg = $config->toArray();
$cfg['logging']['enable'] = 1; $cfg['logging']['enable'] = 1;
$cfg['logging']['type'] = 'stream'; $cfg['logging']['type'] = 'stream';

View File

@ -33,9 +33,6 @@ use \Zend_Config;
use \Zend_Form_Element_Text; use \Zend_Form_Element_Text;
use \Zend_Form_Element_Select; use \Zend_Form_Element_Select;
use \Zend_View_Helper_DateFormat; use \Zend_View_Helper_DateFormat;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\Icinga;
use \Icinga\User\Preferences;
use \Icinga\Web\Form; use \Icinga\Web\Form;
use \Icinga\Web\Form\Validator\TimeFormatValidator; use \Icinga\Web\Form\Validator\TimeFormatValidator;
use \Icinga\Web\Form\Validator\DateFormatValidator; use \Icinga\Web\Form\Validator\DateFormatValidator;
@ -45,20 +42,6 @@ use \Icinga\Web\Form\Validator\DateFormatValidator;
*/ */
class GeneralForm extends Form class GeneralForm extends Form
{ {
/**
* The configuration to use for populating this form
*
* @var IcingaConfig
*/
private $config;
/**
* The preference object to use instead of the one from the user (used for testing)
*
* @var Zend_Config
*/
private $preferences;
/** /**
* The view helper to format date/time strings * The view helper to format date/time strings
* *
@ -66,39 +49,6 @@ class GeneralForm extends Form
*/ */
private $dateHelper; private $dateHelper;
/**
* Set the configuration to be used for this form when no preferences are set yet
*
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
$this->config = $cfg;
}
/**
* Set preferences to be used instead of the one from the user object (used for testing)
*
* @param Zend_Config $prefs
*/
public function setUserPreferences($prefs)
{
$this->preferences = $prefs;
}
/**
* Return the preferences of the user or the overwritten ones
*
* @return Zend_Config
*/
public function getUserPreferences()
{
if ($this->preferences) {
return $this->preferences;
}
return $this->getRequest()->getUser()->getPreferences();
}
/** /**
* Return the view helper to format date/time strings * Return the view helper to format date/time strings
* *
@ -252,11 +202,10 @@ class GeneralForm extends Form
*/ */
public function create() public function create()
{ {
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$this->setName('form_preference_set'); $this->setName('form_preference_set');
$global = $this->config->global;
$config = $this->getConfiguration();
$global = $config->global;
if ($global === null) { if ($global === null) {
$global = new Zend_Config(array()); $global = new Zend_Config(array());
} }

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

@ -37,7 +37,23 @@ class DateTimeFactory implements ConfigAwareFactory
} }
/** /**
* Return new DateTime object using the set time zone * Return new DateTime object using the given format, time and set time zone
*
* Wraps DateTime::createFromFormat()
*
* @param string $format
* @param string $time
* @param DateTimeZone $timeZone
* @return DateTime
* @see DateTime::createFromFormat()
*/
public static function parse($time, $format, DateTimeZone $timeZone = null)
{
return DateTime::createFromFormat($format, $time, $timeZone !== null ? $timeZone : self::$timeZone);
}
/**
* Return new DateTime object using the given date/time string and set time zone
* *
* Wraps DateTime::__construct() * Wraps DateTime::__construct()
* *

View File

@ -27,13 +27,15 @@ namespace Icinga\Web;
use \Zend_Controller_Request_Abstract; use \Zend_Controller_Request_Abstract;
use \Zend_Form; use \Zend_Form;
use \Zend_Config;
use \Zend_Form_Element_Submit; use \Zend_Form_Element_Submit;
use \Zend_Form_Element_Reset; use \Zend_Form_Element_Reset;
use \Zend_View_Interface; use \Zend_View_Interface;
use \Icinga\Web\Form\Element\Note;
use \Icinga\Exception\ProgrammingError; use \Icinga\Exception\ProgrammingError;
use \Icinga\Web\Form\Decorator\HelpText; use \Icinga\Web\Form\Decorator\HelpText;
use \Icinga\Web\Form\InvalidCSRFTokenException; use \Icinga\Web\Form\InvalidCSRFTokenException;
use \Icinga\Web\Form\Element\Note; use \Icinga\Application\Config as IcingaConfig;
/** /**
* Base class for forms providing CSRF protection, confirmation logic and auto submission * Base class for forms providing CSRF protection, confirmation logic and auto submission
@ -47,6 +49,22 @@ class Form extends Zend_Form
*/ */
private $request; private $request;
/**
* Main configuration
*
* Used as fallback if user preferences are not available.
*
* @var IcingaConfig
*/
private $config;
/**
* The preference object to use instead of the one from the user (used for testing)
*
* @var Zend_Config
*/
private $preferences;
/** /**
* Whether this form should NOT add random generated "challenge" tokens that are associated with the user's current * Whether this form should NOT add random generated "challenge" tokens that are associated with the user's current
* session in order to prevent Cross-Site Request Forgery (CSRF). It is the form's responsibility to verify the * session in order to prevent Cross-Site Request Forgery (CSRF). It is the form's responsibility to verify the
@ -192,6 +210,54 @@ class Form extends Zend_Form
return $this->request; return $this->request;
} }
/**
* Set the configuration to be used for this form when no preferences are set yet
*
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
$this->config = $cfg;
}
/**
* Get the main configuration
*
* Returns the set configuration or an empty default one.
*
* @return Zend_Config
*/
public function getConfiguration()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
return $this->config;
}
/**
* Set preferences to be used instead of the one from the user object (used for testing)
*
* @param Zend_Config $prefs
*/
public function setUserPreferences($prefs)
{
$this->preferences = $prefs;
}
/**
* Return the preferences of the user or the overwritten ones
*
* @return Zend_Config
*/
public function getUserPreferences()
{
if ($this->preferences) {
return $this->preferences;
}
return $this->getRequest()->getUser()->getPreferences();
}
/** /**
* Create the form if not done already * Create the form if not done already
* *

View File

@ -28,10 +28,9 @@
namespace Icinga\Web\Form\Element; namespace Icinga\Web\Form\Element;
use \Exception;
use \Zend_Form_Element_Xhtml; use \Zend_Form_Element_Xhtml;
use \Icinga\Application\Icinga;
use \Icinga\Util\DateTimeFactory; use \Icinga\Util\DateTimeFactory;
use \Icinga\Exception\ProgrammingError;
/** /**
* Datetime form element which returns the input as Unix timestamp after the input has been proven valid. Utilizes * Datetime form element which returns the input as Unix timestamp after the input has been proven valid. Utilizes
@ -48,14 +47,20 @@ class DateTimePicker extends Zend_Form_Element_Xhtml
public $helper = 'formDateTime'; public $helper = 'formDateTime';
/** /**
* Find whether a variable is a Unix timestamp * Valid formats to check user input against
* @var array
*/
public $patterns;
/**
* Check whether a variable is a Unix timestamp
* *
* @param mixed $timestamp * @param mixed $timestamp
* @return bool * @return bool
*/ */
public function isUnixTimestamp($timestamp) public function isUnixTimestamp($timestamp)
{ {
return ((string) (int) $timestamp === (string) $timestamp) return (is_int($timestamp) || ctype_digit($timestamp))
&& ($timestamp <= PHP_INT_MAX) && ($timestamp <= PHP_INT_MAX)
&& ($timestamp >= ~PHP_INT_MAX); && ($timestamp >= ~PHP_INT_MAX);
} }
@ -63,13 +68,12 @@ class DateTimePicker extends Zend_Form_Element_Xhtml
/** /**
* Validate filtered date/time strings * Validate filtered date/time strings
* *
* Expects formats that the php date parser understands. Sets element value as Unix timestamp if the input is * Expects one or more valid formats being set in $this->patterns. Sets element value as Unix timestamp
* considered valid. Utilizes DateTimeFactory to ensure time zone awareness * if the input is considered valid. Utilizes DateTimeFactory to ensure time zone awareness.
* *
* @param string $value * @param string $value
* @param mixed $context * @param mixed $context
* @return bool * @return bool
* @see \Icinga\Util\DateTimeFactory::create()
*/ */
public function isValid($value, $context = null) public function isValid($value, $context = null)
{ {
@ -88,15 +92,19 @@ class DateTimePicker extends Zend_Form_Element_Xhtml
$dt = DateTimeFactory::create(); $dt = DateTimeFactory::create();
$dt->setTimestamp($value); $dt->setTimestamp($value);
} else { } else {
try { if (!isset($this->patterns)) {
$dt = DateTimeFactory::create($value); throw new ProgrammingError('Cannot parse datetime string without any pattern');
} catch (Exception $e) { }
$this->addErrorMessage(
_( $match_found = false;
'Failed to parse datetime string. See ' foreach ($this->patterns as $pattern) {
. 'http://www.php.net/manual/en/datetime.formats.php for valid formats' $dt = DateTimeFactory::parse($value, $pattern);
) if ($dt !== false && $dt->format($pattern) === $value) {
); $match_found = true;
break;
}
}
if (!$match_found) {
return false; return false;
} }
} }

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

@ -49,10 +49,9 @@ require_once BaseTestCase::$appDir . '/forms/Config/Authentication/DbBackendForm
require_once BaseTestCase::$appDir . '/forms/Config/Authentication/LdapBackendForm.php'; require_once BaseTestCase::$appDir . '/forms/Config/Authentication/LdapBackendForm.php';
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
use \Icinga\Web\Form;
use Icinga\Web\Url;
use Tests\Icinga\Web\RequestMock;
use \Zend_Config; use \Zend_Config;
use \Icinga\Web\Url;
use \Tests\Icinga\Web\RequestMock;
/** /**
* Test for the authentication provider form * Test for the authentication provider form
@ -60,7 +59,6 @@ use \Zend_Config;
*/ */
class AuthenticationFormTest extends BaseTestCase class AuthenticationFormTest extends BaseTestCase
{ {
/** /**
* Return a test configuration containing a database and a ldap backend * Return a test configuration containing a database and a ldap backend
* *
@ -76,14 +74,14 @@ class AuthenticationFormTest extends BaseTestCase
'resource' => 'db_resource' 'resource' => 'db_resource'
), ),
'test-ldap' => array( 'test-ldap' => array(
'backend' => 'ldap', 'backend' => 'ldap',
'target' => 'user', 'target' => 'user',
'hostname' => 'test host', 'hostname' => 'test host',
'root_dn' => 'ou=test,dc=icinga,dc=org', 'root_dn' => 'ou=test,dc=icinga,dc=org',
'bind_dn' => 'cn=testuser,cn=config', 'bind_dn' => 'cn=testuser,cn=config',
'bind_pw' => 'password', 'bind_pw' => 'password',
'user_class' => 'testClass', 'user_class' => 'testClass',
'user_name_attribute' => 'testAttribute' 'user_name_attribute' => 'testAttribute'
) )
) )
); );
@ -91,7 +89,6 @@ class AuthenticationFormTest extends BaseTestCase
/** /**
* Test the ldap provider form population from config * Test the ldap provider form population from config
*
*/ */
public function testLdapProvider() public function testLdapProvider()
{ {
@ -99,14 +96,14 @@ class AuthenticationFormTest extends BaseTestCase
$form = $this->createForm('Icinga\Form\Config\Authentication\LdapBackendForm'); $form = $this->createForm('Icinga\Form\Config\Authentication\LdapBackendForm');
$config = new Zend_Config( $config = new Zend_Config(
array( array(
'backend' => 'ldap', 'backend' => 'ldap',
'target' => 'user', 'target' => 'user',
'hostname' => 'test host', 'hostname' => 'test host',
'root_dn' => 'ou=test,dc=icinga,dc=org', 'root_dn' => 'ou=test,dc=icinga,dc=org',
'bind_dn' => 'cn=testuser,cn=config', 'bind_dn' => 'cn=testuser,cn=config',
'bind_pw' => 'password', 'bind_pw' => 'password',
'user_class' => 'testClass', 'user_class' => 'testClass',
'user_name_attribute' => 'testAttribute' 'user_name_attribute' => 'testAttribute'
) )
); );
$form->setBackendName('testldap'); $form->setBackendName('testldap');
@ -126,7 +123,7 @@ class AuthenticationFormTest extends BaseTestCase
); );
} }
} }
/*
/** /**
* Test the database provider form population from config * Test the database provider form population from config
*/ */

View File

@ -46,7 +46,6 @@ require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
use \DateTimeZone; use \DateTimeZone;
use \Icinga\Web\Form;
use \DOMDocument; use \DOMDocument;
use \Zend_Config; use \Zend_Config;
use \Zend_View; use \Zend_View;

View File

@ -41,7 +41,6 @@ require_once BaseTestCase::$libDir . '/Web/Form.php';
require_once BaseTestCase::$appDir . '/forms/Config/GeneralForm.php'; require_once BaseTestCase::$appDir . '/forms/Config/GeneralForm.php';
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
use \Icinga\Web\Form;
use \Zend_Config; use \Zend_Config;
/** /**
@ -50,7 +49,6 @@ use \Zend_Config;
*/ */
class LoggingFormTest extends BaseTestCase class LoggingFormTest extends BaseTestCase
{ {
/** /**
* Test the logging form to be correctly populated from configuration * Test the logging form to be correctly populated from configuration
* *

View File

@ -35,22 +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 \Icinga\Web\Form;
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;
@ -60,7 +55,6 @@ use \Icinga\Util\DateTimeFactory;
*/ */
class GeneralFormTest extends BaseTestCase class GeneralFormTest extends BaseTestCase
{ {
/** /**
* Test whether fields using the default values have input disabled * Test whether fields using the default values have input disabled
* *
@ -72,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,
@ -103,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);
} }
} }
} }

View File

@ -34,12 +34,32 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
{ {
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$dt = new DateTimePicker('foo'); $dt = new DateTimePicker(
'foo',
array(
'patterns' => array(
'd/m/Y g:i A',
'd.m.Y H:i:s'
)
)
);
$this->assertFalse(
$dt->isValid('08/27/2013 12:40 PM'),
'Wrong placed month/day must not be valid input'
);
$this->assertFalse( $this->assertFalse(
$dt->isValid('bar'), $dt->isValid('bar'),
'Arbitrary strings must not be valid input' 'Arbitrary strings must not be valid input'
); );
$this->assertFalse(
$dt->isValid('12:40 AM'),
'Time strings must not be valid input'
);
$this->assertFalse(
$dt->isValid('27/08/2013'),
'Date strings must not be valid input'
);
$this->assertFalse( $this->assertFalse(
$dt->isValid('13736a16223'), $dt->isValid('13736a16223'),
'Invalid Unix timestamps must not be valid input' 'Invalid Unix timestamps must not be valid input'
@ -58,20 +78,36 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
{ {
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$dt = new DateTimePicker('foo'); $dt = new DateTimePicker(
'foo',
array(
'patterns' => array(
'd/m/Y g:i A',
'd.m.Y H:i:s'
)
)
);
$this->assertTrue( $this->assertTrue(
$dt->isValid('2013-07-12 08:03:43'), $dt->isValid('27/08/2013 12:40 PM'),
'Using a valid date/time string must not fail'
);
$this->assertTrue(
$dt->isValid('12.07.2013 08:03:43'),
'Using a valid date/time string must not fail' 'Using a valid date/time string must not fail'
); );
$this->assertTrue( $this->assertTrue(
$dt->isValid(1373616223), $dt->isValid(1373616223),
'Using valid Unix timestamps must not fail' 'Using valid Unix timestamps must not fail'
); );
$this->assertTrue(
$dt->isValid('1373616223'),
'Using strings as Unix timestamps must not fail'
);
} }
/** /**
* Test that DateTimePicker::getValue() returns a timestamp after a call to isValid considers the input as correct * Test that DateTimePicker::getValue() returns a timestamp after a successful call to isValid
* *
* Utilizes singleton DateTimeFactory * Utilizes singleton DateTimeFactory
* *
@ -81,25 +117,26 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
{ {
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$dt = new DateTimePicker('foo'); $dt = new DateTimePicker(
$dt->setValue('2013-07-12 08:03:43'); 'foo',
array(
$this->assertTrue( 'patterns' => array(
$dt->isValid($dt->getValue()), 'd.m.Y H:i:s'
'Using a valid date/time string must not fail' )
)
); );
$dt->isValid('12.07.2013 08:03:43');
$this->assertEquals( $this->assertEquals(
$dt->getValue(),
1373616223, 1373616223,
'getValue did not return the correct Unix timestamp according to the given date/time ' $dt->getValue(),
. 'string' 'getValue did not return the correct Unix timestamp according to the given date/time string'
); );
} }
/** /**
* Test that DateTimePicker::getValue() returns a timestamp respecting the given non-UTC time zone after a call to * Test that DateTimePicker::getValue() returns a timestamp respecting
* isValid considers the input as correct * the given non-UTC time zone after a successful call to isValid
* *
* Utilizes singleton DateTimeFactory * Utilizes singleton DateTimeFactory
* *
@ -109,17 +146,19 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
{ {
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('Europe/Berlin'))); DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('Europe/Berlin')));
$dt = new DateTimePicker('foo'); $dt = new DateTimePicker(
$dt->setValue('2013-07-12 08:03:43'); 'foo',
array(
$this->assertTrue( 'patterns' => array(
$dt->isValid($dt->getValue()), 'd.m.Y H:i:s'
'Using a valid date/time string must not fail' )
)
); );
$dt->isValid('12.07.2013 08:03:43');
$this->assertEquals( $this->assertEquals(
$dt->getValue(),
1373609023, 1373609023,
$dt->getValue(),
'getValue did not return the correct Unix timestamp according to the given date/time string and time zone' 'getValue did not return the correct Unix timestamp according to the given date/time string and time zone'
); );
} }