Skip to content

Commit a2faa0c

Browse files
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
1 parent 0b9f8c8 commit a2faa0c

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

tests/phpunit/tests/date/currentTime.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,33 @@ public function test_should_return_correct_local_time() {
128128
$this->assertEqualsWithDelta( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 2, 'The dates should be equal' );
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
}

tests/phpunit/tests/date/dateI18n.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ public function test_adjusts_format_based_on_timezone_string() {
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' ) ) );
109109
}
110110

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' ) ) );
126+
}
127+
111128
/**
112129
* @ticket 34835
113130
*/

tests/phpunit/tests/date/mysql2date.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ public function test_mysql2date_should_format_time_with_changed_time_zone() {
6262
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
6363
}
6464

65+
/**
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+
6581
/**
6682
* @ticket 28992
6783
*/

tests/phpunit/tests/date/wpTimezone.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public function test_should_return_timezone_string() {
5151
$this->assertSame( 'Europe/Helsinki', $timezone->getName() );
5252
}
5353

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() );
68+
}
69+
5470
/**
5571
* Data provider to test numeric offset conversion.
5672
*

tests/phpunit/tests/formatting/date.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ public function timezone_provider() {
240240
'timezone_string' => '',
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
}
245250
}

0 commit comments

Comments
 (0)