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
This commit is contained in:
Sergey Biryukov
2022-01-14 17:50:52 +00:00
parent bde79ae542
commit e70b0f7c3d
2 changed files with 6 additions and 12 deletions

View File

@@ -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 );
}
}