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

Props joehoyle, jnylen0.
Fixes #38617.

git-svn-id: https://develop.svn.wordpress.org/trunk@39105 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Rachel Baker
2016-11-03 02:17:39 +00:00
parent 25b892c1ca
commit baf52c815a
12 changed files with 294 additions and 89 deletions

View File

@@ -300,6 +300,15 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$data = $response->get_data();
$this->assertEquals( 2, count( $data ) );
$this->assertEquals( $id3, $data[0]['id'] );
// Orderby=>invalid should fail.
$request->set_param( 'orderby', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
// fails on invalid id.
$request->set_param( 'orderby', array( 'include' ) );
$request->set_param( 'include', array( 'invalid' ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_exclude_query() {
@@ -320,6 +329,11 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$data = $response->get_data();
$this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
$this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
// fails on invalid id.
$request->set_param( 'exclude', array( 'invalid' ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_offset_query() {
@@ -343,6 +357,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$request->set_param( 'page', 3 );
$response = $this->server->dispatch( $request );
$this->assertCount( 2, $response->get_data() );
// 'offset' with invalid value errors.
$request->set_param( 'offset', 'moreplease' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_order_query() {
@@ -364,6 +382,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( self::$approved_id, $data[0]['id'] );
// order=>asc,id should fail
$request->set_param( 'order', 'asc,id' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_private_post_no_permissions() {
@@ -402,8 +424,13 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertEquals( 200, $response->get_status() );
$comments = $response->get_data();
$this->assertCount( 2, $comments );
// Invalid author param errors
$request->set_param( 'author', 'skippy' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
// Unavailable to unauthenticated; defaults to error
wp_set_current_user( 0 );
$request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
}
@@ -441,8 +468,14 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertEquals( 200, $response->get_status() );
$comments = $response->get_data();
$this->assertCount( 2, $comments );
// 'author_exclude' for both invalid author
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
$request->set_param( 'author_exclude', 'skippy' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
// Unavailable to unauthenticated; defaults to error
wp_set_current_user( 0 );
$request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
}
@@ -470,6 +503,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$request->set_param( 'parent', array( $parent_id, $parent_id2 ) );
$response = $this->server->dispatch( $request );
$this->assertCount( 2, $response->get_data() );
// Invalid parent should error
$request->set_param( 'parent', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_parent_exclude_arg() {
@@ -495,6 +532,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) );
$response = $this->server->dispatch( $request );
$this->assertCount( 3, $response->get_data() );
// Invalid parent id should error
$request->set_param( 'parent_exclude', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_search_query() {
@@ -957,6 +998,28 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertEquals( $comment_id, $collection_data[0]['id'] );
}
public function test_create_comment_invalid_email() {
$post_id = $this->factory->post->create();
wp_set_current_user( self::$admin_id );
$params = array(
'post' => $post_id,
'author' => self::$admin_id,
'author_name' => 'Comic Book Guy',
'author_email' => 'hello:)',
'author_url' => 'http://androidsdungeon.com',
'content' => 'Worst Comment Ever!',
'date' => '2014-11-07T10:14:25',
);
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_create_item_current_user() {
$user_id = $this->factory->user->create( array(
'role' => 'subscriber',
@@ -1057,6 +1120,47 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertErrorResponse( 'rest_comment_invalid_karma', $response, 403 );
}
public function test_create_comment_invalid_post() {
wp_set_current_user( self::$subscriber_id );
$params = array(
'post' => 'some-slug',
'author_name' => 'Homer Jay Simpson',
'author_email' => 'chunkylover53@aol.com',
'author_url' => 'http://compuglobalhypermeganet.com',
'content' => 'Here\s to alcohol: the cause of, and solution to, all of life\s problems.',
'author' => self::$subscriber_id,
);
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_create_comment_karma_invalid_value() {
wp_set_current_user( self::$subscriber_id );
$params = array(
'post' => self::$post_id,
'author_name' => 'Homer Jay Simpson',
'author_email' => 'chunkylover53@aol.com',
'author_url' => 'http://compuglobalhypermeganet.com',
'content' => 'Here\s to alcohol: the cause of, and solution to, all of life\s problems.',
'author' => self::$subscriber_id,
'karma' => 'themostkarmaever',
);
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_create_comment_status_without_permission() {
wp_set_current_user( self::$subscriber_id );
@@ -1893,6 +1997,10 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertArrayHasKey( 'post', $properties );
$this->assertArrayHasKey( 'status', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertEquals( '127.0.0.1', $properties['author_ip']['default'] );
$this->assertEquals( 0, $properties['parent']['default'] );
$this->assertEquals( 0, $properties['post']['default'] );
}
public function test_get_item_schema_show_avatar() {