From 2e63f69564aa2f0d5d9f97bcca2ee00b9556f2f9 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Thu, 17 Jan 2019 04:33:42 +0000 Subject: [PATCH] Tests: Ensure meta keys are cleaned up after each test. Props jnylen0. Fixes #46007. git-svn-id: https://develop.svn.wordpress.org/trunk@44633 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/testcase.php | 22 +++++++++++++++++++ .../tests/rest-api/rest-schema-setup.php | 9 ++++++++ 2 files changed, 31 insertions(+) diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 619264e3f4..65fcc1f54a 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -167,6 +167,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $GLOBALS[ $global ] = null; } + $this->unregister_all_meta_keys(); remove_theme_support( 'html5' ); remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); @@ -330,6 +331,27 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) ); } + /** + * Clean up any registered meta keys. + * + * @since 5.1.0 + * + * @global array $wp_meta_keys + */ + function unregister_all_meta_keys() { + global $wp_meta_keys; + if ( ! is_array( $wp_meta_keys ) ) { + return; + } + foreach ( $wp_meta_keys as $object_type => $type_keys ) { + foreach ( $type_keys as $object_subtype => $subtype_keys ) { + foreach ( $subtype_keys as $key => $value ) { + unregister_meta_key( $object_type, $key, $object_subtype ); + } + } + } + } + function start_transaction() { global $wpdb; $wpdb->query( 'SET autocommit = 0;' ); diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php index 8d5006a756..8eb03cb4c2 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-setup.php +++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php @@ -257,7 +257,16 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase { 'show_in_rest' => true, ); + $meta_multi_args = $meta_args; + $meta_multi_args['single'] = false; + // Set up meta. + register_meta( 'term', 'test_single', $meta_args ); + register_meta( 'term', 'test_multi', $meta_multi_args ); + register_term_meta( 'category', 'test_cat_single', $meta_args ); + register_term_meta( 'category', 'test_cat_multi', $meta_multi_args ); + register_term_meta( 'post_tag', 'test_tag_meta', $meta_args ); + register_meta( 'user', 'meta_key', $meta_args ); update_user_meta( 1, 'meta_key', 'meta_value' ); // Always use the first user. register_meta( 'post', 'meta_key', $meta_args );