WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/03/2016 01:45:48 AM (5 years ago)
Author:
joehoyle
Message:

REST API: Support querying for multiple post statuses.

Multiple post statuses can be specified by the usual CSV or array-propper format.

Props jnylen0, kadamwhite, websupporter.
Fixes #38420.

File:
1 edited

Legend:

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

    r39093 r39104  
    309309        $this->assertEquals( 200, $response->get_status() );
    310310        $this->assertEquals( 1, count( $response->get_data() ) );
     311    }
     312
     313    public function test_get_items_multiple_statuses_string_query() {
     314        wp_set_current_user( self::$editor_id );
     315
     316        $this->factory->post->create( array( 'post_status' => 'draft' ) );
     317        $this->factory->post->create( array( 'post_status' => 'private' ) );
     318        $this->factory->post->create( array( 'post_status' => 'publish' ) );
     319
     320        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     321        $request->set_param( 'context', 'edit' );
     322        $request->set_param( 'status', 'draft,private' );
     323
     324        $response = $this->server->dispatch( $request );
     325        $this->assertEquals( 200, $response->get_status() );
     326        $data = $response->get_data();
     327        $this->assertEquals( 2, count( $data ) );
     328        $statuses = array(
     329            $data[0]['status'],
     330            $data[1]['status'],
     331        );
     332        sort( $statuses );
     333        $this->assertEquals( array( 'draft', 'private' ), $statuses );
     334    }
     335
     336    public function test_get_items_multiple_statuses_array_query() {
     337        wp_set_current_user( self::$editor_id );
     338
     339        $this->factory->post->create( array( 'post_status' => 'draft' ) );
     340        $this->factory->post->create( array( 'post_status' => 'pending' ) );
     341        $this->factory->post->create( array( 'post_status' => 'publish' ) );
     342
     343        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     344        $request->set_param( 'context', 'edit' );
     345        $request->set_param( 'status', array( 'draft', 'pending' ) );
     346
     347        $response = $this->server->dispatch( $request );
     348        $this->assertEquals( 200, $response->get_status() );
     349        $data = $response->get_data();
     350        $this->assertEquals( 2, count( $data ) );
     351        $statuses = array(
     352            $data[0]['status'],
     353            $data[1]['status'],
     354        );
     355        sort( $statuses );
     356        $this->assertEquals( array( 'draft', 'pending' ), $statuses );
     357    }
     358
     359    public function test_get_items_multiple_statuses_one_invalid_query() {
     360        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     361        $request->set_param( 'context', 'edit' );
     362        $request->set_param( 'status', array( 'draft', 'nonsense' ) );
     363        $response = $this->server->dispatch( $request );
     364        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    311365    }
    312366
     
    19642018    }
    19652019
     2020    public function test_status_array_enum_args() {
     2021        $request = new WP_REST_Request( 'GET', '/wp/v2' );
     2022        $response = $this->server->dispatch( $request );
     2023        $data = $response->get_data();
     2024        $list_posts_args = $data['routes']['/wp/v2/posts']['endpoints'][0]['args'];
     2025        $status_arg = $list_posts_args['status'];
     2026        $this->assertEquals( 'array', $status_arg['type'] );
     2027        $this->assertEquals( array(
     2028            'type' => 'string',
     2029            'enum' => array( 'publish', 'future', 'draft', 'pending', 'private', 'trash', 'auto-draft', 'inherit', 'any' ),
     2030        ), $status_arg['items'] );
     2031    }
     2032
    19662033    public function test_get_additional_field_registration() {
    19672034
Note: See TracChangeset for help on using the changeset viewer.