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
This commit is contained in:
parent
91f60afbe5
commit
051aa3847c
@ -16,12 +16,19 @@ class Tests_Date_CurrentTime extends WP_UnitTestCase {
|
||||
$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 );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -29,15 +36,18 @@ class Tests_Date_CurrentTime extends WP_UnitTestCase {
|
||||
*/
|
||||
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 @@ class Tests_Date_CurrentTime extends WP_UnitTestCase {
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user