From e70b0f7c3da94838485d5b8df6718cd16b674a90 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 14 Jan 2022 17:50:52 +0000 Subject: [PATCH] Tests: Avoid duplicate queries in some `WP_Query` tests. When passing args to the `WP_Query::__construct()` method, it internally executes the `WP_Query::get_posts()` method and stores the result in the `WP_Query::posts` property. When calling `WP_Query::get_posts()` again, the same SQL query gets executed, and the result is again stored in the `WP_Query::posts` property. Thus, the correct usage is to read the posts already stored in the `WP_Query::posts` property. Follow-up to [1062/tests], [1065/tests], [1066/tests], [1070/tests], [29805], [31286], [49900], [51144]. Props david.binda. Fixes #54822. git-svn-id: https://develop.svn.wordpress.org/trunk@52577 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/query/invalidQueries.php | 12 ++++-------- tests/phpunit/tests/query/taxQuery.php | 6 ++---- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/phpunit/tests/query/invalidQueries.php b/tests/phpunit/tests/query/invalidQueries.php index ea18617b28..4c0b631f80 100644 --- a/tests/phpunit/tests/query/invalidQueries.php +++ b/tests/phpunit/tests/query/invalidQueries.php @@ -91,11 +91,10 @@ class Tests_Query_InvalidQueries extends WP_UnitTestCase { global $wpdb; $query = new WP_Query( array( 'post_type' => 'unregistered_cpt' ) ); - $posts = $query->get_posts(); $this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request ); $this->assertStringContainsString( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request ); - $this->assertCount( 0, $posts ); + $this->assertCount( 0, $query->posts ); } /** @@ -111,10 +110,9 @@ class Tests_Query_InvalidQueries extends WP_UnitTestCase { 'post_type' => array( 'unregistered_cpt', 'page' ), ) ); - $posts = $query->get_posts(); $this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request ); - $this->assertCount( 1, $posts, 'the valid `page` post type should still return one post' ); + $this->assertCount( 1, $query->posts, 'the valid `page` post type should still return one post' ); } /** @@ -143,10 +141,9 @@ class Tests_Query_InvalidQueries extends WP_UnitTestCase { 'post_type' => 'page', ) ); - $posts = $query->get_posts(); // Only the published page should be returned. - $this->assertCount( 1, $posts ); + $this->assertCount( 1, $query->posts ); } /** @@ -158,9 +155,8 @@ class Tests_Query_InvalidQueries extends WP_UnitTestCase { 'static' => 'a', ) ); - $posts = $query->get_posts(); // Only the published post should be returned. - $this->assertCount( 1, $posts ); + $this->assertCount( 1, $query->posts ); } } diff --git a/tests/phpunit/tests/query/taxQuery.php b/tests/phpunit/tests/query/taxQuery.php index 2094262e24..c44f19880f 100644 --- a/tests/phpunit/tests/query/taxQuery.php +++ b/tests/phpunit/tests/query/taxQuery.php @@ -1024,8 +1024,7 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { ) ); - $posts = $query->get_posts(); - $this->assertCount( 0, $posts ); + $this->assertCount( 0, $query->posts ); } /** @@ -1059,8 +1058,7 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { ) ); - $posts = $query->get_posts(); - $this->assertCount( 0, $posts ); + $this->assertCount( 0, $query->posts ); } public function test_tax_query_include_children() {