Make WordPress Core


Ignore:
Timestamp:
11/02/2016 07:13:24 AM (8 years ago)
Author:
rmccue
Message:

REST API: Allow querying for multiple slug values.

Props jnylen0, rachelbaker.
Fixes #38579.

File:
1 edited

Legend:

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

    r39084 r39093  
    253253        $this->assertEquals( 1, count( $data ) );
    254254        $this->assertEquals( 'Apple', $data[0]['title']['rendered'] );
     255    }
     256
     257    public function test_get_items_multiple_slugs_array_query() {
     258        $this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) );
     259        $this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) );
     260        $this->factory->post->create( array( 'post_title' => 'Peach', 'post_status' => 'publish' ) );
     261        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     262        $request->set_param( 'slug', array( 'banana', 'peach' ) );
     263        $response = $this->server->dispatch( $request );
     264        $this->assertEquals( 200, $response->get_status() );
     265        $data = $response->get_data();
     266        $this->assertEquals( 2, count( $data ) );
     267        $titles = array(
     268            $data[0]['title']['rendered'],
     269            $data[1]['title']['rendered'],
     270        );
     271        sort( $titles );
     272        $this->assertEquals( array( 'Banana', 'Peach' ), $titles );
     273    }
     274
     275    public function test_get_items_multiple_slugs_string_query() {
     276        $this->factory->post->create( array( 'post_title' => 'Apple', 'post_status' => 'publish' ) );
     277        $this->factory->post->create( array( 'post_title' => 'Banana', 'post_status' => 'publish' ) );
     278        $this->factory->post->create( array( 'post_title' => 'Peach', 'post_status' => 'publish' ) );
     279        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     280        $request->set_param( 'slug', 'apple,banana' );
     281        $response = $this->server->dispatch( $request );
     282        $this->assertEquals( 200, $response->get_status() );
     283        $data = $response->get_data();
     284        $this->assertEquals( 2, count( $data ) );
     285        $titles = array(
     286            $data[0]['title']['rendered'],
     287            $data[1]['title']['rendered'],
     288        );
     289        sort( $titles );
     290        $this->assertEquals( array( 'Apple', 'Banana' ), $titles );
    255291    }
    256292
Note: See TracChangeset for help on using the changeset viewer.