From 6da93c458870cc0478c3b19ece59e7c485aed214 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Wed, 23 Dec 2020 03:03:56 +0000 Subject: [PATCH] Query: Add bad path tests with invalid `WP_Query` parameters. See #48556. git-svn-id: https://develop.svn.wordpress.org/trunk@49900 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/query/invalidQueries.php | 147 +++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 tests/phpunit/tests/query/invalidQueries.php diff --git a/tests/phpunit/tests/query/invalidQueries.php b/tests/phpunit/tests/query/invalidQueries.php new file mode 100644 index 0000000000..b796400095 --- /dev/null +++ b/tests/phpunit/tests/query/invalidQueries.php @@ -0,0 +1,147 @@ +user->create(); + + foreach ( array( 'publish', 'private' ) as $status ) { + self::$page_ids[ $status ] = $factory->post->create( + array( + 'post_type' => 'page', + 'post_status' => $status, + 'post_author' => self::$author_id, + ) + ); + + self::$post_ids[ $status ] = $factory->post->create( + array( + 'post_status' => $status, + 'post_author' => self::$author_id, + ) + ); + } + } + + /** + * Set up prior to each test. + */ + public function setUp() { + parent::setUp(); + + // Clean up variable before each test. + self::$last_posts_request = ''; + // Store last query for tests. + add_filter( 'posts_request', array( $this, '_set_last_posts_request' ) ); + } + + /** + * Filter to store last SQL query generated by WP_Query. + * + * @param string $request Generated SQL query. + * @return string Unmodified SQL query. + */ + public function _set_last_posts_request( $request ) { + self::$last_posts_request = $request; + return $request; + } + + /** + * Test WP Query with an invalid post type. + * + * @ticket 48556. + */ + public function test_unregistered_post_type_wp_query() { + global $wpdb; + + $query = new WP_Query( array( 'post_type' => 'unregistered_cpt' ) ); + $posts = $query->get_posts(); + + $this->assertContains( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request ); + $this->assertContains( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request ); + $this->assertCount( 0, $posts ); + } + + /** + * Test WP Query with an invalid post type specified in the URL. + * + * @ticket 48556. + */ + public function test_unregistered_post_type_goto() { + global $wpdb, $wp_query; + + $this->go_to( home_url( '?post_type=unregistered_cpt' ) ); + + $this->assertContains( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request ); + $this->assertContains( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request ); + // $wp_query recovers to the post type "post" and is expected to return one. + $this->assertCount( 1, $wp_query->get_posts() ); + } + + /** + * Ensure deprecated static parameter has no effect on queries. + */ + public function test_deprecated_parameters_have_no_effect_on_page() { + $query = new WP_Query( + array( + 'static' => 'a', + 'post_type' => 'page', + ) + ); + $posts = $query->get_posts(); + + // Only the published page should be returned. + $this->assertCount( 1, $posts ); + } + + /** + * Ensure deprecated static parameter has no effect on queries. + */ + public function test_deprecated_parameters_have_no_effect_on_post() { + $query = new WP_Query( + array( + 'static' => 'a', + ) + ); + $posts = $query->get_posts(); + + // Only the published post should be returned. + $this->assertCount( 1, $posts ); + } +}