REST API: Return a WP_Error if meta property is not an array.

Fixes bug where a PHP Warning is currently thrown if a client sends a request where `meta` is not an array value.

Props timmydcrawford, jnylen0, rachelbaker, pento.
Fixes #38989.

git-svn-id: https://develop.svn.wordpress.org/trunk@39436 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Rachel Baker
2016-12-02 21:55:09 +00:00
parent 511ba69e52
commit a55506974e
2 changed files with 88 additions and 6 deletions

View File

@@ -786,6 +786,65 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
$this->assertContains( 'graeme', $meta );
}
/**
* @ticket 38989
*/
public function test_set_value_invalid_meta_string_request_type() {
update_post_meta( self::$post_id, 'test_single', 'So I tied an onion to my belt, which was the style at the time.' );
$post_original = get_post( self::$post_id );
$this->grant_write_permission();
$data = array(
'title' => 'Ignore this title',
'meta' => 'Not an array.',
);
$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
$request->set_body_params( $data );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
// The meta value should not have changed.
$current_value = get_post_meta( self::$post_id, 'test_single', true );
$this->assertEquals( 'So I tied an onion to my belt, which was the style at the time.', $current_value );
// Ensure the post title update was not processed.
$post_updated = get_post( self::$post_id );
$this->assertEquals( $post_original->post_title, $post_updated->post_title );
}
/**
* @ticket 38989
*/
public function test_set_value_invalid_meta_float_request_type() {
update_post_meta( self::$post_id, 'test_single', 'Now, to take the ferry cost a nickel, and in those days, nickels had pictures of bumblebees on them.' );
$post_original = get_post( self::$post_id );
$this->grant_write_permission();
$data = array(
'content' => 'Ignore this content.',
'meta' => 1.234,
);
$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
$request->set_body_params( $data );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
// The meta value should not have changed.
$current_value = get_post_meta( self::$post_id, 'test_single', true );
$this->assertEquals( 'Now, to take the ferry cost a nickel, and in those days, nickels had pictures of bumblebees on them.', $current_value );
// Ensure the post content update was not processed.
$post_updated = get_post( self::$post_id );
$this->assertEquals( $post_original->post_content, $post_updated->post_content );
}
public function test_remove_multi_value_db_error() {
add_post_meta( self::$post_id, 'test_multi', 'val1' );
$values = get_post_meta( self::$post_id, 'test_multi', false );