Make WordPress Core

Ticket #42069: 42069.9.diff

File 42069.9.diff, 4.7 KB (added by boonebgorges, 5 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 46c2d06250..54e0498132 100644
    abstract class WP_REST_Meta_Fields { 
    295295                        );
    296296                }
    297297
    298                 $meta_key   = wp_slash( $meta_key );
    299                 $meta_value = wp_slash( $value );
    300 
    301298                // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
    302299                $old_value = get_metadata( $meta_type, $object_id, $meta_key );
     300                $subtype   = get_object_subtype( $meta_type, $object_id );
    303301
    304302                if ( 1 === count( $old_value ) ) {
    305                         if ( $old_value[0] === $meta_value ) {
     303                        if ( (string) sanitize_meta( $meta_key, $value, $meta_type, $subtype ) === $old_value[0] ) {
    306304                                return true;
    307305                        }
    308306                }
    309307
    310                 if ( ! update_metadata( $meta_type, $object_id, $meta_key, $meta_value ) ) {
     308                if ( ! update_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
    311309                        return new WP_Error(
    312310                                'rest_meta_database_error',
    313311                                __( 'Could not update meta value in database.' ),
  • 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 cfeec389c5..58a2e2f449 100644
    class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    2020                        'supports'     => array( 'custom-fields' ),
    2121                ) );
    2222
    23                 self::$wp_meta_keys_saved = $GLOBALS['wp_meta_keys'];
     23                self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
    2424                self::$post_id = $factory->post->create();
    2525                self::$cpt_post_id        = $factory->post->create( array( 'post_type' => 'cpt' ) );
    2626        }
    class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    130130                        'auth_callback'  => '__return_false',
    131131                ) );
    132132
     133                register_meta(
     134                        'post',
     135                        'test_boolean_update',
     136                        array(
     137                                'single'            => true,
     138                                'type'              => 'boolean',
     139                                'sanitize_callback' => 'absint',
     140                                'show_in_rest'      => true,
     141                        )
     142                );
     143
     144                register_meta(
     145                        'post',
     146                        'test_textured_text_update',
     147                        array(
     148                                'single'            => true,
     149                                'type'              => 'string',
     150                                'sanitize_callback' => 'sanitize_text_field',
     151                                'show_in_rest'      => true,
     152                        )
     153                );
     154
     155                register_meta(
     156                        'post',
     157                        'test_json_encoded',
     158                        array(
     159                                'single'       => true,
     160                                'type'         => 'string',
     161                                'show_in_rest' => true,
     162                        )
     163                );
     164
     165                register_meta(
     166                        'post',
     167                        'test\'slashed\'key',
     168                        array(
     169                                'single'       => true,
     170                                'type'         => 'string',
     171                                'show_in_rest' => true,
     172                        )
     173                );
     174
    133175                /** @var WP_REST_Server $wp_rest_server */
    134176                global $wp_rest_server;
    135177                $this->server = $wp_rest_server = new Spy_REST_Server;
    class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase { 
    11611203                return $data;
    11621204        }
    11631205
     1206        /**
     1207         * @ticket 42069
     1208         * @dataProvider data_update_value_return_success_with_same_value
     1209         */
     1210        public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) {
     1211                add_post_meta( self::$post_id, $meta_key, $meta_value );
     1212
     1213                $this->grant_write_permission();
     1214
     1215                $data = array(
     1216                        'meta' => array(
     1217                                $meta_key => $meta_value,
     1218                        ),
     1219                );
     1220
     1221                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1222                $request->set_body_params( $data );
     1223
     1224                $response = rest_get_server()->dispatch( $request );
     1225
     1226                $this->assertEquals( 200, $response->get_status() );
     1227        }
     1228
     1229        public function data_update_value_return_success_with_same_value() {
     1230                return array(
     1231                        array( 'test_boolean_update', false ),
     1232                        array( 'test_boolean_update', true ),
     1233                        array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ),
     1234                        array( 'test_textured_text_update', "He's about to do something rash..." ),
     1235                        array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ),
     1236                        array( 'test\'slashed\'key', 'Hello' ),
     1237                );
     1238        }
     1239
     1240        /**
     1241         * @ticket 42069
     1242         */
     1243        public function test_slashed_meta_key() {
     1244
     1245                add_post_meta( self::$post_id, 'test\'slashed\'key', 'Hello' );
     1246
     1247                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1248
     1249                $response = rest_get_server()->dispatch( $request );
     1250                $data     = $response->get_data();
     1251
     1252                $this->assertArrayHasKey( 'test\'slashed\'key', $data['meta'] );
     1253                $this->assertEquals( 'Hello', $data['meta']['test\'slashed\'key'] );
     1254        }
     1255
    11641256        /**
    11651257         * Internal function used to disable an insert query which
    11661258         * will trigger a wpdb error for testing purposes.