Make WordPress Core


Ignore:
Timestamp:
12/13/2018 04:29:57 PM (8 years ago)
Author:
desrosj
Message:

REST API: Slash existing meta values when comparing with incoming meta upates.

When comparing the old and new values for a meta key being set, ensure both values are sanitized using the same logic so that equal values match.

props boonebgorges, dcavins, MattGeri, pilou69, TimothyBlynJacobs, kadamwhite.

Merges [43740] to trunk.

Fixes #42069.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r43571 r44113  
    190190                                'single'        => true,
    191191                                'auth_callback' => '__return_false',
     192                        )
     193                );
     194
     195                register_meta(
     196                        'post',
     197                        'test_boolean_update',
     198                        array(
     199                                'single'            => true,
     200                                'type'              => 'boolean',
     201                                'sanitize_callback' => 'absint',
     202                                'show_in_rest'      => true,
     203                        )
     204                );
     205
     206                register_meta(
     207                        'post',
     208                        'test_textured_text_update',
     209                        array(
     210                                'single'            => true,
     211                                'type'              => 'string',
     212                                'sanitize_callback' => 'sanitize_text_field',
     213                                'show_in_rest'      => true,
     214                        )
     215                );
     216
     217                register_meta(
     218                        'post',
     219                        'test_json_encoded',
     220                        array(
     221                                'single'       => true,
     222                                'type'         => 'string',
     223                                'show_in_rest' => true,
     224                        )
     225                );
     226
     227                register_meta(
     228                        'post',
     229                        'test\'slashed\'key',
     230                        array(
     231                                'single'       => true,
     232                                'type'         => 'string',
     233                                'show_in_rest' => true,
    192234                        )
    193235                );
    … …  
    12571299
    12581300        /**
     1301         * @ticket 42069
     1302         * @dataProvider data_update_value_return_success_with_same_value
     1303         */
     1304        public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) {
     1305                add_post_meta( self::$post_id, $meta_key, $meta_value );
     1306
     1307                $this->grant_write_permission();
     1308
     1309                $data = array(
     1310                        'meta' => array(
     1311                                $meta_key => $meta_value,
     1312                        ),
     1313                );
     1314
     1315                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1316                $request->set_body_params( $data );
     1317
     1318                $response = rest_get_server()->dispatch( $request );
     1319
     1320                $this->assertEquals( 200, $response->get_status() );
     1321        }
     1322
     1323        public function data_update_value_return_success_with_same_value() {
     1324                return array(
     1325                        array( 'test_boolean_update', false ),
     1326                        array( 'test_boolean_update', true ),
     1327                        array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ),
     1328                        array( 'test_textured_text_update', "He's about to do something rash..." ),
     1329                        array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ),
     1330                        array( 'test\'slashed\'key', 'Hello' ),
     1331                );
     1332        }
     1333
     1334        /**
     1335         * @ticket 42069
     1336         */
     1337        public function test_slashed_meta_key() {
     1338
     1339                add_post_meta( self::$post_id, 'test\'slashed\'key', 'Hello' );
     1340
     1341                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1342
     1343                $response = rest_get_server()->dispatch( $request );
     1344                $data     = $response->get_data();
     1345
     1346                $this->assertArrayHasKey( 'test\'slashed\'key', $data['meta'] );
     1347                $this->assertEquals( 'Hello', $data['meta']['test\'slashed\'key'] );
     1348        }
     1349
     1350        /**
    12591351         * Internal function used to disable an insert query which
    12601352         * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.