WordPress.org

Make WordPress Core

Ticket #38877: 38877.2.diff

File 38877.2.diff, 4.4 KB (added by joehoyle, 3 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

     
    19281928                $schema['properties']['template'] = array(
    19291929                        'description' => __( 'The theme file to use to display the object.' ),
    19301930                        'type'        => 'string',
    1931                         'enum'        => array_keys( wp_get_theme()->get_page_templates( null, $this->post_type ) ),
     1931                        'enum'        => array_merge( array_keys( wp_get_theme()->get_page_templates( null, $this->post_type ) ), array( '' ) ),
    19321932                        'context'     => array( 'view', 'edit' ),
    19331933                );
    19341934
  • tests/phpunit/includes/spy-rest-server.php

     
    55        public $sent_headers = array();
    66        public $sent_body = '';
    77        public $last_request = null;
     8        public $override_by_default = false;
    89
    910        /**
    1011         * Get the raw $endpoints data from the server
     
    4142                return parent::dispatch( $request );
    4243        }
    4344
     45        /**
     46         * Override the register_route method so we can re-register routes internally if needed.
     47         *
     48         * @param string $namespace  Namespace.
     49         * @param string $route      The REST route.
     50         * @param array  $route_args Route arguments.
     51         * @param bool   $override   Optional. Whether the route should be overridden if it already exists.
     52         *                           Default false. Also set $GLOBALS['wp_rest_server']->override_by_default = true
     53         *                           to set overrides when you don't have access to the caller context.
     54         */
     55        public function register_route( $namespace, $route, $route_args, $override = false ) {
     56                parent::register_route( $namespace, $route, $route_args, $override || $this->override_by_default );
     57        }
     58
    4459        public function serve_request( $path = null ) {
    4560
    4661                ob_start();
  • tests/phpunit/tests/rest-api/rest-pages-controller.php

     
    2727                parent::setUp();
    2828                $this->has_setup_template = false;
    2929                add_filter( 'theme_page_templates', array( $this, 'filter_theme_page_templates' ) );
     30                // reregister the route as we now have a template available.
     31                $GLOBALS['wp_rest_server']->override_by_default = true;
     32                $controller = new WP_REST_Posts_Controller( 'page' );
     33                $controller->register_routes();
     34                $GLOBALS['wp_rest_server']->override_by_default = false;
    3035        }
    3136
    3237        public function test_register_routes() {
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

     
    10321032                wp_set_current_user( self::$editor_id );
    10331033                add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
    10341034
     1035                // reregister the route as we now have a template available.
     1036                $GLOBALS['wp_rest_server']->override_by_default = true;
     1037                $controller = new WP_REST_Posts_Controller( 'post' );
     1038                $controller->register_routes();
     1039                $GLOBALS['wp_rest_server']->override_by_default = false;
     1040
    10351041                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    10361042                $params = $this->set_post_data( array(
    10371043                        'template' => 'post-my-test-template.php',
     
    10611067                $request->set_body_params( $params );
    10621068                $response = $this->server->dispatch( $request );
    10631069
     1070                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1071        }
     1072
     1073        /**
     1074         * @ticket 38877
     1075         */
     1076        public function test_create_item_with_template_none() {
     1077                wp_set_current_user( self::$editor_id );
     1078                add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
     1079
     1080                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1081                $params = $this->set_post_data( array(
     1082                        'template' => '',
     1083                ) );
     1084                $request->set_body_params( $params );
     1085                $response = $this->server->dispatch( $request );
     1086
    10641087                $data = $response->get_data();
    10651088                $post_template = get_page_template_slug( get_post( $data['id'] ) );
    10661089