From 654d07ebf921b58213e64588ec799475e07a7314 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 12 Sep 2013 14:47:58 +0000 Subject: [PATCH] There was way too much duplicated code in my notice cleanup, it built up over time, and there's definitely a need to standardize. * Remove duplicated code for deprecated function notice suppression * Add support in `WP_UnitTestCase` setUp/tearDown methods for `$deprecated_functions` fixture if the extending class has added it * Add a `$deprecated_functions` fixture to each extending class that needs it To use this fixture, add something to your Test Case class like so: `protected $deprecated_functions = array( 'get_theme', 'get_themes', 'get_theme_data', 'get_current_theme' );` See #25282. git-svn-id: https://develop.svn.wordpress.org/trunk@25402 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/testcase.php | 16 ++++++++++++++ tests/phpunit/tests/admin/includesTheme.php | 14 ++---------- tests/phpunit/tests/formatting/CleanPre.php | 20 +---------------- tests/phpunit/tests/image/functions.php | 18 ++-------------- tests/phpunit/tests/image/size.php | 24 ++------------------- tests/phpunit/tests/media.php | 17 +-------------- tests/phpunit/tests/ms.php | 18 +--------------- tests/phpunit/tests/theme.php | 20 +++-------------- tests/phpunit/tests/theme/themeDir.php | 14 ++---------- tests/phpunit/tests/user/capabilities.php | 19 ++-------------- 10 files changed, 32 insertions(+), 148 deletions(-) diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index c3fb4373df..c5ab2e1681 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -6,6 +6,7 @@ require_once dirname( __FILE__ ) . '/trac.php'; class WP_UnitTestCase extends PHPUnit_Framework_TestCase { protected static $forced_tickets = array(); + protected $deprecated_functions = array(); /** * @var WP_UnitTest_Factory @@ -24,6 +25,9 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $this->clean_up_global_scope(); $this->start_transaction(); add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); + + if ( ! empty( $this->deprecated_functions ) ) + add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); } function tearDown() { @@ -32,6 +36,18 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { remove_filter( 'dbdelta_create_queries', array( $this, '_create_temporary_tables' ) ); remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); + if ( ! empty( $this->deprecated_functions ) ) + remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); + } + + function deprecated_function_run( $function ) { + if ( in_array( $function, $this->deprecated_functions ) ) + add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); + } + + function deprecated_function_trigger_error() { + remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); + return false; } function clean_up_global_scope() { diff --git a/tests/phpunit/tests/admin/includesTheme.php b/tests/phpunit/tests/admin/includesTheme.php index a0e6707f46..960d172a4b 100644 --- a/tests/phpunit/tests/admin/includesTheme.php +++ b/tests/phpunit/tests/admin/includesTheme.php @@ -3,6 +3,8 @@ * @group themes */ class Tests_Admin_includesTheme extends WP_UnitTestCase { + protected $deprecated_functions = array( 'get_theme', 'get_themes', 'get_theme_data', 'get_current_theme' ); + function setUp() { parent::setUp(); $this->theme_root = DIR_TESTDATA . '/themedir1'; @@ -17,18 +19,6 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase { // clear caches wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); - - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run_check' ) ); - } - - function deprecated_function_run_check( $function ) { - if ( in_array( $function, array( 'get_theme', 'get_themes', 'get_theme_data', 'get_current_theme' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'filter_deprecated_function_trigger_error' ) ); - } - - function filter_deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'filter_deprecated_function_trigger_error' ) ); - return false; } function tearDown() { diff --git a/tests/phpunit/tests/formatting/CleanPre.php b/tests/phpunit/tests/formatting/CleanPre.php index 410b5095f8..caff19cd77 100644 --- a/tests/phpunit/tests/formatting/CleanPre.php +++ b/tests/phpunit/tests/formatting/CleanPre.php @@ -7,25 +7,7 @@ * @group formatting */ class Tests_Formatting_CleanPre extends WP_UnitTestCase { - function setUp() { - parent::setUp(); - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run_check' ) ); - } - - function tearDown() { - parent::tearDown(); - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run_check' ) ); - } - - function deprecated_function_run_check( $function ) { - if ( in_array( $function, array( 'clean_pre' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - } - - function deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - return false; - } + protected $deprecated_functions = array( 'clean_pre' ); function test_removes_self_closing_br_with_space() { $source = 'a b c\n
sldfj
'; diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 2b5b1d7245..37efa2aa19 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -6,6 +6,8 @@ * @group upload */ class Tests_Image_Functions extends WP_UnitTestCase { + protected $deprecated_functions = array( 'wp_load_image' ); + /** * Setup test fixture */ @@ -17,22 +19,6 @@ class Tests_Image_Functions extends WP_UnitTestCase { require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' ); include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' ); - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function tearDown() { - parent::tearDown(); - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function deprecated_function_run( $function ) { - if ( in_array( $function, array( 'wp_load_image' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - } - - function deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - return false; } /** diff --git a/tests/phpunit/tests/image/size.php b/tests/phpunit/tests/image/size.php index da50606082..a06f10de24 100644 --- a/tests/phpunit/tests/image/size.php +++ b/tests/phpunit/tests/image/size.php @@ -6,28 +6,8 @@ * @group upload */ class Tests_Image_Size extends WP_UnitTestCase { - function setUp() { - parent::setUp(); - - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run_check' ) ); - } - - function tearDown() { - parent::tearDown(); - - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run_check' ) ); - } - - function deprecated_function_run_check( $function ) { - if ( in_array( $function, array( 'wp_shrink_dimensions' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'filter_deprecated_function_trigger_error' ) ); - } - - function filter_deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'filter_deprecated_function_trigger_error' ) ); - return false; - } - + protected $deprecated_functions = array( 'wp_shrink_dimensions' ); + function test_constrain_dims_zero() { if (!is_callable('wp_constrain_dimensions')) $this->markTestSkipped('wp_constrain_dimensions() is not callable.'); diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index f1550c4131..c41c89c7b5 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -5,6 +5,7 @@ * @group shortcode */ class Tests_Media extends WP_UnitTestCase { + protected $deprecated_functions = array( 'wp_convert_bytes_to_hr' ); function setUp() { parent::setUp(); @@ -19,22 +20,6 @@ CAP; $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name; $this->img_html = ''; $this->img_dimensions = array( 'width' => 100, 'height' => 100 ); - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function tearDown() { - parent::tearDown(); - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function deprecated_function_run( $function ) { - if ( in_array( $function, array( 'wp_convert_bytes_to_hr' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - } - - function deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - return false; } function test_img_caption_shortcode_added() { diff --git a/tests/phpunit/tests/ms.php b/tests/phpunit/tests/ms.php index 610c47ebfc..f3f8df3b59 100644 --- a/tests/phpunit/tests/ms.php +++ b/tests/phpunit/tests/ms.php @@ -8,29 +8,13 @@ if ( is_multisite() ) : * @group multisite */ class Tests_MS extends WP_UnitTestCase { - + protected $deprecated_functions = array( 'is_blog_user', 'get_dashboard_blog' ); protected $plugin_hook_count = 0; function setUp() { parent::setUp(); $_SERVER['REMOTE_ADDR'] = ''; - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run_check' ) ); - } - - function tearDown() { - parent::tearDown(); - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run_check' ) ); - } - - function deprecated_function_run_check( $function ) { - if ( in_array( $function, array( 'is_blog_user', 'get_dashboard_blog' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'filter_deprecated_function_trigger_error' ) ); - } - - function filter_deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'filter_deprecated_function_trigger_error' ) ); - return false; } function test_create_and_delete_blog() { diff --git a/tests/phpunit/tests/theme.php b/tests/phpunit/tests/theme.php index 601f87fdd6..ea3273184d 100644 --- a/tests/phpunit/tests/theme.php +++ b/tests/phpunit/tests/theme.php @@ -6,17 +6,15 @@ * @group themes */ class Tests_Theme extends WP_UnitTestCase { - - var $theme_slug = 'twentyeleven'; - var $theme_name = 'Twenty Eleven'; + protected $deprecated_functions = array( 'get_theme', 'get_themes', 'get_theme_data', 'get_current_theme' ); + protected $theme_slug = 'twentyeleven'; + protected $theme_name = 'Twenty Eleven'; function setUp() { parent::setUp(); add_filter( 'extra_theme_headers', array( $this, '_theme_data_extra_headers' ) ); wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); - - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); } function tearDown() { @@ -24,18 +22,6 @@ class Tests_Theme extends WP_UnitTestCase { wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); parent::tearDown(); - - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function deprecated_function_run( $function ) { - if ( in_array( $function, array( 'get_theme', 'get_themes', 'get_theme_data', 'get_current_theme' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - } - - function deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - return false; } function test_wp_get_themes_default() { diff --git a/tests/phpunit/tests/theme/themeDir.php b/tests/phpunit/tests/theme/themeDir.php index f62f165ee7..6a8187f93f 100644 --- a/tests/phpunit/tests/theme/themeDir.php +++ b/tests/phpunit/tests/theme/themeDir.php @@ -6,6 +6,8 @@ * @group themes */ class Tests_Theme_ThemeDir extends WP_UnitTestCase { + protected $deprecated_functions = array( 'get_theme', 'get_themes', 'get_theme_data', 'get_current_theme', 'get_broken_themes' ); + function setUp() { parent::setUp(); $this->theme_root = DIR_TESTDATA . '/themedir1'; @@ -21,7 +23,6 @@ class Tests_Theme_ThemeDir extends WP_UnitTestCase { // clear caches wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); } function tearDown() { @@ -32,17 +33,6 @@ class Tests_Theme_ThemeDir extends WP_UnitTestCase { wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); parent::tearDown(); - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function deprecated_function_run( $function ) { - if ( in_array( $function, array( 'get_theme', 'get_themes', 'get_theme_data', 'get_current_theme', 'get_broken_themes' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - } - - function deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - return false; } // replace the normal theme root dir with our premade test dir diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 995fbc49c9..c68ba99e50 100644 --- a/tests/phpunit/tests/user/capabilities.php +++ b/tests/phpunit/tests/user/capabilities.php @@ -7,7 +7,8 @@ * @group capabilities */ class Tests_User_Capabilities extends WP_UnitTestCase { - var $user_ids = array(); + protected $user_ids = array(); + protected $deprecated_functions = array( 'set_current_user' ); function setUp() { parent::setUp(); @@ -15,22 +16,6 @@ class Tests_User_Capabilities extends WP_UnitTestCase { $this->_flush_roles(); $this->orig_users = get_users(); - add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function tearDown() { - parent::tearDown(); - remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); - } - - function deprecated_function_run( $function ) { - if ( in_array( $function, array( 'set_current_user' ) ) ) - add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - } - - function deprecated_function_trigger_error() { - remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); - return false; } function _flush_roles() {