From e4ec768a77f3b5bd5d15d1020c011b418f35b61e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 19 Aug 2026 12:12:33 +0400 Subject: [PATCH] Tests: Prevent state leaks in functions tests --- tests/phpunit/tests/functions/forceSslAdmin.php | 6 ++++++ .../tests/functions/wpTimezoneChoice.php | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/functions/forceSslAdmin.php b/tests/phpunit/tests/functions/forceSslAdmin.php index e0432c382e959..179c81ae7b2f8 100644 --- a/tests/phpunit/tests/functions/forceSslAdmin.php +++ b/tests/phpunit/tests/functions/forceSslAdmin.php @@ -16,6 +16,12 @@ public function set_up() { force_ssl_admin( false ); } + public function tear_down() { + force_ssl_admin( false ); + + parent::tear_down(); + } + /** * Tests that force_ssl_admin() returns expected values based on various inputs. * diff --git a/tests/phpunit/tests/functions/wpTimezoneChoice.php b/tests/phpunit/tests/functions/wpTimezoneChoice.php index bb2ff0057222b..6fc4e5ddf219b 100644 --- a/tests/phpunit/tests/functions/wpTimezoneChoice.php +++ b/tests/phpunit/tests/functions/wpTimezoneChoice.php @@ -10,10 +10,23 @@ class Tests_Functions_WpTimezoneChoice extends WP_UnitTestCase { /** - * Restores the current locale after each test runs. + * Whether timezone translations need to be restored after the test. + * + * @var bool + */ + private $restore_timezone_translations = false; + + /** + * Restores the current locale and timezone translations after each test runs. */ public function tear_down(): void { restore_current_locale(); + + if ( $this->restore_timezone_translations ) { + // Synchronize the function's static locale tracking with the restored translations. + wp_timezone_choice( '', get_locale() ); + } + parent::tear_down(); } @@ -98,6 +111,7 @@ public function data_wp_timezone_choice_selected(): array { * @param string $expected Expected string HTML fragment. */ public function test_wp_timezone_choice_es( string $expected ): void { + $this->restore_timezone_translations = true; $timezone_list = wp_timezone_choice( '', 'es_ES' ); $this->assertStringContainsString( $expected, $timezone_list ); } @@ -125,6 +139,7 @@ public function data_wp_timezone_choice_es(): array { * @param string $expected Expected string HTML fragment. */ public function test_wp_timezone_choice_es_set( string $expected ): void { + $this->restore_timezone_translations = true; switch_to_locale( 'es_ES' ); $timezone_list = wp_timezone_choice( '' ); $this->assertStringContainsString( $expected, $timezone_list );