Make WordPress Core

Changeset 54230


Ignore:
Timestamp:
09/19/2022 11:51:20 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Add tests with deprecated timezone strings.

This commit adds tests in select places to ensure that these date/time related functions continue to behave as expected when the timezone_string option is set to an outdated/deprecated timezone name.

The timezone string used in these tests, America/Buenos_Aires, is a timezone string which was already deprecated in PHP 5.6.20 (the current minimum PHP version), so using this timezone string, we can safely test the handling of deprecated timezone names on all supported PHP versions.

See: timezone_identifiers_list() output for PHP 5.6.20.

Follow-up to [54207], [54217], [54227], [54229].

Props jrf, costdev.
See #56468.

Location:
trunk/tests/phpunit/tests
Files:
5 edited

Legend:

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

    r54217 r54230  
    129129        $this->assertEqualsWithDelta( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 2, 'The dates should be equal' );
    130130    }
     131
     132    /**
     133     * Ensures that deprecated timezone strings are handled correctly.
     134     *
     135     * @ticket 56468
     136     */
     137    public function test_should_work_with_deprecated_timezone() {
     138        $format          = 'Y-m-d H:i';
     139        $timezone_string = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6.
     140        update_option( 'timezone_string', $timezone_string );
     141        $datetime = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
     142
     143        // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
     144        date_default_timezone_set( $timezone_string );
     145
     146        $current_time_custom_timezone_gmt = current_time( $format, true );
     147        $current_time_custom_timezone     = current_time( $format );
     148
     149        // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
     150        date_default_timezone_set( 'UTC' );
     151
     152        $current_time_gmt = current_time( $format, true );
     153        $current_time     = current_time( $format );
     154
     155        $this->assertSame( gmdate( $format ), $current_time_custom_timezone_gmt, 'The dates should be equal [1]' );
     156        $this->assertSame( $datetime->format( $format ), $current_time_custom_timezone, 'The dates should be equal [2]' );
     157        $this->assertSame( gmdate( $format ), $current_time_gmt, 'The dates should be equal [3]' );
     158        $this->assertSame( $datetime->format( $format ), $current_time, 'The dates should be equal [4]' );
     159    }
    131160}
  • trunk/tests/phpunit/tests/date/dateI18n.php

    r54217 r54230  
    107107
    108108        $this->assertSame( '2012-12-01 00:00:00 CST -06:00 America/Regina', date_i18n( 'Y-m-d H:i:s T P e', strtotime( '2012-12-01 00:00:00' ) ) );
     109    }
     110
     111    /**
     112     * Ensures that deprecated timezone strings are handled correctly.
     113     *
     114     * @ticket 56468
     115     */
     116    public function test_adjusts_format_based_on_deprecated_timezone_string() {
     117        update_option( 'timezone_string', 'America/Buenos_Aires' ); // This timezone was deprecated pre-PHP 5.6.
     118
     119        $expected = '2022-08-01 00:00:00 -03 -03:00 America/Buenos_Aires';
     120        if ( PHP_VERSION_ID < 70000 ) {
     121            // PHP 5.6.
     122            $expected = '2022-08-01 00:00:00 ART -03:00 America/Buenos_Aires';
     123        }
     124
     125        $this->assertSame( $expected, date_i18n( 'Y-m-d H:i:s T P e', strtotime( '2022-08-01 00:00:00' ) ) );
    109126    }
    110127
  • trunk/tests/phpunit/tests/date/mysql2date.php

    r54217 r54230  
    6464
    6565    /**
     66     * Ensures that deprecated timezone strings are handled correctly.
     67     *
     68     * @ticket 56468
     69     */
     70    public function test_mysql2date_should_format_time_with_deprecated_time_zone() {
     71        $timezone = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6.
     72        update_option( 'timezone_string', $timezone );
     73        $datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
     74        $rfc3339  = $datetime->format( DATE_RFC3339 );
     75        $mysql    = $datetime->format( 'Y-m-d H:i:s' );
     76
     77        $this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
     78        $this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
     79    }
     80
     81    /**
    6682     * @ticket 28992
    6783     */
  • trunk/tests/phpunit/tests/date/wpTimezone.php

    r54217 r54230  
    5050
    5151        $this->assertSame( 'Europe/Helsinki', $timezone->getName() );
     52    }
     53
     54    /**
     55     * Ensures that deprecated timezone strings are handled correctly.
     56     *
     57     * @ticket 56468
     58     */
     59    public function test_should_return_deprecated_timezone_string() {
     60        $tz_string = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6.
     61        update_option( 'timezone_string', $tz_string );
     62
     63        $this->assertSame( $tz_string, wp_timezone_string() );
     64
     65        $timezone = wp_timezone();
     66
     67        $this->assertSame( $tz_string, $timezone->getName() );
    5268    }
    5369
  • trunk/tests/phpunit/tests/formatting/date.php

    r54217 r54230  
    241241                'gmt_offset'      => 3,
    242242            ),
     243            // @ticket 56468.
     244            'deprecated timezone string and no GMT offset set' => array(
     245                'timezone_string' => 'America/Buenos_Aires',
     246                'gmt_offset'      => 0,
     247            ),
    243248        );
    244249    }
Note: See TracChangeset for help on using the changeset viewer.