- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php
r42228 r42343 21 21 self::$page_id = $factory->post->create( array( 'post_type' => 'page' ) ); 22 22 23 self::$editor_id = $factory->user->create( array( 24 'role' => 'editor', 25 ) ); 26 self::$contributor_id = $factory->user->create( array( 27 'role' => 'contributor', 28 ) ); 29 30 wp_set_current_user( self::$editor_id ); 31 wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => self::$post_id ) ); 32 wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => self::$post_id ) ); 23 self::$editor_id = $factory->user->create( 24 array( 25 'role' => 'editor', 26 ) 27 ); 28 self::$contributor_id = $factory->user->create( 29 array( 30 'role' => 'contributor', 31 ) 32 ); 33 34 wp_set_current_user( self::$editor_id ); 35 wp_update_post( 36 array( 37 'post_content' => 'This content is better.', 38 'ID' => self::$post_id, 39 ) 40 ); 41 wp_update_post( 42 array( 43 'post_content' => 'This content is marvelous.', 44 'ID' => self::$post_id, 45 ) 46 ); 33 47 wp_set_current_user( 0 ); 34 48 } … … 46 60 parent::setUp(); 47 61 48 $revisions = wp_get_post_revisions( self::$post_id );49 $this->revision_1 = array_pop( $revisions );62 $revisions = wp_get_post_revisions( self::$post_id ); 63 $this->revision_1 = array_pop( $revisions ); 50 64 $this->revision_id1 = $this->revision_1->ID; 51 $this->revision_2 = array_pop( $revisions );65 $this->revision_2 = array_pop( $revisions ); 52 66 $this->revision_id2 = $this->revision_2->ID; 53 67 } … … 63 77 public function test_context_param() { 64 78 // Collection 65 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );66 $response = $this->server->dispatch( $request ); 67 $data = $response->get_data();79 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' ); 80 $response = $this->server->dispatch( $request ); 81 $data = $response->get_data(); 68 82 $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); 69 83 $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] ); 70 84 // Single 71 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_1->ID );72 $response = $this->server->dispatch( $request ); 73 $data = $response->get_data();85 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_1->ID ); 86 $response = $this->server->dispatch( $request ); 87 $data = $response->get_data(); 74 88 $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); 75 89 $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] ); … … 78 92 public function test_get_items() { 79 93 wp_set_current_user( self::$editor_id ); 80 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );81 $response = $this->server->dispatch( $request ); 82 $data = $response->get_data();94 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' ); 95 $response = $this->server->dispatch( $request ); 96 $data = $response->get_data(); 83 97 $this->assertEquals( 200, $response->get_status() ); 84 98 $this->assertCount( 2, $data ); … … 94 108 public function test_get_items_no_permission() { 95 109 wp_set_current_user( 0 ); 96 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );110 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' ); 97 111 $response = $this->server->dispatch( $request ); 98 112 … … 105 119 public function test_get_items_missing_parent() { 106 120 wp_set_current_user( self::$editor_id ); 107 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );121 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' ); 108 122 $response = $this->server->dispatch( $request ); 109 123 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); … … 112 126 public function test_get_items_invalid_parent_post_type() { 113 127 wp_set_current_user( self::$editor_id ); 114 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions' );128 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions' ); 115 129 $response = $this->server->dispatch( $request ); 116 130 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); … … 119 133 public function test_get_item() { 120 134 wp_set_current_user( self::$editor_id ); 121 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );135 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 122 136 $response = $this->server->dispatch( $request ); 123 137 $this->assertEquals( 200, $response->get_status() ); … … 137 151 'content', 138 152 ); 139 $data = $response->get_data();153 $data = $response->get_data(); 140 154 $this->assertEqualSets( $fields, array_keys( $data ) ); 141 155 $this->assertSame( self::$editor_id, $data['author'] ); … … 147 161 $request->set_param( 'context', 'embed' ); 148 162 $response = $this->server->dispatch( $request ); 149 $fields = array(163 $fields = array( 150 164 'author', 151 165 'date', … … 156 170 'excerpt', 157 171 ); 158 $data = $response->get_data();172 $data = $response->get_data(); 159 173 $this->assertEqualSets( $fields, array_keys( $data ) ); 160 174 } … … 173 187 public function test_get_item_missing_parent() { 174 188 wp_set_current_user( self::$editor_id ); 175 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 );189 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 ); 176 190 $response = $this->server->dispatch( $request ); 177 191 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); … … 180 194 public function test_get_item_invalid_parent_post_type() { 181 195 wp_set_current_user( self::$editor_id ); 182 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 );196 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 ); 183 197 $response = $this->server->dispatch( $request ); 184 198 $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 ); … … 197 211 wp_set_current_user( self::$editor_id ); 198 212 199 $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );213 $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 200 214 $response = $this->server->dispatch( $request ); 201 215 $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); … … 211 225 public function test_delete_item_no_permission() { 212 226 wp_set_current_user( self::$contributor_id ); 213 $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );227 $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 214 228 $response = $this->server->dispatch( $request ); 215 229 $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); … … 218 232 public function test_prepare_item() { 219 233 wp_set_current_user( self::$editor_id ); 220 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );234 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 221 235 $response = $this->server->dispatch( $request ); 222 236 $this->assertEquals( 200, $response->get_status() ); … … 225 239 226 240 public function test_get_item_schema() { 227 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );228 $response = $this->server->dispatch( $request );229 $data = $response->get_data();241 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' ); 242 $response = $this->server->dispatch( $request ); 243 $data = $response->get_data(); 230 244 $properties = $data['schema']['properties']; 231 245 $this->assertEquals( 12, count( $properties ) ); … … 245 259 246 260 public function test_create_item() { 247 $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions' );261 $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions' ); 248 262 $response = $this->server->dispatch( $request ); 249 263 $this->assertErrorResponse( 'rest_no_route', $response, 404 ); … … 251 265 252 266 public function test_update_item() { 253 $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );267 $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 ); 254 268 $response = $this->server->dispatch( $request ); 255 269 $this->assertErrorResponse( 'rest_no_route', $response, 404 ); … … 265 279 ); 266 280 267 register_rest_field( 'post-revision', 'my_custom_int', array( 268 'schema' => $schema, 269 'get_callback' => array( $this, 'additional_field_get_callback' ), 270 'update_callback' => array( $this, 'additional_field_update_callback' ), 271 ) ); 281 register_rest_field( 282 'post-revision', 'my_custom_int', array( 283 'schema' => $schema, 284 'get_callback' => array( $this, 'additional_field_get_callback' ), 285 'update_callback' => array( $this, 'additional_field_update_callback' ), 286 ) 287 ); 272 288 273 289 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' ); 274 290 275 291 $response = $this->server->dispatch( $request ); 276 $data = $response->get_data();292 $data = $response->get_data(); 277 293 278 294 $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] ); … … 300 316 protected function check_get_revision_response( $response, $revision ) { 301 317 if ( $response instanceof WP_REST_Response ) { 302 $links = $response->get_links();318 $links = $response->get_links(); 303 319 $response = $response->get_data(); 304 320 } else { … … 329 345 $this->assertEquals( $rendered_title, $response['title']['rendered'] ); 330 346 331 $parent = get_post( $revision->post_parent );347 $parent = get_post( $revision->post_parent ); 332 348 $parent_controller = new WP_REST_Posts_Controller( $parent->post_type ); 333 $parent_object = get_post_type_object( $parent->post_type );334 $parent_base = ! empty( $parent_object->rest_base ) ? $parent_object->rest_base : $parent_object->name;349 $parent_object = get_post_type_object( $parent->post_type ); 350 $parent_base = ! empty( $parent_object->rest_base ) ? $parent_object->rest_base : $parent_object->name; 335 351 $this->assertEquals( rest_url( '/wp/v2/' . $parent_base . '/' . $revision->post_parent ), $links['parent'][0]['href'] ); 336 352 } … … 341 357 $this->server->dispatch( $request ); 342 358 343 $post = get_post();359 $post = get_post(); 344 360 $parent_post_id = wp_is_post_revision( $post->ID ); 345 361
Note: See TracChangeset
for help on using the changeset viewer.