From 13e28a6d34b1d547624490b6891092674103c192 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 30 Sep 2015 13:08:49 +0000 Subject: [PATCH] Prevent Multisite term tests from hitting database for 'db_version'. [34718] introduced a 'db_version' check to term meta functions, to ensure that they don't run when the term meta schema is not yet in place. This call to `get_option()` causes a database hit during Multisite tests, due to the presence of the `WP_INSTALLING` constant. See #31130. The extra database queries are causing cache tests to fail. In similar cases, we have `markTestSkipped()` when `is_multisite()`. Because the term meta API is so extensive - term meta caches can be primed anywhere a `WP_Query` loop is fired up - we implement a more generous workaround in this case. To prevent `get_option( 'db_version' )` from hitting the database during multisite unit tests, we use a 'pre_option_' filter. Heaven help us. See #34091. git-svn-id: https://develop.svn.wordpress.org/trunk@34719 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/testcase.php | 26 +++++++++++++++++++++++ tests/phpunit/tests/term/wpInsertTerm.php | 6 +++--- 2 files changed, 29 insertions(+), 3 deletions(-) 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() {