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: [https://3v4l.org/Holsr#v5.6.20 timezone_identifiers_list() output for PHP 5.6.20].

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

Props jrf, costdev.
See #56468.

git-svn-id: https://develop.svn.wordpress.org/trunk@54230 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-09-19 23:51:20 +00:00
parent 0b9f8c8f7b
commit a2faa0c897
5 changed files with 83 additions and 0 deletions

View File

@ -128,4 +128,33 @@ class Tests_Date_CurrentTime extends WP_UnitTestCase {
$this->assertEqualsWithDelta( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 2, 'The dates should be equal' );
}
/**
* Ensures that deprecated timezone strings are handled correctly.
*
* @ticket 56468
*/
public function test_should_work_with_deprecated_timezone() {
$format = 'Y-m-d H:i';
$timezone_string = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6.
update_option( 'timezone_string', $timezone_string );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
date_default_timezone_set( $timezone_string );
$current_time_custom_timezone_gmt = current_time( $format, true );
$current_time_custom_timezone = current_time( $format );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
date_default_timezone_set( 'UTC' );
$current_time_gmt = current_time( $format, true );
$current_time = current_time( $format );
$this->assertSame( gmdate( $format ), $current_time_custom_timezone_gmt, 'The dates should be equal [1]' );
$this->assertSame( $datetime->format( $format ), $current_time_custom_timezone, 'The dates should be equal [2]' );
$this->assertSame( gmdate( $format ), $current_time_gmt, 'The dates should be equal [3]' );
$this->assertSame( $datetime->format( $format ), $current_time, 'The dates should be equal [4]' );
}
}

View File

@ -108,6 +108,23 @@ class Tests_Date_DateI18n extends WP_UnitTestCase {
$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' ) ) );
}
/**
* Ensures that deprecated timezone strings are handled correctly.
*
* @ticket 56468
*/
public function test_adjusts_format_based_on_deprecated_timezone_string() {
update_option( 'timezone_string', 'America/Buenos_Aires' ); // This timezone was deprecated pre-PHP 5.6.
$expected = '2022-08-01 00:00:00 -03 -03:00 America/Buenos_Aires';
if ( PHP_VERSION_ID < 70000 ) {
// PHP 5.6.
$expected = '2022-08-01 00:00:00 ART -03:00 America/Buenos_Aires';
}
$this->assertSame( $expected, date_i18n( 'Y-m-d H:i:s T P e', strtotime( '2022-08-01 00:00:00' ) ) );
}
/**
* @ticket 34835
*/

View File

@ -62,6 +62,22 @@ class Tests_Date_mysql2date extends WP_UnitTestCase {
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
}
/**
* Ensures that deprecated timezone strings are handled correctly.
*
* @ticket 56468
*/
public function test_mysql2date_should_format_time_with_deprecated_time_zone() {
$timezone = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6.
update_option( 'timezone_string', $timezone );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
$rfc3339 = $datetime->format( DATE_RFC3339 );
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
}
/**
* @ticket 28992
*/

View File

@ -51,6 +51,22 @@ class Tests_Date_wpTimezone extends WP_UnitTestCase {
$this->assertSame( 'Europe/Helsinki', $timezone->getName() );
}
/**
* Ensures that deprecated timezone strings are handled correctly.
*
* @ticket 56468
*/
public function test_should_return_deprecated_timezone_string() {
$tz_string = 'America/Buenos_Aires'; // This timezone was deprecated pre-PHP 5.6.
update_option( 'timezone_string', $tz_string );
$this->assertSame( $tz_string, wp_timezone_string() );
$timezone = wp_timezone();
$this->assertSame( $tz_string, $timezone->getName() );
}
/**
* Data provider to test numeric offset conversion.
*

View File

@ -240,6 +240,11 @@ class Tests_Formatting_Date extends WP_UnitTestCase {
'timezone_string' => '',
'gmt_offset' => 3,
),
// @ticket 56468.
'deprecated timezone string and no GMT offset set' => array(
'timezone_string' => 'America/Buenos_Aires',
'gmt_offset' => 0,
),
);
}
}