Make WordPress Core


Ignore:
Timestamp:
11/23/2016 02:46:42 AM (8 years ago)
Author:
joehoyle
Message:

REST API: Allow unsetting of page templates in update requests.

Sending a request to update a page with the template property set to an empty string resulted in an error because “” was not a valid value in the enum.

Props lucasstark, swissspidy.
Fixes #38877.

File:
1 edited

Legend:

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

    r39182 r39343  
    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(
     
    10581064        $params = $this->set_post_data( array(
    10591065            'template' => 'post-my-test-template.php',
     1066        ) );
     1067        $request->set_body_params( $params );
     1068        $response = $this->server->dispatch( $request );
     1069
     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        update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' );
     1080
     1081        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1082        $params = $this->set_post_data( array(
     1083            'template' => '',
    10601084        ) );
    10611085        $request->set_body_params( $params );
     
    20552079        $this->assertErrorResponse( 'rest_cannot_assign_term', $response, 403 );
    20562080    }
     2081
     2082    /**
     2083     * @ticket 38698
     2084     */
     2085    public function test_update_item_with_template() {
     2086        wp_set_current_user( self::$editor_id );
     2087        add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
     2088
     2089        // reregister the route as we now have a template available.
     2090        $GLOBALS['wp_rest_server']->override_by_default = true;
     2091        $controller = new WP_REST_Posts_Controller( 'post' );
     2092        $controller->register_routes();
     2093        $GLOBALS['wp_rest_server']->override_by_default = false;
     2094
     2095        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2096        $params = $this->set_post_data( array(
     2097            'template' => 'post-my-test-template.php',
     2098        ) );
     2099        $request->set_body_params( $params );
     2100        $response = $this->server->dispatch( $request );
     2101
     2102        $data = $response->get_data();
     2103        $post_template = get_page_template_slug( get_post( $data['id'] ) );
     2104
     2105        $this->assertEquals( 'post-my-test-template.php', $data['template'] );
     2106        $this->assertEquals( 'post-my-test-template.php', $post_template );
     2107    }
     2108
     2109    /**
     2110     * @ticket 38877
     2111     */
     2112    public function test_update_item_with_template_none() {
     2113        wp_set_current_user( self::$editor_id );
     2114        add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
     2115        update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' );
     2116
     2117        // reregister the route as we now have a template available.
     2118        $GLOBALS['wp_rest_server']->override_by_default = true;
     2119        $controller = new WP_REST_Posts_Controller( 'post' );
     2120        $controller->register_routes();
     2121        $GLOBALS['wp_rest_server']->override_by_default = false;
     2122
     2123        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2124        $params = $this->set_post_data( array(
     2125            'template' => '',
     2126        ) );
     2127        $request->set_body_params( $params );
     2128        $response = $this->server->dispatch( $request );
     2129
     2130        $data = $response->get_data();
     2131        $post_template = get_page_template_slug( get_post( $data['id'] ) );
     2132
     2133        $this->assertEquals( '', $data['template'] );
     2134        $this->assertEquals( '', $post_template );
     2135    }
     2136
    20572137
    20582138    public function verify_post_roundtrip( $input = array(), $expected_output = array() ) {
Note: See TracChangeset for help on using the changeset viewer.