diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index f5a7db8362..9b33079f5f 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -55,6 +55,19 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); add_filter( 'wp_mail', array( $this, 'set_wp_mail_globals' ) ); + + /* + * During multisite tests, WP_INSTALLING forces `get_option()` to miss the cache, which causes problems + * with our query-counting cache tests. As a workaround in the case of tests that require checking + * 'db_version' (such as any test that uses the Term Meta API), we filter 'pre_option_db_version' and + * avoid hitting the database. + * + * See #31130. + */ + if ( is_multisite() ) { + $this->db_version = get_option( 'db_version' ); + add_filter( 'pre_option_db_version', array( $this, 'db_version' ) ); + } } /** @@ -617,4 +630,17 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { return wp_delete_user( $user_id ); } } + + /** + * Return the current database version without hitting the database. + * + * This is used to bypass cache problems with some multisite tests. See #31130. + * + * @todo Don't do this anymore once #31130 is fixed. + * + * @since 4.4.0 + */ + public function db_version() { + return $this->db_version; + } } diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index 0a63d26976..660872c14d 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -469,8 +469,8 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase { */ public function test_wp_insert_term_duplicate_slug_different_taxonomy_before_410_schema_change() { - $db_version = get_option( 'db_version' ); - update_option( 'db_version', 30055 ); + $_db_version = $this->db_version; + $this->db_version = 30055; register_taxonomy( 'wptests_tax', 'post' ); register_taxonomy( 'wptests_tax_2', 'post' ); @@ -497,8 +497,8 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase { $this->assertSame( 'foo-2', $new_term->slug ); $this->assertNotEquals( $new_term->term_id, $term->term_id ); + $this->db_version = $_db_version; _unregister_taxonomy( 'wptests_tax', 'post' ); - update_option( 'db_version', $db_version ); } public function test_wp_insert_term_alias_of_no_term_group() {