From 7f08fac29960be57e943c2f982026639d9e98f0f Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 31 Aug 2022 16:02:05 +0000 Subject: [PATCH] Tests: Correctly back up and restore theme directories in `Tests_Theme`. In the `set_up()` method, a copy would be made of the original value of the global `$wp_theme_directories` variable, with the intention to restore that original value in the `tear_down()` method after running each test. Unfortunately, this was not implemented correctly. * The backup is made to a function local variable in `set_up()` and not stored somewhere where it is accessible from the `tear_down()` method. * The restoring then references a non-existent property to restore, which would effectively set the `$wp_theme_directories` global to `null`. Fixed by declaring and using a private property to store the original `$wp_theme_directories` value. This bug was discovered while fixing (removing) the magic methods in the `WP_UnitTestCase_Base` class in an effort to improve compatibility with PHP 8.2. Follow-up to [38907]. Props jrf, costdev, johnbillion, swissspidy. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@54040 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/theme.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index f6a418d3d3..c7742536a4 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -23,13 +23,20 @@ class Tests_Theme extends WP_UnitTestCase { 'twentytwentytwo', ); + /** + * Original theme directory. + * + * @var string[] + */ + private $orig_theme_dir; + public function set_up() { global $wp_theme_directories; parent::set_up(); - $backup_wp_theme_directories = $wp_theme_directories; - $wp_theme_directories = array( WP_CONTENT_DIR . '/themes' ); + $this->orig_theme_dir = $wp_theme_directories; + $wp_theme_directories = array( WP_CONTENT_DIR . '/themes' ); add_filter( 'extra_theme_headers', array( $this, 'theme_data_extra_headers' ) ); wp_clean_themes_cache(); @@ -39,7 +46,7 @@ class Tests_Theme extends WP_UnitTestCase { public function tear_down() { global $wp_theme_directories; - $wp_theme_directories = $this->wp_theme_directories; + $wp_theme_directories = $this->orig_theme_dir; remove_filter( 'extra_theme_headers', array( $this, 'theme_data_extra_headers' ) ); wp_clean_themes_cache();