Skip to content
Permalink
Browse files

Date/Time: Fix race conditions in `current_time()` tests.

* Restore default timezone before performing assertions to avoid affecting other tests in case of failure.
* Use delta comparison for timestamps to avoid race conditions.

Props SergeyBiryukov, desrosj.
Fixes #45821.

git-svn-id: https://develop.svn.wordpress.org/trunk@45857 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
SergeyBiryukov committed Aug 20, 2019
1 parent 91f60af commit 051aa3847c4d0fc0916b8755dc4f645f055a8e3c
Showing with 21 additions and 10 deletions.
  1. +21 −10 tests/phpunit/tests/date/currentTime.php
@@ -16,28 +16,38 @@ public function test_should_work_with_changed_timezone() {
$datetime = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
date_default_timezone_set( $timezone_string );
$this->assertEquals( gmdate( $format ), current_time( $format, true ) );
$this->assertEquals( $datetime->format( $format ), current_time( $format ) );
$current_time_custom_timezone_gmt = current_time( $format, true );
$current_time_custom_timezone = current_time( $format );
date_default_timezone_set( 'UTC' );
$this->assertEquals( gmdate( $format ), current_time( $format, true ) );
$this->assertEquals( $datetime->format( $format ), current_time( $format ) );
$current_time_gmt = current_time( $format, true );
$current_time = current_time( $format );
$this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_custom_timezone_gmt ), 'The dates should be equal', 2 );
$this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time_custom_timezone ), 'The dates should be equal', 2 );
$this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_gmt ), 'The dates should be equal', 2 );
$this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time ), 'The dates should be equal', 2 );
}
/**
* @ticket 40653
*/
public function test_should_return_wp_timestamp() {
update_option( 'timezone_string', 'Europe/Kiev' );
$timestamp = time();
$datetime = new DateTime( '@' . $timestamp );
$datetime->setTimezone( wp_timezone() );
$wp_timestamp = $timestamp + $datetime->getOffset();
$this->assertEquals( $timestamp, current_time( 'timestamp', true ), '', 2 );
$this->assertEquals( $timestamp, current_time( 'U', true ), '', 2 );
$this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), '', 2 );
$this->assertEquals( $wp_timestamp, current_time( 'U' ), '', 2 );
$this->assertEquals( $timestamp, current_time( 'timestamp', true ), 'The dates should be equal', 2 );
$this->assertEquals( $timestamp, current_time( 'U', true ), 'The dates should be equal', 2 );
$this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), 'The dates should be equal', 2 );
$this->assertEquals( $wp_timestamp, current_time( 'U' ), 'The dates should be equal', 2 );
$this->assertInternalType( 'int', current_time( 'timestamp' ) );
}
@@ -46,13 +56,14 @@ public function test_should_return_wp_timestamp() {
*/
public function test_should_return_correct_local_time() {
update_option( 'timezone_string', 'Europe/Kiev' );
$timestamp = time();
$datetime_local = new DateTime( '@' . $timestamp );
$datetime_local->setTimezone( wp_timezone() );
$datetime_utc = new DateTime( '@' . $timestamp );
$datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) );
$this->assertEquals( $datetime_local->format( DATE_W3C ), current_time( DATE_W3C ), '', 2 );
$this->assertEquals( $datetime_utc->format( DATE_W3C ), current_time( DATE_W3C, true ), '', 2 );
$this->assertEquals( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 'The dates should be equal', 2 );
$this->assertEquals( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 'The dates should be equal', 2 );
}
}

0 comments on commit 051aa38

Please sign in to comment.
You can’t perform that action at this time.