Skip to content

Commit

Permalink
BUGFIX: Use UTC consistently across all tests for date/time calculations
Browse files Browse the repository at this point in the history
This ensures that tests will not pass or fail based on whether the test
machine is on NZ time.

This partially reverts df050ed, which has
already been merged. Instead of finding tests that use date calculations, we
are now setting the default time zone in SapphireTest so it will apply to the
whole test suite and any future tests.

Adjust expected values in certain tests for UTC, where the expected values had
previously been expressed in NZ time.

When creating a temp DB for test fixtures, create the DB connection with
timezone UTC.
  • Loading branch information
oddnoc committed Mar 27, 2012
1 parent 82bb12b commit 5954774
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
11 changes: 10 additions & 1 deletion dev/SapphireTest.php
Expand Up @@ -143,6 +143,9 @@ function setUp() {
i18n::set_date_format(null);
i18n::set_time_format(null);

// Set default timezone consistently to avoid NZ-specific dependencies
date_default_timezone_set('UTC');

// Remove password validation
$this->originalMemberPasswordValidator = Member::password_validator();
$this->originalRequirements = Requirements::backend();
Expand Down Expand Up @@ -272,6 +275,9 @@ function setUpOnce() {
// which is used in DatabaseAdmin->doBuild()
global $_SINGLETONS;
$_SINGLETONS = array();

// Set default timezone consistently to avoid NZ-specific dependencies
date_default_timezone_set('UTC');
}

/**
Expand Down Expand Up @@ -765,7 +771,10 @@ static function create_temp_db() {
// Disable PHPUnit error handling
restore_error_handler();

// Create a temporary database
// Create a temporary database, and force the connection to use UTC for time
global $databaseConfig;
$databaseConfig['timezone'] = '+0:00';
DB::connect($databaseConfig);
$dbConn = DB::getConn();
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000,9999999);
Expand Down
16 changes: 8 additions & 8 deletions tests/model/DateTest.php
Expand Up @@ -32,16 +32,16 @@ function tearDown() {
}

function testNiceDate() {
$this->assertEquals('01/04/2008', DBField::create('Date', 1206968400)->Nice(),
$this->assertEquals('31/03/2008', DBField::create('Date', 1206968400)->Nice(),
"Date->Nice() works with timestamp integers"
);
$this->assertEquals('31/03/2008', DBField::create('Date', 1206882000)->Nice(),
$this->assertEquals('30/03/2008', DBField::create('Date', 1206882000)->Nice(),
"Date->Nice() works with timestamp integers"
);
$this->assertEquals('01/04/2008', DBField::create('Date', '1206968400')->Nice(),
$this->assertEquals('31/03/2008', DBField::create('Date', '1206968400')->Nice(),
"Date->Nice() works with timestamp strings"
);
$this->assertEquals('31/03/2008', DBField::create('Date', '1206882000')->Nice(),
$this->assertEquals('30/03/2008', DBField::create('Date', '1206882000')->Nice(),
"Date->Nice() works with timestamp strings"
);
$this->assertEquals('04/03/2003', DBField::create('Date', '4/3/03')->Nice(),
Expand Down Expand Up @@ -74,16 +74,16 @@ function testNiceDate() {
}

function testLongDate() {
$this->assertEquals('1 April 2008', DBField::create('Date', 1206968400)->Long(),
$this->assertEquals('31 March 2008', DBField::create('Date', 1206968400)->Long(),
"Date->Long() works with numeric timestamp"
);
$this->assertEquals('1 April 2008', DBField::create('Date', '1206968400')->Long(),
$this->assertEquals('31 March 2008', DBField::create('Date', '1206968400')->Long(),
"Date->Long() works with string timestamp"
);
$this->assertEquals('31 March 2008', DBField::create('Date', 1206882000)->Long(),
$this->assertEquals('30 March 2008', DBField::create('Date', 1206882000)->Long(),
"Date->Long() works with numeric timestamp"
);
$this->assertEquals('31 March 2008', DBField::create('Date', '1206882000')->Long(),
$this->assertEquals('30 March 2008', DBField::create('Date', '1206882000')->Long(),
"Date->Long() works with numeric timestamp"
);
$this->assertEquals('3 April 2003', DBField::create('Date', '2003-4-3')->Long(),
Expand Down
2 changes: 0 additions & 2 deletions tests/model/DatetimeTest.php
Expand Up @@ -35,8 +35,6 @@ function testNowWithMockDate() {
}

function testSetNullAndZeroValues() {
date_default_timezone_set('UTC');

$date = DBField::create('SS_Datetime', '');
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL');

Expand Down

0 comments on commit 5954774

Please sign in to comment.