From 9d7e8fec8ce88fa479ee450b3fe7edb1ddd4826a Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 21 Oct 2016 11:02:37 +0000 Subject: [PATCH] Tests: Use a minimal theme for tests. New default themes require workarounds being added to several unit tests, as they often alter default WordPress behaviour. To avoid ongoing maintenance issues, this change switches to a minimal theme when running tests. This change also removes the old workarounds for default themes. Fixes #31550. git-svn-id: https://develop.svn.wordpress.org/trunk@38858 602fd350-edb4-49c9-b593-d223f7449a82 --- .../data/themedir1/default/comments.php | 4 ++++ .../data/themedir1/default/functions.php | 17 ++++++++++++++--- tests/phpunit/data/themedir1/default/index.php | 6 +----- tests/phpunit/includes/bootstrap.php | 3 +++ tests/phpunit/includes/functions.php | 18 ++++++++++++++++++ tests/phpunit/tests/customize/control.php | 3 --- .../tests/customize/custom-css-setting.php | 3 --- tests/phpunit/tests/customize/manager.php | 3 --- .../tests/customize/nav-menu-item-setting.php | 3 --- .../tests/customize/nav-menu-setting.php | 3 --- tests/phpunit/tests/customize/nav-menus.php | 3 --- tests/phpunit/tests/customize/partial.php | 3 --- .../tests/customize/selective-refresh-ajax.php | 8 -------- .../tests/customize/selective-refresh.php | 3 --- tests/phpunit/tests/customize/widgets.php | 7 ------- tests/phpunit/tests/media.php | 12 ------------ wp-tests-config-sample.php | 8 ++++++++ 17 files changed, 48 insertions(+), 59 deletions(-) create mode 100644 tests/phpunit/data/themedir1/default/comments.php diff --git a/tests/phpunit/data/themedir1/default/comments.php b/tests/phpunit/data/themedir1/default/comments.php new file mode 100644 index 0000000000..b1803eb489 --- /dev/null +++ b/tests/phpunit/data/themedir1/default/comments.php @@ -0,0 +1,4 @@ + 'sidebar-1' ) ); +} +add_action( 'widgets_init', 'default_widgets_init' ); -?> +function default_after_setup_theme() { + add_theme_support( 'post-thumbnails' ); + + // Don't call it after wp_loaded has happened, for tests that manually re-run load actions. + if( ! did_action( 'wp_loaded' ) ) { + add_theme_support( 'title-tag' ); + } +} +add_action( 'after_setup_theme', 'default_after_setup_theme' ); diff --git a/tests/phpunit/data/themedir1/default/index.php b/tests/phpunit/data/themedir1/default/index.php index 1c8413d3a1..9ac1e43d0d 100644 --- a/tests/phpunit/data/themedir1/default/index.php +++ b/tests/phpunit/data/themedir1/default/index.php @@ -1,7 +1,3 @@ +// Empty theme diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index dd75409c3c..b4bff16698 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -53,6 +53,9 @@ $multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE ); require_once( dirname( __FILE__ ) . '/mock-mailer.php' ); $phpmailer = new MockPHPMailer(); +// Add a symlink to the empty default theme to the themes directory, so it can be used for the tests. +_symlink_default_theme(); + system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $multisite ); if ( $multisite ) { diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index a8702767ef..dc8e472ccd 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -161,3 +161,21 @@ function _upload_dir_https( $uploads ) { return $uploads; } + +/** + * Helper functions to link and unlink the empty default theme into the WordPress install + */ +function _symlink_default_theme() { + _unlink_default_theme(); + symlink( DIR_TESTDATA . '/themedir1/default', ABSPATH . '/wp-content/themes/default' ); +} + +function _unlink_default_theme() { + if ( file_exists( ABSPATH . '/wp-content/themes/default' ) ) { + unlink( ABSPATH . '/wp-content/themes/default' ); + } +} +// Only unlink when we're in the main process. +if ( 'phpunit' === substr( $GLOBALS['argv'][0], -7 ) ) { + register_shutdown_function( '_unlink_default_theme' ); +} diff --git a/tests/phpunit/tests/customize/control.php b/tests/phpunit/tests/customize/control.php index 276939327e..e6d1141219 100644 --- a/tests/phpunit/tests/customize/control.php +++ b/tests/phpunit/tests/customize/control.php @@ -31,9 +31,6 @@ class Test_WP_Customize_Control extends WP_UnitTestCase { $GLOBALS['wp_customize'] = new WP_Customize_Manager(); // @codingStandardsIgnoreEnd $this->wp_customize = $GLOBALS['wp_customize']; - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** diff --git a/tests/phpunit/tests/customize/custom-css-setting.php b/tests/phpunit/tests/customize/custom-css-setting.php index a74fe7039e..4c98012626 100644 --- a/tests/phpunit/tests/customize/custom-css-setting.php +++ b/tests/phpunit/tests/customize/custom-css-setting.php @@ -36,9 +36,6 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { $this->wp_customize = new WP_Customize_Manager(); $wp_customize = $this->wp_customize; - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); - do_action( 'customize_register', $this->wp_customize ); $this->setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentysixteen]' ); $this->wp_customize->add_setting( $this->setting ); diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index d050285ff3..3016c70f0b 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -58,9 +58,6 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); $this->manager = $this->instantiate(); $this->undefined = new stdClass(); - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** diff --git a/tests/phpunit/tests/customize/nav-menu-item-setting.php b/tests/phpunit/tests/customize/nav-menu-item-setting.php index 66723ad3fe..ebb9748fa3 100644 --- a/tests/phpunit/tests/customize/nav-menu-item-setting.php +++ b/tests/phpunit/tests/customize/nav-menu-item-setting.php @@ -26,9 +26,6 @@ class Test_WP_Customize_Nav_Menu_Item_Setting extends WP_UnitTestCase { global $wp_customize; $this->wp_customize = new WP_Customize_Manager(); $wp_customize = $this->wp_customize; - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** diff --git a/tests/phpunit/tests/customize/nav-menu-setting.php b/tests/phpunit/tests/customize/nav-menu-setting.php index ff2c4cb63d..81e3e74960 100644 --- a/tests/phpunit/tests/customize/nav-menu-setting.php +++ b/tests/phpunit/tests/customize/nav-menu-setting.php @@ -27,9 +27,6 @@ class Test_WP_Customize_Nav_Menu_Setting extends WP_UnitTestCase { global $wp_customize; $this->wp_customize = new WP_Customize_Manager(); $wp_customize = $this->wp_customize; - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** diff --git a/tests/phpunit/tests/customize/nav-menus.php b/tests/phpunit/tests/customize/nav-menus.php index 3fbe6f0b82..06e2333be3 100644 --- a/tests/phpunit/tests/customize/nav-menus.php +++ b/tests/phpunit/tests/customize/nav-menus.php @@ -26,9 +26,6 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { global $wp_customize; $this->wp_customize = new WP_Customize_Manager(); $wp_customize = $this->wp_customize; - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** diff --git a/tests/phpunit/tests/customize/partial.php b/tests/phpunit/tests/customize/partial.php index 57f3c59dc0..944cb4e2b7 100644 --- a/tests/phpunit/tests/customize/partial.php +++ b/tests/phpunit/tests/customize/partial.php @@ -39,9 +39,6 @@ class Test_WP_Customize_Partial extends WP_UnitTestCase { if ( isset( $this->wp_customize->selective_refresh ) ) { $this->selective_refresh = $this->wp_customize->selective_refresh; } - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** diff --git a/tests/phpunit/tests/customize/selective-refresh-ajax.php b/tests/phpunit/tests/customize/selective-refresh-ajax.php index d0cfadc305..35ac86c281 100644 --- a/tests/phpunit/tests/customize/selective-refresh-ajax.php +++ b/tests/phpunit/tests/customize/selective-refresh-ajax.php @@ -51,20 +51,12 @@ class Test_WP_Customize_Selective_Refresh_Ajax extends WP_UnitTestCase { if ( isset( $this->wp_customize->selective_refresh ) ) { $this->selective_refresh = $this->wp_customize->selective_refresh; } - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** * Do Customizer boot actions. */ function do_customize_boot_actions() { - // Remove actions that call add_theme_support( 'title-tag' ). - remove_action( 'after_setup_theme', 'twentyfifteen_setup' ); - remove_action( 'after_setup_theme', 'twentysixteen_setup' ); - remove_action( 'after_setup_theme', 'twentyseventeen_setup' ); - $_SERVER['REQUEST_METHOD'] = 'POST'; do_action( 'setup_theme' ); do_action( 'after_setup_theme' ); diff --git a/tests/phpunit/tests/customize/selective-refresh.php b/tests/phpunit/tests/customize/selective-refresh.php index d5557dd9a8..73e7dc36eb 100644 --- a/tests/phpunit/tests/customize/selective-refresh.php +++ b/tests/phpunit/tests/customize/selective-refresh.php @@ -39,9 +39,6 @@ class Test_WP_Customize_Selective_Refresh extends WP_UnitTestCase { if ( isset( $this->wp_customize->selective_refresh ) ) { $this->selective_refresh = $this->wp_customize->selective_refresh; } - - // Remove default theme actions that interfere with tests - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); } /** diff --git a/tests/phpunit/tests/customize/widgets.php b/tests/phpunit/tests/customize/widgets.php index 7d395c558e..a2fbf372a6 100644 --- a/tests/phpunit/tests/customize/widgets.php +++ b/tests/phpunit/tests/customize/widgets.php @@ -40,13 +40,6 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase { $this->assertArrayHasKey( 2, $widget_categories ); $this->assertEquals( '', $widget_categories[2]['title'] ); - // @todo We should not be including a theme anyway - remove_action( 'after_setup_theme', 'twentyfifteen_setup' ); - remove_action( 'after_setup_theme', 'twentysixteen_setup' ); - remove_action( 'customize_register', 'twentysixteen_customize_register', 11 ); - remove_action( 'after_setup_theme', 'twentyseventeen_setup' ); - remove_action( 'customize_register', 'twentyseventeen_customize_register' ); - $this->backup_registered_sidebars = $GLOBALS['wp_registered_sidebars']; // Reset protected static var on class. diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index d8d1569967..48999301ea 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -33,18 +33,6 @@ CAP; $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name; $this->img_html = ''; $this->img_meta = array( 'width' => 100, 'height' => 100, 'sizes' => '' ); - - // Disable Twenty Seventeen changes to the image size attribute - remove_filter( 'wp_calculate_image_sizes', 'twentyseventeen_content_image_sizes_attr' ); - remove_filter( 'wp_get_attachment_image_attributes', 'twentyseventeen_post_thumbnail_sizes_attr' ); - } - - function tearDown() { - parent::tearDown(); - - // Reset Twenty Seventeen behaviour - add_filter( 'wp_calculate_image_sizes', 'twentyseventeen_content_image_sizes_attr', 10, 2 ); - add_filter( 'wp_get_attachment_image_attributes', 'twentyseventeen_post_thumbnail_sizes_attr', 10, 3 ); } function test_img_caption_shortcode_added() { diff --git a/wp-tests-config-sample.php b/wp-tests-config-sample.php index 746c793156..3ea3c0296a 100644 --- a/wp-tests-config-sample.php +++ b/wp-tests-config-sample.php @@ -3,6 +3,14 @@ /* Path to the WordPress codebase you'd like to test. Add a forward slash in the end. */ define( 'ABSPATH', dirname( __FILE__ ) . '/src/' ); +/* + * Path to the theme to test with. + * + * The 'default' theme is symlinked from test/phpunit/data/themedir1/default into + * the themes directory of the WordPress install defined above. + */ +define( 'WP_DEFAULT_THEME', 'default' ); + // Test with multisite enabled. // Alternatively, use the tests/phpunit/multisite.xml configuration file. // define( 'WP_TESTS_MULTISITE', true );