@@ -661,27 +661,32 @@ public function test_get_items_search_query() {
661661 $ this ->assertSame ( 'Search Result ' , $ data [0 ]['title ' ]['rendered ' ] );
662662 }
663663
664+ /**
665+ * @ticket 63307
666+ */
664667 public function test_get_items_slug_query () {
665- self ::factory ()->post ->create (
668+ $ id1 = self ::factory ()->post ->create (
666669 array (
667670 'post_title ' => 'Apple ' ,
668671 'post_status ' => 'publish ' ,
669672 )
670673 );
671- self ::factory ()->post ->create (
674+ $ id2 = self ::factory ()->post ->create (
672675 array (
673676 'post_title ' => 'Banana ' ,
674677 'post_status ' => 'publish ' ,
675678 )
676679 );
677680
681+ update_option ( 'sticky_posts ' , array ( $ id2 ) );
682+
678683 $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/posts ' );
679684 $ request ->set_param ( 'slug ' , 'apple ' );
680685 $ response = rest_get_server ()->dispatch ( $ request );
681686 $ this ->assertSame ( 200 , $ response ->get_status () );
682687 $ data = $ response ->get_data ();
683688 $ this ->assertCount ( 1 , $ data );
684- $ this ->assertSame ( 'Apple ' , $ data [0 ]['title ' ]['rendered ' ] );
689+ $ this ->assertSame ( 'Apple ' , $ data [0 ]['title ' ]['rendered ' ], ' Return the post with the given slug ' );
685690 }
686691
687692 public function test_get_items_multiple_slugs_array_query () {
@@ -5970,13 +5975,14 @@ public function test_get_posts_with_pagination() {
59705975 }
59715976
59725977 /**
5973- * Test the REST API support for `ignore_sticky_posts` .
5978+ * Test the REST API doesn't prioritize sticky posts by default .
59745979 *
59755980 * @ticket 35907
5981+ * @ticket 63307
59765982 *
59775983 * @covers WP_REST_Posts_Controller::get_items
59785984 */
5979- public function test_get_posts_ignore_sticky_default_prepends_sticky_posts () {
5985+ public function test_get_posts_ignore_sticky_by_default () {
59805986 $ id1 = self ::$ post_id ;
59815987 // Create more recent post to avoid automatically placing other at the top.
59825988 $ id2 = self ::factory ()->post ->create ( array ( 'post_status ' => 'publish ' ) );
@@ -5986,40 +5992,54 @@ public function test_get_posts_ignore_sticky_default_prepends_sticky_posts() {
59865992 $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/posts ' );
59875993 $ response = rest_get_server ()->dispatch ( $ request );
59885994 $ data = $ response ->get_data ();
5995+ $ rest_ids = wp_list_pluck ( $ data , 'id ' );
59895996
5990- $ this ->assertSame ( $ data [0 ]['id ' ], $ id1 , 'Response has sticky post at the top. ' );
5991- $ this ->assertSame ( $ data [1 ]['id ' ], $ id2 , 'It is followed by most recent post. ' );
5997+ $ this ->assertSame ( $ data [0 ]['id ' ], $ id2 , 'Response has no sticky post at the top. ' );
5998+
5999+ $ posts_query = new WP_Query ( array ( 'ignore_sticky_posts ' => true ) );
6000+ $ post_ids = wp_list_pluck ( $ posts_query ->get_posts (), 'ID ' );
6001+ $ this ->assertSame ( $ rest_ids , $ post_ids , 'Response is same as WP_Query with ignore_sticky_posts=true. ' );
59926002 }
59936003
59946004 /**
59956005 * Test the REST API support for `ignore_sticky_posts`.
59966006 *
59976007 * @ticket 35907
6008+ * @ticket 63307
59986009 *
59996010 * @covers WP_REST_Posts_Controller::get_items
60006011 */
6001- public function test_get_posts_ignore_sticky_ignores_post_stickiness () {
6012+ public function test_get_posts_ignore_sticky_false_prepends_sticky_posts () {
60026013 $ id1 = self ::$ post_id ;
6014+ // Create more recent post to avoid automatically placing other at the top.
60036015 $ id2 = self ::factory ()->post ->create ( array ( 'post_status ' => 'publish ' ) );
60046016
60056017 update_option ( 'sticky_posts ' , array ( $ id1 ) );
60066018
60076019 $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/posts ' );
6008- $ request ->set_param ( 'ignore_sticky ' , true );
6020+ $ request ->set_param ( 'ignore_sticky ' , false );
60096021 $ response = rest_get_server ()->dispatch ( $ request );
60106022 $ data = $ response ->get_data ();
6023+ $ rest_ids = wp_list_pluck ( $ data , 'id ' );
60116024
6012- $ this ->assertSame ( $ data [0 ]['id ' ], $ id2 , 'Response has no sticky post at the top. ' );
6025+ $ this ->assertSame ( $ data [0 ]['id ' ], $ id1 , 'Response has sticky post at the top. ' );
6026+ $ this ->assertSame ( $ data [1 ]['id ' ], $ id2 , 'It is followed by most recent post. ' );
6027+
6028+ $ posts_query = new WP_Query ();
6029+ $ post_ids = wp_list_pluck ( $ posts_query ->get_posts (), 'ID ' );
6030+ $ this ->assertSame ( $ rest_ids , $ post_ids , 'Response is same as WP_Query with ignore_sticky_posts=false. ' );
60136031 }
60146032
60156033 /**
60166034 * Test the REST API support for `ignore_sticky_posts`.
60176035 *
60186036 * @ticket 35907
6037+ * @ticket 63307
60196038 *
60206039 * @covers WP_REST_Posts_Controller::get_items
60216040 */
60226041 public function test_get_posts_ignore_sticky_honors_include () {
6042+
60236043 $ id1 = self ::$ post_id ;
60246044 $ id2 = self ::factory ()->post ->create ( array ( 'post_status ' => 'publish ' ) );
60256045
@@ -6029,9 +6049,19 @@ public function test_get_posts_ignore_sticky_honors_include() {
60296049 $ request ->set_param ( 'include ' , array ( $ id2 ) );
60306050 $ response = rest_get_server ()->dispatch ( $ request );
60316051 $ data = $ response ->get_data ();
6052+ $ rest_ids = wp_list_pluck ( $ data , 'id ' );
60326053
60336054 $ this ->assertCount ( 1 , $ data , 'Only one post is expected to be returned. ' );
60346055 $ this ->assertSame ( $ data [0 ]['id ' ], $ id2 , 'Returns the included post. ' );
6056+
6057+ $ posts_query = new WP_Query (
6058+ array (
6059+ 'post__in ' => array ( $ id2 ),
6060+ 'ignore_sticky_posts ' => true ,
6061+ )
6062+ );
6063+ $ post_ids = wp_list_pluck ( $ posts_query ->get_posts (), 'ID ' );
6064+ $ this ->assertSame ( $ rest_ids , $ post_ids , 'Response is same as WP_Query with ignore_sticky_posts=truehas no sticky post at the top. ' );
60356065 }
60366066
60376067 /**
0 commit comments