Make WordPress Core

Changeset 45857


Ignore:
Timestamp:
08/20/2019 01:34:37 AM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/date/currentTime.php

    r45856 r45857  
    1717
    1818        date_default_timezone_set( $timezone_string );
    19         $this->assertEquals( gmdate( $format ), current_time( $format, true ) );
    20         $this->assertEquals( $datetime->format( $format ), current_time( $format ) );
     19
     20        $current_time_custom_timezone_gmt = current_time( $format, true );
     21        $current_time_custom_timezone     = current_time( $format );
    2122
    2223        date_default_timezone_set( 'UTC' );
    23         $this->assertEquals( gmdate( $format ), current_time( $format, true ) );
    24         $this->assertEquals( $datetime->format( $format ), current_time( $format ) );
     24
     25        $current_time_gmt = current_time( $format, true );
     26        $current_time     = current_time( $format );
     27
     28        $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_custom_timezone_gmt ), 'The dates should be equal', 2 );
     29        $this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time_custom_timezone ), 'The dates should be equal', 2 );
     30        $this->assertEquals( strtotime( gmdate( $format ) ), strtotime( $current_time_gmt ), 'The dates should be equal', 2 );
     31        $this->assertEquals( strtotime( $datetime->format( $format ) ), strtotime( $current_time ), 'The dates should be equal', 2 );
    2532    }
    2633
     
    3037    public function test_should_return_wp_timestamp() {
    3138        update_option( 'timezone_string', 'Europe/Kiev' );
     39
    3240        $timestamp = time();
    3341        $datetime  = new DateTime( '@' . $timestamp );
     
    3543        $wp_timestamp = $timestamp + $datetime->getOffset();
    3644
    37         $this->assertEquals( $timestamp, current_time( 'timestamp', true ), '', 2 );
    38         $this->assertEquals( $timestamp, current_time( 'U', true ), '', 2 );
    39         $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), '', 2 );
    40         $this->assertEquals( $wp_timestamp, current_time( 'U' ), '', 2 );
     45        $this->assertEquals( $timestamp, current_time( 'timestamp', true ), 'The dates should be equal', 2 );
     46        $this->assertEquals( $timestamp, current_time( 'U', true ), 'The dates should be equal', 2 );
     47
     48        $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), 'The dates should be equal', 2 );
     49        $this->assertEquals( $wp_timestamp, current_time( 'U' ), 'The dates should be equal', 2 );
     50
    4151        $this->assertInternalType( 'int', current_time( 'timestamp' ) );
    4252    }
     
    4757    public function test_should_return_correct_local_time() {
    4858        update_option( 'timezone_string', 'Europe/Kiev' );
     59
    4960        $timestamp      = time();
    5061        $datetime_local = new DateTime( '@' . $timestamp );
     
    5364        $datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) );
    5465
    55         $this->assertEquals( $datetime_local->format( DATE_W3C ), current_time( DATE_W3C ), '', 2 );
    56         $this->assertEquals( $datetime_utc->format( DATE_W3C ), current_time( DATE_W3C, true ), '', 2 );
     66        $this->assertEquals( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 'The dates should be equal', 2 );
     67        $this->assertEquals( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 'The dates should be equal', 2 );
    5768    }
    5869}
Note: See TracChangeset for help on using the changeset viewer.