Make WordPress Core

Ticket #39079: 39079.diff

File 39079.diff, 6.6 KB (added by jnylen0, 7 years ago)
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 886ae3c..78f4fdf 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    2020        protected static $supported_formats;
    2121
    2222        protected $forbidden_cat;
     23        protected $posts_orderby;
    2324
    2425        public static function wpSetUpBeforeClass( $factory ) {
    2526                self::$post_id = $factory->post->create();
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    6566        public function setUp() {
    6667                parent::setUp();
    6768                register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) );
     69                add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
     70                add_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 );
     71        }
     72
     73        public function wpSetUpBeforeRequest( $result, $server, $request ) {
     74                $this->posts_orderby = array();
     75                return $result;
     76        }
     77
     78        public function save_posts_orderby( $orderby, $query ) {
     79                array_push( $this->posts_orderby, $orderby );
     80                return $orderby;
     81        }
     82
     83        public function assertPostsOrderedBy( $pattern ) {
     84                global $wpdb;
     85                $orderby = str_replace( '{posts}', $wpdb->posts, $pattern );
     86                $this->assertEquals( array( $orderby ), $this->posts_orderby );
    6887        }
    6988
    7089        public function test_register_routes() {
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    214233                $data = $response->get_data();
    215234                $this->assertEquals( 2, count( $data ) );
    216235                $this->assertEquals( $id3, $data[0]['id'] );
     236                $this->assertPostsOrderedBy( '{posts}.post_date DESC' );
    217237                // Orderby=>include
    218238                $request->set_param( 'orderby', 'include' );
    219239                $response = $this->server->dispatch( $request );
    220240                $data = $response->get_data();
    221241                $this->assertEquals( 2, count( $data ) );
    222242                $this->assertEquals( $id1, $data[0]['id'] );
     243                $this->assertPostsOrderedBy( 'FIELD( {posts}.ID, 9,11 )' );
    223244                // Invalid include should error
    224245                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    225246                $request->set_param( 'include', 'invalid' );
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    428449                $response = $this->server->dispatch( $request );
    429450                $data = $response->get_data();
    430451                $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] );
     452                $this->assertPostsOrderedBy( '{posts}.post_title DESC' );
    431453                // order=>asc
    432454                $request->set_param( 'order', 'asc' );
    433455                $response = $this->server->dispatch( $request );
    434456                $data = $response->get_data();
    435457                $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );
     458                $this->assertPostsOrderedBy( '{posts}.post_title ASC' );
    436459                // order=>asc,id should fail
    437460                $request->set_param( 'order', 'asc,id' );
    438461                $response = $this->server->dispatch( $request );
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    470493                $this->assertEquals( $id3, $data[0]['id'] );
    471494                $this->assertEquals( $id2, $data[1]['id'] );
    472495                $this->assertEquals( $id1, $data[2]['id'] );
     496                $this->assertPostsOrderedBy( '{posts}.ID DESC' );
    473497        }
    474498
    475499        public function test_get_items_with_orderby_slug() {
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    486510                // Default ORDER is DESC.
    487511                $this->assertEquals( 'xyz', $data[0]['slug'] );
    488512                $this->assertEquals( 'abc', $data[1]['slug'] );
     513                $this->assertPostsOrderedBy( '{posts}.post_name DESC' );
    489514        }
    490515
    491516        public function test_get_items_with_orderby_relevance() {
    492                 $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
    493                 $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
     517                $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
     518                $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
     519                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     520                $request->set_param( 'orderby', 'relevance' );
     521                $request->set_param( 'search', 'relevant' );
     522                $response = $this->server->dispatch( $request );
     523                $this->assertEquals( 200, $response->get_status() );
     524                $data = $response->get_data();
     525                $this->assertCount( 2, $data );
     526                $this->assertEquals( $id1, $data[0]['id'] );
     527                $this->assertEquals( $id2, $data[1]['id'] );
     528                $this->assertPostsOrderedBy( '{posts}.post_title LIKE \'%relevant%\' DESC, {posts}.post_date DESC' );
     529        }
     530
     531        public function test_get_items_with_orderby_relevance_two_terms() {
     532                $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
     533                $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
     534                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     535                $request->set_param( 'orderby', 'relevance' );
     536                $request->set_param( 'search', 'relevant content' );
     537                $response = $this->server->dispatch( $request );
     538                $this->assertEquals( 200, $response->get_status() );
     539                $data = $response->get_data();
     540                $this->assertCount( 2, $data );
     541                $this->assertEquals( $id1, $data[0]['id'] );
     542                $this->assertEquals( $id2, $data[1]['id'] );
     543                $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' );
     544        }
    494545
     546        public function test_get_items_with_orderby_relevance_missing_search() {
    495547                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    496548                $request->set_param( 'orderby', 'relevance' );
    497549                $response = $this->server->dispatch( $request );
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    26882740                if ( isset( $this->attachment_id ) ) {
    26892741                        $this->remove_added_uploads();
    26902742                }
     2743                remove_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
     2744                remove_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 );
    26912745                parent::tearDown();
    26922746        }
    26932747