Fix GeneralForm tests

refs #4609
This commit is contained in:
Johannes Meyer 2013-08-28 15:55:21 +02:00 committed by Marius Hein
parent f4272f482d
commit bf5849a769
5 changed files with 102 additions and 9 deletions

View File

@ -32,6 +32,7 @@ use \DateTimeZone;
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\DbAdapterFactory;
use \Icinga\Web\Form;
@ -68,6 +69,13 @@ class GeneralForm extends Form
*/
private $resources = null;
/**
* The view helper to format date/time strings
*
* @var Zend_View_Helper_DateFormat
*/
private $dateHelper;
/**
* Set the configuration to be used for this form
*
@ -100,6 +108,29 @@ class GeneralForm extends Form
return $this->configDir === null ? IcingaConfig::$configDir : $this->configDir;
}
/**
* Return the view helper to format date/time strings
*
* @return Zend_View_Helper_DateFormat
*/
public function getDateFormatter()
{
if ($this->dateHelper === null) {
return $this->getView()->dateFormat();
}
return $this->dateHelper;
}
/**
* Set the view helper that is used to format date/time strings (used for testing)
*
* @param Zend_View_Helper_DateFormat $dateHelper
*/
public function setDateFormatter(Zend_View_Helper_DateFormat $dateHelper)
{
$this->dateHelper = $dateHelper;
}
/**
* Set an alternative array of resources that should be used instead of the DBFactory resource set
* (used for testing)
@ -215,7 +246,7 @@ class GeneralForm extends Form
'name' => 'date_format',
'label' => 'Date Format',
'helptext' => 'Display dates according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getView()->dateFormat()->format(time(), $dateFormatValue),
. 'Example result: ' . $this->getDateFormatter()->format(time(), $dateFormatValue),
'required' => true,
'value' => $dateFormatValue
)
@ -230,7 +261,7 @@ class GeneralForm extends Form
'label' => 'Time Format',
'required' => true,
'helptext' => 'Display times according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getView()->dateFormat()->format(time(), $timeFormatValue),
. 'Example result: ' . $this->getDateFormatter()->format(time(), $timeFormatValue),
'value' => $timeFormatValue
)
);

View File

@ -32,6 +32,7 @@ use \DateTimeZone;
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;
@ -58,6 +59,13 @@ class GeneralForm extends Form
*/
private $preferences;
/**
* The view helper to format date/time strings
*
* @var Zend_View_Helper_DateFormat
*/
private $dateHelper;
/**
* Set the configuration to be used for this form when no preferences are set yet
*
@ -91,6 +99,29 @@ class GeneralForm extends Form
return $this->getRequest()->getUser()->getPreferences();
}
/**
* Return the view helper to format date/time strings
*
* @return Zend_View_Helper_DateFormat
*/
public function getDateFormatter()
{
if ($this->dateHelper === null) {
return $this->getView()->dateFormat();
}
return $this->dateHelper;
}
/**
* Set the view helper that is used to format date/time strings (used for testing)
*
* @param Zend_View_Helper_DateFormat $dateHelper
*/
public function setDateFormatter(Zend_View_Helper_DateFormat $dateHelper)
{
$this->dateHelper = $dateHelper;
}
/**
* Add a select field for setting the user's timezone.
*
@ -170,7 +201,7 @@ class GeneralForm extends Form
'name' => 'date_format',
'label' => 'Preferred Date Format',
'helptext' => 'Display dates according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getView()->dateFormat()->format(time(), $dateFormatValue),
. 'Example result: ' . $this->getDateFormatter()->format(time(), $dateFormatValue),
'required' => !$useGlobalDateFormat,
'value' => $dateFormatValue
)
@ -201,7 +232,7 @@ class GeneralForm extends Form
'label' => 'Preferred Time Format',
'required' => !$useGlobalTimeFormat,
'helptext' => 'Display times according to this format. (See ' . $phpUrl . ' for possible values.) '
. 'Example result: ' . $this->getView()->dateFormat()->format(time(), $timeFormatValue),
. 'Example result: ' . $this->getDateFormatter()->format(time(), $timeFormatValue),
'value' => $timeFormatValue
)
);

View File

@ -8,6 +8,7 @@ use \DateTime;
use \Icinga\Application\Icinga;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Util\DateTimeFactory;
use \Zend_Controller_Request_Http;
/**
* Helper to format date and time. Utilizes DateTimeFactory to ensure time zone awareness
@ -16,9 +17,9 @@ use \Icinga\Util\DateTimeFactory;
*/
class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
{
/**
* Current request
*
* @var Zend_Controller_Request_Abstract
*/
private $request;
@ -26,11 +27,17 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
/**
* Constructor
*
* Retrieve request
* Retrieves the request from the application's front controller if not given.
*
* @param Zend_Controller_Request_Abstract $request The request to use
*/
public function __construct()
public function __construct(Zend_Controller_Request_Abstract $request = null)
{
$this->request = Icinga::app()->getFrontController()->getRequest();
if ($request === null) {
$this->request = Icinga::app()->getFrontController()->getRequest();
} else {
$this->request = $request;
}
}
/**

View File

@ -37,14 +37,21 @@ use Icinga\Test\BaseTestCase;
require_once 'Zend/Form.php';
require_once 'Zend/Config.php';
require_once 'Zend/Config/Ini.php';
require_once 'Zend/View/Helper/Abstract.php';
require_once BaseTestCase::$libDir . '/Web/Form.php';
require_once BaseTestCase::$appDir . '/forms/Config/GeneralForm.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 \DOMDocument;
use \Zend_Config;
use \Zend_View;
use \Zend_View_Helper_DateFormat;
use \Icinga\Util\DateTimeFactory;
class GeneralFormTest extends BaseTestCase
{
@ -71,8 +78,10 @@ class GeneralFormTest extends BaseTestCase
*/
public function testCorrectFieldPopulation()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Config\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setConfiguration(
new Zend_Config(
array(
@ -146,8 +155,10 @@ class GeneralFormTest extends BaseTestCase
public function testCorrectConditionalIniFieldRendering()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Config\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setConfiguration(
new Zend_Config(
array(
@ -182,8 +193,10 @@ class GeneralFormTest extends BaseTestCase
public function testCorrectConditionalDbFieldRendering()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$form = $this->createForm('Icinga\Form\Config\GeneralForm');
$form->setDateFormatter(new Zend_View_Helper_DateFormat($this->getRequest()));
$form->setConfiguration(
new Zend_Config(
array(

View File

@ -38,15 +38,22 @@ use Icinga\Test\BaseTestCase;
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 \Icinga\User\Preferences;
use \Zend_View_Helper_DateFormat;
use \Icinga\Util\DateTimeFactory;
/**
* Test for general form, mainly testing enable/disable behaviour
@ -60,8 +67,10 @@ class GeneralFormTest extends BaseTestCase
*/
public function testDisableFormIfUsingDefault()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$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(
@ -89,8 +98,10 @@ class GeneralFormTest extends BaseTestCase
*/
public function testEnableFormIfUsingPreference()
{
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
$this->requireFormLibraries();
$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(