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
@@ -428,6 +428,15 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
$this->assertEquals( 'Child', $data[0]['name'] );
}
public function test_get_terms_invalid_parent_arg() {
$category1 = $this->factory->category->create( array( 'name' => 'Parent' ) );
$this->factory->category->create( array( 'name' => 'Child', 'parent' => $category1 ) );
$request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
$request->set_param( 'parent', 'invalid-parent' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_terms_private_taxonomy() {
register_taxonomy( 'robin', 'post', array( 'public' => false ) );
$this->factory->term->create( array( 'name' => 'Cape', 'taxonomy' => 'robin' ) );
@@ -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() {
@@ -95,6 +95,10 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te
$data = $response->get_data();
$this->assertEquals( 1, count( $data ) );
$this->assertEquals( $id2, $data[0]['id'] );
// Invalid parent should fail
$request->set_param( 'parent', 'some-slug' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_parents_query() {
@@ -129,6 +133,10 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te
$data = $response->get_data();
$this->assertEquals( 1, count( $data ) );
$this->assertEquals( $id1, $data[0]['id'] );
// Invalid parent_exclude should error
$request->set_param( 'parent_exclude', 'some-slug' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_menu_order_query() {
@@ -156,6 +164,11 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertEquals( $id4, $data[1]['id'] );
$this->assertEquals( $id2, $data[2]['id'] );
$this->assertEquals( $id3, $data[3]['id'] );
// Invalid menu_order should fail
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
$request->set_param( 'menu_order', 'top-first' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_min_max_pages_query() {
@@ -92,6 +92,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
'author_exclude',
'before',
'categories',
'categories_exclude',
'context',
'exclude',
'include',
@@ -105,6 +106,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
'status',
'sticky',
'tags',
'tags_exclude',
), $keys );
}
@@ -183,6 +185,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertEquals( 2, count( $data ) );
$this->assertNotEquals( self::$editor_id, $data[0]['author'] );
$this->assertNotEquals( self::$editor_id, $data[1]['author'] );
// invalid author_exclude errors
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'author_exclude', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_include_query() {
@@ -202,6 +209,11 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$data = $response->get_data();
$this->assertEquals( 2, count( $data ) );
$this->assertEquals( $id1, $data[0]['id'] );
// Invalid include should error
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'include', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_exclude_query() {
@@ -224,6 +236,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$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 ) );
$request->set_param( 'exclude', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_search_query() {
@@ -406,6 +422,15 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );
// order=>asc,id should fail
$request->set_param( 'order', 'asc,id' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
// orderby=>content should fail (invalid param test)
$request->set_param( 'order', 'asc' );
$request->set_param( 'orderby', 'content' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_with_orderby_relevance() {
@@ -456,6 +481,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$request->set_param( 'page', 3 );
$response = $this->server->dispatch( $request );
$this->assertCount( 2, $response->get_data() );
// Invalid 'offset' should error
$request->set_param( 'offset', 'moreplease' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_tags_query() {
@@ -512,6 +541,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$response = $this->server->dispatch( $request );
$this->assertCount( 1, $response->get_data() );
$request->set_param( 'tags', array( 'my-tag' ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_tags_and_categories_exclude_query() {
@@ -534,6 +567,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$data = $response->get_data();
$this->assertCount( 1, $data );
$this->assertEquals( $id2, $data[0]['id'] );
$request->set_param( 'tags_exclude', array( 'my-tag' ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_sticky_query() {
@@ -551,6 +588,10 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$posts = $response->get_data();
$post = $posts[0];
$this->assertEquals( $id2, $post['id'] );
$request->set_param( 'sticky', 'nothanks' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_sticky_with_post__in_query() {
@@ -1989,7 +2030,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 25, count( $properties ) );
$this->assertEquals( 23, count( $properties ) );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'comment_status', $properties );
$this->assertArrayHasKey( 'content', $properties );
@@ -2012,9 +2053,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'tags', $properties );
$this->assertArrayHasKey( 'tags_exclude', $properties );
$this->assertArrayHasKey( 'categories', $properties );
$this->assertArrayHasKey( 'categories_exclude', $properties );
}
public function test_status_array_enum_args() {
@@ -98,6 +98,10 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertEquals( 2, count( $data ) );
$this->assertEquals( 'Season 5', $data[0]['name'] );
$this->assertEquals( 'The Be Sharps', $data[1]['name'] );
// invalid value should fail
$request->set_param( 'hide_empty', 'nothanks' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_include_query() {
@@ -117,6 +121,10 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$data = $response->get_data();
$this->assertEquals( 2, count( $data ) );
$this->assertEquals( $id3, $data[0]['id'] );
// Include invalid value shoud fail
$request->set_param( 'include', array( 'myterm' ) );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_exclude_query() {
@@ -132,6 +140,10 @@ class WP_Test_REST_Tags_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 ) );
// Invalid exclude value should fail
$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() {
@@ -151,6 +163,10 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$request->set_param( 'page', 3 );
$response = $this->server->dispatch( $request );
$this->assertCount( 2, $response->get_data() );
// 'offset' invalid value shoudl fail
$request->set_param( 'offset', 'moreplease' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
@@ -181,6 +197,10 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$data = $response->get_data();
$this->assertEquals( 2, count( $data ) );
$this->assertEquals( 'Apple', $data[0]['name'] );
// Invalid orderby should fail.
$request->set_param( 'orderby', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_orderby_id() {
@@ -231,6 +251,12 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$data = $response->get_data();
$this->assertEquals( 2, count( $data ) );
$this->assertEquals( 'DC', $data[0]['name'] );
// Invalid post should error.
$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
$request->set_param( 'post', 'invalid-post' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_terms_post_args_paging() {
@@ -363,6 +363,20 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertErrorResponse( 'rest_forbidden_orderby', $response, 401 );
}
public function test_get_items_invalid_order() {
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
$request->set_param( 'order', 'asc,id' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_invalid_orderby() {
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
$request->set_param( 'orderby', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_offset() {
wp_set_current_user( self::$user );
// 2 users created in __construct(), plus default user
@@ -379,6 +393,10 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$request->set_param( 'page', 3 );
$response = $this->server->dispatch( $request );
$this->assertCount( 2, $response->get_data() );
// 'offset' invalid value should error
$request->set_param( 'offset', 'moreplease' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_include_query() {
@@ -399,7 +417,12 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$data = $response->get_data();
$this->assertEquals( 2, count( $data ) );
$this->assertEquals( $id3, $data[0]['id'] );
// Invalid include should fail
$request->set_param( 'include', 'invalid' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
// No privileges
$request->set_param( 'include', array( $id3, $id1 ) );
wp_set_current_user( 0 );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
@@ -421,6 +444,10 @@ class WP_Test_REST_Users_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 ) );
// Invalid exlude value should error.
$request->set_param( 'exclude', 'none-of-those-please' );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
public function test_get_items_search() {