mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-05-23 04:34:41 +00:00
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:
@@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user