From a2faa0c8970fca80c73ab76d2d3a77117f8c0eaa Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 19 Sep 2022 23:51:20 +0000 Subject: [PATCH] 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 --- tests/phpunit/tests/date/currentTime.php | 29 ++++++++++++++++++++++++ tests/phpunit/tests/date/dateI18n.php | 17 ++++++++++++++ tests/phpunit/tests/date/mysql2date.php | 16 +++++++++++++ tests/phpunit/tests/date/wpTimezone.php | 16 +++++++++++++ tests/phpunit/tests/formatting/date.php | 5 ++++ 5 files changed, 83 insertions(+) diff --git a/tests/phpunit/tests/date/currentTime.php b/tests/phpunit/tests/date/currentTime.php index 0c4ff698d1..66c2fdf3c5 100644 --- a/tests/phpunit/tests/date/currentTime.php +++ b/tests/phpunit/tests/date/currentTime.php @@ -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]' ); + } } diff --git a/tests/phpunit/tests/date/dateI18n.php b/tests/phpunit/tests/date/dateI18n.php index b52f43e0e2..dfc98dd369 100644 --- a/tests/phpunit/tests/date/dateI18n.php +++ b/tests/phpunit/tests/date/dateI18n.php @@ -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 */ diff --git a/tests/phpunit/tests/date/mysql2date.php b/tests/phpunit/tests/date/mysql2date.php index e8418467c7..2ae4939c56 100644 --- a/tests/phpunit/tests/date/mysql2date.php +++ b/tests/phpunit/tests/date/mysql2date.php @@ -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 */ diff --git a/tests/phpunit/tests/date/wpTimezone.php b/tests/phpunit/tests/date/wpTimezone.php index e48a1b03b5..3e8b6fef5f 100644 --- a/tests/phpunit/tests/date/wpTimezone.php +++ b/tests/phpunit/tests/date/wpTimezone.php @@ -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. * diff --git a/tests/phpunit/tests/formatting/date.php b/tests/phpunit/tests/formatting/date.php index d3e0971789..684cf86d97 100644 --- a/tests/phpunit/tests/formatting/date.php +++ b/tests/phpunit/tests/formatting/date.php @@ -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, + ), ); } }