Make WordPress Core

Ticket #42069: 42069.6.diff

File 42069.6.diff, 3.0 KB (added by boonebgorges, 5 years ago)
  • src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    diff --git a/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
    index 46c2d06250..23cf67613c 100644
    a b abstract class WP_REST_Meta_Fields { 
    302302                $old_value = get_metadata( $meta_type, $object_id, $meta_key );
    303303
    304304                if ( 1 === count( $old_value ) ) {
    305                         if ( $old_value[0] === $meta_value ) {
     305                        if ( (string) sanitize_meta( $meta_key, wp_unslash( $meta_value ), $meta_type ) === $old_value[0] ) {
    306306                                return true;
    307307                        }
    308308                }
  • tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    diff --git a/tests/phpunit/tests/rest-api/rest-post-meta-fields.php b/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
    index cfeec389c5..e9dc1c628b 100644
    a b class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    108108                        ),
    109109                ));
    110110
     111                register_meta(
     112                        'post', 'test_boolean_update', array(
     113                                'single'            => true,
     114                                'type'              => 'boolean',
     115                                'sanitize_callback' => 'absint',
     116                                'show_in_rest'      => true,
     117                        )
     118                );
     119                register_meta(
     120                        'post', 'test_textured_text_update', array(
     121                                'single'            => true,
     122                                'type'              => 'string',
     123                                'sanitize_callback' => 'sanitize_text_field',
     124                                'show_in_rest'      => true,
     125                        )
     126                );
     127
     128                register_meta(
     129                        'post', 'test_json_encoded', array(
     130                                'single'            => true,
     131                                'type'              => 'string',
     132                                'show_in_rest'      => true,
     133                        )
     134                );
     135
    111136                register_post_type( 'cpt', array(
    112137                        'show_in_rest' => true,
    113138                        'supports'     => array( 'custom-fields' ),
    class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    11611186                return $data;
    11621187        }
    11631188
     1189        /**
     1190         * @ticket 42069
     1191         * @dataProvider data_update_value_return_success_with_same_value
     1192         */
     1193        public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) {
     1194                add_post_meta( self::$post_id, $meta_key, $meta_value );
     1195
     1196                $this->grant_write_permission();
     1197
     1198                $data = array(
     1199                        'meta' => array(
     1200                                $meta_key => $meta_value,
     1201                        ),
     1202                );
     1203
     1204                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1205                $request->set_body_params( $data );
     1206
     1207                $response = rest_get_server()->dispatch( $request );
     1208
     1209                $this->assertEquals( 200, $response->get_status() );
     1210        }
     1211
     1212        public function data_update_value_return_success_with_same_value() {
     1213                return array(
     1214                        array( 'test_boolean_update', false ),
     1215                        array( 'test_boolean_update', true ),
     1216                        array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ),
     1217                        array( 'test_textured_text_update', "He's about to do something rash..." ),
     1218                        array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ),
     1219                );
     1220        }
     1221
    11641222        /**
    11651223         * Internal function used to disable an insert query which
    11661224         * will trigger a wpdb error for testing purposes.