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
This commit is contained in:
Scott Taylor 2013-09-12 14:47:58 +00:00
parent 1851da1382
commit 654d07ebf9
10 changed files with 32 additions and 148 deletions

View File

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

View File

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

View File

@ -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<br />sldfj<br />';

View File

@ -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;
}
/**

View File

@ -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.');

View File

@ -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 = '<img src="' . $this->img_url . '"/>';
$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() {

View File

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

View File

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

View File

@ -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

View File

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