Make WordPress Core


Ignore:
Timestamp:
09/29/2023 03:05:40 PM (3 years ago)
Author:
adamsilverstein
Message:

Revisions: slash meta values for autosave (preview) revisions.

Correct an issue where meta values containing characters like quote could not be previewed on published posts. The function update_metadata expects data to be slashed.

Also, add a test to confirm that storing JSON data which requires slashing in autosave meta works as expected, and improve naming for a data provider added in [56714].

Follow up to [56714].

Props mukesh27, spacedmonkey.
Fixes #20564.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php

    r56714 r56745  
    342342        }
    343343
     344        public function test_update_item_with_meta() {
     345                wp_set_current_user( self::$editor_id );
     346                $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/autosaves' );
     347                $request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
     348                register_post_meta(
     349                        'post',
     350                        'foo',
     351                        array(
     352                                'show_in_rest'      => true,
     353                                'revisions_enabled' => true,
     354                                'single'            => true,
     355                        )
     356                );
     357                $params = $this->set_post_data(
     358                        array(
     359                                'id'     => self::$post_id,
     360                                'author' => self::$contributor_id,
     361                                'meta'   => array(
     362                                        'foo' => 'bar',
     363                                ),
     364                        )
     365                );
     366
     367                $request->set_body_params( $params );
     368                $response = rest_get_server()->dispatch( $request );
     369
     370                $this->check_create_autosave_response( $response );
     371
     372                $data = $response->get_data();
     373                $this->assertArrayHasKey( 'meta', $data );
     374                $this->assertArrayHasKey( 'foo', $data['meta'] );
     375                $this->assertSame( 'bar', $data['meta']['foo'] );
     376        }
     377
     378        public function test_update_item_with_json_meta() {
     379                $meta = '[{\"content\":\"foot 1\",\"id\":\"fa97a10d-7401-42b9-ac54-df8f4510749a\"},{\"content\":\"fdddddoot 2\\\"\",\"id\":\"2216d0aa-34b8-42b4-b441-84dedc0406e0\"}]';
     380                wp_set_current_user( self::$editor_id );
     381                $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/autosaves' );
     382                $request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
     383                register_post_meta(
     384                        'post',
     385                        'foo',
     386                        array(
     387                                'show_in_rest'      => true,
     388                                'revisions_enabled' => true,
     389                                'single'            => true,
     390                        )
     391                );
     392                $params = $this->set_post_data(
     393                        array(
     394                                'id'     => self::$post_id,
     395                                'author' => self::$contributor_id,
     396                                'meta'   => array(
     397                                        'foo' => $meta,
     398                                ),
     399                        )
     400                );
     401
     402                $request->set_body_params( $params );
     403                $response = rest_get_server()->dispatch( $request );
     404
     405                $this->check_create_autosave_response( $response );
     406
     407                $data = $response->get_data();
     408                $this->assertArrayHasKey( 'meta', $data );
     409                $this->assertArrayHasKey( 'foo', $data['meta'] );
     410                $values = json_decode( wp_unslash( $data['meta']['foo'] ), true );
     411                $this->assertNotNull( $values );
     412        }
     413
    344414        public function test_update_item_nopriv() {
    345415                wp_set_current_user( self::$contributor_id );
Note: See TracChangeset for help on using the changeset viewer.