From 6bfb976577428b559ac2bc00c3bca115c2574fad Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Mon, 27 Feb 2017 19:53:43 +0000 Subject: [PATCH] REST API: Fix behavior of `sticky` posts filter when no posts are sticky. Previously, when getting posts from the API with `sticky=true`, if there were no sticky posts set, the query would return all posts as if the `sticky` argument was not set. In this situation, the query should return an empty array instead. A `sticky=true` query that should return an empty array (in the previous situation, or with `include` and no intersecting post IDs) was also broken in that it would query the post with ID 1. Finally, this commit significantly improves test coverage for the `sticky` filter argument, including direct testing of the `WHERE` clauses generated by `WP_Query`. Merge of [40037] and [40122] to the 4.7 branch. Props ryelle, jnylen0. See #39079. Fixes #39947. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40136 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-posts-controller.php | 7 +- .../tests/rest-api/rest-posts-controller.php | 146 +++++++++++++++++- 2 files changed, 145 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 9011f49cc7..9e726b4891 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -220,7 +220,10 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { if ( isset( $registered['sticky'], $request['sticky'] ) ) { $sticky_posts = get_option( 'sticky_posts', array() ); - if ( $sticky_posts && $request['sticky'] ) { + if ( ! is_array( $sticky_posts ) ) { + $sticky_posts = array(); + } + if ( $request['sticky'] ) { /* * As post__in will be used to only get sticky posts, * we have to support the case where post__in was already @@ -234,7 +237,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { * so we have to fake it a bit. */ if ( ! $args['post__in'] ) { - $args['post__in'] = array( -1 ); + $args['post__in'] = array( 0 ); } } elseif ( $sticky_posts ) { /* diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 9a4a47ed33..f7b7a74206 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -20,6 +20,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te protected static $supported_formats; protected $forbidden_cat; + protected $posts_clauses; public static function wpSetUpBeforeClass( $factory ) { self::$post_id = $factory->post->create(); @@ -65,6 +66,33 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te public function setUp() { parent::setUp(); register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) ); + add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); + add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 ); + } + + public function wpSetUpBeforeRequest( $result, $server, $request ) { + $this->posts_clauses = array(); + return $result; + } + + public function save_posts_clauses( $orderby, $query ) { + array_push( $this->posts_clauses, $orderby ); + return $orderby; + } + + public function assertPostsClause( $clause, $pattern ) { + global $wpdb; + $expected_clause = str_replace( '{posts}', $wpdb->posts, $pattern ); + $this->assertCount( 1, $this->posts_clauses ); + $this->assertEquals( $expected_clause, $this->posts_clauses[0][ $clause ] ); + } + + public function assertPostsOrderedBy( $pattern ) { + $this->assertPostsClause( 'orderby', $pattern ); + } + + public function assertPostsWhere( $pattern ) { + $this->assertPostsClause( 'where', $pattern ); } public function test_register_routes() { @@ -223,12 +251,14 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( $id3, $data[0]['id'] ); + $this->assertPostsOrderedBy( '{posts}.post_date DESC' ); // Orderby=>include $request->set_param( 'orderby', 'include' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 2, count( $data ) ); $this->assertEquals( $id1, $data[0]['id'] ); + $this->assertPostsOrderedBy( "FIELD( {posts}.ID, $id1,$id3 )" ); // Invalid include should error $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'include', 'invalid' ); @@ -437,11 +467,13 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] ); + $this->assertPostsOrderedBy( '{posts}.post_title DESC' ); // order=>asc $request->set_param( 'order', 'asc' ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] ); + $this->assertPostsOrderedBy( '{posts}.post_title ASC' ); // order=>asc,id should fail $request->set_param( 'order', 'asc,id' ); $response = $this->server->dispatch( $request ); @@ -479,6 +511,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEquals( $id3, $data[0]['id'] ); $this->assertEquals( $id2, $data[1]['id'] ); $this->assertEquals( $id1, $data[2]['id'] ); + $this->assertPostsOrderedBy( '{posts}.ID DESC' ); } public function test_get_items_with_orderby_slug() { @@ -495,12 +528,40 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te // Default ORDER is DESC. $this->assertEquals( 'xyz', $data[0]['slug'] ); $this->assertEquals( 'abc', $data[1]['slug'] ); + $this->assertPostsOrderedBy( '{posts}.post_name DESC' ); } public function test_get_items_with_orderby_relevance() { - $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); - $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); + $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); + $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'orderby', 'relevance' ); + $request->set_param( 'search', 'relevant' ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + $this->assertCount( 2, $data ); + $this->assertEquals( $id1, $data[0]['id'] ); + $this->assertEquals( $id2, $data[1]['id'] ); + $this->assertPostsOrderedBy( '{posts}.post_title LIKE \'%relevant%\' DESC, {posts}.post_date DESC' ); + } + public function test_get_items_with_orderby_relevance_two_terms() { + $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); + $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'orderby', 'relevance' ); + $request->set_param( 'search', 'relevant content' ); + $response = $this->server->dispatch( $request ); + $this->assertEquals( 200, $response->get_status() ); + $data = $response->get_data(); + $this->assertCount( 2, $data ); + $this->assertEquals( $id1, $data[0]['id'] ); + $this->assertEquals( $id2, $data[1]['id'] ); + $this->assertPostsOrderedBy( '(CASE WHEN {posts}.post_title LIKE \'%relevant content%\' THEN 1 WHEN {posts}.post_title LIKE \'%relevant%\' AND {posts}.post_title LIKE \'%content%\' THEN 2 WHEN {posts}.post_title LIKE \'%relevant%\' OR {posts}.post_title LIKE \'%content%\' THEN 3 WHEN {posts}.post_excerpt LIKE \'%relevant content%\' THEN 4 WHEN {posts}.post_content LIKE \'%relevant content%\' THEN 5 ELSE 6 END), {posts}.post_date DESC' ); + } + + public function test_get_items_with_orderby_relevance_missing_search() { $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); $request->set_param( 'orderby', 'relevance' ); $response = $this->server->dispatch( $request ); @@ -637,7 +698,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } - public function test_get_items_sticky_query() { + public function test_get_items_sticky() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); @@ -658,7 +719,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } - public function test_get_items_sticky_with_post__in_query() { + public function test_get_items_sticky_with_include() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); @@ -672,6 +733,12 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $response = $this->server->dispatch( $request ); $this->assertCount( 0, $response->get_data() ); + // FIXME Since this request returns zero posts, the query is executed twice. + $this->assertCount( 2, $this->posts_clauses ); + $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 ); + + $this->assertPostsWhere( " AND {posts}.ID IN (0) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); + update_option( 'sticky_posts', array( $id1, $id2 ) ); $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); @@ -685,9 +752,48 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $posts = $response->get_data(); $post = $posts[0]; $this->assertEquals( $id1, $post['id'] ); + + $this->assertPostsWhere( " AND {posts}.ID IN ($id1) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); } - public function test_get_items_not_sticky_query() { + public function test_get_items_sticky_no_sticky_posts() { + $id1 = self::$post_id; + + update_option( 'sticky_posts', array() ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'sticky', true ); + + $response = $this->server->dispatch( $request ); + $this->assertCount( 0, $response->get_data() ); + + // FIXME Since this request returns zero posts, the query is executed twice. + $this->assertCount( 2, $this->posts_clauses ); + $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 ); + + $this->assertPostsWhere( " AND {posts}.ID IN (0) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); + } + + public function test_get_items_sticky_with_include_no_sticky_posts() { + $id1 = self::$post_id; + + update_option( 'sticky_posts', array() ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'sticky', true ); + $request->set_param( 'include', array( $id1 ) ); + + $response = $this->server->dispatch( $request ); + $this->assertCount( 0, $response->get_data() ); + + // FIXME Since this request returns zero posts, the query is executed twice. + $this->assertCount( 2, $this->posts_clauses ); + $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 ); + + $this->assertPostsWhere( " AND {posts}.ID IN (0) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); + } + + public function test_get_items_not_sticky() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); @@ -702,9 +808,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $posts = $response->get_data(); $post = $posts[0]; $this->assertEquals( $id1, $post['id'] ); + + $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); } - public function test_get_items_sticky_with_post__not_in_query() { + public function test_get_items_not_sticky_with_exclude() { $id1 = self::$post_id; $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); @@ -721,6 +829,30 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $posts = $response->get_data(); $post = $posts[0]; $this->assertEquals( $id1, $post['id'] ); + + $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); + } + + public function test_get_items_not_sticky_with_exclude_no_sticky_posts() { + $id1 = self::$post_id; + $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + + update_option( 'sticky_posts', array() ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $request->set_param( 'sticky', false ); + $request->set_param( 'exclude', array( $id3 ) ); + + $response = $this->server->dispatch( $request ); + $this->assertCount( 2, $response->get_data() ); + + $posts = $response->get_data(); + $ids = wp_list_pluck( $posts, 'id' ); + sort( $ids ); + $this->assertEquals( array( $id1, $id2 ), $ids ); + + $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); } /** @@ -2917,6 +3049,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te if ( isset( $this->attachment_id ) ) { $this->remove_added_uploads(); } + remove_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); + remove_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 ); parent::tearDown(); }