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
{
/**
* The configuration to use for populating this form
*
* @var IcingaConfig
*/
private $config = null;
/**
* The base directory of the icingaweb configuration
*
@ -60,7 +53,6 @@ class GeneralForm extends Form
*/
private $configDir = null;
/**
* The resources to use instead of the factory provided ones (use for testing)
*
@ -75,16 +67,6 @@ class GeneralForm extends Form
*/
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
*
@ -349,15 +331,12 @@ class GeneralForm extends Form
*/
public function create()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$global = $this->config->global;
$config = $this->getConfiguration();
$global = $config->global;
if ($global === null) {
$global = new Zend_Config(array());
}
$preferences = $this->config->preferences;
$preferences = $config->preferences;
if ($preferences === null) {
$preferences = new Zend_Config(array());
}
@ -378,18 +357,16 @@ class GeneralForm extends Form
*/
public function getConfig()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
$config = $this->getConfiguration();
if ($config->global === null) {
$config->global = new Zend_Config(array());
}
if ($this->config->global === null) {
$this->config->global = new Zend_Config(array());
}
if ($this->config->preferences === null) {
$this->config->preferences = new Zend_Config(array());
if ($config->preferences === null) {
$config->preferences = new Zend_Config(array());
}
$values = $this->getValues();
$cfg = clone $this->config;
$cfg = clone $config;
$cfg->global->environment = ($values['environment'] == 1) ? 'development' : 'production';
$cfg->global->timezone = $values['timezone'];
$cfg->global->moduleFolder = $values['module_folder'];

View File

@ -41,13 +41,6 @@ use \Icinga\Web\Form\Decorator\ConditionalHidden;
*/
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)
*
@ -55,18 +48,6 @@ class LoggingForm extends Form
*/
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()
*
@ -123,15 +104,13 @@ class LoggingForm extends Form
public function create()
{
$this->setName('form_config_logging');
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$logging = $this->config->logging;
$config = $this->getConfiguration();
$logging = $config->logging;
if ($logging === null) {
$logging = new IcingaConfig(array());
}
$debug = $logging->debug;
$debug = $config->logging->debug;
if ($debug === null) {
$debug = new IcingaConfig(array());
}
@ -200,19 +179,16 @@ class LoggingForm extends Form
*/
public function getConfig()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
$config = $this->getConfiguration();
if ($config->logging === null) {
$config->logging = new IcingaConfig(array());
}
if ($this->config->logging === null) {
$this->config->logging = new Zend_Config(array());
}
if ($this->config->logging->debug === null) {
$this->config->logging->debug = new Zend_Config(array());
if ($config->logging->debug === null) {
$config->logging->debug = new IcingaConfig(array());
}
$values = $this->getValues();
$cfg = $this->config->toArray();
$cfg = $config->toArray();
$cfg['logging']['enable'] = 1;
$cfg['logging']['type'] = 'stream';

View File

@ -33,9 +33,6 @@ use \Zend_Config;
use \Zend_Form_Element_Text;
use \Zend_Form_Element_Select;
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\Validator\TimeFormatValidator;
use \Icinga\Web\Form\Validator\DateFormatValidator;
@ -45,20 +42,6 @@ use \Icinga\Web\Form\Validator\DateFormatValidator;
*/
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
*
@ -66,39 +49,6 @@ class GeneralForm extends Form
*/
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
*
@ -252,11 +202,10 @@ class GeneralForm extends Form
*/
public function create()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$this->setName('form_preference_set');
$global = $this->config->global;
$config = $this->getConfiguration();
$global = $config->global;
if ($global === null) {
$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_Oci;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\User\Preferences;
use \Icinga\Web\Form;
/**
@ -327,12 +328,23 @@ class BaseTestCase extends Zend_Test_PHPUnit_ControllerTestCase implements DbTes
require_once $classFile;
$form = new $formClass();
$form->initCsrfToken();
$token = $form->getValue($form->getTokenElementName());
if ($token !== null) {
$requestData[$form->getTokenElementName()] = $token;
}
$request = $this->getRequest();
$request->setMethod('POST');
$request->setPost($requestData);
$form->setRequest($request);
$form->setUserPreferences(
new Preferences(
array()
)
);
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 . '/User/Preferences.php';
// @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()
*

View File

@ -27,13 +27,15 @@ namespace Icinga\Web;
use \Zend_Controller_Request_Abstract;
use \Zend_Form;
use \Zend_Config;
use \Zend_Form_Element_Submit;
use \Zend_Form_Element_Reset;
use \Zend_View_Interface;
use \Icinga\Web\Form\Element\Note;
use \Icinga\Exception\ProgrammingError;
use \Icinga\Web\Form\Decorator\HelpText;
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
@ -47,6 +49,22 @@ class Form extends Zend_Form
*/
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
* 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;
}
/**
* 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
*

View File

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

View File

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

View File

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

View File

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

View File

@ -28,7 +28,8 @@
namespace Monitoring\Form\Command;
use Icinga\Web\Form;
use \Zend_Config;
use \Icinga\Web\Form;
use \Zend_Form_Element_Hidden;
/**
@ -101,4 +102,28 @@ class CommandForm extends Form
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(
new DateTimePicker(
array(
'name' => 'checktime',
'label' => t('Check Time'),
'value' => DateTimeFactory::create()->getTimestamp(),
'required' => !$this->getRequest()->getPost('forcecheck'),
'helptext' => t('Set the date/time when this check should be executed.')
'name' => 'checktime',
'label' => t('Check Time'),
'patterns' => $this->getValidDateTimeFormats(),
'value' => DateTimeFactory::create()->getTimestamp(),
'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(
new DateTimePicker(
array(
'name' => 'starttime',
'label' => t('Start Time'),
'value' => $now->getTimestamp(),
'helptext' => t('Set the start date/time for the downtime.')
'name' => 'starttime',
'label' => t('Start Time'),
'value' => $now->getTimestamp(),
'patterns' => $this->getValidDateTimeFormats(),
'helptext' => t('Set the start date/time for the downtime.')
)
)
);
$this->addElement(
new DateTimePicker(
array(
'name' => 'endtime',
'label' => t('End Time'),
'value' => $now->getTimestamp() + 3600,
'helptext' => t('Set the end date/time for the downtime.')
'name' => 'endtime',
'label' => t('End Time'),
'value' => $now->getTimestamp() + 3600,
'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',
'persistent' => '0',
'expire' => '1',
'expiretime' => '2013-07-10 17:32:16',
'expiretime' => '10/07/2013 5:32 PM',
'sticky' => '0',
'notify' => '0',
'btn_submit' => 'Submit'

View File

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

View File

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

View File

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

View File

@ -41,7 +41,6 @@ require_once BaseTestCase::$libDir . '/Web/Form.php';
require_once BaseTestCase::$appDir . '/forms/Config/GeneralForm.php';
// @codingStandardsIgnoreEnd
use \Icinga\Web\Form;
use \Zend_Config;
/**
@ -50,7 +49,6 @@ use \Zend_Config;
*/
class LoggingFormTest extends BaseTestCase
{
/**
* 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;
// @codingStandardsIgnoreStart
require_once 'Zend/Config.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/Form/Element/Select.php';
require_once 'Zend/View/Helper/Abstract.php';
require_once BaseTestCase::$libDir . '/User/Preferences.php';
require_once BaseTestCase::$libDir . '/Web/Form.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::$libDir . '/Util/ConfigAwareFactory.php';
require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
// @codingStandardsIgnoreEnd
use \DateTimeZone;
use \Icinga\Web\Form;
use \Zend_Config;
use \Icinga\User\Preferences;
use \Zend_View_Helper_DateFormat;
use \Icinga\Util\DateTimeFactory;
@ -60,7 +55,6 @@ use \Icinga\Util\DateTimeFactory;
*/
class GeneralFormTest extends BaseTestCase
{
/**
* 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->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setRequest($this->getRequest());
$form->setConfiguration(
new Zend_Config(
array(
'timezone' => 'UTC'
)
)
);
$form->setUserPreferences(
new Preferences(
array()
)
);
$form->create();
$this->assertSame(
1,
@ -103,13 +85,6 @@ class GeneralFormTest extends BaseTestCase
$form = $this->createForm('Icinga\Form\Preference\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setRequest($this->getRequest());
$form->setConfiguration(
new Zend_Config(
array(
'timezone' => 'UTC'
)
)
);
$form->setUserPreferences(
new Preferences(
array(

View File

@ -20,59 +20,21 @@ namespace {
namespace Test\Icinga\Web\Form {
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/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('../../library/Icinga/Test/BaseTestCase.php');
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/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;
use \Icinga\Test\BaseTestCase;
/**
* 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
*
* @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
* Temporary wrapper for BaseTestCase::createForm until this testcase is not used anymore
*/
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;
return $this->createForm($formClass, $data);
}
/**
@ -84,5 +46,4 @@ namespace Test\Icinga\Web\Form {
$this->assertTrue(true);
}
}
}

View File

@ -34,12 +34,32 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
{
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(
$dt->isValid('bar'),
'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(
$dt->isValid('13736a16223'),
'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')));
$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(
$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'
);
$this->assertTrue(
$dt->isValid(1373616223),
'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
*
@ -81,25 +117,26 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$dt = new DateTimePicker('foo');
$dt->setValue('2013-07-12 08:03:43');
$this->assertTrue(
$dt->isValid($dt->getValue()),
'Using a valid date/time string must not fail'
$dt = new DateTimePicker(
'foo',
array(
'patterns' => array(
'd.m.Y H:i:s'
)
)
);
$dt->isValid('12.07.2013 08:03:43');
$this->assertEquals(
$dt->getValue(),
1373616223,
'getValue did not return the correct Unix timestamp according to the given date/time '
. 'string'
$dt->getValue(),
'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
* isValid considers the input as correct
* Test that DateTimePicker::getValue() returns a timestamp respecting
* the given non-UTC time zone after a successful call to isValid
*
* Utilizes singleton DateTimeFactory
*
@ -109,17 +146,19 @@ class DateTimeTest extends PHPUnit_Framework_TestCase
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('Europe/Berlin')));
$dt = new DateTimePicker('foo');
$dt->setValue('2013-07-12 08:03:43');
$this->assertTrue(
$dt->isValid($dt->getValue()),
'Using a valid date/time string must not fail'
$dt = new DateTimePicker(
'foo',
array(
'patterns' => array(
'd.m.Y H:i:s'
)
)
);
$dt->isValid('12.07.2013 08:03:43');
$this->assertEquals(
$dt->getValue(),
1373609023,
$dt->getValue(),
'getValue did not return the correct Unix timestamp according to the given date/time string and time zone'
);
}