Make WordPress Core


Ignore:
Timestamp:
11/03/2016 02:17:39 AM (8 years ago)
Author:
rachelbaker
Message:

REST API: Clean-up our validation callbacks and add missing array items properties in our endpoint schemas.

Props joehoyle, jnylen0.
Fixes #38617.

File:
1 edited

Legend:

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

    r38974 r39105  
    9999        $this->assertEquals( 'Season 5', $data[0]['name'] );
    100100        $this->assertEquals( 'The Be Sharps', $data[1]['name'] );
     101        // invalid value should fail
     102        $request->set_param( 'hide_empty', 'nothanks' );
     103        $response = $this->server->dispatch( $request );
     104        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    101105    }
    102106
     
    118122        $this->assertEquals( 2, count( $data ) );
    119123        $this->assertEquals( $id3, $data[0]['id'] );
     124        // Include invalid value shoud fail
     125        $request->set_param( 'include', array( 'myterm' ) );
     126        $response = $this->server->dispatch( $request );
     127        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    120128    }
    121129
     
    133141        $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    134142        $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     143        // Invalid exclude value should fail
     144        $request->set_param( 'exclude', array( 'invalid' ) );
     145        $response = $this->server->dispatch( $request );
     146        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    135147    }
    136148
     
    152164        $response = $this->server->dispatch( $request );
    153165        $this->assertCount( 2, $response->get_data() );
     166        // 'offset' invalid value shoudl fail
     167        $request->set_param( 'offset', 'moreplease' );
     168        $response = $this->server->dispatch( $request );
     169        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    154170    }
    155171
     
    182198        $this->assertEquals( 2, count( $data ) );
    183199        $this->assertEquals( 'Apple', $data[0]['name'] );
     200        // Invalid orderby should fail.
     201        $request->set_param( 'orderby', 'invalid' );
     202        $response = $this->server->dispatch( $request );
     203        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    184204    }
    185205
     
    232252        $this->assertEquals( 2, count( $data ) );
    233253        $this->assertEquals( 'DC', $data[0]['name'] );
     254
     255        // Invalid post should error.
     256        $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     257        $request->set_param( 'post', 'invalid-post' );
     258        $response = $this->server->dispatch( $request );
     259        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    234260    }
    235261
Note: See TracChangeset for help on using the changeset viewer.