Make WordPress Core


Ignore:
Timestamp:
11/03/2016 02:17:39 AM (5 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-posts-controller.php

    r39104 r39105  
    9393            'before',
    9494            'categories',
     95            'categories_exclude',
    9596            'context',
    9697            'exclude',
     
    106107            'sticky',
    107108            'tags',
     109            'tags_exclude',
    108110            ), $keys );
    109111    }
     
    184186        $this->assertNotEquals( self::$editor_id, $data[0]['author'] );
    185187        $this->assertNotEquals( self::$editor_id, $data[1]['author'] );
     188        // invalid author_exclude errors
     189        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     190        $request->set_param( 'author_exclude', 'invalid' );
     191        $response = $this->server->dispatch( $request );
     192        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    186193    }
    187194
     
    203210        $this->assertEquals( 2, count( $data ) );
    204211        $this->assertEquals( $id1, $data[0]['id'] );
     212        // Invalid include should error
     213        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     214        $request->set_param( 'include', 'invalid' );
     215        $response = $this->server->dispatch( $request );
     216        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    205217    }
    206218
     
    225237        $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    226238        $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     239
     240        $request->set_param( 'exclude', 'invalid' );
     241        $response = $this->server->dispatch( $request );
     242        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    227243    }
    228244
     
    407423        $data = $response->get_data();
    408424        $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );
     425        // order=>asc,id should fail
     426        $request->set_param( 'order', 'asc,id' );
     427        $response = $this->server->dispatch( $request );
     428        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     429        // orderby=>content should fail (invalid param test)
     430        $request->set_param( 'order', 'asc' );
     431        $request->set_param( 'orderby', 'content' );
     432        $response = $this->server->dispatch( $request );
     433        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    409434    }
    410435
     
    457482        $response = $this->server->dispatch( $request );
    458483        $this->assertCount( 2, $response->get_data() );
     484        // Invalid 'offset' should error
     485        $request->set_param( 'offset', 'moreplease' );
     486        $response = $this->server->dispatch( $request );
     487        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    459488    }
    460489
     
    513542        $response = $this->server->dispatch( $request );
    514543        $this->assertCount( 1, $response->get_data() );
     544
     545        $request->set_param( 'tags', array( 'my-tag' ) );
     546        $response = $this->server->dispatch( $request );
     547        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    515548    }
    516549
     
    535568        $this->assertCount( 1, $data );
    536569        $this->assertEquals( $id2, $data[0]['id'] );
     570
     571        $request->set_param( 'tags_exclude', array( 'my-tag' ) );
     572        $response = $this->server->dispatch( $request );
     573        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    537574    }
    538575
     
    552589        $post = $posts[0];
    553590        $this->assertEquals( $id2, $post['id'] );
     591
     592        $request->set_param( 'sticky', 'nothanks' );
     593        $response = $this->server->dispatch( $request );
     594        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    554595    }
    555596
     
    19902031        $data = $response->get_data();
    19912032        $properties = $data['schema']['properties'];
    1992         $this->assertEquals( 25, count( $properties ) );
     2033        $this->assertEquals( 23, count( $properties ) );
    19932034        $this->assertArrayHasKey( 'author', $properties );
    19942035        $this->assertArrayHasKey( 'comment_status', $properties );
     
    20132054        $this->assertArrayHasKey( 'type', $properties );
    20142055        $this->assertArrayHasKey( 'tags', $properties );
    2015         $this->assertArrayHasKey( 'tags_exclude', $properties );
    20162056        $this->assertArrayHasKey( 'categories', $properties );
    2017         $this->assertArrayHasKey( 'categories_exclude', $properties );
    20182057    }
    20192058
Note: See TracChangeset for help on using the changeset viewer.