Ticket #38877: 38877.3.diff
File 38877.3.diff, 7.3 KB (added by , 8 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index ba241c9..314fe3f 100644
a b class WP_REST_Posts_Controller extends WP_REST_Controller { 1928 1928 $schema['properties']['template'] = array( 1929 1929 'description' => __( 'The theme file to use to display the object.' ), 1930 1930 '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( '' ) ), 1932 1932 'context' => array( 'view', 'edit' ), 1933 1933 ); 1934 1934 -
tests/phpunit/includes/spy-rest-server.php
diff --git a/tests/phpunit/includes/spy-rest-server.php b/tests/phpunit/includes/spy-rest-server.php index 4ba9017..58e3c3b 100644
a b class Spy_REST_Server extends WP_REST_Server { 5 5 public $sent_headers = array(); 6 6 public $sent_body = ''; 7 7 public $last_request = null; 8 public $override_by_default = false; 8 9 9 10 /** 10 11 * Get the raw $endpoints data from the server … … class Spy_REST_Server extends WP_REST_Server { 41 42 return parent::dispatch( $request ); 42 43 } 43 44 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 44 59 public function serve_request( $path = null ) { 45 60 46 61 ob_start(); -
tests/phpunit/tests/rest-api/rest-pages-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index b74d01c..450cd5e 100644
a b class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te 27 27 parent::setUp(); 28 28 $this->has_setup_template = false; 29 29 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; 30 35 } 31 36 32 37 public function test_register_routes() { -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 26af946..526c390 100644
a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 1032 1032 wp_set_current_user( self::$editor_id ); 1033 1033 add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) ); 1034 1034 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 1035 1041 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' ); 1036 1042 $params = $this->set_post_data( array( 1037 1043 'template' => 'post-my-test-template.php', … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 1061 1067 $request->set_body_params( $params ); 1062 1068 $response = $this->server->dispatch( $request ); 1063 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' => '', 1084 ) ); 1085 $request->set_body_params( $params ); 1086 $response = $this->server->dispatch( $request ); 1087 1064 1088 $data = $response->get_data(); 1065 1089 $post_template = get_page_template_slug( get_post( $data['id'] ) ); 1066 1090 … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 2055 2079 $this->assertErrorResponse( 'rest_cannot_assign_term', $response, 403 ); 2056 2080 } 2057 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 2137 2058 2138 public function verify_post_roundtrip( $input = array(), $expected_output = array() ) { 2059 2139 // Create the post 2060 2140 $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );