Skip to content
This repository
Browse code

BUGFIX: Use UTC consistently across all tests for date/time calculations

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...
commit 59547745303bda7d08171ebca089e0de38a20007 1 parent 82bb12b
Fred Condo authored March 11, 2012
11  dev/SapphireTest.php
@@ -143,6 +143,9 @@ function setUp() {
143 143
 		i18n::set_date_format(null);
144 144
 		i18n::set_time_format(null);
145 145
 		
  146
+		// Set default timezone consistently to avoid NZ-specific dependencies
  147
+		date_default_timezone_set('UTC');
  148
+		
146 149
 		// Remove password validation
147 150
 		$this->originalMemberPasswordValidator = Member::password_validator();
148 151
 		$this->originalRequirements = Requirements::backend();
@@ -272,6 +275,9 @@ function setUpOnce() {
272 275
 		// which is used in DatabaseAdmin->doBuild()
273 276
 		global $_SINGLETONS;
274 277
 		$_SINGLETONS = array();
  278
+
  279
+		// Set default timezone consistently to avoid NZ-specific dependencies
  280
+		date_default_timezone_set('UTC');
275 281
 	}
276 282
 	
277 283
 	/**
@@ -765,7 +771,10 @@ static function create_temp_db() {
765 771
 		// Disable PHPUnit error handling
766 772
 		restore_error_handler();
767 773
 		
768  
-		// Create a temporary database
  774
+		// Create a temporary database, and force the connection to use UTC for time
  775
+		global $databaseConfig;
  776
+		$databaseConfig['timezone'] = '+0:00';
  777
+		DB::connect($databaseConfig);
769 778
 		$dbConn = DB::getConn();
770 779
 		$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
771 780
 		$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000,9999999);
16  tests/model/DateTest.php
@@ -32,16 +32,16 @@ function tearDown() {
32 32
 	}
33 33
 	
34 34
 	function testNiceDate() {
35  
-		$this->assertEquals('01/04/2008', DBField::create('Date', 1206968400)->Nice(),
  35
+		$this->assertEquals('31/03/2008', DBField::create('Date', 1206968400)->Nice(),
36 36
 			"Date->Nice() works with timestamp integers"
37 37
 		);
38  
-		$this->assertEquals('31/03/2008', DBField::create('Date', 1206882000)->Nice(),
  38
+		$this->assertEquals('30/03/2008', DBField::create('Date', 1206882000)->Nice(),
39 39
 			"Date->Nice() works with timestamp integers"
40 40
 		);
41  
-		$this->assertEquals('01/04/2008', DBField::create('Date', '1206968400')->Nice(),
  41
+		$this->assertEquals('31/03/2008', DBField::create('Date', '1206968400')->Nice(),
42 42
 			"Date->Nice() works with timestamp strings"
43 43
 		);
44  
-		$this->assertEquals('31/03/2008', DBField::create('Date', '1206882000')->Nice(),
  44
+		$this->assertEquals('30/03/2008', DBField::create('Date', '1206882000')->Nice(),
45 45
 			"Date->Nice() works with timestamp strings"
46 46
 		);
47 47
 		$this->assertEquals('04/03/2003', DBField::create('Date', '4/3/03')->Nice(),
@@ -74,16 +74,16 @@ function testNiceDate() {
74 74
 	}
75 75
 	
76 76
 	function testLongDate() {
77  
-		$this->assertEquals('1 April 2008', DBField::create('Date', 1206968400)->Long(),
  77
+		$this->assertEquals('31 March 2008', DBField::create('Date', 1206968400)->Long(),
78 78
 			"Date->Long() works with numeric timestamp"
79 79
 		);
80  
-		$this->assertEquals('1 April 2008', DBField::create('Date', '1206968400')->Long(),
  80
+		$this->assertEquals('31 March 2008', DBField::create('Date', '1206968400')->Long(),
81 81
 			"Date->Long() works with string timestamp"
82 82
 		);
83  
-		$this->assertEquals('31 March 2008', DBField::create('Date', 1206882000)->Long(),
  83
+		$this->assertEquals('30 March 2008', DBField::create('Date', 1206882000)->Long(),
84 84
 			"Date->Long() works with numeric timestamp"
85 85
 		);
86  
-		$this->assertEquals('31 March 2008', DBField::create('Date', '1206882000')->Long(),
  86
+		$this->assertEquals('30 March 2008', DBField::create('Date', '1206882000')->Long(),
87 87
 			"Date->Long() works with numeric timestamp"
88 88
 		);
89 89
 		$this->assertEquals('3 April 2003', DBField::create('Date', '2003-4-3')->Long(),
2  tests/model/DatetimeTest.php
@@ -35,8 +35,6 @@ function testNowWithMockDate() {
35 35
 	}
36 36
 
37 37
 	function testSetNullAndZeroValues() {
38  
-		date_default_timezone_set('UTC');
39  
-
40 38
 		$date = DBField::create('SS_Datetime', '');
41 39
 		$this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
42 40
 

0 notes on commit 5954774

Please sign in to comment.
Something went wrong with that request. Please try again.