egroupware/api/src/test/DateTimeTest.php

121 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
* EGroupware Api: DateTime tests
*
* @link http://www.stylite.de
* @package api
* @author Nathan Gray
* @copyright (c) 2017 Nathan Gray
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
*/
namespace EGroupware\Api;
require_once realpath(__DIR__.'/../loader/common.php'); // autoloader & check_load_extension
use EGroupware\Api;
use PHPUnit_Framework_TestCase as TestCase;
/**
* Testing the Egroupware extension of DateTime
*
*/
class DateTimeTest extends TestCase {
static $usertime;
/**
* Work in server time, so tests match expectations
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
static::$usertime = DateTime::$user_timezone;
// Set user time to server time for consistency
DateTime::setUserPrefs(date_default_timezone_get());
}
public static function tearDownAfterClass()
{
// Reset
DateTime::setUserPrefs(static::$usertime->getName());
}
/**
* Test that dates before 1970 are handled correctly
*/
public function testBefore1970()
{
$checklist = array(
// Use sub array instead of key/value pair to preserve type
// Timestamp Expected
array('19690811', '1969-08-11 00:00:00'),
array(-3600, '1969-12-31 23:00:00'),
array('-119322000', '1966-03-21 23:00:00')
);
foreach($checklist as $test)
{
list($ts, $expected) = $test;
$this->checkTimestamp($ts, $expected);
}
}
/**
* Check timezone conversion is sane - convert back and forth between same
* timezone
*/
public function testUserTimeEqualsServerTime()
{
$time = time();
$this->assertEquals($time, DateTime::server2user($time, 'ts'));
$this->assertEquals(date('Y-m-d H:i:s',$time), DateTime::server2user($time,'Y-m-d H:i:s'));
$this->assertEquals('2009-10-20, 12:00',DateTime::to(array('full' => '20091020', 'hour' => 12, 'minute' => 0)));
$ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals(DateTime::user2server($ts,''), DateTime::server2user(DateTime::user2server($ts),''));
$ts = DateTime::to(array('full' => '20090627', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals(DateTime::user2server($ts,''), DateTime::server2user(DateTime::user2server($ts),''));
}
/**
* Test timezone conversion with actual changes
*/
public function testTimezoneConversion()
{
// Set server to UTC
$server_tz = date_default_timezone_get();
date_default_timezone_set('UTC');
// Set user to Berlin (UTC-1)
DateTime::setUserPrefs('Europe/Berlin');
$ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals('2009-10-27 09:00:00', DateTime::user2server($ts,'Y-m-d H:i:s'));
// Set user to Cape Verde (UTC+1)
DateTime::setUserPrefs('Atlantic/Cape_Verde');
$ts = DateTime::to(array('full' => '20091027', 'hour' => 10, 'minute' => 0),'ts');
$this->assertEquals('2009-10-27 11:00:00', DateTime::user2server($ts,'Y-m-d H:i:s'));
date_default_timezone_set($server_tz);
}
/**
* Check that a timestamp matches expectations
*
* @param string|int $ts Something that looks like a time
* @param string $expected Expected time, in Y-m-d H:i:s format
*/
protected function checkTimestamp($ts, $expected)
{
$fail_message = "Checking $ts = $expected";
$this->assertEquals($expected, DateTime::to($ts,'Y-m-d H:i:s'), $fail_message);
$dt = new DateTime($ts);
$this->assertEquals($expected, $dt->format('Y-m-d H:i:s'), $fail_message);
}
}