Skip to content

Commit ee65fc1

Browse files
committed
Tests: Use a minimal theme for tests.
This functionality was originally added in [38858], using `symlink()` to put a link to the theme in WordPress' `themes` directory. Unfortunately, not all installs have write access to the `themes` directory, causing unit tests to fail. The new method is to add the test theme directory to `$wp_theme_directories`, and fix the handful of tests that don't expect `$wp_theme_directories` to have multiple entries. The test install/bootstrap routines now also check that `WP_DEFAULT_THEME` is defined, in case the tests are being run on a system that hasn't upgraded its' `wp-tests-config.php`. See #31550. Fixes #38457. git-svn-id: https://develop.svn.wordpress.org/trunk@38907 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4e8410a commit ee65fc1

4 files changed

Lines changed: 23 additions & 21 deletions

File tree

tests/phpunit/includes/bootstrap.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Globalize some WordPress variables, because PHPUnit loads this file inside a function
1717
* See: https://github.com/sebastianbergmann/phpunit/issues/325
1818
*/
19-
global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer;
19+
global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer, $wp_theme_directories;
2020

2121
if ( !is_readable( $config_file_path ) ) {
2222
die( "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n" );
@@ -53,8 +53,11 @@
5353
require_once( dirname( __FILE__ ) . '/mock-mailer.php' );
5454
$phpmailer = new MockPHPMailer();
5555

56-
// Add a symlink to the empty default theme to the themes directory, so it can be used for the tests.
57-
_symlink_default_theme();
56+
// Set the theme to our special empty theme, to avoid interference from the current Twenty* theme.
57+
if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
58+
define( 'WP_DEFAULT_THEME', 'default' );
59+
}
60+
$wp_theme_directories = array( DIR_TESTDATA . '/themedir1' );
5861

5962
system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $multisite );
6063

tests/phpunit/includes/functions.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,3 @@ function _upload_dir_https( $uploads ) {
161161

162162
return $uploads;
163163
}
164-
165-
/**
166-
* Helper functions to link and unlink the empty default theme into the WordPress install
167-
*/
168-
function _symlink_default_theme() {
169-
_unlink_default_theme();
170-
symlink( DIR_TESTDATA . '/themedir1/default', ABSPATH . '/wp-content/themes/default' );
171-
}
172-
173-
function _unlink_default_theme() {
174-
if ( file_exists( ABSPATH . '/wp-content/themes/default' ) ) {
175-
unlink( ABSPATH . '/wp-content/themes/default' );
176-
}
177-
}
178-
// Only unlink when we're in the main process.
179-
if ( 'phpunit' === substr( $GLOBALS['argv'][0], -7 ) ) {
180-
register_shutdown_function( '_unlink_default_theme' );
181-
}

tests/phpunit/includes/install.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
$config_file_path = $argv[1];
1010
$multisite = ! empty( $argv[2] );
1111

12+
// Set the theme to our special empty theme, to avoid interference from the current Twenty* theme.
13+
if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
14+
define( 'WP_DEFAULT_THEME', 'default' );
15+
}
16+
$wp_theme_directories = array( dirname( __FILE__ ) . '/../data/themedir1' );
17+
1218
define( 'WP_INSTALLING', true );
1319
require_once $config_file_path;
1420
require_once dirname( __FILE__ ) . '/functions.php';

tests/phpunit/tests/theme.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@ class Tests_Theme extends WP_UnitTestCase {
1414
);
1515

1616
function setUp() {
17+
global $wp_theme_directories;
18+
1719
parent::setUp();
20+
21+
$backup_wp_theme_directories = $wp_theme_directories;
22+
$wp_theme_directories = array( WP_CONTENT_DIR . '/themes' );
23+
1824
add_filter( 'extra_theme_headers', array( $this, '_theme_data_extra_headers' ) );
1925
wp_clean_themes_cache();
2026
unset( $GLOBALS['wp_themes'] );
2127
}
2228

2329
function tearDown() {
30+
global $wp_theme_directories;
31+
32+
$wp_theme_directories = $this->wp_theme_directories;
33+
2434
remove_filter( 'extra_theme_headers', array( $this, '_theme_data_extra_headers' ) );
2535
wp_clean_themes_cache();
2636
unset( $GLOBALS['wp_themes'] );
37+
2738
parent::tearDown();
2839
}
2940

0 commit comments

Comments
 (0)