Make WordPress Core


Ignore:
Timestamp:
04/08/2022 05:27:42 PM (3 years ago)
Author:
spacedmonkey
Message:

REST API: Use rest_parse_embed_param function in WP_REST_Server class.

Ensure that the value get parameter _embed that is passed to the envelope_response method, is run through the rest_parse_embed_param function.

Props Spacedmonkey, johnbillion, TimothyBlynJacobs.
Fixes #54015.

File:
1 edited

Legend:

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

    r52381 r53110  
    7474
    7575        $this->assertSame( $data, $enveloped['body'] );
     76        $this->assertSame( $status, $enveloped['status'] );
     77        $this->assertSame( $headers, $enveloped['headers'] );
     78    }
     79
     80    /**
     81     * @dataProvider data_envelope_params
     82     * @ticket 54015
     83     */
     84    public function test_envelope_param( $_embed ) {
     85        // Register our testing route.
     86        rest_get_server()->register_route(
     87            'test',
     88            '/test/embeddable',
     89            array(
     90                'methods'  => 'GET',
     91                'callback' => array( $this, 'embedded_response_callback' ),
     92            )
     93        );
     94        $data    = array(
     95            'amount of arbitrary data' => 'alot',
     96        );
     97        $status  = 987;
     98        $headers = array(
     99            'Arbitrary-Header' => 'value',
     100            'Multiple'         => 'maybe, yes',
     101        );
     102
     103        $response = new WP_REST_Response( $data, $status );
     104        $response->header( 'Arbitrary-Header', 'value' );
     105
     106        // Check header concatenation as well.
     107        $response->header( 'Multiple', 'maybe' );
     108        $response->header( 'Multiple', 'yes', false );
     109
     110        // All others should be embedded.
     111        $response->add_link( 'alternate', rest_url( '/test/embeddable' ), array( 'embeddable' => true ) );
     112
     113        $embed             = rest_parse_embed_param( $_embed );
     114        $envelope_response = rest_get_server()->envelope_response( $response, $embed );
     115
     116        // The envelope should still be a response, but with defaults.
     117        $this->assertInstanceOf( WP_REST_Response::class, $envelope_response );
     118        $this->assertSame( 200, $envelope_response->get_status() );
     119        $this->assertEmpty( $envelope_response->get_headers() );
     120        $this->assertEmpty( $envelope_response->get_links() );
     121
     122        $enveloped = $envelope_response->get_data();
     123
     124        $this->assertArrayHasKey( 'body', $enveloped );
     125        $this->assertArrayHasKey( '_links', $enveloped['body'] );
     126        $this->assertArrayHasKey( '_embedded', $enveloped['body'] );
     127        $this->assertArrayHasKey( 'alternate', $enveloped['body']['_embedded'] );
     128
     129        $alternate = $enveloped['body']['_embedded']['alternate'];
     130        $this->assertCount( 1, $alternate );
     131
    76132        $this->assertSame( $status, $enveloped['status'] );
    77133        $this->assertSame( $headers, $enveloped['headers'] );
     
    21742230        return rest_get_server()->sent_headers;
    21752231    }
     2232
     2233    /**
     2234     * Data provider.
     2235     *
     2236     * @return array
     2237     */
     2238    public function data_envelope_params() {
     2239        return array(
     2240            array( '1' ),
     2241            array( 'true' ),
     2242            array( false ),
     2243            array( 'alternate' ),
     2244            array( array( 'alternate' ) ),
     2245        );
     2246    }
    21762247}
Note: See TracChangeset for help on using the changeset viewer.