Ticket #42069: 42069.2.diff
File 42069.2.diff, 4.6 KB (added by , 7 years ago) |
---|
-
src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
diff --git src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php index e0b41440a9..251218f0fe 100644
abstract class WP_REST_Meta_Fields { 307 307 $meta_key = wp_slash( $meta_key ); 308 308 $meta_value = wp_slash( $value ); 309 309 310 // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. 310 /* 311 * The function update_metadata() has a surprising behavior: it returns false 312 * if the new value matches the existing value. To avoid returning a server error 313 * response when updating meta, we must check for values that will be interpreted 314 * as equivalent by update_metadata() and its underlying function, $wpdb->query(). 315 */ 311 316 $old_value = get_metadata( $meta_type, $object_id, $meta_key ); 312 313 317 if ( 1 === count( $old_value ) ) { 314 if ( $old_value[0] === $meta_value ) { 318 $old_check_value = sanitize_meta( $meta_key, $old_value[0], $meta_type ); 319 $meta_check_value = sanitize_meta( $meta_key, $value, $meta_type ); 320 if ( $old_check_value === $meta_check_value ) { 315 321 return true; 316 322 } 317 323 } -
tests/phpunit/tests/rest-api/rest-post-meta-fields.php
diff --git tests/phpunit/tests/rest-api/rest-post-meta-fields.php tests/phpunit/tests/rest-api/rest-post-meta-fields.php index 05685791ad..b1b5cb6dd2 100644
class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 119 119 ), 120 120 ) 121 121 ); 122 register_meta( 123 'post', 'test_boolean_update', array( 124 'single' => true, 125 'type' => 'boolean', 126 'sanitize_callback' => 'absint', 127 'show_in_rest' => true, 128 ) 129 ); 130 register_meta( 131 'post', 'test_textured_text_update', array( 132 'single' => true, 133 'type' => 'string', 134 'sanitize_callback' => 'sanitize_text_field', 135 'show_in_rest' => true, 136 ) 137 ); 122 138 123 139 /** @var WP_REST_Server $wp_rest_server */ 124 140 global $wp_rest_server; … … class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 888 904 $this->assertEquals( $post_original->post_content, $post_updated->post_content ); 889 905 } 890 906 907 /** 908 * @ticket 42069 909 */ 910 public function test_update_value_return_success_response_when_updating_boolean_values_no_change() { 911 add_post_meta( self::$post_id, 'test_boolean_update', 0); 912 $post_original = get_post( self::$post_id ); 913 914 $this->grant_write_permission(); 915 916 $data = array( 917 'meta' => array( 918 'test_boolean_update' => 0, 919 ), 920 ); 921 922 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 923 $request->set_body_params( $data ); 924 925 $response = rest_get_server()->dispatch( $request ); 926 927 $this->assertEquals( 200, $response->get_status() ); 928 } 929 930 /** 931 * @ticket 42069 932 */ 933 public function test_update_value_return_success_response_when_updating_textured_string_values_no_change() { 934 add_post_meta( self::$post_id, 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ); 935 $post_original = get_post( self::$post_id ); 936 937 $this->grant_write_permission(); 938 939 $data = array( 940 'meta' => array( 941 'test_textured_text_update' => 'She said, "What about the > 10,000 penguins in the kitchen?"', 942 ), 943 ); 944 945 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 946 $request->set_body_params( $data ); 947 948 $response = rest_get_server()->dispatch( $request ); 949 950 $this->assertEquals( 200, $response->get_status() ); 951 } 952 953 /** 954 * @ticket 42069 955 */ 956 public function test_update_value_return_success_response_when_updating_textured_string_values_slashed_no_change() { 957 add_post_meta( self::$post_id, 'test_textured_text_update', "He's about to do something rash..." ); 958 $post_original = get_post( self::$post_id ); 959 960 $this->grant_write_permission(); 961 962 $data = array( 963 'meta' => array( 964 'test_textured_text_update' => "He's about to do something rash...", 965 ), 966 ); 967 968 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 969 $request->set_body_params( $data ); 970 971 $response = rest_get_server()->dispatch( $request ); 972 973 $this->assertEquals( 200, $response->get_status() ); 974 } 975 891 976 public function test_remove_multi_value_db_error() { 892 977 add_post_meta( self::$post_id, 'test_multi', 'val1' ); 893 978 $values = get_post_meta( self::$post_id, 'test_multi', false );