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-comments-controller.php

    r39101 r39105  
    301301        $this->assertEquals( 2, count( $data ) );
    302302        $this->assertEquals( $id3, $data[0]['id'] );
     303        // Orderby=>invalid should fail.
     304        $request->set_param( 'orderby', 'invalid' );
     305        $response = $this->server->dispatch( $request );
     306        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     307        // fails on invalid id.
     308        $request->set_param( 'orderby', array( 'include' ) );
     309        $request->set_param( 'include', array( 'invalid' ) );
     310        $response = $this->server->dispatch( $request );
     311        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    303312    }
    304313
     
    321330        $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    322331        $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     332
     333        // fails on invalid id.
     334        $request->set_param( 'exclude', array( 'invalid' ) );
     335        $response = $this->server->dispatch( $request );
     336        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    323337    }
    324338
     
    344358        $response = $this->server->dispatch( $request );
    345359        $this->assertCount( 2, $response->get_data() );
     360        // 'offset' with invalid value errors.
     361        $request->set_param( 'offset', 'moreplease' );
     362        $response = $this->server->dispatch( $request );
     363        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    346364    }
    347365
     
    365383        $data = $response->get_data();
    366384        $this->assertEquals( self::$approved_id, $data[0]['id'] );
     385        // order=>asc,id should fail
     386        $request->set_param( 'order', 'asc,id' );
     387        $response = $this->server->dispatch( $request );
     388        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    367389    }
    368390
     
    403425        $comments = $response->get_data();
    404426        $this->assertCount( 2, $comments );
     427        // Invalid author param errors
     428        $request->set_param( 'author', 'skippy' );
     429        $response = $this->server->dispatch( $request );
     430        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    405431        // Unavailable to unauthenticated; defaults to error
    406432        wp_set_current_user( 0 );
     433        $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
    407434        $response = $this->server->dispatch( $request );
    408435        $this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
     
    442469        $comments = $response->get_data();
    443470        $this->assertCount( 2, $comments );
     471        // 'author_exclude' for both invalid author
     472        $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     473        $request->set_param( 'author_exclude', 'skippy' );
     474        $response = $this->server->dispatch( $request );
     475        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    444476        // Unavailable to unauthenticated; defaults to error
    445477        wp_set_current_user( 0 );
     478        $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
    446479        $response = $this->server->dispatch( $request );
    447480        $this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
     
    471504        $response = $this->server->dispatch( $request );
    472505        $this->assertCount( 2, $response->get_data() );
     506        // Invalid parent should error
     507        $request->set_param( 'parent', 'invalid' );
     508        $response = $this->server->dispatch( $request );
     509        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    473510    }
    474511
     
    496533        $response = $this->server->dispatch( $request );
    497534        $this->assertCount( 3, $response->get_data() );
     535        // Invalid parent id should error
     536        $request->set_param( 'parent_exclude', 'invalid' );
     537        $response = $this->server->dispatch( $request );
     538        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    498539    }
    499540
     
    958999    }
    9591000
     1001    public function test_create_comment_invalid_email() {
     1002        $post_id = $this->factory->post->create();
     1003        wp_set_current_user( self::$admin_id );
     1004
     1005        $params = array(
     1006            'post'  => $post_id,
     1007            'author'       => self::$admin_id,
     1008            'author_name'  => 'Comic Book Guy',
     1009            'author_email' => 'hello:)',
     1010            'author_url'   => 'http://androidsdungeon.com',
     1011            'content' => 'Worst Comment Ever!',
     1012            'date'  => '2014-11-07T10:14:25',
     1013        );
     1014
     1015        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1016        $request->add_header( 'content-type', 'application/json' );
     1017        $request->set_body( wp_json_encode( $params ) );
     1018
     1019        $response = $this->server->dispatch( $request );
     1020        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1021    }
     1022
    9601023    public function test_create_item_current_user() {
    9611024        $user_id = $this->factory->user->create( array(
     
    10561119
    10571120        $this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 );
     1121    }
     1122
     1123    public function test_create_comment_invalid_post() {
     1124        wp_set_current_user( self::$subscriber_id );
     1125
     1126        $params = array(
     1127            'post'         => 'some-slug',
     1128            'author_name'  => 'Homer Jay Simpson',
     1129            'author_email' => 'chunkylover53@aol.com',
     1130            'author_url'   => 'http://compuglobalhypermeganet.com',
     1131            'content'      => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
     1132            'author'       => self::$subscriber_id,
     1133        );
     1134
     1135        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1136        $request->add_header( 'content-type', 'application/json' );
     1137        $request->set_body( wp_json_encode( $params ) );
     1138        $response = $this->server->dispatch( $request );
     1139
     1140        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1141    }
     1142
     1143    public function test_create_comment_karma_invalid_value() {
     1144        wp_set_current_user( self::$subscriber_id );
     1145
     1146        $params = array(
     1147            'post'       => self::$post_id,
     1148            'author_name'  => 'Homer Jay Simpson',
     1149            'author_email' => 'chunkylover53@aol.com',
     1150            'author_url'   => 'http://compuglobalhypermeganet.com',
     1151            'content'     => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
     1152            'author'       => self::$subscriber_id,
     1153            'karma'     => 'themostkarmaever',
     1154        );
     1155
     1156        $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1157        $request->add_header( 'content-type', 'application/json' );
     1158        $request->set_body( wp_json_encode( $params ) );
     1159        $response = $this->server->dispatch( $request );
     1160
     1161        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    10581162    }
    10591163
     
    18941998        $this->assertArrayHasKey( 'status', $properties );
    18951999        $this->assertArrayHasKey( 'type', $properties );
     2000
     2001        $this->assertEquals( '127.0.0.1', $properties['author_ip']['default'] );
     2002        $this->assertEquals( 0, $properties['parent']['default'] );
     2003        $this->assertEquals( 0, $properties['post']['default'] );
    18962004    }
    18972005
Note: See TracChangeset for help on using the changeset viewer.