From 051aa3847c4d0fc0916b8755dc4f645f055a8e3c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 20 Aug 2019 01:34:37 +0000 Subject: [PATCH] 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 --- tests/phpunit/tests/date/currentTime.php | 31 ++++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/date/currentTime.php b/tests/phpunit/tests/date/currentTime.php index 7cc88dbe3e..8a38574d98 100644 --- a/tests/phpunit/tests/date/currentTime.php +++ b/tests/phpunit/tests/date/currentTime.php @@ -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 ); } }