Etemplate simple date widget tests

This commit is contained in:
nathangray 2017-08-24 14:28:31 -06:00
parent 1128f771b5
commit 61dc75621d
3 changed files with 135 additions and 5 deletions

View File

@ -0,0 +1,92 @@
<?php
/**
* Tests for Date widget
*
* @link http://www.egroupware.org
* @author Nathan Gray
* @package api
* @subpackage etemplate
* @copyright (c) 2017 Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
namespace EGroupware\Api\Etemplate\Widget;
require_once realpath(__DIR__.'/../../test/WidgetBaseTest.php');
use EGroupware\Api\Etemplate;
use EGroupware\Api\DateTime;
class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest
{
const TEST_TEMPLATE = 'api.date_test';
protected static $usertime;
protected static $server_tz;
/**
* Work in server time, so tests match expectations
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
static::$usertime = DateTime::$user_timezone;
static::$server_tz = date_default_timezone_get();
// Set time to UTC time for consistency
DateTime::setUserPrefs('UTC');
date_default_timezone_set('UTC');
DateTime::$server_timezone = new \DateTimeZone('UTC');
}
public static function tearDownAfterClass()
{
// Reset
DateTime::setUserPrefs(static::$usertime->getName());
date_default_timezone_set(static::$server_tz);
unset($GLOBALS['egw']);
parent::tearDownAfterClass();
}
/**
* Test the widget's basic functionality - we put data in, it comes back
* unchanged.
*
* @dataProvider basicProvider
*/
public function testBasic($content, $expected)
{
// Instanciate the template\
$etemplate = new Etemplate();
$etemplate->read(static::TEST_TEMPLATE, 'test');
$result = $this->mockedRoundTrip($etemplate, $content);
$this->validateTest($result, $expected ? $expected : $content);
}
public function basicProvider()
{
$now = new DateTime(time());
$now->setTime(22, 13, 20); // Just because 80000 seconds after epoch is 22:13:20
$today = clone $now;
$today->setTime(0,0);
$time = new DateTime(80000); // 22:13:20
$data = array(
array(
array('date' => $today->getTimestamp(), 'date_time' => $today->getTimestamp()),
false
),
array(
// Timestamp in a date field gets adjusted to day start, timeonly is epoch
array('date' => $now->getTimestamp(), 'date_time' => $now->getTimestamp(), 'date_timeonly' => $now->getTimestamp()),
array('date' => $now->getTimestamp(), 'date_time' => $now->getTimestamp(), 'date_timeonly' => $time->getTimestamp())
)
);
return $data;
}
}

View File

@ -22,7 +22,7 @@ require_once realpath(__DIR__.'/../../test/LoggedInTest.php');
/**
* Base class for all widget tests doing needed setup so the tests can run, and
* providing common utilities.
* providing common utilities to make testing a little easier.
*
* Widget scans the apps for widgets, which needs the app list, pulled from the
* database, so we need to log in.
@ -98,6 +98,19 @@ abstract class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
return $result;
}
/**
* Mocks what is needed to fake a call to Etemplate->exec(), and catch its output.
* The resulting array of information, which would normally be sent to the
* client as JSON, is then processed and validated by the server as if it had
* been sent from the client.
*
* @param \EGroupware\Api\Etemplate $etemplate
* @param array $content
* @param array $sel_options
* @param array $readonlys
* @param array $preserv
* @return type
*/
protected function mockedRoundTrip(\EGroupware\Api\Etemplate $etemplate, array $content,array $sel_options=null,array $readonlys=null,array $preserv=null)
{
@ -148,13 +161,17 @@ abstract class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
/**
* Exec the template with the provided content, change the values according to
* $set_values, then validate against $expected_values
* $set_values to simulate client side changes by the user, then validate
* against $expected_values. Optionally, it can check that validation errors
* are created by particular widgets.
*
*
* @param \EGroupware\Api\Etemplate $etemplate
* @param array $content
* @param array $set_values
* @param array $expected_values
* @param array $validation_errors
* @param array $validation_errors Indexed by widget ID, we just check that an error
* was found, not what that error was.
*/
protected function validateRoundTrip(\EGroupware\Api\Etemplate $etemplate, Array $content, Array $set_values, Array $expected_values = null, Array $validation_errors = array())
{
@ -187,7 +204,18 @@ abstract class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
return $this->validateTest($content, $expected_values, $validation_errors);
}
protected function validateTest($content, $expected_values, $validation_errors)
/**
* Test that the content matches expected_values, and any widgets listed in
* $validation_errors actually did raise a validation error.
*
* Note that in most (all?) cases, a validation error will clear the value.
*
* @param array $content
* @param array $expected_values
* @param array $validation_errors
*/
protected function validateTest(Array $content, Array $expected_values, Array $validation_errors = array())
{
// Make validation errors accessible
$ref = new \ReflectionProperty('\\EGroupware\\Api\\Etemplate\\Widget', 'validation_errors');
@ -203,7 +231,7 @@ abstract class WidgetBaseTest extends \EGroupware\Api\LoggedInTest {
// Check validation errors
foreach($validation_errors as $widget_id => $errored)
{
$this->assertTrue(array_key_exists($widget_id, $validation_errors), "Widget $widget_id caused a validation error");
$this->assertTrue(array_key_exists($widget_id, $validation_errors), "Widget $widget_id did not cause a validation error");
}
$ref->setValue(array());
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE overlay PUBLIC "-//EGroupware GmbH//eTemplate 2//EN" "http://www.egroupware.org/etemplate2.dtd">
<!-- This template is used in automated testing -->
<overlay>
<template id="api.date_test" template="" lang="" group="0" version="16.1">
<date id="date"/>
<date-time id="date_time"/>
<date-timeonly id="date_timeonly"/>
</template>
</overlay>