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
This commit is contained in:
Sergey Biryukov 2022-08-31 16:02:05 +00:00
parent 69b9ecc54e
commit 7f08fac299

View File

@ -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();