Testing max & min on date widget

This commit is contained in:
nathangray 2017-08-28 11:44:08 -06:00
parent 8fd6325c3d
commit 495d67d5d0
2 changed files with 195 additions and 8 deletions

View File

@ -164,7 +164,15 @@ class Date extends Transformer
}
if($value)
{
$date = new Api\DateTime($value);
try
{
$date = new Api\DateTime($value);
}
catch(\Exception $e)
{
$date = null;
$value = '';
}
}
if (!empty($this->attrs['min']))
{

View File

@ -59,12 +59,12 @@ class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest
*/
public function testBasic($content, $expected)
{
// Instanciate the template\
$etemplate = new Etemplate();
$etemplate->read(static::TEST_TEMPLATE, 'test');
// Instanciate the template
$etemplate = new Etemplate();
$etemplate->read(static::TEST_TEMPLATE, 'test');
$result = $this->mockedRoundTrip($etemplate, $content);
$this->validateTest($result, $expected ? $expected : $content);
$result = $this->mockedRoundTrip($etemplate, $content);
$this->validateTest($result, $expected ? $expected : $content);
}
public function basicProvider()
@ -79,14 +79,193 @@ class DateTest extends \EGroupware\Api\Etemplate\WidgetBaseTest
$data = array(
array(
array('date' => $today->getTimestamp(), 'date_time' => $today->getTimestamp()),
false
false // Expect what went in
),
array(
// Timestamp in a date field gets adjusted to day start, timeonly is epoch
// 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;
}
/**
* Check some basic validation stuff
*
* @param type $content
* @param type $validation_errors
*
* @dataProvider validationProvider
*/
public function testValidation($content)
{
// Instanciate the template
$etemplate = new Etemplate();
$etemplate->read(static::TEST_TEMPLATE, 'test');
$this->validateRoundTrip($etemplate, Array(), $content, Array(), array_flip(array_keys($content)));
}
public function validationProvider()
{
// All these are invalid, and should not give a value back
return array(
array(array('date' => 'Invalid')),
array(array('date_time' => 'Invalid')),
array(array('date_timeonly' => 'Invalid')),
array(array('date' => -1000)),
array(array('date_time' => -1000)),
array(array('date_timeonly' => -1000)),
);
}
/**
* Test for minimum attribute
*
* @param String|numeric $value
* @param float $min Minimum allowed value
* @param boolean $error
*
* @dataProvider minProvider
*/
public function testMin($value, $min, $error)
{
// Instanciate the template
$etemplate = new Etemplate();
$etemplate->read(static::TEST_TEMPLATE, 'test');
// Content - doesn't really matter, we're changing it
$content = array();
// Need to exec the template so the widget is there to modify
$result = $this->mockedExec($etemplate, $content, array(), array(), array());
// Set limit
$etemplate->getElementById('date')->attrs['min'] = $min;
// Check for the load
$data = array();
foreach($result as $command)
{
if($command['type'] == 'et2_load')
{
$data = $command['data'];
break;
}
}
// 'Edit' the data client side
$data['data']['content'] = array('date' => $value);
// Let it validate
Etemplate::ajax_process_content($data['data']['etemplate_exec_id'], $data['data']['content'], false);
$content = static::$mocked_exec_result;
static::$mocked_exec_result = array();
return $this->validateTest($content,
$error ? array() : array('date' => is_string($value) ? strtotime($value) : $value),
$error ? array('date' => $error) : array()
);
}
public function minProvider()
{
return Array(
// User value, Min, Error
array('', 0, FALSE),
array('2018-01-01', '2017-12-31', FALSE),
array('2018-01-01', '2018-01-01', FALSE),
array('2017-12-01', '2017-12-31', TRUE),
// Relative days
array('two days from now', 2, FALSE),
array(time(), 2, TRUE),
array(time(), -1, FALSE),
array('yesterday', 0, TRUE),
// Different periods
array('yesterday', '+2d', TRUE),
array('yesterday', '-2d', FALSE),
array('yesterday', '-1m', FALSE),
array('yesterday', '-1y +1m', FALSE),
array(time(), '+1d', TRUE),
array(time(), '+1m', TRUE),
array(time(), '+1y -1m', TRUE),
);
}
/**
* Test for maximum attribute
*
* @param String|numeric $value
* @param float $max Maximum allowed value
* @param boolean $error
*
* @dataProvider maxProvider
*/
public function testMax($value, $max, $error)
{
// Instanciate the template
$etemplate = new Etemplate();
$etemplate->read(static::TEST_TEMPLATE, 'test');
// Content - doesn't really matter, we're changing it
$content = array();
// Need to exec the template so the widget is there to modify
$result = $this->mockedExec($etemplate, $content, array(), array(), array());
// Set limit
$etemplate->getElementById('date')->attrs['min'] = $max;
// Check for the load
$data = array();
foreach($result as $command)
{
if($command['type'] == 'et2_load')
{
$data = $command['data'];
break;
}
}
// 'Edit' the data client side
$data['data']['content'] = array('date' => $value);
// Let it validate
Etemplate::ajax_process_content($data['data']['etemplate_exec_id'], $data['data']['content'], false);
$content = static::$mocked_exec_result;
static::$mocked_exec_result = array();
return $this->validateTest($content,
$error ? array() : array('date' => is_string($value) ? strtotime($value) : $value),
$error ? array('date' => $error) : array()
);
}
public function maxProvider()
{
return Array(
// User value, Max, Error
array('', 0, FALSE),
array('2017-12-31', '2018-01-01', FALSE),
array('2018-01-01', '2018-01-01', FALSE),
array('2017-12-31', '2017-12-01', TRUE),
// Relative days
array('two days from now', 2, FALSE),
array(time(), 2, FALSE),
array(time(), -1, TRUE),
array('yesterday', 0, FALSE),
// Different periods
array('yesterday', '+2d', FALSE),
array('yesterday', '-2d', TRUE),
array('yesterday', '+1m', FALSE),
array('yesterday', '+1y -1m', FALSE),
array(time(), '-1d', TRUE),
array(time(), '-1m', TRUE),
array(time(), '-1y -1m', TRUE),
);
}
}