Make WordPress Core

Ticket #53056: 53056.diff

File 53056.diff, 2.4 KB (added by hermpheus, 2 years ago)
  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git src/wp-includes/rest-api/class-wp-rest-server.php src/wp-includes/rest-api/class-wp-rest-server.php
    index f9e35bdcbf..1f1d52966d 100644
    class WP_REST_Server { 
    497497                        $json_error_message = $this->get_json_last_error();
    498498
    499499                        if ( $json_error_message ) {
     500                                $this->set_status( 500 );
    500501                                $json_error_obj = new WP_Error(
    501502                                        'rest_encode_error',
    502503                                        $json_error_message,
  • tests/phpunit/includes/spy-rest-server.php

    diff --git tests/phpunit/includes/spy-rest-server.php tests/phpunit/includes/spy-rest-server.php
    index 596117347c..5bfd1d2dba 100644
    class Spy_REST_Server extends WP_REST_Server { 
    66        public $sent_body           = '';
    77        public $last_request        = null;
    88        public $override_by_default = false;
     9        public $status              = null;
    910
    1011        /**
    1112         * Gets the raw $endpoints data from the server.
    class Spy_REST_Server extends WP_REST_Server { 
    3738                $this->sent_headers[ $header ] = $value;
    3839        }
    3940
     41        /**
     42         * Stores last set status.
     43         * @param int $code HTTP status.
     44         */
     45        public function set_status( $status ) {
     46                $this->status = $status;
     47        }
     48
    4049        /**
    4150         * Removes a header from the list of sent headers.
    4251         *
  • tests/phpunit/tests/rest-api/rest-server.php

    diff --git tests/phpunit/tests/rest-api/rest-server.php tests/phpunit/tests/rest-api/rest-server.php
    index 0dbc308bf9..672a5c2905 100644
    class Tests_REST_Server extends WP_Test_REST_TestCase { 
    20222022                $this->assertArrayHasKey( 'https://api.w.org/active-theme', $index->get_links() );
    20232023        }
    20242024
     2025        /**
     2026         * @ticket 53056
     2027         */
     2028        public function test_json_encode_error_results_in_500_status_code() {
     2029                register_rest_route(
     2030                        'test-ns/v1',
     2031                        '/test',
     2032                        array(
     2033                                array(
     2034                                        'methods'             => \WP_REST_Server::READABLE,
     2035                                        'callback'            => function() {
     2036                                                return new \WP_REST_Response( INF );
     2037                                        },
     2038                                        'permission_callback' => '__return_true',
     2039                                        'args'                => array(),
     2040                                ),
     2041                        )
     2042                );
     2043                rest_get_server()->serve_request( '/test-ns/v1/test' );
     2044                $this->assertSame( 500, rest_get_server()->status );
     2045        }
     2046
    20252047        public function _validate_as_integer_123( $value, $request, $key ) {
    20262048                if ( ! is_int( $value ) ) {
    20272049                        return new WP_Error( 'some-error', 'This is not valid!' );