WordPress.org

Make WordPress Core

Changeset 40037


Ignore:
Timestamp:
02/01/2017 08:30:17 PM (3 years ago)
Author:
jnylen0
Message:

REST API: Improve posts orderby tests

This commit adds tests for orderby=relevance combined with a search term in the REST API.

It also improves tests for the orderby parameter in WP_REST_Posts_Controller by looking at the generated SQL query instead of creating a bunch of carefully arranged test objects. This should be much more robust, and we can use this approach in other places (such as #39055).

Fixes #39079.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39991 r40037  
    2121
    2222    protected $forbidden_cat;
     23    protected $posts_orderby;
    2324
    2425    public static function wpSetUpBeforeClass( $factory ) {
     
    6768        parent::setUp();
    6869        register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) );
     70        add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
     71        add_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 );
     72    }
     73
     74    public function wpSetUpBeforeRequest( $result, $server, $request ) {
     75        $this->posts_orderby = array();
     76        return $result;
     77    }
     78
     79    public function save_posts_orderby( $orderby, $query ) {
     80        array_push( $this->posts_orderby, $orderby );
     81        return $orderby;
     82    }
     83
     84    public function assertPostsOrderedBy( $pattern ) {
     85        global $wpdb;
     86        $orderby = str_replace( '{posts}', $wpdb->posts, $pattern );
     87        $this->assertEquals( array( $orderby ), $this->posts_orderby );
    6988    }
    7089
     
    225244        $this->assertEquals( 2, count( $data ) );
    226245        $this->assertEquals( $id3, $data[0]['id'] );
     246        $this->assertPostsOrderedBy( '{posts}.post_date DESC' );
    227247        // Orderby=>include
    228248        $request->set_param( 'orderby', 'include' );
     
    231251        $this->assertEquals( 2, count( $data ) );
    232252        $this->assertEquals( $id1, $data[0]['id'] );
     253        $this->assertPostsOrderedBy( "FIELD( {posts}.ID, $id1,$id3 )" );
    233254        // Invalid include should error
    234255        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     
    439460        $data = $response->get_data();
    440461        $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] );
     462        $this->assertPostsOrderedBy( '{posts}.post_title DESC' );
    441463        // order=>asc
    442464        $request->set_param( 'order', 'asc' );
     
    444466        $data = $response->get_data();
    445467        $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );
     468        $this->assertPostsOrderedBy( '{posts}.post_title ASC' );
    446469        // order=>asc,id should fail
    447470        $request->set_param( 'order', 'asc,id' );
     
    481504        $this->assertEquals( $id2, $data[1]['id'] );
    482505        $this->assertEquals( $id1, $data[2]['id'] );
     506        $this->assertPostsOrderedBy( '{posts}.ID DESC' );
    483507    }
    484508
     
    497521        $this->assertEquals( 'xyz', $data[0]['slug'] );
    498522        $this->assertEquals( 'abc', $data[1]['slug'] );
     523        $this->assertPostsOrderedBy( '{posts}.post_name DESC' );
    499524    }
    500525
    501526    public function test_get_items_with_orderby_relevance() {
    502         $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
    503         $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
    504 
     527        $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
     528        $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
     529        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     530        $request->set_param( 'orderby', 'relevance' );
     531        $request->set_param( 'search', 'relevant' );
     532        $response = $this->server->dispatch( $request );
     533        $this->assertEquals( 200, $response->get_status() );
     534        $data = $response->get_data();
     535        $this->assertCount( 2, $data );
     536        $this->assertEquals( $id1, $data[0]['id'] );
     537        $this->assertEquals( $id2, $data[1]['id'] );
     538        $this->assertPostsOrderedBy( '{posts}.post_title LIKE \'%relevant%\' DESC, {posts}.post_date DESC' );
     539    }
     540
     541    public function test_get_items_with_orderby_relevance_two_terms() {
     542        $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) );
     543        $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) );
     544        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     545        $request->set_param( 'orderby', 'relevance' );
     546        $request->set_param( 'search', 'relevant content' );
     547        $response = $this->server->dispatch( $request );
     548        $this->assertEquals( 200, $response->get_status() );
     549        $data = $response->get_data();
     550        $this->assertCount( 2, $data );
     551        $this->assertEquals( $id1, $data[0]['id'] );
     552        $this->assertEquals( $id2, $data[1]['id'] );
     553        $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' );
     554    }
     555
     556    public function test_get_items_with_orderby_relevance_missing_search() {
    505557        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    506558        $request->set_param( 'orderby', 'relevance' );
     
    27202772            $this->remove_added_uploads();
    27212773        }
     2774        remove_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
     2775        remove_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 );
    27222776        parent::tearDown();
    27232777    }
Note: See TracChangeset for help on using the changeset viewer.