diff --git a/src/wp-admin/includes/class-wp-comments-list-table.php b/src/wp-admin/includes/class-wp-comments-list-table.php index cd57ce80b3..b62bfc50bf 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -164,8 +164,6 @@ public function prepare_items() { $_comments = get_comments( $args ); if ( is_array( $_comments ) ) { - update_comment_cache( $_comments ); - $this->items = array_slice( $_comments, 0, $comments_per_page ); $this->extra_items = array_slice( $_comments, $comments_per_page ); diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 4447b1169b..bc9c59bde5 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -481,12 +481,16 @@ public function get_comments() { $comment_ids = array_map( 'intval', $comment_ids ); + if ( $this->query_vars['update_comment_meta_cache'] ) { + wp_lazyload_comment_meta( $comment_ids ); + } + if ( 'ids' === $this->query_vars['fields'] ) { $this->comments = $comment_ids; return $this->comments; } - _prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] ); + _prime_comment_caches( $comment_ids, false ); // Fetch full comment objects from the primed cache. $_comments = array(); diff --git a/src/wp-includes/class-wp-metadata-lazyloader.php b/src/wp-includes/class-wp-metadata-lazyloader.php index 914d72d3b5..0f8e011edf 100644 --- a/src/wp-includes/class-wp-metadata-lazyloader.php +++ b/src/wp-includes/class-wp-metadata-lazyloader.php @@ -55,11 +55,15 @@ public function __construct() { $this->settings = array( 'term' => array( 'filter' => 'get_term_metadata', - 'callback' => array( $this, 'lazyload_term_meta' ), + 'callback' => array( $this, 'lazyload_meta_callback' ), ), 'comment' => array( 'filter' => 'get_comment_metadata', - 'callback' => array( $this, 'lazyload_comment_meta' ), + 'callback' => array( $this, 'lazyload_meta_callback' ), + ), + 'blog' => array( + 'filter' => 'get_blog_metadata', + 'callback' => array( $this, 'lazyload_meta_callback' ), ), ); } @@ -91,7 +95,7 @@ public function queue_objects( $object_type, $object_ids ) { } } - add_filter( $type_settings['filter'], $type_settings['callback'] ); + add_filter( $type_settings['filter'], $type_settings['callback'], 10, 5 ); /** * Fires after objects are added to the metadata lazy-load queue. @@ -131,20 +135,15 @@ public function reset_queue( $object_type ) { * is no need to invoke it directly. * * @since 4.5.0 + * @deprecated 6.3.0 Use WP_Metadata_Lazyloader::lazyload_meta_callback() instead. * * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook. * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be * another value if filtered by a plugin. */ public function lazyload_term_meta( $check ) { - if ( ! empty( $this->pending_objects['term'] ) ) { - update_termmeta_cache( array_keys( $this->pending_objects['term'] ) ); - - // No need to run again for this set of terms. - $this->reset_queue( 'term' ); - } - - return $check; + _deprecated_function( __METHOD__, '6.3.0', 'WP_Metadata_Lazyloader::lazyload_meta_callback' ); + return $this->lazyload_meta_callback( $check, 0, '', false, 'term' ); } /** @@ -154,18 +153,48 @@ public function lazyload_term_meta( $check ) { * directly, from either inside or outside the `WP_Query` object. * * @since 4.5.0 + * @deprecated 6.3.0 Use WP_Metadata_Lazyloader::lazyload_meta_callback() instead. * * @param mixed $check The `$check` param passed from the {@see 'get_comment_metadata'} hook. * @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`. */ public function lazyload_comment_meta( $check ) { - if ( ! empty( $this->pending_objects['comment'] ) ) { - update_meta_cache( 'comment', array_keys( $this->pending_objects['comment'] ) ); + _deprecated_function( __METHOD__, '6.3.0', 'WP_Metadata_Lazyloader::lazyload_meta_callback' ); + return $this->lazyload_meta_callback( $check, 0, '', false, 'comment' ); + } - // No need to run again for this set of comments. - $this->reset_queue( 'comment' ); + /** + * Lazy-loads meta for queued objects. + * + * This method is public so that it can be used as a filter callback. As a rule, there + * is no need to invoke it directly. + * + * @since 6.3.0 + * + * @param mixed $check The `$check` param passed from the 'get_*_metadata' hook. + * @param int $object_id ID of the object metadata is for. + * @param string $meta_key Unused. + * @param bool $single Unused. + * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', + * or any other object type with an associated meta table. + * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be + * another value if filtered by a plugin. + */ + public function lazyload_meta_callback( $check, $object_id, $meta_key, $single, $meta_type ) { + if ( empty( $this->pending_objects[ $meta_type ] ) ) { + return $check; } + $object_ids = array_keys( $this->pending_objects[ $meta_type ] ); + if ( $object_id && ! in_array( $object_id, $object_ids, true ) ) { + $object_ids[] = $object_id; + } + + update_meta_cache( $meta_type, $object_ids ); + + // No need to run again for this set of objects. + $this->reset_queue( $meta_type ); + return $check; } } diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 46cbd80040..9f56a2acc4 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -2813,7 +2813,7 @@ public function get_posts() { $comment_ids = $wpdb->get_col( $comments_request ); wp_cache_add( $cache_key, $comment_ids, 'comment' ); } - _prime_comment_caches( $comment_ids, false ); + _prime_comment_caches( $comment_ids ); // Convert to WP_Comment. /** @var WP_Comment[] */ @@ -3372,7 +3372,7 @@ public function get_posts() { $comment_ids = $wpdb->get_col( $comments_request ); wp_cache_add( $comment_cache_key, $comment_ids, 'comment' ); } - _prime_comment_caches( $comment_ids, false ); + _prime_comment_caches( $comment_ids ); // Convert to WP_Comment. /** @var WP_Comment[] */ @@ -3487,11 +3487,6 @@ public function get_posts() { } } - // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up. - if ( ! empty( $this->comments ) ) { - wp_queue_comments_for_comment_meta_lazyload( $this->comments ); - } - if ( ! $q['suppress_filters'] ) { /** * Filters the array of retrieved posts after they've been fetched and @@ -4873,7 +4868,7 @@ public function lazyload_term_meta( $check, $term_id ) { * Lazyload comment meta for comments in the loop. * * @since 4.4.0 - * @deprecated 4.5.0 See wp_queue_comments_for_comment_meta_lazyload(). + * @deprecated 4.5.0 See wp_lazyload_comment_meta(). * * @param mixed $check * @param int $comment_id diff --git a/src/wp-includes/class-wp-site-query.php b/src/wp-includes/class-wp-site-query.php index d89453b74e..35aac3a9c5 100644 --- a/src/wp-includes/class-wp-site-query.php +++ b/src/wp-includes/class-wp-site-query.php @@ -388,6 +388,10 @@ public function get_sites() { $site_ids = array_map( 'intval', $site_ids ); + if ( $this->query_vars['update_site_meta_cache'] ) { + wp_lazyload_site_meta( $site_ids ); + } + if ( 'ids' === $this->query_vars['fields'] ) { $this->sites = $site_ids; @@ -396,7 +400,7 @@ public function get_sites() { // Prime site network caches. if ( $this->query_vars['update_site_cache'] ) { - _prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] ); + _prime_site_caches( $site_ids, false ); } // Fetch full site objects from the primed cache. diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index a7ffbcf608..b02d670b89 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -865,7 +865,7 @@ public function get_terms() { // Prime termmeta cache. if ( $args['update_term_meta_cache'] ) { $term_ids = wp_list_pluck( $term_objects, 'term_id' ); - update_termmeta_cache( $term_ids ); + wp_lazyload_term_meta( $term_ids ); } if ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) ) { diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 7a619d4c6a..0fce39cf79 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -1494,7 +1494,6 @@ function comments_template( $file = '/comments.php', $separate_comments = false 'status' => 'approve', 'post_id' => $post->ID, 'no_found_rows' => false, - 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance. ); if ( get_option( 'thread_comments' ) ) { @@ -2406,8 +2405,6 @@ function wp_list_comments( $args = array(), $comments = null ) { $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) ); } - wp_queue_comments_for_comment_meta_lazyload( $_comments ); - if ( empty( $parsed_args['walker'] ) ) { $walker = new Walker_Comment(); } else { diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 158f9d2810..5b84788af7 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -484,6 +484,21 @@ function get_comment_meta( $comment_id, $key = '', $single = false ) { return get_metadata( 'comment', $comment_id, $key, $single ); } +/** + * Queue comment meta for lazy-loading. + * + * @since 6.3.0 + * + * @param array $comment_ids List of comment IDs. + */ +function wp_lazyload_comment_meta( array $comment_ids ) { + if ( empty( $comment_ids ) ) { + return; + } + $lazyloader = wp_metadata_lazyloader(); + $lazyloader->queue_objects( 'comment', $comment_ids ); +} + /** * Updates comment meta field based on comment ID. * @@ -510,30 +525,6 @@ function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); } -/** - * Queues comments for metadata lazy-loading. - * - * @since 4.5.0 - * - * @param WP_Comment[] $comments Array of comment objects. - */ -function wp_queue_comments_for_comment_meta_lazyload( $comments ) { - // Don't use `wp_list_pluck()` to avoid by-reference manipulation. - $comment_ids = array(); - if ( is_array( $comments ) ) { - foreach ( $comments as $comment ) { - if ( $comment instanceof WP_Comment ) { - $comment_ids[] = $comment->comment_ID; - } - } - } - - if ( $comment_ids ) { - $lazyloader = wp_metadata_lazyloader(); - $lazyloader->queue_objects( 'comment', $comment_ids ); - } -} - /** * Sets the cookies used to store an unauthenticated commentator's identity. Typically used * to recall previous comments by this commentator that are still held in moderation. @@ -3330,6 +3321,7 @@ function update_comment_cache( $comments, $update_meta_cache = true ) { * * @since 4.4.0 * @since 6.1.0 This function is no longer marked as "private". + * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta. * * @see update_comment_cache() * @global wpdb $wpdb WordPress database abstraction object. @@ -3344,7 +3336,11 @@ function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) { if ( ! empty( $non_cached_ids ) ) { $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); - update_comment_cache( $fresh_comments, $update_meta_cache ); + update_comment_cache( $fresh_comments, false ); + } + + if ( $update_meta_cache ) { + wp_lazyload_comment_meta( $comment_ids ); } } diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 94430852d4..870e1e7059 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -4453,3 +4453,26 @@ function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) function wlwmanifest_link() { _deprecated_function( __FUNCTION__, '6.3.0' ); } + +/** + * Queues comments for metadata lazy-loading. + * + * @since 4.5.0 + * @deprecated 6.3.0 Use wp_lazyload_comment_meta() instead. + * + * @param WP_Comment[] $comments Array of comment objects. + */ +function wp_queue_comments_for_comment_meta_lazyload( $comments ) { + _deprecated_function( __FUNCTION__, '6.3.0', 'wp_lazyload_comment_meta' ); + // Don't use `wp_list_pluck()` to avoid by-reference manipulation. + $comment_ids = array(); + if ( is_array( $comments ) ) { + foreach ( $comments as $comment ) { + if ( $comment instanceof WP_Comment ) { + $comment_ids[] = $comment->comment_ID; + } + } + } + + wp_lazyload_comment_meta( $comment_ids ); +} diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index 2dc57a67d0..fd27bea883 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -10,6 +10,8 @@ * @subpackage Meta */ +require ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php'; + /** * Adds metadata for the specified object. * diff --git a/src/wp-includes/ms-site.php b/src/wp-includes/ms-site.php index ae9310ca89..5fdc144181 100644 --- a/src/wp-includes/ms-site.php +++ b/src/wp-includes/ms-site.php @@ -340,6 +340,7 @@ function get_site( $site = null ) { * @since 4.6.0 * @since 5.1.0 Introduced the `$update_meta_cache` parameter. * @since 6.1.0 This function is no longer marked as "private". + * @since 6.3.0 Use wp_lazyload_site_meta() for lazy-loading of site meta. * * @see update_site_cache() * @global wpdb $wpdb WordPress database abstraction object. @@ -354,8 +355,27 @@ function _prime_site_caches( $ids, $update_meta_cache = true ) { if ( ! empty( $non_cached_ids ) ) { $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - update_site_cache( $fresh_sites, $update_meta_cache ); + update_site_cache( $fresh_sites, false ); } + + if ( $update_meta_cache ) { + wp_lazyload_site_meta( $ids ); + } +} + +/** + * Queue site meta for lazy-loading. + * + * @since 6.3.0 + * + * @param array $site_ids List of site IDs. + */ +function wp_lazyload_site_meta( array $site_ids ) { + if ( empty( $site_ids ) ) { + return; + } + $lazyloader = wp_metadata_lazyloader(); + $lazyloader->queue_objects( 'blog', $site_ids ); } /** diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 965d9d086d..21bdf99239 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -7561,10 +7561,7 @@ function wp_queue_posts_for_term_meta_lazyload( $posts ) { } } - if ( $term_ids ) { - $lazyloader = wp_metadata_lazyloader(); - $lazyloader->queue_objects( 'term', $term_ids ); - } + wp_lazyload_term_meta( $term_ids ); } /** diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 5b75bbe299..a90818d4a9 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1384,6 +1384,22 @@ function update_termmeta_cache( $term_ids ) { return update_meta_cache( 'term', $term_ids ); } + +/** + * Queue term meta for lazy-loading. + * + * @since 6.3.0 + * + * @param array $term_ids List of term IDs. + */ +function wp_lazyload_term_meta( array $term_ids ) { + if ( empty( $term_ids ) ) { + return; + } + $lazyloader = wp_metadata_lazyloader(); + $lazyloader->queue_objects( 'term', $term_ids ); +} + /** * Gets all meta data, including meta IDs, for the given term ID. * @@ -3961,6 +3977,7 @@ function _pad_term_counts( &$terms, $taxonomy ) { * * @since 4.6.0 * @since 6.1.0 This function is no longer marked as "private". + * @since 6.3.0 Use wp_lazyload_term_meta() for lazy-loading of term meta. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -3975,10 +3992,10 @@ function _prime_term_caches( $term_ids, $update_meta_cache = true ) { $fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); update_term_cache( $fresh_terms ); + } - if ( $update_meta_cache ) { - update_termmeta_cache( $non_cached_ids ); - } + if ( $update_meta_cache ) { + wp_lazyload_term_meta( $term_ids ); } } diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index f69ec9f348..4258fb7491 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -1006,7 +1006,6 @@ function get_blogs_of_user( $user_id, $all = false ) { $args = array( 'number' => '', 'site__in' => $site_ids, - 'update_site_meta_cache' => false, ); if ( ! $all ) { $args['archived'] = 0; diff --git a/src/wp-settings.php b/src/wp-settings.php index 7702d07e6d..e0c079e4bd 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -179,7 +179,6 @@ require ABSPATH . WPINC . '/class-wp-user-query.php'; require ABSPATH . WPINC . '/class-wp-session-tokens.php'; require ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php'; -require ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php'; require ABSPATH . WPINC . '/general-template.php'; require ABSPATH . WPINC . '/link-template.php'; require ABSPATH . WPINC . '/author-template.php'; diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 9ad8913b02..d5b32f176b 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -194,9 +194,7 @@ public function tear_down() { $this->_restore_hooks(); wp_set_current_user( 0 ); - $lazyloader = wp_metadata_lazyloader(); - $lazyloader->reset_queue( 'term' ); - $lazyloader->reset_queue( 'comment' ); + $this->reset_lazyload_queue(); } /** @@ -274,6 +272,16 @@ public function skipTestOnTimeout( $response ) { } } + /** + * Reset the lazy load meta queue. + */ + protected function reset_lazyload_queue() { + $lazyloader = wp_metadata_lazyloader(); + $lazyloader->reset_queue( 'term' ); + $lazyloader->reset_queue( 'comment' ); + $lazyloader->reset_queue( 'blog' ); + } + /** * Unregisters existing post types and register defaults. * diff --git a/tests/phpunit/tests/bookmark/getBookmarks.php b/tests/phpunit/tests/bookmark/getBookmarks.php index 45b5af91d0..494737f643 100644 --- a/tests/phpunit/tests/bookmark/getBookmarks.php +++ b/tests/phpunit/tests/bookmark/getBookmarks.php @@ -5,8 +5,6 @@ */ class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase { public function test_should_hit_cache() { - global $wpdb; - $bookmarks = self::factory()->bookmark->create_many( 2 ); $found1 = get_bookmarks( @@ -15,7 +13,7 @@ public function test_should_hit_cache() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found2 = get_bookmarks( array( @@ -24,12 +22,10 @@ public function test_should_hit_cache() { ); $this->assertSameSets( $found1, $found2 ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } public function test_adding_bookmark_should_bust_get_bookmarks_cache() { - global $wpdb; - $bookmarks = self::factory()->bookmark->create_many( 2 ); // Prime cache. @@ -39,7 +35,7 @@ public function test_adding_bookmark_should_bust_get_bookmarks_cache() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $bookmarks[] = wp_insert_link( array( @@ -55,15 +51,13 @@ public function test_adding_bookmark_should_bust_get_bookmarks_cache() { ); $this->assertEqualSets( $bookmarks, wp_list_pluck( $found2, 'link_id' ) ); - $this->assertGreaterThan( $num_queries, $wpdb->num_queries ); + $this->assertGreaterThan( $num_queries, get_num_queries() ); } /** * @ticket 18356 */ public function test_orderby_rand_should_not_be_cached() { - global $wpdb; - $bookmarks = self::factory()->bookmark->create_many( 2 ); $found1 = get_bookmarks( @@ -72,7 +66,7 @@ public function test_orderby_rand_should_not_be_cached() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found2 = get_bookmarks( array( @@ -82,7 +76,7 @@ public function test_orderby_rand_should_not_be_cached() { // Equal sets != same order. $this->assertEqualSets( $found1, $found2 ); - $this->assertGreaterThan( $num_queries, $wpdb->num_queries ); + $this->assertGreaterThan( $num_queries, get_num_queries() ); } public function test_exclude_param_gets_properly_parsed_as_list() { diff --git a/tests/phpunit/tests/comment/getPageOfComment.php b/tests/phpunit/tests/comment/getPageOfComment.php index e1f19ae345..bfb5c92c30 100644 --- a/tests/phpunit/tests/comment/getPageOfComment.php +++ b/tests/phpunit/tests/comment/getPageOfComment.php @@ -100,27 +100,23 @@ public function test_type_pings() { * @ticket 11334 */ public function test_subsequent_calls_should_hit_cache() { - global $wpdb; - $p = self::factory()->post->create(); $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) ); // Prime cache. $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) ); $this->assertSame( $page_1, $page_2 ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 11334 */ public function test_cache_hits_should_be_sensitive_to_comment_type() { - global $wpdb; - $p = self::factory()->post->create(); $comment = self::factory()->comment->create( array( @@ -151,7 +147,7 @@ public function test_cache_hits_should_be_sensitive_to_comment_type() { ); $this->assertSame( 2, $page_trackbacks ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $page_comments = get_page_of_comment( $comment, array( @@ -161,7 +157,7 @@ public function test_cache_hits_should_be_sensitive_to_comment_type() { ); $this->assertSame( 1, $page_comments ); - $this->assertNotEquals( $num_queries, $wpdb->num_queries ); + $this->assertNotEquals( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/comment/metaCache.php b/tests/phpunit/tests/comment/metaCache.php index bd96e8b3e1..5a6d716e91 100644 --- a/tests/phpunit/tests/comment/metaCache.php +++ b/tests/phpunit/tests/comment/metaCache.php @@ -11,9 +11,40 @@ class Tests_Comment_MetaCache extends WP_UnitTestCase { * * @covers ::update_comment_meta */ - public function test_update_comment_meta_cache_should_default_to_true() { - global $wpdb; + public function test_update_comment_meta_cache_should_default_to_lazy_loading() { + $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + $comment_ids = self::factory()->comment->create_post_comments( $p, 3 ); + + foreach ( $comment_ids as $cid ) { + update_comment_meta( $cid, 'foo', 'bar' ); + } + // Clear comment cache, just in case. + clean_comment_cache( $comment_ids ); + + $num_queries = get_num_queries(); + $q = new WP_Comment_Query( + array( + 'post_ID' => $p, + ) + ); + + $this->assertSame( 2, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' ); + + $num_queries = get_num_queries(); + foreach ( $comment_ids as $cid ) { + get_comment_meta( $cid, 'foo', 'bar' ); + } + + $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' ); + } + + /** + * @ticket 57801 + * + * @covers ::wp_lazyload_comment_meta + */ + public function test_update_comment_meta_cache_should_default_to_lazy_loading_fields_id() { $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); $comment_ids = self::factory()->comment->create_post_comments( $p, 3 ); @@ -24,18 +55,22 @@ public function test_update_comment_meta_cache_should_default_to_true() { // Clear comment cache, just in case. clean_comment_cache( $comment_ids ); - $q = new WP_Comment_Query( + $num_queries = get_num_queries(); + $q = new WP_Comment_Query( array( 'post_ID' => $p, + 'fields' => 'ids', ) ); - $num_queries = $wpdb->num_queries; + $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' ); + + $num_queries = get_num_queries(); foreach ( $comment_ids as $cid ) { get_comment_meta( $cid, 'foo', 'bar' ); } - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta is expected to be lazy loaded' ); } /** @@ -44,8 +79,6 @@ public function test_update_comment_meta_cache_should_default_to_true() { * @covers ::update_comment_meta */ public function test_update_comment_meta_cache_true() { - global $wpdb; - $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); $comment_ids = self::factory()->comment->create_post_comments( $p, 3 ); @@ -56,19 +89,59 @@ public function test_update_comment_meta_cache_true() { // Clear comment cache, just in case. clean_comment_cache( $comment_ids ); - $q = new WP_Comment_Query( + $num_queries = get_num_queries(); + $q = new WP_Comment_Query( array( 'post_ID' => $p, 'update_comment_meta_cache' => true, ) ); + $this->assertSame( 2, get_num_queries() - $num_queries, 'Comments should be queries and primed in two database queries' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $comment_ids as $cid ) { get_comment_meta( $cid, 'foo', 'bar' ); } - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' ); + } + + /** + * @ticket 57801 + * + * @covers ::update_comment_meta + */ + public function test_update_comment_meta_cache_true_multiple() { + $posts = self::factory()->post->create_many( 3 ); + $all_comment_ids = array(); + foreach ( $posts as $p ) { + $comment_ids = self::factory()->comment->create_post_comments( $p, 3 ); + + foreach ( $comment_ids as $cid ) { + update_comment_meta( $cid, 'foo', 'bar' ); + $all_comment_ids[] = $cid; + } + + $num_queries = get_num_queries(); + $q = new WP_Comment_Query( + array( + 'post_ID' => $p, + 'update_comment_meta_cache' => true, + ) + ); + $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment query should only add one query' ); + } + + $filter = new MockAction(); + add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + $num_queries = get_num_queries(); + get_comment_meta( $comment_ids[0], 'foo', 'bar' ); + + $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' ); + $args = $filter->get_args(); + $first = reset( $args ); + $prime_comment_ids = end( $first ); + $this->assertSameSets( $prime_comment_ids, $all_comment_ids, 'All comment meta should be loaded all at once' ); } /** @@ -77,8 +150,6 @@ public function test_update_comment_meta_cache_true() { * @covers ::update_comment_meta */ public function test_update_comment_meta_cache_false() { - global $wpdb; - $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); $comment_ids = self::factory()->comment->create_post_comments( $p, 3 ); @@ -93,12 +164,12 @@ public function test_update_comment_meta_cache_false() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $comment_ids as $cid ) { get_comment_meta( $cid, 'foo', 'bar' ); } - $this->assertSame( $num_queries + 3, $wpdb->num_queries ); + $this->assertSame( 3, get_num_queries() - $num_queries ); } /** @@ -107,8 +178,6 @@ public function test_update_comment_meta_cache_false() { * @covers ::get_comment_meta */ public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comments_template() { - global $wpdb; - $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); $comment_ids = self::factory()->comment->create_post_comments( $p, 3 ); @@ -126,14 +195,14 @@ public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comm $cform = get_echo( 'comments_template' ); // First request will hit the database. - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); get_comment_meta( $comment_ids[0], 'sauce' ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( 1, get_num_queries() - $num_queries ); // Second and third requests should be in cache. get_comment_meta( $comment_ids[1], 'sauce' ); get_comment_meta( $comment_ids[2], 'sauce' ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( 1, get_num_queries() - $num_queries ); } } } @@ -142,10 +211,9 @@ public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comm * @ticket 34047 * * @covers ::get_comment_meta + * @covers ::wp_lazyload_comment_meta */ public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries() { - global $wpdb; - $posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) ); $now = time(); @@ -173,29 +241,28 @@ public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries( ); // First comment will cause the cache to be primed. - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Second comment from the results should not cause more queries. $this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // A comment from outside the results will not be primed. $this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 34047 * * @covers ::get_comment_meta + * @covers ::wp_lazyload_comment_meta */ public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_feed_queries() { - global $wpdb; - $posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) ); $now = time(); @@ -224,19 +291,19 @@ public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_f ); // First comment will cause the cache to be primed. - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Second comment from the results should not cause more queries. $this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // A comment from outside the results will not be primed. $this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 0f122a59d4..65f9af6253 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -3585,8 +3585,6 @@ public function test_comment_query_object() { * @covers WP_Comment_Query::query */ public function test_comment_cache_key_should_ignore_custom_params() { - global $wpdb; - $p = self::factory()->post->create(); $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) ); @@ -3598,7 +3596,7 @@ public function test_comment_cache_key_should_ignore_custom_params() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q2 = new WP_Comment_Query(); $q2->query( @@ -3609,7 +3607,7 @@ public function test_comment_cache_key_should_ignore_custom_params() { ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -3626,7 +3624,7 @@ public function test_cache_should_be_sensitive_to_parent__in() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q2 = new WP_Comment_Query( array( @@ -3634,7 +3632,7 @@ public function test_cache_should_be_sensitive_to_parent__in() { ) ); - $this->assertNotEquals( $num_queries, $wpdb->num_queries ); + $this->assertNotEquals( $num_queries, get_num_queries() ); } /** @@ -3651,7 +3649,7 @@ public function test_cache_should_be_sensitive_to_parent__not_in() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q2 = new WP_Comment_Query( array( @@ -3659,7 +3657,7 @@ public function test_cache_should_be_sensitive_to_parent__not_in() { ) ); - $this->assertNotEquals( $num_queries, $wpdb->num_queries ); + $this->assertNotEquals( $num_queries, get_num_queries() ); } /** @@ -4423,7 +4421,7 @@ public function test_cache_should_be_hit_when_querying_descendants() { ); $q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q2 = new WP_Comment_Query( array( 'post_id' => $p, @@ -4433,7 +4431,7 @@ public function test_cache_should_be_hit_when_querying_descendants() { $q2_ids = wp_list_pluck( $q2->comments, 'comment_ID' ); $this->assertSameSets( $q1_ids, $q2_ids ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -4586,9 +4584,9 @@ public function test_update_comment_post_cache_should_be_disabled_by_default() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertTrue( isset( $q->comments[0]->post_name ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); } /** @@ -4609,9 +4607,9 @@ public function test_should_respect_update_comment_post_cache_true() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertTrue( isset( $q->comments[0]->post_name ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -4625,7 +4623,7 @@ public function test_comment_objects_should_be_filled_from_cache() { $comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => self::$post_id ) ); clean_comment_cache( $comments ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( 'post_id' => self::$post_id, @@ -4641,7 +4639,7 @@ public function test_comment_objects_should_be_filled_from_cache() { $found = wp_list_pluck( $q->comments, 'comment_ID' ); $this->assertEqualSets( $comments, $found ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -4688,7 +4686,7 @@ public function test_comment_query_should_be_cached() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q2 = new WP_Comment_Query( array( @@ -4697,7 +4695,7 @@ public function test_comment_query_should_be_cached() { ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -4720,7 +4718,7 @@ public function test_created_comment_should_invalidate_query_cache() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( @@ -4729,7 +4727,7 @@ public function test_created_comment_should_invalidate_query_cache() { ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSameSets( array( $c ), $q->comments ); } @@ -4762,7 +4760,7 @@ public function test_updated_comment_should_invalidate_query_cache() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( @@ -4772,7 +4770,7 @@ public function test_updated_comment_should_invalidate_query_cache() { ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSameSets( array( $c ), $q->comments ); } @@ -4798,7 +4796,7 @@ public function test_deleted_comment_should_invalidate_query_cache() { wp_delete_comment( $c ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( @@ -4808,7 +4806,7 @@ public function test_deleted_comment_should_invalidate_query_cache() { ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSameSets( array(), $q->comments ); } @@ -4834,7 +4832,7 @@ public function test_trashed_comment_should_invalidate_query_cache() { wp_trash_comment( $c ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( @@ -4844,7 +4842,7 @@ public function test_trashed_comment_should_invalidate_query_cache() { ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSameSets( array(), $q->comments ); } @@ -4872,7 +4870,7 @@ public function test_untrashed_comment_should_invalidate_query_cache() { wp_untrash_comment( $c ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( @@ -4882,7 +4880,7 @@ public function test_untrashed_comment_should_invalidate_query_cache() { ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSameSets( array( $c ), $q->comments ); } @@ -4908,7 +4906,7 @@ public function test_spammed_comment_should_invalidate_query_cache() { wp_spam_comment( $c ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( @@ -4918,7 +4916,7 @@ public function test_spammed_comment_should_invalidate_query_cache() { ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSameSets( array(), $q->comments ); } @@ -4946,7 +4944,7 @@ public function test_unspammed_comment_should_invalidate_query_cache() { wp_unspam_comment( $c ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query( array( @@ -4956,7 +4954,7 @@ public function test_unspammed_comment_should_invalidate_query_cache() { ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSameSets( array( $c ), $q->comments ); } @@ -4978,7 +4976,7 @@ public function test_count_query_should_miss_noncount_cache() { ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -4988,7 +4986,7 @@ public function test_count_query_should_miss_noncount_cache() { 'count' => true, ) ); - $this->assertSame( $number_of_queries + 1, $wpdb->num_queries ); + $this->assertSame( $number_of_queries + 1, get_num_queries() ); } /** @@ -5009,7 +5007,7 @@ public function test_count_query_should_hit_count_cache() { 'count' => true, ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -5019,7 +5017,7 @@ public function test_count_query_should_hit_count_cache() { 'count' => true, ) ); - $this->assertSame( $number_of_queries, $wpdb->num_queries ); + $this->assertSame( $number_of_queries, get_num_queries() ); } /** @@ -5039,7 +5037,7 @@ public function test_different_values_of_fields_should_share_cached_values() { 'order' => 'ASC', ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -5049,7 +5047,7 @@ public function test_different_values_of_fields_should_share_cached_values() { ) ); - $this->assertSame( $number_of_queries, $wpdb->num_queries ); + $this->assertSame( $number_of_queries, get_num_queries() ); } /** @@ -5215,7 +5213,7 @@ public function test_comments_pre_query_filter_should_bypass_database_query() { add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Comment_Query(); $results = $q->query( array() ); @@ -5223,7 +5221,7 @@ public function test_comments_pre_query_filter_should_bypass_database_query() { remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 ); // Make sure no queries were executed. - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // We manually inserted a non-existing site and overrode the results with it. $this->assertSame( array( 555 ), $results ); diff --git a/tests/phpunit/tests/comment/wpUpdateCommentCountNow.php b/tests/phpunit/tests/comment/wpUpdateCommentCountNow.php index 42af1b0b5c..76f890f90f 100644 --- a/tests/phpunit/tests/comment/wpUpdateCommentCountNow.php +++ b/tests/phpunit/tests/comment/wpUpdateCommentCountNow.php @@ -14,16 +14,14 @@ public function test_invalid_post_bails_early() { } public function test_regular_post_updates_comment_count() { - global $wpdb; - $post_id = self::factory()->post->create(); self::factory()->comment->create_post_comments( $post_id, 1 ); $this->assertSame( '1', get_comments_number( $post_id ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertTrue( wp_update_comment_count_now( $post_id ) ); - $this->assertSame( $num_queries + 2, $wpdb->num_queries ); + $this->assertSame( $num_queries + 2, get_num_queries() ); $this->assertSame( '1', get_comments_number( $post_id ) ); } @@ -38,10 +36,10 @@ public function test_using_filter_adjusts_comment_count_without_an_additional_da self::factory()->comment->create_post_comments( $post_id, 1 ); $this->assertSame( '100', get_comments_number( $post_id ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertTrue( wp_update_comment_count_now( $post_id ) ); // Only one query is made instead of two. - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); $this->assertSame( '100', get_comments_number( $post_id ) ); diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index 43fe57032d..73242e3c53 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -271,7 +271,7 @@ public function test_setup_theme_in_customize_admin() { * @ticket 41039 */ public function test_fresh_site_flag_clearing() { - global $wp_customize, $wpdb; + global $wp_customize; // Make sure fresh site flag is cleared when publishing a changeset. update_option( 'fresh_site', '1' ); @@ -283,9 +283,9 @@ public function test_fresh_site_flag_clearing() { wp_load_alloptions(); // Make sure no DB write is done when publishing and a site is already non-fresh. - $query_count = $wpdb->num_queries; + $query_count = get_num_queries(); do_action( 'customize_save_after', $wp_customize ); - $this->assertSame( $query_count, $wpdb->num_queries ); + $this->assertSame( $query_count, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/general/wpGetArchives.php b/tests/phpunit/tests/general/wpGetArchives.php index 5409795fd7..8d506a6bb2 100644 --- a/tests/phpunit/tests/general/wpGetArchives.php +++ b/tests/phpunit/tests/general/wpGetArchives.php @@ -16,13 +16,11 @@ public function set_up() { * @ticket 23206 */ public function test_get_archives_cache() { - global $wpdb; - self::factory()->post->create_many( 3, array( 'post_type' => 'post' ) ); wp_cache_delete( 'last_changed', 'posts' ); $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Cache is not primed, expect 1 query. $result = wp_get_archives( @@ -34,9 +32,9 @@ public function test_get_archives_cache() { $this->assertIsString( $result ); $time1 = wp_cache_get( 'last_changed', 'posts' ); $this->assertNotEmpty( $time1 ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Cache is primed, expect no queries. $result = wp_get_archives( @@ -47,7 +45,7 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Change args, resulting in a different query string. Cache is not primed, expect 1 query. $result = wp_get_archives( @@ -59,9 +57,9 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Cache is primed, expect no queries. $result = wp_get_archives( @@ -73,9 +71,9 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Change type. Cache is not primed, expect 1 query. $result = wp_get_archives( @@ -86,9 +84,9 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Cache is primed, expect no queries. $result = wp_get_archives( @@ -99,7 +97,7 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Change type. Cache is not primed, expect 1 query. $result = wp_get_archives( @@ -110,9 +108,9 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Cache is primed, expect no queries. $result = wp_get_archives( @@ -123,7 +121,7 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Change type. Cache is not primed, expect 1 query. $result = wp_get_archives( @@ -134,9 +132,9 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Cache is primed, expect no queries. $result = wp_get_archives( @@ -147,7 +145,7 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Change type. Cache is not primed, expect 1 query. $result = wp_get_archives( @@ -158,9 +156,9 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Cache is primed, expect no queries. $result = wp_get_archives( @@ -171,6 +169,6 @@ public function test_get_archives_cache() { ); $this->assertIsString( $result ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } } diff --git a/tests/phpunit/tests/multisite/network.php b/tests/phpunit/tests/multisite/network.php index 630f76ebaf..db1f35b6d0 100644 --- a/tests/phpunit/tests/multisite/network.php +++ b/tests/phpunit/tests/multisite/network.php @@ -590,14 +590,12 @@ public function test_network_blog_id_set() { * @ticket 42251 */ public function test_get_network_not_found_cache() { - global $wpdb; - $new_network_id = $this->_get_next_network_id(); $this->assertNull( get_network( $new_network_id ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertNull( get_network( $new_network_id ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 3af01a7353..4b201cd912 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -2169,14 +2169,12 @@ public function test_wpmu_new_blog_action_backward_compatible( $meta, $expected_ * @ticket 42251 */ public function test_get_site_not_found_cache() { - global $wpdb; - $new_site_id = $this->_get_next_site_id(); $this->assertNull( get_site( $new_site_id ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertNull( get_site( $new_site_id ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/multisite/siteMeta.php b/tests/phpunit/tests/multisite/siteMeta.php index 4a1a30fee2..ad59c75e34 100644 --- a/tests/phpunit/tests/multisite/siteMeta.php +++ b/tests/phpunit/tests/multisite/siteMeta.php @@ -220,8 +220,6 @@ public function test_site_meta_should_be_deleted_when_site_is_deleted() { } public function test_update_site_meta_cache() { - global $wpdb; - if ( ! is_site_meta_supported() ) { $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); } @@ -229,14 +227,12 @@ public function test_update_site_meta_cache() { update_site_meta( self::$site_id, 'foo', 'bar' ); update_sitemeta_cache( array( self::$site_id ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); get_site_meta( self::$site_id, 'foo', true ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } public function test_query_update_site_meta_cache_true() { - global $wpdb; - if ( ! is_site_meta_supported() ) { $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); } @@ -250,14 +246,84 @@ public function test_query_update_site_meta_cache_true() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); get_site_meta( self::$site_id, 'foo', true ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( 1, get_num_queries() - $num_queries ); } - public function test_query_update_site_meta_cache_false() { - global $wpdb; + /** + * @ticket 58185 + */ + public function test_lazy_load_site_meta() { + if ( ! is_site_meta_supported() ) { + $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); + } + + $filter = new MockAction(); + add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + + $q = new WP_Site_Query( + array( + 'ID' => self::$site_id, + ) + ); + + $this->assertSameSets( array( (string) self::$site_id ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the first test site' ); + + $q = new WP_Site_Query( + array( + 'ID' => self::$site_id2, + ) + ); + + $this->assertSameSets( array( (string) self::$site_id2 ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the second test site' ); + + get_site_meta( self::$site_id2 ); + + $args = $filter->get_args(); + $first = reset( $args ); + $site_ids = end( $first ); + $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two site\'s meta' ); + } + + /** + * @ticket 58185 + */ + public function test_lazy_load_site_meta_fields_id() { + if ( ! is_site_meta_supported() ) { + $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); + } + + $filter = new MockAction(); + add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + + $q = new WP_Site_Query( + array( + 'ID' => self::$site_id, + 'fields' => 'ids', + ) + ); + + $this->assertSameSets( array( self::$site_id ), $q->sites, 'Site query should return the first test site' ); + + $q = new WP_Site_Query( + array( + 'ID' => self::$site_id2, + 'fields' => 'ids', + ) + ); + $this->assertSameSets( array( self::$site_id2 ), $q->sites, 'Site query should return the second test site' ); + + get_site_meta( self::$site_id2 ); + + $args = $filter->get_args(); + $first = reset( $args ); + $site_ids = end( $first ); + $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two sites meta' ); + } + + public function test_query_update_site_meta_cache_false() { if ( ! is_site_meta_supported() ) { $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); } @@ -271,9 +337,9 @@ public function test_query_update_site_meta_cache_false() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); get_site_meta( self::$site_id, 'foo', true ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( 1, get_num_queries() - $num_queries ); } /** diff --git a/tests/phpunit/tests/multisite/wpNetworkQuery.php b/tests/phpunit/tests/multisite/wpNetworkQuery.php index 29b2b8f5f7..f59a4c6628 100644 --- a/tests/phpunit/tests/multisite/wpNetworkQuery.php +++ b/tests/phpunit/tests/multisite/wpNetworkQuery.php @@ -428,8 +428,6 @@ public function test_wp_network_query_by_path_order_by_domain_desc() { * @ticket 41347 */ public function test_wp_network_query_cache_with_different_fields_no_count() { - global $wpdb; - $q = new WP_Network_Query(); $query_1 = $q->query( array( @@ -438,7 +436,7 @@ public function test_wp_network_query_cache_with_different_fields_no_count() { 'order' => 'ASC', ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -448,15 +446,13 @@ public function test_wp_network_query_cache_with_different_fields_no_count() { ) ); - $this->assertSame( $number_of_queries, $wpdb->num_queries ); + $this->assertSame( $number_of_queries, get_num_queries() ); } /** * @ticket 41347 */ public function test_wp_network_query_cache_with_different_fields_active_count() { - global $wpdb; - $q = new WP_Network_Query(); $query_1 = $q->query( @@ -467,7 +463,7 @@ public function test_wp_network_query_cache_with_different_fields_active_count() 'count' => true, ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -477,15 +473,13 @@ public function test_wp_network_query_cache_with_different_fields_active_count() 'count' => true, ) ); - $this->assertSame( $number_of_queries, $wpdb->num_queries ); + $this->assertSame( $number_of_queries, get_num_queries() ); } /** * @ticket 41347 */ public function test_wp_network_query_cache_with_same_fields_different_count() { - global $wpdb; - $q = new WP_Network_Query(); $query_1 = $q->query( @@ -496,7 +490,7 @@ public function test_wp_network_query_cache_with_same_fields_different_count() { ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -506,7 +500,7 @@ public function test_wp_network_query_cache_with_same_fields_different_count() { 'count' => true, ) ); - $this->assertSame( $number_of_queries + 1, $wpdb->num_queries ); + $this->assertSame( $number_of_queries + 1, get_num_queries() ); } /** @@ -568,11 +562,9 @@ public function test_wp_network_query_cache_with_same_fields_different_cache_fie * @ticket 47599 */ public function test_networks_pre_query_filter_should_bypass_database_query() { - global $wpdb; - add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Network_Query(); $results = $q->query( array() ); @@ -580,7 +572,7 @@ public function test_networks_pre_query_filter_should_bypass_database_query() { remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 ); // Make sure no queries were executed. - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // We manually inserted a non-existing site and overrode the results with it. $this->assertSame( array( 555 ), $results ); diff --git a/tests/phpunit/tests/multisite/wpSiteQuery.php b/tests/phpunit/tests/multisite/wpSiteQuery.php index 8d1d84c1f0..2b2bed37de 100644 --- a/tests/phpunit/tests/multisite/wpSiteQuery.php +++ b/tests/phpunit/tests/multisite/wpSiteQuery.php @@ -779,7 +779,6 @@ public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domai * @ticket 41197 */ public function test_wp_site_query_cache_with_different_fields_no_count() { - global $wpdb; $q = new WP_Site_Query(); $query_1 = $q->query( array( @@ -789,7 +788,7 @@ public function test_wp_site_query_cache_with_different_fields_no_count() { 'order' => 'ASC', ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -800,14 +799,13 @@ public function test_wp_site_query_cache_with_different_fields_no_count() { ) ); - $this->assertSame( $number_of_queries, $wpdb->num_queries ); + $this->assertSame( $number_of_queries, get_num_queries() ); } /** * @ticket 41197 */ public function test_wp_site_query_cache_with_different_fields_active_count() { - global $wpdb; $q = new WP_Site_Query(); $query_1 = $q->query( @@ -819,7 +817,7 @@ public function test_wp_site_query_cache_with_different_fields_active_count() { 'count' => true, ) ); - $number_of_queries = $wpdb->num_queries; + $number_of_queries = get_num_queries(); $query_2 = $q->query( array( @@ -830,14 +828,13 @@ public function test_wp_site_query_cache_with_different_fields_active_count() { 'count' => true, ) ); - $this->assertSame( $number_of_queries, $wpdb->num_queries ); + $this->assertSame( $number_of_queries, get_num_queries() ); } /** * @ticket 41197 */ public function test_wp_site_query_cache_with_same_fields_different_count() { - global $wpdb; $q = new WP_Site_Query(); $query_1 = $q->query( @@ -860,7 +857,7 @@ public function test_wp_site_query_cache_with_same_fields_different_count() { 'count' => true, ) ); - $this->assertSame( $number_of_queries + 1, $wpdb->num_queries ); + $this->assertSame( $number_of_queries + 1, get_num_queries() ); } /** @@ -1119,11 +1116,9 @@ public function data_wp_site_query_meta_query() { * @ticket 47599 */ public function test_sites_pre_query_filter_should_bypass_database_query() { - global $wpdb; - add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Site_Query(); $results = $q->query( array() ); @@ -1131,7 +1126,7 @@ public function test_sites_pre_query_filter_should_bypass_database_query() { remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 ); // Make sure no queries were executed. - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // We manually inserted a non-existing site and overrode the results with it. $this->assertSame( array( 555 ), $results ); diff --git a/tests/phpunit/tests/option/updateOption.php b/tests/phpunit/tests/option/updateOption.php index 7832a7f8fb..1be8aa0cf8 100644 --- a/tests/phpunit/tests/option/updateOption.php +++ b/tests/phpunit/tests/option/updateOption.php @@ -28,7 +28,6 @@ public function test_should_respect_default_option_filter_when_option_does_not_y * @covers ::get_option */ public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_missing() { - global $wpdb; $this->flush_cache(); update_option( 'test_update_option_default', 'value' ); $this->flush_cache(); @@ -36,9 +35,9 @@ public function test_should_set_autoload_yes_for_nonexistent_option_when_autoloa // Populate the alloptions cache, which includes autoload=yes options. wp_load_alloptions(); - $before = $wpdb->num_queries; + $before = get_num_queries(); $value = get_option( 'test_update_option_default' ); - $after = $wpdb->num_queries; + $after = get_num_queries(); $this->assertSame( $before, $after ); $this->assertSame( $value, 'value' ); @@ -52,7 +51,6 @@ public function test_should_set_autoload_yes_for_nonexistent_option_when_autoloa * @covers ::get_option */ public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_yes() { - global $wpdb; $this->flush_cache(); update_option( 'test_update_option_default', 'value', 'yes' ); $this->flush_cache(); @@ -60,9 +58,9 @@ public function test_should_set_autoload_yes_for_nonexistent_option_when_autoloa // Populate the alloptions cache, which includes autoload=yes options. wp_load_alloptions(); - $before = $wpdb->num_queries; + $before = get_num_queries(); $value = get_option( 'test_update_option_default' ); - $after = $wpdb->num_queries; + $after = get_num_queries(); $this->assertSame( $before, $after ); $this->assertSame( $value, 'value' ); @@ -76,7 +74,6 @@ public function test_should_set_autoload_yes_for_nonexistent_option_when_autoloa * @covers ::get_option */ public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_no() { - global $wpdb; $this->flush_cache(); update_option( 'test_update_option_default', 'value', 'no' ); $this->flush_cache(); @@ -84,9 +81,9 @@ public function test_should_set_autoload_no_for_nonexistent_option_when_autoload // Populate the alloptions cache, which does not include autoload=no options. wp_load_alloptions(); - $before = $wpdb->num_queries; + $before = get_num_queries(); $value = get_option( 'test_update_option_default' ); - $after = $wpdb->num_queries; + $after = get_num_queries(); // Database has been hit. $this->assertSame( $before + 1, $after ); @@ -101,7 +98,6 @@ public function test_should_set_autoload_no_for_nonexistent_option_when_autoload * @covers ::get_option */ public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_false() { - global $wpdb; $this->flush_cache(); update_option( 'test_update_option_default', 'value', false ); $this->flush_cache(); @@ -109,9 +105,9 @@ public function test_should_set_autoload_no_for_nonexistent_option_when_autoload // Populate the alloptions cache, which does not include autoload=no options. wp_load_alloptions(); - $before = $wpdb->num_queries; + $before = get_num_queries(); $value = get_option( 'test_update_option_default' ); - $after = $wpdb->num_queries; + $after = get_num_queries(); // Database has been hit. $this->assertSame( $before + 1, $after ); @@ -126,7 +122,6 @@ public function test_should_set_autoload_no_for_nonexistent_option_when_autoload * @covers ::get_option */ public function test_autoload_should_be_updated_for_existing_option_when_value_is_changed() { - global $wpdb; add_option( 'foo', 'bar', '', 'no' ); $updated = update_option( 'foo', 'bar2', true ); $this->assertTrue( $updated ); @@ -136,10 +131,10 @@ public function test_autoload_should_be_updated_for_existing_option_when_value_i // Populate the alloptions cache, which includes autoload=yes options. wp_load_alloptions(); - $before = $wpdb->num_queries; + $before = get_num_queries(); $value = get_option( 'foo' ); - $this->assertSame( $before, $wpdb->num_queries ); + $this->assertSame( $before, get_num_queries() ); $this->assertSame( $value, 'bar2' ); } @@ -151,7 +146,6 @@ public function test_autoload_should_be_updated_for_existing_option_when_value_i * @covers ::get_option */ public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_unchanged() { - global $wpdb; add_option( 'foo', 'bar', '', 'yes' ); $updated = update_option( 'foo', 'bar', false ); $this->assertFalse( $updated ); @@ -161,11 +155,11 @@ public function test_autoload_should_not_be_updated_for_existing_option_when_val // Populate the alloptions cache, which includes autoload=yes options. wp_load_alloptions(); - $before = $wpdb->num_queries; + $before = get_num_queries(); $value = get_option( 'foo' ); // 'foo' should still be autoload=yes, so we should see no additional querios. - $this->assertSame( $before, $wpdb->num_queries ); + $this->assertSame( $before, get_num_queries() ); $this->assertSame( $value, 'bar' ); } @@ -177,7 +171,6 @@ public function test_autoload_should_not_be_updated_for_existing_option_when_val * @covers ::get_option */ public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_changed_but_no_value_of_autoload_is_provided() { - global $wpdb; add_option( 'foo', 'bar', '', 'yes' ); // Don't pass a value for `$autoload`. @@ -189,11 +182,11 @@ public function test_autoload_should_not_be_updated_for_existing_option_when_val // Populate the alloptions cache, which includes autoload=yes options. wp_load_alloptions(); - $before = $wpdb->num_queries; + $before = get_num_queries(); $value = get_option( 'foo' ); // 'foo' should still be autoload=yes, so we should see no additional queries. - $this->assertSame( $before, $wpdb->num_queries ); + $this->assertSame( $before, get_num_queries() ); $this->assertSame( $value, 'bar2' ); } diff --git a/tests/phpunit/tests/option/wpLoadAllOptions.php b/tests/phpunit/tests/option/wpLoadAllOptions.php index 56533255fb..329dc64e6e 100644 --- a/tests/phpunit/tests/option/wpLoadAllOptions.php +++ b/tests/phpunit/tests/option/wpLoadAllOptions.php @@ -34,10 +34,9 @@ public function test_if_cached_alloptions_is_deleted() { * @covers ::wp_load_alloptions */ public function test_if_alloptions_are_retrieved_from_cache() { - global $wpdb; - $before = $wpdb->num_queries; + $before = get_num_queries(); wp_load_alloptions(); - $after = $wpdb->num_queries; + $after = get_num_queries(); // Database has not been hit. $this->assertSame( $before, $after ); @@ -49,14 +48,12 @@ public function test_if_alloptions_are_retrieved_from_cache() { * @covers ::wp_load_alloptions */ public function test_if_alloptions_are_retrieved_from_database() { - global $wpdb; - // Delete the existing cache first. wp_cache_delete( 'alloptions', 'options' ); - $before = $wpdb->num_queries; + $before = get_num_queries(); wp_load_alloptions(); - $after = $wpdb->num_queries; + $after = get_num_queries(); // Database has been hit. $this->assertSame( $before + 1, $after ); diff --git a/tests/phpunit/tests/post/getPageByPath.php b/tests/phpunit/tests/post/getPageByPath.php index 7e89a3ffc0..5d26b88d2d 100644 --- a/tests/phpunit/tests/post/getPageByPath.php +++ b/tests/phpunit/tests/post/getPageByPath.php @@ -260,8 +260,6 @@ public function test_should_not_match_parts_out_of_order() { * @ticket 36711 */ public function test_should_hit_cache() { - global $wpdb; - $page = self::factory()->post->create( array( 'post_type' => 'page', @@ -273,35 +271,33 @@ public function test_should_hit_cache() { $found = get_page_by_path( 'foo' ); $this->assertSame( $page, $found->ID ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_page_by_path( 'foo' ); $this->assertSame( $page, $found->ID ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 36711 */ public function test_bad_path_should_be_cached() { - global $wpdb; - // Prime cache. $found = get_page_by_path( 'foo' ); $this->assertNull( $found ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_page_by_path( 'foo' ); $this->assertNull( $found ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 36711 */ public function test_bad_path_served_from_cache_should_not_fall_back_on_current_post() { - global $wpdb, $post; + global $post; // Fake the global. $post = self::factory()->post->create_and_get(); @@ -310,11 +306,11 @@ public function test_bad_path_served_from_cache_should_not_fall_back_on_current_ $found = get_page_by_path( 'foo' ); $this->assertNull( $found ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_page_by_path( 'foo' ); $this->assertNull( $found ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); unset( $post ); } @@ -323,8 +319,6 @@ public function test_bad_path_served_from_cache_should_not_fall_back_on_current_ * @ticket 36711 */ public function test_cache_should_not_match_post_in_different_post_type_with_same_path() { - global $wpdb; - register_post_type( 'wptests_pt' ); $p1 = self::factory()->post->create( @@ -345,20 +339,18 @@ public function test_cache_should_not_match_post_in_different_post_type_with_sam $found = get_page_by_path( 'foo' ); $this->assertSame( $p1, $found->ID ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_page_by_path( 'foo', OBJECT, 'wptests_pt' ); $this->assertSame( $p2, $found->ID ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 36711 */ public function test_cache_should_be_invalidated_when_post_name_is_edited() { - global $wpdb; - $page = self::factory()->post->create( array( 'post_type' => 'page', @@ -377,12 +369,12 @@ public function test_cache_should_be_invalidated_when_post_name_is_edited() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_page_by_path( 'bar' ); $this->assertSame( $page, $found->ID ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index 5f11dcd7b7..29c14e7a7d 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -9,8 +9,6 @@ class Tests_Post_GetPages extends WP_UnitTestCase { * @ticket 23167 */ public function test_get_pages_cache() { - global $wpdb; - self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) ); wp_cache_delete( 'last_changed', 'posts' ); $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) ); @@ -19,7 +17,7 @@ public function test_get_pages_cache() { $this->assertCount( 3, $pages ); $time1 = wp_cache_get( 'last_changed', 'posts' ); $this->assertNotEmpty( $time1 ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $pages as $page ) { $this->assertInstanceOf( 'WP_Post', $page ); } @@ -28,7 +26,7 @@ public function test_get_pages_cache() { $pages = get_pages(); $this->assertCount( 3, $pages ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); foreach ( $pages as $page ) { $this->assertInstanceOf( 'WP_Post', $page ); } @@ -38,18 +36,18 @@ public function test_get_pages_cache() { $pages = get_pages( array( 'number' => 2 ) ); $this->assertCount( 2, $pages ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); foreach ( $pages as $page ) { $this->assertInstanceOf( 'WP_Post', $page ); } - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Again. num_queries and last_changed should remain the same. $pages = get_pages( array( 'number' => 2 ) ); $this->assertCount( 2, $pages ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); foreach ( $pages as $page ) { $this->assertInstanceOf( 'WP_Post', $page ); } @@ -58,7 +56,7 @@ public function test_get_pages_cache() { $pages = get_pages(); $this->assertCount( 3, $pages ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); foreach ( $pages as $page ) { $this->assertInstanceOf( 'WP_Post', $page ); } @@ -66,14 +64,14 @@ public function test_get_pages_cache() { // Force last_changed to increment. clean_post_cache( $pages[0]->ID ); $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) ); - - $num_queries = $wpdb->num_queries; + get_post( $pages[0]->ID ); + $num_queries = get_num_queries(); // last_changed bumped so num_queries should increment. $pages = get_pages( array( 'number' => 2 ) ); $this->assertCount( 2, $pages ); $this->assertSame( $time2, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); foreach ( $pages as $page ) { $this->assertInstanceOf( 'WP_Post', $page ); } @@ -86,14 +84,14 @@ public function test_get_pages_cache() { $new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) ); $this->assertGreaterThan( $old_changed_float, $new_changed_float ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $last_changed = wp_cache_get( 'last_changed', 'posts' ); // num_queries should bump after wp_delete_post() bumps last_changed. $pages = get_pages(); $this->assertCount( 2, $pages ); $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'posts' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); foreach ( $pages as $page ) { $this->assertInstanceOf( 'WP_Post', $page ); } diff --git a/tests/phpunit/tests/post/getPostClass.php b/tests/phpunit/tests/post/getPostClass.php index faedf73c96..64f60a2636 100644 --- a/tests/phpunit/tests/post/getPostClass.php +++ b/tests/phpunit/tests/post/getPostClass.php @@ -121,8 +121,6 @@ public function test_with_utf8_term_slugs() { * @group cache */ public function test_taxonomy_classes_hit_cache() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'wptests_tax' ); wp_set_post_terms( $this->post_id, array( 'footag', 'bartag' ), 'post_tag' ); @@ -131,10 +129,10 @@ public function test_taxonomy_classes_hit_cache() { update_object_term_cache( $this->post_id, 'post' ); update_meta_cache( 'post', $this->post_id ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_post_class( '', $this->post_id ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } } diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index 865f2f1b64..a5c5a73ca8 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -258,6 +258,7 @@ public function test_update_menu_item_cache_primes_terms() { add_filter( 'update_term_metadata_cache', array( $action, 'filter' ), 10, 2 ); update_menu_item_cache( $query_result ); + get_term_meta( $term_id ); $args = $action->get_args(); $last = end( $args ); @@ -337,12 +338,14 @@ public function test_wp_get_nav_menu_items_cache_primes_terms() { $start_num_queries = get_num_queries(); wp_get_nav_menu_items( $this->menu_id ); + get_term_meta( $term_ids[0] ); $queries_made = get_num_queries() - $start_num_queries; $this->assertSame( 6, $queries_made, 'Only does 6 database queries when running wp_get_nav_menu_items.' ); $args = $action_terms->get_args(); - $last = end( $args ); - $this->assertSameSets( $term_ids, $last[1], '_prime_term_caches() was not executed.' ); + $first = reset( $args ); + $term_ids[] = $this->menu_id; + $this->assertSameSets( $term_ids, $first[1], '_prime_term_caches() was not executed.' ); $args = $action_posts->get_args(); $this->assertSameSets( $menu_nav_ids, $args[0][1], '_prime_post_caches() was not executed.' ); diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index da27d40328..5fa3d79c0b 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -511,11 +511,9 @@ public function test_post_name__in() { * @ticket 36687 */ public function test_posts_pre_query_filter_should_bypass_database_query() { - global $wpdb; - add_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Query( array( 'fields' => 'ids', @@ -525,7 +523,7 @@ public function test_posts_pre_query_filter_should_bypass_database_query() { remove_filter( 'posts_pre_query', array( __CLASS__, 'filter_posts_pre_query' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSame( array( 12345 ), $q->posts ); } diff --git a/tests/phpunit/tests/query/commentFeed.php b/tests/phpunit/tests/query/commentFeed.php index 35c4eb0b60..23a37f2b40 100644 --- a/tests/phpunit/tests/query/commentFeed.php +++ b/tests/phpunit/tests/query/commentFeed.php @@ -28,7 +28,6 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { * @ticket 36904 */ public function test_archive_comment_feed() { - global $wpdb; add_filter( 'split_the_query', '__return_false' ); $q1 = new WP_Query(); $args = array( @@ -42,12 +41,12 @@ public function test_archive_comment_feed() { 'cache_results' => false, ); $q1->query( $args ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q2 = new WP_Query(); $q2->query( $args ); $this->assertTrue( $q2->is_comment_feed() ); $this->assertFalse( $q2->is_singular() ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); } /** @@ -87,7 +86,6 @@ public function test_archive_comment_feed_invalid_cache() { * @ticket 36904 */ public function test_single_comment_feed() { - global $wpdb; $post = get_post( self::$post_ids[0] ); $q1 = new WP_Query(); @@ -103,12 +101,12 @@ public function test_single_comment_feed() { ); $q1->query( $args ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q2 = new WP_Query(); $q2->query( $args ); $this->assertTrue( $q2->is_comment_feed() ); $this->assertTrue( $q2->is_singular() ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); } } diff --git a/tests/phpunit/tests/query/lazyLoadCommentMeta.php b/tests/phpunit/tests/query/lazyLoadCommentMeta.php new file mode 100644 index 0000000000..420fd45b23 --- /dev/null +++ b/tests/phpunit/tests/query/lazyLoadCommentMeta.php @@ -0,0 +1,70 @@ +post->create(); + self::$comment_ids = $factory->comment->create_post_comments( self::$post_id, 11 ); + } + + /** + * @ticket 57901 + * + * @covers ::wp_queue_comments_for_comment_meta_lazyload + * + * @expectedDeprecated wp_queue_comments_for_comment_meta_lazyload + */ + public function test_wp_queue_comments_for_comment_meta_lazyload() { + $filter = new MockAction(); + add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + $comments = array_map( 'get_comment', self::$comment_ids ); + $comment_id = reset( self::$comment_ids ); + wp_queue_comments_for_comment_meta_lazyload( $comments ); + get_comment_meta( $comment_id ); + + $args = $filter->get_args(); + $first = reset( $args ); + $comment_meta_ids = end( $first ); + $this->assertSameSets( self::$comment_ids, $comment_meta_ids ); + } + + /** + * @ticket 57901 + * + * @covers ::wp_queue_comments_for_comment_meta_lazyload + * + * @expectedDeprecated wp_queue_comments_for_comment_meta_lazyload + */ + public function test_wp_queue_comments_for_comment_meta_lazyload_new_comment() { + $filter = new MockAction(); + add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + $comments = array_map( 'get_comment', self::$comment_ids ); + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + ) + ); + wp_queue_comments_for_comment_meta_lazyload( $comments ); + get_comment_meta( $comment_id ); + + $args = $filter->get_args(); + $first = reset( $args ); + $comment_meta_ids = end( $first ); + $this->assertContains( $comment_id, $comment_meta_ids ); + } +} diff --git a/tests/phpunit/tests/query/lazyLoadTermMeta.php b/tests/phpunit/tests/query/lazyLoadTermMeta.php index 0677a2d0bd..fc34f84bb3 100644 --- a/tests/phpunit/tests/query/lazyLoadTermMeta.php +++ b/tests/phpunit/tests/query/lazyLoadTermMeta.php @@ -45,6 +45,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { * @covers ::wp_queue_posts_for_term_meta_lazyload */ public function test_wp_queue_posts_for_term_meta_lazyload() { + $this->reset_lazyload_queue(); $filter = new MockAction(); add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 ); new WP_Query( @@ -108,6 +109,36 @@ public function test_wp_queue_posts_for_term_meta_lazyload_false() { $this->assertSameSets( $term_ids, array( $term_id ) ); } + + /** + * @ticket 57901 + * + * @covers ::wp_queue_posts_for_term_meta_lazyload + */ + public function test_wp_queue_posts_for_term_meta_lazyload_insert_term() { + $filter = new MockAction(); + add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + + register_taxonomy( 'wptests_tax', 'post' ); + + $t1 = wp_insert_term( 'Foo', 'wptests_tax' ); + $term_id = $t1['term_id']; + + new WP_Query( + array( + 'post__in' => self::$post_ids, + 'lazy_load_term_meta' => true, + ) + ); + + get_term_meta( $term_id ); + + $args = $filter->get_args(); + $first = reset( $args ); + $term_ids = end( $first ); + $this->assertContains( $term_id, $term_ids ); + } + /** * @ticket 57150 * @covers ::wp_queue_posts_for_term_meta_lazyload @@ -133,6 +164,6 @@ public function test_wp_queue_posts_for_term_meta_lazyload_delete_term() { $args = $filter->get_args(); $first = reset( $args ); $term_ids = end( $first ); - $this->assertNotContains( $remove_term_id, $term_ids ); + $this->assertContains( $remove_term_id, $term_ids ); } } diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php index 078c3b5663..6ab8a33464 100644 --- a/tests/phpunit/tests/rest-api/rest-tags-controller.php +++ b/tests/phpunit/tests/rest-api/rest-tags-controller.php @@ -1336,8 +1336,6 @@ public function additional_field_update_callback( $value, $tag ) { * @ticket 38504 */ public function test_object_term_queries_are_cached() { - global $wpdb; - $tags = self::factory()->tag->create_many( 2 ); $p = self::factory()->post->create(); wp_set_object_terms( $p, $tags[0], 'post_tag' ); @@ -1349,7 +1347,7 @@ public function test_object_term_queries_are_cached() { unset( $request, $response ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $request = new WP_REST_Request( 'GET', '/wp/v2/tags' ); $request->set_param( 'post', $p ); @@ -1357,7 +1355,7 @@ public function test_object_term_queries_are_cached() { $found_2 = wp_list_pluck( $response->data, 'id' ); $this->assertSameSets( $found_1, $found_2 ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/term/cache.php b/tests/phpunit/tests/term/cache.php index 3a12bc1b04..8efd55da00 100644 --- a/tests/phpunit/tests/term/cache.php +++ b/tests/phpunit/tests/term/cache.php @@ -101,8 +101,6 @@ public function test_hierachy_invalidation() { } public function test_get_term_should_update_term_cache_when_passed_an_object() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $term = self::factory()->term->create( array( @@ -116,7 +114,7 @@ public function test_get_term_should_update_term_cache_when_passed_an_object() { // Affirm that the cache is empty. $this->assertEmpty( wp_cache_get( $term, 'terms' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // get_term() will only be update the cache if the 'filter' prop is unset. unset( $term_object->filter ); @@ -124,13 +122,11 @@ public function test_get_term_should_update_term_cache_when_passed_an_object() { $term_object_2 = get_term( $term_object, 'wptests_tax' ); // No new queries should have fired. - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSame( $term_object, $term_object_2 ); } public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $term = self::factory()->term->create( array( @@ -143,23 +139,21 @@ public function test_get_term_should_update_term_cache_when_passed_a_valid_term_ // Affirm that the cache is empty. $this->assertEmpty( wp_cache_get( $term, 'terms' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Prime cache. $term_object = get_term( $term, 'wptests_tax' ); $this->assertNotEmpty( wp_cache_get( $term, 'terms' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); $term_object_2 = get_term( $term, 'wptests_tax' ); // No new queries should have fired. - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); $this->assertEquals( $term_object, $term_object_2 ); } public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $term = self::factory()->term->create( array( @@ -172,17 +166,17 @@ public function test_get_term_by_should_update_term_cache_when_passed_a_valid_te // Affirm that the cache is empty. $this->assertEmpty( wp_cache_get( $term, 'terms' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Prime cache. $term_object = get_term_by( 'id', $term, 'wptests_tax' ); $this->assertNotEmpty( wp_cache_get( $term, 'terms' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); $term_object_2 = get_term( $term, 'wptests_tax' ); // No new queries should have fired. - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); $this->assertEquals( $term_object, $term_object_2 ); } @@ -190,8 +184,6 @@ public function test_get_term_by_should_update_term_cache_when_passed_a_valid_te * @ticket 30749 */ public function test_get_terms_should_update_cache_for_located_terms() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( @@ -208,13 +200,13 @@ public function test_get_terms_should_update_cache_for_located_terms() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $terms as $term_id ) { get_term( $term_id, 'wptests_tax' ); } - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); _unregister_taxonomy( 'wptests_tax' ); } @@ -242,8 +234,6 @@ public function test_term_objects_should_not_be_modified_by_update_term_cache() * @ticket 21760 */ public function test_get_term_by_slug_cache() { - global $wpdb; - $term_id = self::factory()->term->create( array( 'slug' => 'burrito', @@ -253,28 +243,26 @@ public function test_get_term_by_slug_cache() { ); clean_term_cache( $term_id, 'post_tag' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $term = get_term_by( 'slug', 'burrito', 'post_tag' ); $num_queries = $num_queries + 2; $this->assertSame( 'Taco', $term->name ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // This should now hit cache. $term = get_term_by( 'slug', 'burrito', 'post_tag' ); $this->assertSame( 'Taco', $term->name ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertEquals( get_term( $term_id, 'post_tag' ), $term ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 21760 */ public function test_get_term_by_slug_cache_update() { - global $wpdb; - $term_id = self::factory()->term->create( array( 'slug' => 'burrito', @@ -284,35 +272,33 @@ public function test_get_term_by_slug_cache_update() { ); clean_term_cache( $term_id, 'post_tag' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $term = get_term_by( 'slug', 'burrito', 'post_tag' ); $num_queries = $num_queries + 2; $this->assertSame( 'Taco', $term->name ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // This should now hit cache. $term = get_term_by( 'slug', 'burrito', 'post_tag' ); $this->assertSame( 'Taco', $term->name ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Update the tag which invalidates the cache. wp_update_term( $term_id, 'post_tag', array( 'name' => 'No Taco' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // This should not hit cache. $term = get_term_by( 'slug', 'burrito', 'post_tag' ); $num_queries = $num_queries + 2; $this->assertSame( 'No Taco', $term->name ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 21760 */ public function test_get_term_by_name_cache() { - global $wpdb; - $term_id = self::factory()->term->create( array( 'name' => 'Burrito', @@ -322,26 +308,24 @@ public function test_get_term_by_name_cache() { ); clean_term_cache( $term_id, 'post_tag' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); get_term_by( 'name', 'Burrito', 'post_tag' ); $num_queries = $num_queries + 2; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // This should now hit cache. $term = get_term_by( 'name', 'Burrito', 'post_tag' ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertEquals( get_term( $term_id, 'post_tag' ), $term ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 21760 */ public function test_get_term_by_name_cache_update() { - global $wpdb; - $term_id = self::factory()->term->create( array( 'name' => 'Burrito', @@ -351,32 +335,30 @@ public function test_get_term_by_name_cache_update() { ); clean_term_cache( $term_id, 'post_tag' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); get_term_by( 'name', 'Burrito', 'post_tag' ); $num_queries = $num_queries + 2; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // This should now hit cache. get_term_by( 'name', 'Burrito', 'post_tag' ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Update the tag which invalidates the cache. wp_update_term( $term_id, 'post_tag', array( 'slug' => 'taco' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // This should not hit cache. get_term_by( 'name', 'burrito', 'post_tag' ); $num_queries = $num_queries + 2; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 21760 */ public function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() { - global $wpdb; - $term_id = self::factory()->term->create( array( 'name' => 'Burrito', @@ -385,7 +367,7 @@ public function test_invalidating_term_caches_should_fail_when_invalidation_is_s ); clean_term_cache( $term_id, 'post_tag' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $last_changed = wp_cache_get( 'last_changed', 'terms' ); $term1 = get_term_by( 'name', 'Burrito', 'post_tag' ); @@ -393,18 +375,18 @@ public function test_invalidating_term_caches_should_fail_when_invalidation_is_s // Verify the term is cached. $term2 = get_term_by( 'name', 'Burrito', 'post_tag' ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertEquals( $term1, $term2 ); $suspend = wp_suspend_cache_invalidation(); // Update the tag. wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Verify that the cached term still matches the initial cached term. $term3 = get_term_by( 'name', 'Burrito', 'post_tag' ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertEquals( $term1, $term3 ); // Verify that last changed has not been updated as part of an invalidation routine. @@ -418,8 +400,6 @@ public function test_invalidating_term_caches_should_fail_when_invalidation_is_s * @ticket 21760 */ public function test_get_term_by_does_not_prime_term_meta_cache() { - global $wpdb; - $term_id = self::factory()->term->create( array( 'name' => 'Burrito', @@ -429,18 +409,18 @@ public function test_get_term_by_does_not_prime_term_meta_cache() { add_term_meta( $term_id, 'foo', 'bar' ); clean_term_cache( $term_id, 'post_tag' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $term = get_term_by( 'name', 'Burrito', 'post_tag' ); $num_queries = $num_queries + 2; $this->assertInstanceOf( 'WP_Term', $term ); $this->assertSame( $term_id, $term->term_id ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $term_meta = get_term_meta( $term_id, 'foo', true ); $num_queries++; $this->assertSame( $term_meta, 'bar' ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/term/getTerm.php b/tests/phpunit/tests/term/getTerm.php index ec618e190d..879690f985 100644 --- a/tests/phpunit/tests/term/getTerm.php +++ b/tests/phpunit/tests/term/getTerm.php @@ -60,17 +60,15 @@ public function test_should_return_error_for_invalid_taxonomy() { } public function test_passing_term_object_should_skip_database_query_when_filter_property_is_empty() { - global $wpdb; - $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax' ) ); clean_term_cache( $term->term_id, 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); unset( $term->filter ); $term_a = get_term( $term, 'wptests_tax' ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } public function test_passing_term_string_that_casts_to_int_0_should_return_null() { @@ -82,18 +80,16 @@ public function test_should_return_null_for_invalid_term_id() { } public function test_cache_should_be_populated_by_successful_fetch() { - global $wpdb; - $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); clean_term_cache( $t, 'wptests_tax' ); // Prime cache. $term_a = get_term( $t, 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Second call shouldn't require a database query. $term_b = get_term( $t, 'wptests_tax' ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertEquals( $term_a, $term_b ); } @@ -196,19 +192,17 @@ public function test_should_return_null_when_term_is_shared_and_incorrect_taxono * @ticket 34533 */ public function test_shared_term_in_cache_should_be_ignored_when_specifying_a_different_taxonomy() { - global $wpdb; - $terms = $this->generate_shared_terms(); // Prime cache for 'wptests_tax'. get_term( $terms[0]['term_id'], 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Database should be hit again. $found = get_term( $terms[1]['term_id'], 'wptests_tax_2' ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertInstanceOf( 'WP_Term', $found ); $this->assertSame( 'wptests_tax_2', $found->taxonomy ); } diff --git a/tests/phpunit/tests/term/getTermBy.php b/tests/phpunit/tests/term/getTermBy.php index 6422e44f0a..32974d13c5 100644 --- a/tests/phpunit/tests/term/getTermBy.php +++ b/tests/phpunit/tests/term/getTermBy.php @@ -111,8 +111,6 @@ public function test_taxonomy_should_be_ignored_if_matching_by_term_taxonomy_id( * @ticket 14162 */ public function test_should_prime_term_cache() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $t = self::factory()->term->create( array( @@ -123,18 +121,18 @@ public function test_should_prime_term_cache() { clean_term_cache( $t, 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_term_by( 'slug', 'foo', 'wptests_tax' ); $num_queries = $num_queries + 2; $this->assertInstanceOf( 'WP_Term', $found ); $this->assertSame( $t, $found->term_id ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Calls to `get_term()` should now hit cache. $found2 = get_term( $t ); $this->assertSame( $t, $found->term_id ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 853b57512a..93ae1f95e8 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -134,26 +134,24 @@ public function test_excluding_taxonomy_arg_should_return_terms_from_all_taxonom * @ticket 23326 */ public function test_get_terms_cache() { - global $wpdb; - $this->set_up_three_posts_and_tags(); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // last_changed and num_queries should bump. $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) ); $this->assertCount( 3, $terms ); $time1 = wp_cache_get( 'last_changed', 'terms' ); $this->assertNotEmpty( $time1 ); - $this->assertSame( $num_queries + 2, $wpdb->num_queries ); + $this->assertSame( $num_queries + 2, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Again. last_changed and num_queries should remain the same. $terms = get_terms( 'post_tag', array( 'update_term_meta_cache' => false ) ); $this->assertCount( 3, $terms ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'terms' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -167,21 +165,21 @@ public function test_get_terms_cache_should_be_missed_when_passing_number() { // Prime cache. $terms = get_terms( 'post_tag' ); $time1 = wp_cache_get( 'last_changed', 'terms' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // num_queries should bump, last_changed should remain the same. $terms = get_terms( 'post_tag', array( 'number' => 2 ) ); $this->assertCount( 2, $terms ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'terms' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Again. last_changed and num_queries should remain the same. $terms = get_terms( 'post_tag', array( 'number' => 2 ) ); $this->assertCount( 2, $terms ); $this->assertSame( $time1, wp_cache_get( 'last_changed', 'terms' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -195,12 +193,12 @@ public function test_wp_delete_term_should_invalidate_cache() { // Prime cache. $terms = get_terms( 'post_tag' ); $time1 = wp_cache_get( 'last_changed', 'terms' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Force last_changed to bump. wp_delete_term( $terms[0]->term_id, 'post_tag' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $time2 = wp_cache_get( 'last_changed', 'terms' ); $this->assertNotEquals( $time1, $time2 ); @@ -208,15 +206,15 @@ public function test_wp_delete_term_should_invalidate_cache() { $terms = get_terms( 'post_tag' ); $this->assertCount( 2, $terms ); $this->assertSame( $time2, wp_cache_get( 'last_changed', 'terms' ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); // Again. last_changed and num_queries should remain the same. $terms = get_terms( 'post_tag' ); $this->assertCount( 2, $terms ); $this->assertSame( $time2, wp_cache_get( 'last_changed', 'terms' ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // @todo Repeat with term insert and update. } @@ -846,13 +844,11 @@ public function test_get_terms_child_of_fields_id_slug() { * @ticket 31118 */ public function test_child_of_should_skip_query_when_specified_parent_is_not_found_in_hierarchy_cache() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_terms( 'wptests_tax', @@ -863,7 +859,7 @@ public function test_child_of_should_skip_query_when_specified_parent_is_not_fou ); $this->assertEmpty( $found ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -2471,13 +2467,11 @@ public function test_parent_should_override_child_of() { * @ticket 31118 */ public function test_parent_should_skip_query_when_specified_parent_is_not_found_in_hierarchy_cache() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_terms( 'wptests_tax', @@ -2488,7 +2482,7 @@ public function test_parent_should_skip_query_when_specified_parent_is_not_found ); $this->assertEmpty( $found ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -2754,7 +2748,7 @@ public function test_pad_counts_should_work_when_first_taxonomy_is_nonhierarchic /** * @ticket 10142 */ - public function test_termmeta_cache_should_be_primed_by_default() { + public function test_termmeta_cache_should_be_lazy_loaded_by_default() { global $wpdb; register_taxonomy( 'wptests_tax', 'post' ); @@ -2771,13 +2765,13 @@ public function test_termmeta_cache_should_be_primed_by_default() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $terms as $t ) { $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); } - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); } /** @@ -2801,13 +2795,13 @@ public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_c ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $terms as $t ) { $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); } - $this->assertSame( $num_queries + 3, $wpdb->num_queries ); + $this->assertSame( $num_queries + 3, get_num_queries() ); } /** @@ -2906,7 +2900,7 @@ public function test_should_return_wp_term_objects_when_pulling_from_the_cache() ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_terms( 'wptests_tax', @@ -2916,7 +2910,7 @@ public function test_should_return_wp_term_objects_when_pulling_from_the_cache() ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertNotEmpty( $found ); @@ -2929,8 +2923,6 @@ public function test_should_return_wp_term_objects_when_pulling_from_the_cache() * @ticket 14162 */ public function test_should_prime_individual_term_cache_when_fields_is_all() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); @@ -2942,9 +2934,9 @@ public function test_should_prime_individual_term_cache_when_fields_is_all() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $term0 = get_term( $terms[0] ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/term/getTheTerms.php b/tests/phpunit/tests/term/getTheTerms.php index 46f16f980e..95c4080fcf 100644 --- a/tests/phpunit/tests/term/getTheTerms.php +++ b/tests/phpunit/tests/term/getTheTerms.php @@ -197,8 +197,6 @@ public function test_count_should_not_be_improperly_cached() { * @ticket 36814 */ public function test_uncached_terms_should_be_primed_with_a_single_query() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); @@ -210,13 +208,13 @@ public function test_uncached_terms_should_be_primed_with_a_single_query() { // Clean cache for two of the terms. clean_term_cache( array( $terms[0], $terms[1] ), 'wptests_tax', false ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $found = get_the_terms( self::$post_ids[0], 'wptests_tax' ); $this->assertSameSets( $terms, wp_list_pluck( $found, 'term_id' ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/term/isObjectInTerm.php b/tests/phpunit/tests/term/isObjectInTerm.php index f0984ef49b..fbaf7e1776 100644 --- a/tests/phpunit/tests/term/isObjectInTerm.php +++ b/tests/phpunit/tests/term/isObjectInTerm.php @@ -135,46 +135,42 @@ public function test_should_not_return_true_if_term_name_begins_with_existing_te * @ticket 32044 */ public function test_should_populate_and_hit_relationships_cache() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); $o = 12345; wp_set_object_terms( $o, $terms[0], 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) ); $num_queries = $num_queries + 2; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertFalse( is_object_in_term( $o, 'wptests_tax', $terms[1] ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** * @ticket 32044 */ public function test_should_not_be_fooled_by_a_stale_relationship_cache() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); $o = 12345; wp_set_object_terms( $o, $terms[0], 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) ); $num_queries = $num_queries + 2; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); wp_set_object_terms( $o, $terms[1], 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[1] ) ); $num_queries = $num_queries + 2; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/term/meta.php b/tests/phpunit/tests/term/meta.php index 976889fd8b..d46f35ae1d 100644 --- a/tests/phpunit/tests/term/meta.php +++ b/tests/phpunit/tests/term/meta.php @@ -114,11 +114,6 @@ public function test_update_should_return_true_when_updating_existing_value_for_ } public function test_term_meta_should_be_lazy_loaded_for_all_terms_in_wp_query_loop() { - global $wpdb; - - // Clear any previous term IDs from the queue. - wp_metadata_lazyloader()->reset_queue( 'term' ); - $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); register_taxonomy( 'wptests_tax', 'post' ); @@ -142,20 +137,20 @@ public function test_term_meta_should_be_lazy_loaded_for_all_terms_in_wp_query_l the_post(); // First request will hit the database. - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Second and third requests should be in cache. $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) ); $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // Querying a term not primed should result in a hit. $num_queries++; $this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } } } @@ -189,8 +184,6 @@ public function test_lazy_load_term_meta_should_fall_back_on_update_post_term_ca * @ticket 36593 */ public function test_lazy_load_term_meta_false() { - global $wpdb; - $p = self::factory()->post->create( array( 'post_status' => 'publish' ) ); register_taxonomy( 'wptests_tax', 'post' ); @@ -199,7 +192,7 @@ public function test_lazy_load_term_meta_false() { foreach ( $terms as $t ) { add_term_meta( $t, 'foo', 'bar' ); } - + $this->reset_lazyload_queue(); $q = new WP_Query( array( 'cache_results' => true, @@ -213,14 +206,14 @@ public function test_lazy_load_term_meta_false() { $q->the_post(); // Requests will hit the database. - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) ); $num_queries++; - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } } } diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 9390ed3fd5..0e118a3c16 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -46,6 +46,46 @@ public function test_taxonomy_should_accept_single_taxonomy_as_string() { $this->assertSameSets( array( $term_2 ), $q->terms ); } + /** + * @ticket 57645 + */ + public function test_lazy_load_term_meta() { + $filter = new MockAction(); + add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + register_taxonomy( 'wptests_tax_1', 'post' ); + register_taxonomy( 'wptests_tax_2', 'post' ); + + $term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) ); + $term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_2' ) ); + + $q = new WP_Term_Query( + array( + 'taxonomy' => 'wptests_tax_1', + 'fields' => 'ids', + 'hide_empty' => false, + ) + ); + + $this->assertSameSets( array( $term_1 ), $q->terms ); + + $q = new WP_Term_Query( + array( + 'taxonomy' => 'wptests_tax_2', + 'fields' => 'ids', + 'hide_empty' => false, + ) + ); + + $this->assertSameSets( array( $term_2 ), $q->terms ); + + get_term_meta( $term_1 ); + + $args = $filter->get_args(); + $first = reset( $args ); + $term_ids = end( $first ); + $this->assertSameSets( $term_ids, array( $term_1, $term_2 ) ); + } + public function test_taxonomy_should_accept_taxonomy_array() { register_taxonomy( 'wptests_tax_1', 'post' ); register_taxonomy( 'wptests_tax_2', 'post' ); @@ -416,8 +456,6 @@ public function test_object_ids_cache_should_be_invalidated_by_term_relationship * @group cache */ public function test_count_query_should_be_cached() { - global $wpdb; - register_taxonomy( 'wptests_tax_1', 'post' ); $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) ); @@ -432,7 +470,7 @@ public function test_count_query_should_be_cached() { $count = $query->get_terms(); $this->assertEquals( 2, $count ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $query = new WP_Term_Query( array( @@ -443,7 +481,7 @@ public function test_count_query_should_be_cached() { ); $count = $query->get_terms(); $this->assertEquals( 2, $count ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -763,11 +801,9 @@ public function filter_term_to_wp_error( $term ) { * @ticket 41246 */ public function test_terms_pre_query_filter_should_bypass_database_query() { - global $wpdb; - add_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_Term_Query(); $results = $q->query( @@ -779,7 +815,7 @@ public function test_terms_pre_query_filter_should_bypass_database_query() { remove_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 ); // Make sure no queries were executed. - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // We manually inserted a non-existing term and overrode the results with it. $this->assertSame( array( 555 ), $q->terms ); diff --git a/tests/phpunit/tests/term/termExists.php b/tests/phpunit/tests/term/termExists.php index c50cf7086d..caf88cc4de 100644 --- a/tests/phpunit/tests/term/termExists.php +++ b/tests/phpunit/tests/term/termExists.php @@ -320,7 +320,6 @@ public function test_term_lookup_by_slug_and_update() { * @covers ::term_exists() */ public function test_term_exists_caching() { - global $wpdb; register_taxonomy( 'wptests_tax', 'post' ); $slug = __FUNCTION__; @@ -331,14 +330,14 @@ public function test_term_exists_caching() { ) ); $this->assertEquals( $t, term_exists( $slug ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertEquals( $t, term_exists( $slug ) ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); $this->assertTrue( wp_delete_term( $t, 'wptests_tax' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertNull( term_exists( $slug ) ); - $this->assertSame( $num_queries + 2, $wpdb->num_queries ); + $this->assertSame( $num_queries + 2, get_num_queries() ); // Clean up. _unregister_taxonomy( 'wptests_tax' ); @@ -349,7 +348,6 @@ public function test_term_exists_caching() { * @covers ::term_exists() */ public function test_term_exists_caching_suspend_cache_invalidation() { - global $wpdb; register_taxonomy( 'wptests_tax', 'post' ); wp_suspend_cache_invalidation( true ); @@ -362,9 +360,9 @@ public function test_term_exists_caching_suspend_cache_invalidation() { ); $this->assertEquals( $t, term_exists( $slug ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $this->assertEquals( $t, term_exists( $slug ) ); - $this->assertSame( $num_queries + 1, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); wp_suspend_cache_invalidation( false ); // Clean up. diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php index 848abc3f45..e2d49bd04c 100644 --- a/tests/phpunit/tests/term/wpGetObjectTerms.php +++ b/tests/phpunit/tests/term/wpGetObjectTerms.php @@ -615,9 +615,7 @@ public function test_parent_0() { /** * @ticket 10142 */ - public function test_termmeta_cache_should_be_primed_by_default() { - global $wpdb; - + public function test_termmeta_cache_should_be_lazy_loaded_by_default() { register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); add_term_meta( $terms[0], 'foo', 'bar' ); @@ -629,21 +627,19 @@ public function test_termmeta_cache_should_be_primed_by_default() { $found = wp_get_object_terms( $p, 'wptests_tax' ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $terms as $t ) { $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); } - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); } /** * @ticket 10142 */ public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); add_term_meta( $terms[0], 'foo', 'bar' ); @@ -661,21 +657,19 @@ public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_c ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $terms as $t ) { $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); } - $this->assertSame( $num_queries + 3, $wpdb->num_queries ); + $this->assertSame( $num_queries + 3, get_num_queries() ); } /** * @ticket 36932 */ public function test_termmeta_cache_should_be_primed_when_fields_is_all_with_object_id() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); add_term_meta( $terms[0], 'foo', 'bar' ); @@ -694,21 +688,19 @@ public function test_termmeta_cache_should_be_primed_when_fields_is_all_with_obj ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $terms as $t ) { $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); } - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); } /** * @ticket 36932 */ public function test_termmeta_cache_should_be_primed_when_fields_is_ids() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); add_term_meta( $terms[0], 'foo', 'bar' ); @@ -727,13 +719,13 @@ public function test_termmeta_cache_should_be_primed_when_fields_is_ids() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); foreach ( $terms as $t ) { $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); } - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries + 1, get_num_queries() ); } /** @@ -816,8 +808,6 @@ public function test_should_return_wp_term_objects_for_fields_all_with_object_id * @ticket 14162 */ public function test_should_prime_cache_for_found_terms() { - global $wpdb; - register_taxonomy( 'wptests_tax', 'post' ); $p = self::factory()->post->create(); $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); @@ -831,9 +821,9 @@ public function test_should_prime_cache_for_found_terms() { ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $term = get_term( $t ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** @@ -865,8 +855,6 @@ public function test_object_id_should_not_be_cached_with_term_object() { * @ticket 14162 */ public function test_term_cache_should_be_primed_for_all_taxonomies() { - global $wpdb; - register_taxonomy( 'wptests_tax1', 'post' ); register_taxonomy( 'wptests_tax2', 'post' ); $p = self::factory()->post->create(); @@ -888,10 +876,10 @@ public function test_term_cache_should_be_primed_for_all_taxonomies() { $this->assertSameSets( array( $t1, $t2 ), wp_list_pluck( $found, 'term_id' ) ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $term1 = get_term( $t1 ); $term2 = get_term( $t2 ); - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); } /** diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index ac9678a908..e8b8ffd501 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -1719,11 +1719,9 @@ public function test_search_by_display_name_only_ignore_others() { * @ticket 44169 */ public function test_users_pre_query_filter_should_bypass_database_query() { - global $wpdb; - add_filter( 'users_pre_query', array( __CLASS__, 'filter_users_pre_query' ), 10, 2 ); - $num_queries = $wpdb->num_queries; + $num_queries = get_num_queries(); $q = new WP_User_Query( array( 'fields' => 'ID', @@ -1733,7 +1731,7 @@ public function test_users_pre_query_filter_should_bypass_database_query() { remove_filter( 'users_pre_query', array( __CLASS__, 'filter_users_pre_query' ), 10, 2 ); // Make sure no queries were executed. - $this->assertSame( $num_queries, $wpdb->num_queries ); + $this->assertSame( $num_queries, get_num_queries() ); // We manually inserted a non-existing user and overrode the results with it. $this->assertSame( array( 555 ), $q->results );