- 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-attachments-controller.php
r40306 r42343 19 19 20 20 public static function wpSetUpBeforeClass( $factory ) { 21 self::$superadmin_id = $factory->user->create( array( 22 'role' => 'administrator', 23 'user_login' => 'superadmin', 24 ) ); 25 self::$editor_id = $factory->user->create( array( 26 'role' => 'editor', 27 ) ); 28 self::$author_id = $factory->user->create( array( 29 'role' => 'author', 30 ) ); 31 self::$contributor_id = $factory->user->create( array( 32 'role' => 'contributor', 33 ) ); 34 self::$uploader_id = $factory->user->create( array( 35 'role' => 'uploader', 36 ) ); 21 self::$superadmin_id = $factory->user->create( 22 array( 23 'role' => 'administrator', 24 'user_login' => 'superadmin', 25 ) 26 ); 27 self::$editor_id = $factory->user->create( 28 array( 29 'role' => 'editor', 30 ) 31 ); 32 self::$author_id = $factory->user->create( 33 array( 34 'role' => 'author', 35 ) 36 ); 37 self::$contributor_id = $factory->user->create( 38 array( 39 'role' => 'contributor', 40 ) 41 ); 42 self::$uploader_id = $factory->user->create( 43 array( 44 'role' => 'uploader', 45 ) 46 ); 37 47 38 48 if ( is_multisite() ) { … … 58 68 $role->add_cap( 'level_0' ); 59 69 60 $orig_file = DIR_TESTDATA . '/images/canola.jpg';70 $orig_file = DIR_TESTDATA . '/images/canola.jpg'; 61 71 $this->test_file = '/tmp/canola.jpg'; 62 72 copy( $orig_file, $this->test_file ); 63 $orig_file2 = DIR_TESTDATA . '/images/codeispoetry.png';73 $orig_file2 = DIR_TESTDATA . '/images/codeispoetry.png'; 64 74 $this->test_file2 = '/tmp/codeispoetry.png'; 65 75 copy( $orig_file2, $this->test_file2 ); … … 111 121 public function test_parse_disposition( $header, $expected ) { 112 122 $header_list = array( $header ); 113 $parsed = WP_REST_Attachments_Controller::get_filename_from_disposition( $header_list );123 $parsed = WP_REST_Attachments_Controller::get_filename_from_disposition( $header_list ); 114 124 $this->assertEquals( $expected, $parsed ); 115 125 } … … 117 127 public function test_context_param() { 118 128 // Collection 119 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );120 $response = $this->server->dispatch( $request ); 121 $data = $response->get_data();129 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ); 130 $response = $this->server->dispatch( $request ); 131 $data = $response->get_data(); 122 132 $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); 123 133 $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); 124 134 // Single 125 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 126 'post_mime_type' => 'image/jpeg', 127 'post_excerpt' => 'A sample caption', 128 ) ); 129 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media/' . $attachment_id ); 130 $response = $this->server->dispatch( $request ); 131 $data = $response->get_data(); 135 $attachment_id = $this->factory->attachment->create_object( 136 $this->test_file, 0, array( 137 'post_mime_type' => 'image/jpeg', 138 'post_excerpt' => 'A sample caption', 139 ) 140 ); 141 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media/' . $attachment_id ); 142 $response = $this->server->dispatch( $request ); 143 $data = $response->get_data(); 132 144 $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); 133 145 $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); … … 135 147 136 148 public function test_registered_query_params() { 137 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );138 $response = $this->server->dispatch( $request ); 139 $data = $response->get_data();140 $keys = array_keys( $data['endpoints'][0]['args'] );149 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ); 150 $response = $this->server->dispatch( $request ); 151 $data = $response->get_data(); 152 $keys = array_keys( $data['endpoints'][0]['args'] ); 141 153 sort( $keys ); 142 $this->assertEquals( array( 143 'after', 144 'author', 145 'author_exclude', 146 'before', 147 'context', 148 'exclude', 149 'include', 150 'media_type', 151 'mime_type', 152 'offset', 153 'order', 154 'orderby', 155 'page', 156 'parent', 157 'parent_exclude', 158 'per_page', 159 'search', 160 'slug', 161 'status', 162 ), $keys ); 154 $this->assertEquals( 155 array( 156 'after', 157 'author', 158 'author_exclude', 159 'before', 160 'context', 161 'exclude', 162 'include', 163 'media_type', 164 'mime_type', 165 'offset', 166 'order', 167 'orderby', 168 'page', 169 'parent', 170 'parent_exclude', 171 'per_page', 172 'search', 173 'slug', 174 'status', 175 ), $keys 176 ); 163 177 $media_types = array( 164 178 'application', … … 174 188 175 189 public function test_registered_get_item_params() { 176 $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 177 'post_mime_type' => 'image/jpeg', 178 'post_excerpt' => 'A sample caption', 179 ) ); 180 $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/media/%d', $id1 ) ); 181 $response = $this->server->dispatch( $request ); 182 $data = $response->get_data(); 183 $keys = array_keys( $data['endpoints'][0]['args'] ); 190 $id1 = $this->factory->attachment->create_object( 191 $this->test_file, 0, array( 192 'post_mime_type' => 'image/jpeg', 193 'post_excerpt' => 'A sample caption', 194 ) 195 ); 196 $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/media/%d', $id1 ) ); 197 $response = $this->server->dispatch( $request ); 198 $data = $response->get_data(); 199 $keys = array_keys( $data['endpoints'][0]['args'] ); 184 200 sort( $keys ); 185 201 $this->assertEquals( array( 'context', 'id' ), $keys ); … … 188 204 public function test_get_items() { 189 205 wp_set_current_user( 0 ); 190 $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 191 'post_mime_type' => 'image/jpeg', 192 'post_excerpt' => 'A sample caption', 193 ) ); 194 $draft_post = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 195 $id2 = $this->factory->attachment->create_object( $this->test_file, $draft_post, array( 196 'post_mime_type' => 'image/jpeg', 197 'post_excerpt' => 'A sample caption', 198 ) ); 206 $id1 = $this->factory->attachment->create_object( 207 $this->test_file, 0, array( 208 'post_mime_type' => 'image/jpeg', 209 'post_excerpt' => 'A sample caption', 210 ) 211 ); 212 $draft_post = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 213 $id2 = $this->factory->attachment->create_object( 214 $this->test_file, $draft_post, array( 215 'post_mime_type' => 'image/jpeg', 216 'post_excerpt' => 'A sample caption', 217 ) 218 ); 199 219 $published_post = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 200 $id3 = $this->factory->attachment->create_object( $this->test_file, $published_post, array( 201 'post_mime_type' => 'image/jpeg', 202 'post_excerpt' => 'A sample caption', 203 ) ); 204 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 205 $response = $this->server->dispatch( $request ); 206 $data = $response->get_data(); 220 $id3 = $this->factory->attachment->create_object( 221 $this->test_file, $published_post, array( 222 'post_mime_type' => 'image/jpeg', 223 'post_excerpt' => 'A sample caption', 224 ) 225 ); 226 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 227 $response = $this->server->dispatch( $request ); 228 $data = $response->get_data(); 207 229 $this->assertCount( 2, $data ); 208 230 $ids = wp_list_pluck( $data, 'id' ); … … 216 238 public function test_get_items_logged_in_editor() { 217 239 wp_set_current_user( self::$editor_id ); 218 $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 219 'post_mime_type' => 'image/jpeg', 220 'post_excerpt' => 'A sample caption', 221 ) ); 222 $draft_post = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 223 $id2 = $this->factory->attachment->create_object( $this->test_file, $draft_post, array( 224 'post_mime_type' => 'image/jpeg', 225 'post_excerpt' => 'A sample caption', 226 ) ); 240 $id1 = $this->factory->attachment->create_object( 241 $this->test_file, 0, array( 242 'post_mime_type' => 'image/jpeg', 243 'post_excerpt' => 'A sample caption', 244 ) 245 ); 246 $draft_post = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 247 $id2 = $this->factory->attachment->create_object( 248 $this->test_file, $draft_post, array( 249 'post_mime_type' => 'image/jpeg', 250 'post_excerpt' => 'A sample caption', 251 ) 252 ); 227 253 $published_post = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 228 $id3 = $this->factory->attachment->create_object( $this->test_file, $published_post, array( 229 'post_mime_type' => 'image/jpeg', 230 'post_excerpt' => 'A sample caption', 231 ) ); 232 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 233 $response = $this->server->dispatch( $request ); 254 $id3 = $this->factory->attachment->create_object( 255 $this->test_file, $published_post, array( 256 'post_mime_type' => 'image/jpeg', 257 'post_excerpt' => 'A sample caption', 258 ) 259 ); 260 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 261 $response = $this->server->dispatch( $request ); 234 262 235 263 $data = $response->get_data(); … … 242 270 243 271 public function test_get_items_media_type() { 244 $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 245 'post_mime_type' => 'image/jpeg', 246 ) ); 247 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 248 $response = $this->server->dispatch( $request ); 249 $data = $response->get_data(); 272 $id1 = $this->factory->attachment->create_object( 273 $this->test_file, 0, array( 274 'post_mime_type' => 'image/jpeg', 275 ) 276 ); 277 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 278 $response = $this->server->dispatch( $request ); 279 $data = $response->get_data(); 250 280 $this->assertEquals( $id1, $data[0]['id'] ); 251 281 // media_type=video … … 256 286 $request->set_param( 'media_type', 'image' ); 257 287 $response = $this->server->dispatch( $request ); 258 $data = $response->get_data();288 $data = $response->get_data(); 259 289 $this->assertEquals( $id1, $data[0]['id'] ); 260 290 } 261 291 262 292 public function test_get_items_mime_type() { 263 $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 264 'post_mime_type' => 'image/jpeg', 265 ) ); 266 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 267 $response = $this->server->dispatch( $request ); 268 $data = $response->get_data(); 293 $id1 = $this->factory->attachment->create_object( 294 $this->test_file, 0, array( 295 'post_mime_type' => 'image/jpeg', 296 ) 297 ); 298 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 299 $response = $this->server->dispatch( $request ); 300 $data = $response->get_data(); 269 301 $this->assertEquals( $id1, $data[0]['id'] ); 270 302 // mime_type=image/png … … 275 307 $request->set_param( 'mime_type', 'image/jpeg' ); 276 308 $response = $this->server->dispatch( $request ); 277 $data = $response->get_data();309 $data = $response->get_data(); 278 310 $this->assertEquals( $id1, $data[0]['id'] ); 279 311 } 280 312 281 313 public function test_get_items_parent() { 282 $post_id = $this->factory->post->create( array( 'post_title' => 'Test Post' ) ); 283 $attachment_id = $this->factory->attachment->create_object( $this->test_file, $post_id, array( 284 'post_mime_type' => 'image/jpeg', 285 'post_excerpt' => 'A sample caption', 286 ) ); 287 $attachment_id2 = $this->factory->attachment->create_object( $this->test_file, 0, array( 288 'post_mime_type' => 'image/jpeg', 289 'post_excerpt' => 'A sample caption', 290 ) ); 314 $post_id = $this->factory->post->create( array( 'post_title' => 'Test Post' ) ); 315 $attachment_id = $this->factory->attachment->create_object( 316 $this->test_file, $post_id, array( 317 'post_mime_type' => 'image/jpeg', 318 'post_excerpt' => 'A sample caption', 319 ) 320 ); 321 $attachment_id2 = $this->factory->attachment->create_object( 322 $this->test_file, 0, array( 323 'post_mime_type' => 'image/jpeg', 324 'post_excerpt' => 'A sample caption', 325 ) 326 ); 291 327 // all attachments 292 $request = new WP_REST_Request( 'GET', '/wp/v2/media' );328 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 293 329 $response = $this->server->dispatch( $request ); 294 330 $this->assertEquals( 2, count( $response->get_data() ) ); … … 297 333 $request->set_param( 'parent', 0 ); 298 334 $response = $this->server->dispatch( $request ); 299 $data = $response->get_data();335 $data = $response->get_data(); 300 336 $this->assertEquals( 1, count( $data ) ); 301 337 $this->assertEquals( $attachment_id2, $data[0]['id'] ); … … 304 340 $request->set_param( 'parent', $post_id ); 305 341 $response = $this->server->dispatch( $request ); 306 $data = $response->get_data();342 $data = $response->get_data(); 307 343 $this->assertEquals( 1, count( $data ) ); 308 344 $this->assertEquals( $attachment_id, $data[0]['id'] ); … … 311 347 $request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 312 348 $response = $this->server->dispatch( $request ); 313 $data = $response->get_data();349 $data = $response->get_data(); 314 350 $this->assertEquals( 0, count( $data ) ); 315 351 } … … 317 353 public function test_get_items_invalid_status_param_is_error_response() { 318 354 wp_set_current_user( self::$editor_id ); 319 $this->factory->attachment->create_object( $this->test_file, 0, array( 320 'post_mime_type' => 'image/jpeg', 321 'post_excerpt' => 'A sample caption', 322 ) ); 355 $this->factory->attachment->create_object( 356 $this->test_file, 0, array( 357 'post_mime_type' => 'image/jpeg', 358 'post_excerpt' => 'A sample caption', 359 ) 360 ); 323 361 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 324 362 $request->set_param( 'status', 'publish' ); 325 363 $request->set_param( 'context', 'edit' ); 326 364 $response = $this->server->dispatch( $request ); 327 $data = $response->get_data();365 $data = $response->get_data(); 328 366 $this->assertCount( 3, $data ); 329 367 $this->assertEquals( 'rest_invalid_param', $data['code'] ); … … 333 371 // Logged out users can't make the request 334 372 wp_set_current_user( 0 ); 335 $attachment_id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 336 'post_mime_type' => 'image/jpeg', 337 'post_excerpt' => 'A sample caption', 338 'post_status' => 'private', 339 ) ); 340 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 373 $attachment_id1 = $this->factory->attachment->create_object( 374 $this->test_file, 0, array( 375 'post_mime_type' => 'image/jpeg', 376 'post_excerpt' => 'A sample caption', 377 'post_status' => 'private', 378 ) 379 ); 380 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 341 381 $request->set_param( 'status', 'private' ); 342 382 $response = $this->server->dispatch( $request ); … … 353 393 // Logged out users can't make the request 354 394 wp_set_current_user( 0 ); 355 $attachment_id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 356 'post_mime_type' => 'image/jpeg', 357 'post_excerpt' => 'A sample caption', 358 'post_status' => 'private', 359 ) ); 360 $attachment_id2 = $this->factory->attachment->create_object( $this->test_file, 0, array( 361 'post_mime_type' => 'image/jpeg', 362 'post_excerpt' => 'A sample caption', 363 'post_status' => 'trash', 364 ) ); 365 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 395 $attachment_id1 = $this->factory->attachment->create_object( 396 $this->test_file, 0, array( 397 'post_mime_type' => 'image/jpeg', 398 'post_excerpt' => 'A sample caption', 399 'post_status' => 'private', 400 ) 401 ); 402 $attachment_id2 = $this->factory->attachment->create_object( 403 $this->test_file, 0, array( 404 'post_mime_type' => 'image/jpeg', 405 'post_excerpt' => 'A sample caption', 406 'post_status' => 'trash', 407 ) 408 ); 409 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 366 410 $request->set_param( 'status', array( 'private', 'trash' ) ); 367 411 $response = $this->server->dispatch( $request ); … … 390 434 391 435 public function test_get_items_valid_date() { 392 $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 393 'post_date' => '2016-01-15T00:00:00Z', 394 'post_mime_type' => 'image/jpeg', 395 'post_excerpt' => 'A sample caption', 396 ) ); 397 $id2 = $this->factory->attachment->create_object( $this->test_file, 0, array( 398 'post_date' => '2016-01-16T00:00:00Z', 399 'post_mime_type' => 'image/jpeg', 400 'post_excerpt' => 'A sample caption', 401 ) ); 402 $id3 = $this->factory->attachment->create_object( $this->test_file, 0, array( 403 'post_date' => '2016-01-17T00:00:00Z', 404 'post_mime_type' => 'image/jpeg', 405 'post_excerpt' => 'A sample caption', 406 ) ); 436 $id1 = $this->factory->attachment->create_object( 437 $this->test_file, 0, array( 438 'post_date' => '2016-01-15T00:00:00Z', 439 'post_mime_type' => 'image/jpeg', 440 'post_excerpt' => 'A sample caption', 441 ) 442 ); 443 $id2 = $this->factory->attachment->create_object( 444 $this->test_file, 0, array( 445 'post_date' => '2016-01-16T00:00:00Z', 446 'post_mime_type' => 'image/jpeg', 447 'post_excerpt' => 'A sample caption', 448 ) 449 ); 450 $id3 = $this->factory->attachment->create_object( 451 $this->test_file, 0, array( 452 'post_date' => '2016-01-17T00:00:00Z', 453 'post_mime_type' => 'image/jpeg', 454 'post_excerpt' => 'A sample caption', 455 ) 456 ); 407 457 $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); 408 458 $request->set_param( 'after', '2016-01-15T00:00:00Z' ); 409 459 $request->set_param( 'before', '2016-01-17T00:00:00Z' ); 410 460 $response = $this->server->dispatch( $request ); 411 $data = $response->get_data();461 $data = $response->get_data(); 412 462 $this->assertCount( 1, $data ); 413 463 $this->assertEquals( $id2, $data[0]['id'] ); … … 415 465 416 466 public function test_get_item() { 417 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 418 'post_mime_type' => 'image/jpeg', 419 'post_excerpt' => 'A sample caption', 420 ) ); 467 $attachment_id = $this->factory->attachment->create_object( 468 $this->test_file, 0, array( 469 'post_mime_type' => 'image/jpeg', 470 'post_excerpt' => 'A sample caption', 471 ) 472 ); 421 473 update_post_meta( $attachment_id, '_wp_attachment_image_alt', 'Sample alt text' ); 422 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );474 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id ); 423 475 $response = $this->server->dispatch( $request ); 424 476 $this->check_get_post_response( $response ); … … 428 480 429 481 public function test_get_item_sizes() { 430 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 431 'post_mime_type' => 'image/jpeg', 432 'post_excerpt' => 'A sample caption', 433 ), $this->test_file ); 482 $attachment_id = $this->factory->attachment->create_object( 483 $this->test_file, 0, array( 484 'post_mime_type' => 'image/jpeg', 485 'post_excerpt' => 'A sample caption', 486 ), $this->test_file 487 ); 434 488 435 489 add_image_size( 'rest-api-test', 119, 119, true ); 436 490 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $this->test_file ) ); 437 491 438 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );439 $response = $this->server->dispatch( $request );440 $data = $response->get_data();441 $image_src = wp_get_attachment_image_src( $attachment_id, 'rest-api-test' );492 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id ); 493 $response = $this->server->dispatch( $request ); 494 $data = $response->get_data(); 495 $image_src = wp_get_attachment_image_src( $attachment_id, 'rest-api-test' ); 442 496 $original_image_src = wp_get_attachment_image_src( $attachment_id, 'full' ); 443 497 remove_image_size( 'rest-api-test' ); … … 450 504 451 505 public function test_get_item_sizes_with_no_url() { 452 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 453 'post_mime_type' => 'image/jpeg', 454 'post_excerpt' => 'A sample caption', 455 ), $this->test_file ); 506 $attachment_id = $this->factory->attachment->create_object( 507 $this->test_file, 0, array( 508 'post_mime_type' => 'image/jpeg', 509 'post_excerpt' => 'A sample caption', 510 ), $this->test_file 511 ); 456 512 457 513 add_image_size( 'rest-api-test', 119, 119, true ); … … 460 516 add_filter( 'wp_get_attachment_image_src', '__return_false' ); 461 517 462 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );463 $response = $this->server->dispatch( $request ); 464 $data = $response->get_data();518 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id ); 519 $response = $this->server->dispatch( $request ); 520 $data = $response->get_data(); 465 521 remove_filter( 'wp_get_attachment_image_src', '__return_false' ); 466 522 remove_image_size( 'rest-api-test' ); … … 472 528 wp_set_current_user( 0 ); 473 529 $draft_post = $this->factory->post->create( array( 'post_status' => 'draft' ) ); 474 $id1 = $this->factory->attachment->create_object( $this->test_file, $draft_post, array( 475 'post_mime_type' => 'image/jpeg', 476 'post_excerpt' => 'A sample caption', 477 ) ); 478 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $id1 ); 479 $response = $this->server->dispatch( $request ); 530 $id1 = $this->factory->attachment->create_object( 531 $this->test_file, $draft_post, array( 532 'post_mime_type' => 'image/jpeg', 533 'post_excerpt' => 'A sample caption', 534 ) 535 ); 536 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $id1 ); 537 $response = $this->server->dispatch( $request ); 480 538 $this->assertEquals( 403, $response->get_status() ); 481 539 } 482 540 483 541 public function test_get_item_inherit_status_with_invalid_parent() { 484 $attachment_id = $this->factory->attachment->create_object( $this->test_file, REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, array( 485 'post_mime_type' => 'image/jpeg', 486 'post_excerpt' => 'A sample caption', 487 ) ); 488 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) ); 489 $response = $this->server->dispatch( $request ); 490 $data = $response->get_data(); 542 $attachment_id = $this->factory->attachment->create_object( 543 $this->test_file, REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, array( 544 'post_mime_type' => 'image/jpeg', 545 'post_excerpt' => 'A sample caption', 546 ) 547 ); 548 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) ); 549 $response = $this->server->dispatch( $request ); 550 $data = $response->get_data(); 491 551 492 552 $this->assertEquals( 200, $response->get_status() ); … … 495 555 496 556 public function test_get_item_auto_status_with_invalid_parent_returns_error() { 497 $attachment_id = $this->factory->attachment->create_object( $this->test_file, REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, array( 498 'post_mime_type' => 'image/jpeg', 499 'post_excerpt' => 'A sample caption', 500 'post_status' => 'auto-draft', 501 ) ); 502 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) ); 503 $response = $this->server->dispatch( $request ); 557 $attachment_id = $this->factory->attachment->create_object( 558 $this->test_file, REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, array( 559 'post_mime_type' => 'image/jpeg', 560 'post_excerpt' => 'A sample caption', 561 'post_status' => 'auto-draft', 562 ) 563 ); 564 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) ); 565 $response = $this->server->dispatch( $request ); 504 566 505 567 $this->assertErrorResponse( 'rest_forbidden', $response, 403 ); … … 519 581 $request->set_body( file_get_contents( $this->test_file ) ); 520 582 $response = $this->server->dispatch( $request ); 521 $data = $response->get_data();583 $data = $response->get_data(); 522 584 523 585 $this->assertEquals( 201, $response->get_status() ); … … 538 600 wp_set_current_user( self::$author_id ); 539 601 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 540 $request->set_file_params( array( 541 'file' => array( 542 'file' => file_get_contents( $this->test_file2 ), 543 'name' => 'codeispoetry.png', 544 'size' => filesize( $this->test_file2 ), 545 'tmp_name' => $this->test_file2, 546 ), 547 ) ); 602 $request->set_file_params( 603 array( 604 'file' => array( 605 'file' => file_get_contents( $this->test_file2 ), 606 'name' => 'codeispoetry.png', 607 'size' => filesize( $this->test_file2 ), 608 'tmp_name' => $this->test_file2, 609 ), 610 ) 611 ); 548 612 $request->set_header( 'Content-MD5', md5_file( $this->test_file2 ) ); 549 613 $response = $this->server->dispatch( $request ); … … 556 620 wp_set_current_user( self::$author_id ); 557 621 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 558 $request->set_file_params( array( 559 'file' => array( 560 'file' => file_get_contents( $this->test_file ), 561 'name' => 'canola.jpg', 562 'size' => filesize( $this->test_file ), 563 'tmp_name' => $this->test_file, 564 ), 565 ) ); 622 $request->set_file_params( 623 array( 624 'file' => array( 625 'file' => file_get_contents( $this->test_file ), 626 'name' => 'canola.jpg', 627 'size' => filesize( $this->test_file ), 628 'tmp_name' => $this->test_file, 629 ), 630 ) 631 ); 566 632 $request->set_header( 'Content-MD5', md5_file( $this->test_file ) ); 567 633 $response = $this->server->dispatch( $request ); … … 572 638 wp_set_current_user( self::$uploader_id ); 573 639 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 574 $request->set_file_params( array( 575 'file' => array( 576 'file' => file_get_contents( $this->test_file ), 577 'name' => 'canola.jpg', 578 'size' => filesize( $this->test_file ), 579 'tmp_name' => $this->test_file, 580 ), 581 ) ); 640 $request->set_file_params( 641 array( 642 'file' => array( 643 'file' => file_get_contents( $this->test_file ), 644 'name' => 'canola.jpg', 645 'size' => filesize( $this->test_file ), 646 'tmp_name' => $this->test_file, 647 ), 648 ) 649 ); 582 650 $request->set_header( 'Content-MD5', md5_file( $this->test_file ) ); 583 651 $response = $this->server->dispatch( $request ); … … 587 655 public function test_create_item_empty_body() { 588 656 wp_set_current_user( self::$author_id ); 589 $request = new WP_REST_Request( 'POST', '/wp/v2/media' );657 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 590 658 $response = $this->server->dispatch( $request ); 591 659 $this->assertErrorResponse( 'rest_upload_no_data', $response, 400 ); … … 623 691 wp_set_current_user( self::$author_id ); 624 692 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 625 $request->set_file_params( array( 626 'file' => array( 627 'file' => file_get_contents( $this->test_file ), 628 'name' => 'canola.jpg', 629 'size' => filesize( $this->test_file ), 630 'tmp_name' => $this->test_file, 631 ), 632 ) ); 693 $request->set_file_params( 694 array( 695 'file' => array( 696 'file' => file_get_contents( $this->test_file ), 697 'name' => 'canola.jpg', 698 'size' => filesize( $this->test_file ), 699 'tmp_name' => $this->test_file, 700 ), 701 ) 702 ); 633 703 $request->set_header( 'Content-MD5', 'abc123' ); 634 704 $response = $this->server->dispatch( $request ); … … 638 708 public function test_create_item_invalid_upload_files_capability() { 639 709 wp_set_current_user( self::$contributor_id ); 640 $request = new WP_REST_Request( 'POST', '/wp/v2/media' );710 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 641 711 $response = $this->server->dispatch( $request ); 642 712 $this->assertErrorResponse( 'rest_cannot_create', $response, 403 ); … … 662 732 663 733 public function test_create_item_invalid_post_type() { 664 $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) ); 734 $attachment_id = $this->factory->post->create( 735 array( 736 'post_type' => 'attachment', 737 'post_status' => 'inherit', 738 'post_parent' => 0, 739 ) 740 ); 665 741 wp_set_current_user( self::$editor_id ); 666 742 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); … … 681 757 $request->set_body( file_get_contents( $this->test_file ) ); 682 758 $request->set_param( 'alt_text', 'test alt text' ); 683 $response = $this->server->dispatch( $request );759 $response = $this->server->dispatch( $request ); 684 760 $attachment = $response->get_data(); 685 761 $this->assertEquals( 'test alt text', $attachment['alt_text'] ); … … 693 769 $request->set_body( file_get_contents( $this->test_file ) ); 694 770 $request->set_param( 'alt_text', '<script>alert(document.cookie)</script>' ); 695 $response = $this->server->dispatch( $request );771 $response = $this->server->dispatch( $request ); 696 772 $attachment = $response->get_data(); 697 773 $this->assertEquals( '', $attachment['alt_text'] ); … … 700 776 public function test_update_item() { 701 777 wp_set_current_user( self::$editor_id ); 702 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 703 'post_mime_type' => 'image/jpeg', 704 'post_excerpt' => 'A sample caption', 705 'post_author' => self::$editor_id, 706 ) ); 707 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 778 $attachment_id = $this->factory->attachment->create_object( 779 $this->test_file, 0, array( 780 'post_mime_type' => 'image/jpeg', 781 'post_excerpt' => 'A sample caption', 782 'post_author' => self::$editor_id, 783 ) 784 ); 785 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 708 786 $request->set_param( 'title', 'My title is very cool' ); 709 787 $request->set_param( 'caption', 'This is a better caption.' ); 710 788 $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' ); 711 789 $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' ); 712 $response = $this->server->dispatch( $request );713 $data = $response->get_data();790 $response = $this->server->dispatch( $request ); 791 $data = $response->get_data(); 714 792 $attachment = get_post( $data['id'] ); 715 793 $this->assertEquals( 'My title is very cool', $data['title']['raw'] ); … … 726 804 wp_set_current_user( self::$editor_id ); 727 805 $original_parent = $this->factory->post->create( array() ); 728 $attachment_id = $this->factory->attachment->create_object( $this->test_file, $original_parent, array( 729 'post_mime_type' => 'image/jpeg', 730 'post_excerpt' => 'A sample caption', 731 'post_author' => $this->editor_id, 732 ) ); 806 $attachment_id = $this->factory->attachment->create_object( 807 $this->test_file, $original_parent, array( 808 'post_mime_type' => 'image/jpeg', 809 'post_excerpt' => 'A sample caption', 810 'post_author' => $this->editor_id, 811 ) 812 ); 733 813 734 814 $attachment = get_post( $attachment_id ); … … 736 816 737 817 $new_parent = $this->factory->post->create( array() ); 738 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );818 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 739 819 $request->set_param( 'post', $new_parent ); 740 820 $this->server->dispatch( $request ); … … 746 826 public function test_update_item_invalid_permissions() { 747 827 wp_set_current_user( self::$author_id ); 748 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 749 'post_mime_type' => 'image/jpeg', 750 'post_excerpt' => 'A sample caption', 751 'post_author' => self::$editor_id, 752 ) ); 753 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 828 $attachment_id = $this->factory->attachment->create_object( 829 $this->test_file, 0, array( 830 'post_mime_type' => 'image/jpeg', 831 'post_excerpt' => 'A sample caption', 832 'post_author' => self::$editor_id, 833 ) 834 ); 835 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 754 836 $request->set_param( 'caption', 'This is a better caption.' ); 755 837 $response = $this->server->dispatch( $request ); … … 758 840 759 841 public function test_update_item_invalid_post_type() { 760 $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) ); 842 $attachment_id = $this->factory->post->create( 843 array( 844 'post_type' => 'attachment', 845 'post_status' => 'inherit', 846 'post_parent' => 0, 847 ) 848 ); 761 849 wp_set_current_user( self::$editor_id ); 762 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 763 'post_mime_type' => 'image/jpeg', 764 'post_excerpt' => 'A sample caption', 765 'post_author' => self::$editor_id, 766 ) ); 767 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 850 $attachment_id = $this->factory->attachment->create_object( 851 $this->test_file, 0, array( 852 'post_mime_type' => 'image/jpeg', 853 'post_excerpt' => 'A sample caption', 854 'post_author' => self::$editor_id, 855 ) 856 ); 857 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); 768 858 $request->set_param( 'post', $attachment_id ); 769 859 $response = $this->server->dispatch( $request ); … … 790 880 $content = explode( "\n", trim( $content ) ); 791 881 if ( preg_match( '/^<p class="attachment">/', $content[0] ) ) { 792 $content = implode( "\n", array_slice( $content, 1 ) );882 $content = implode( "\n", array_slice( $content, 1 ) ); 793 883 $actual_output['description']['rendered'] = $content; 794 884 } 795 885 796 886 // Compare expected API output to actual API output 797 $this->assertEquals( $expected_output['title']['raw'] 798 $this->assertEquals( $expected_output['title']['rendered'] 799 $this->assertEquals( $expected_output['description']['raw'] 887 $this->assertEquals( $expected_output['title']['raw'], $actual_output['title']['raw'] ); 888 $this->assertEquals( $expected_output['title']['rendered'], trim( $actual_output['title']['rendered'] ) ); 889 $this->assertEquals( $expected_output['description']['raw'], $actual_output['description']['raw'] ); 800 890 $this->assertEquals( $expected_output['description']['rendered'], trim( $actual_output['description']['rendered'] ) ); 801 $this->assertEquals( $expected_output['caption']['raw'] 802 $this->assertEquals( $expected_output['caption']['rendered'] 891 $this->assertEquals( $expected_output['caption']['raw'], $actual_output['caption']['raw'] ); 892 $this->assertEquals( $expected_output['caption']['rendered'], trim( $actual_output['caption']['rendered'] ) ); 803 893 804 894 // Compare expected API output to WP internal values … … 822 912 $content = explode( "\n", trim( $content ) ); 823 913 if ( preg_match( '/^<p class="attachment">/', $content[0] ) ) { 824 $content = implode( "\n", array_slice( $content, 1 ) );914 $content = implode( "\n", array_slice( $content, 1 ) ); 825 915 $actual_output['description']['rendered'] = $content; 826 916 } 827 917 828 918 // Compare expected API output to actual API output 829 $this->assertEquals( $expected_output['title']['raw'] 830 $this->assertEquals( $expected_output['title']['rendered'] 831 $this->assertEquals( $expected_output['description']['raw'] 919 $this->assertEquals( $expected_output['title']['raw'], $actual_output['title']['raw'] ); 920 $this->assertEquals( $expected_output['title']['rendered'], trim( $actual_output['title']['rendered'] ) ); 921 $this->assertEquals( $expected_output['description']['raw'], $actual_output['description']['raw'] ); 832 922 $this->assertEquals( $expected_output['description']['rendered'], trim( $actual_output['description']['rendered'] ) ); 833 $this->assertEquals( $expected_output['caption']['raw'] 834 $this->assertEquals( $expected_output['caption']['rendered'] 923 $this->assertEquals( $expected_output['caption']['raw'], $actual_output['caption']['raw'] ); 924 $this->assertEquals( $expected_output['caption']['rendered'], trim( $actual_output['caption']['rendered'] ) ); 835 925 836 926 // Compare expected API output to WP internal values 837 927 $post = get_post( $actual_output['id'] ); 838 $this->assertEquals( $expected_output['title']['raw'] 928 $this->assertEquals( $expected_output['title']['raw'], $post->post_title ); 839 929 $this->assertEquals( $expected_output['description']['raw'], $post->post_content ); 840 930 $this->assertEquals( $expected_output['caption']['raw'], $post->post_excerpt ); … … 846 936 // Raw values. 847 937 array( 848 'title' => '\o/ ¯\_(ツ)_/¯',938 'title' => '\o/ ¯\_(ツ)_/¯', 849 939 'description' => '\o/ ¯\_(ツ)_/¯', 850 'caption' => '\o/ ¯\_(ツ)_/¯',940 'caption' => '\o/ ¯\_(ツ)_/¯', 851 941 ), 852 942 // Expected returned values. 853 943 array( 854 'title' => array(944 'title' => array( 855 945 'raw' => '\o/ ¯\_(ツ)_/¯', 856 946 'rendered' => '\o/ ¯\_(ツ)_/¯', … … 860 950 'rendered' => '<p>\o/ ¯\_(ツ)_/¯</p>', 861 951 ), 862 'caption' => array(952 'caption' => array( 863 953 'raw' => '\o/ ¯\_(ツ)_/¯', 864 954 'rendered' => '<p>\o/ ¯\_(ツ)_/¯</p>', 865 955 ), 866 ) 956 ), 867 957 ), 868 958 array( 869 959 // Raw values. 870 960 array( 871 'title' => '\\\&\\\ & &invalid; < < &lt;',961 'title' => '\\\&\\\ & &invalid; < < &lt;', 872 962 'description' => '\\\&\\\ & &invalid; < < &lt;', 873 'caption' => '\\\&\\\ & &invalid; < < &lt;',963 'caption' => '\\\&\\\ & &invalid; < < &lt;', 874 964 ), 875 965 // Expected returned values. 876 966 array( 877 'title' => array(967 'title' => array( 878 968 'raw' => '\\\&\\\ & &invalid; < < &lt;', 879 969 'rendered' => '\\\&\\\ & &invalid; < < &lt;', … … 883 973 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>', 884 974 ), 885 'caption' => array(975 'caption' => array( 886 976 'raw' => '\\\&\\\ & &invalid; < < &lt;', 887 977 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>', … … 892 982 // Raw values. 893 983 array( 894 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',984 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 895 985 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 896 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',986 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 897 987 ), 898 988 // Expected returned values. 899 989 array( 900 'title' => array(990 'title' => array( 901 991 'raw' => 'div <strong>strong</strong> oh noes', 902 992 'rendered' => 'div <strong>strong</strong> oh noes', … … 906 996 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 907 997 ), 908 'caption' => array(998 'caption' => array( 909 999 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 910 1000 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 911 1001 ), 912 ) 1002 ), 913 1003 ), 914 1004 array( 915 1005 // Raw values. 916 1006 array( 917 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',1007 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', 918 1008 'description' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', 919 'caption' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',1009 'caption' => '<a href="#" target="_blank" data-unfiltered=true>link</a>', 920 1010 ), 921 1011 // Expected returned values. 922 1012 array( 923 'title' => array(1013 'title' => array( 924 1014 'raw' => '<a href="#">link</a>', 925 1015 'rendered' => '<a href="#">link</a>', … … 929 1019 'rendered' => '<p><a href="#" target="_blank">link</a></p>', 930 1020 ), 931 'caption' => array(1021 'caption' => array( 932 1022 'raw' => '<a href="#" target="_blank">link</a>', 933 1023 'rendered' => '<p><a href="#" target="_blank">link</a></p>', 934 1024 ), 935 ) 1025 ), 936 1026 ), 937 1027 ); … … 951 1041 if ( is_multisite() ) { 952 1042 $this->assertFalse( current_user_can( 'unfiltered_html' ) ); 953 $this->verify_attachment_roundtrip( array( 1043 $this->verify_attachment_roundtrip( 1044 array( 1045 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1046 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1047 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1048 ), array( 1049 'title' => array( 1050 'raw' => 'div <strong>strong</strong> oh noes', 1051 'rendered' => 'div <strong>strong</strong> oh noes', 1052 ), 1053 'description' => array( 1054 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 1055 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 1056 ), 1057 'caption' => array( 1058 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 1059 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 1060 ), 1061 ) 1062 ); 1063 } else { 1064 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 1065 $this->verify_attachment_roundtrip( 1066 array( 1067 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1068 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1069 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1070 ), array( 1071 'title' => array( 1072 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1073 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1074 ), 1075 'description' => array( 1076 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1077 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 1078 ), 1079 'caption' => array( 1080 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1081 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 1082 ), 1083 ) 1084 ); 1085 } 1086 } 1087 1088 public function test_attachment_roundtrip_as_superadmin_unfiltered_html() { 1089 wp_set_current_user( self::$superadmin_id ); 1090 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 1091 $this->verify_attachment_roundtrip( 1092 array( 954 1093 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 955 1094 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 956 1095 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 957 1096 ), array( 958 'title' => array( 959 'raw' => 'div <strong>strong</strong> oh noes', 960 'rendered' => 'div <strong>strong</strong> oh noes', 961 ), 962 'description' => array( 963 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 964 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 965 ), 966 'caption' => array( 967 'raw' => '<div>div</div> <strong>strong</strong> oh noes', 968 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>", 969 ), 970 ) ); 971 } else { 972 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 973 $this->verify_attachment_roundtrip( array( 974 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 975 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 976 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 977 ), array( 978 'title' => array( 1097 'title' => array( 979 1098 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 980 1099 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', … … 984 1103 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 985 1104 ), 986 'caption' => array(1105 'caption' => array( 987 1106 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 988 1107 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 989 1108 ), 990 ) ); 991 } 992 } 993 994 public function test_attachment_roundtrip_as_superadmin_unfiltered_html() { 995 wp_set_current_user( self::$superadmin_id ); 996 $this->assertTrue( current_user_can( 'unfiltered_html' ) ); 997 $this->verify_attachment_roundtrip( array( 998 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 999 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1000 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1001 ), array( 1002 'title' => array( 1003 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1004 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1005 ), 1006 'description' => array( 1007 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1008 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 1009 ), 1010 'caption' => array( 1011 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', 1012 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>", 1013 ), 1014 ) ); 1109 ) 1110 ); 1015 1111 } 1016 1112 1017 1113 public function test_delete_item() { 1018 1114 wp_set_current_user( self::$editor_id ); 1019 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1020 'post_mime_type' => 'image/jpeg', 1021 'post_excerpt' => 'A sample caption', 1022 ) ); 1023 $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); 1115 $attachment_id = $this->factory->attachment->create_object( 1116 $this->test_file, 0, array( 1117 'post_mime_type' => 'image/jpeg', 1118 'post_excerpt' => 'A sample caption', 1119 ) 1120 ); 1121 $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); 1024 1122 $request['force'] = true; 1025 $response = $this->server->dispatch( $request );1123 $response = $this->server->dispatch( $request ); 1026 1124 $this->assertEquals( 200, $response->get_status() ); 1027 1125 } … … 1029 1127 public function test_delete_item_no_trash() { 1030 1128 wp_set_current_user( self::$editor_id ); 1031 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1032 'post_mime_type' => 'image/jpeg', 1033 'post_excerpt' => 'A sample caption', 1034 ) ); 1129 $attachment_id = $this->factory->attachment->create_object( 1130 $this->test_file, 0, array( 1131 'post_mime_type' => 'image/jpeg', 1132 'post_excerpt' => 'A sample caption', 1133 ) 1134 ); 1035 1135 1036 1136 // Attempt trashing 1037 $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id );1137 $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); 1038 1138 $response = $this->server->dispatch( $request ); 1039 1139 $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); … … 1050 1150 public function test_delete_item_invalid_delete_permissions() { 1051 1151 wp_set_current_user( self::$author_id ); 1052 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1053 'post_mime_type' => 'image/jpeg', 1054 'post_excerpt' => 'A sample caption', 1055 'post_author' => self::$editor_id, 1056 ) ); 1057 $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); 1058 $response = $this->server->dispatch( $request ); 1152 $attachment_id = $this->factory->attachment->create_object( 1153 $this->test_file, 0, array( 1154 'post_mime_type' => 'image/jpeg', 1155 'post_excerpt' => 'A sample caption', 1156 'post_author' => self::$editor_id, 1157 ) 1158 ); 1159 $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); 1160 $response = $this->server->dispatch( $request ); 1059 1161 $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); 1060 1162 } 1061 1163 1062 1164 public function test_prepare_item() { 1063 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1064 'post_mime_type' => 'image/jpeg', 1065 'post_excerpt' => 'A sample caption', 1066 'post_author' => self::$editor_id, 1067 ) ); 1165 $attachment_id = $this->factory->attachment->create_object( 1166 $this->test_file, 0, array( 1167 'post_mime_type' => 'image/jpeg', 1168 'post_excerpt' => 'A sample caption', 1169 'post_author' => self::$editor_id, 1170 ) 1171 ); 1068 1172 1069 1173 $attachment = get_post( $attachment_id ); 1070 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) );1071 $response = $this->server->dispatch( $request );1072 $data = $response->get_data();1174 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) ); 1175 $response = $this->server->dispatch( $request ); 1176 $data = $response->get_data(); 1073 1177 $this->check_post_data( $attachment, $data, 'view', $response->get_links() ); 1074 1178 $this->check_post_data( $attachment, $data, 'embed', $response->get_links() ); … … 1076 1180 1077 1181 public function test_get_item_schema() { 1078 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );1079 $response = $this->server->dispatch( $request );1080 $data = $response->get_data();1182 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ); 1183 $response = $this->server->dispatch( $request ); 1184 $data = $response->get_data(); 1081 1185 $properties = $data['schema']['properties']; 1082 1186 $this->assertEquals( 24, count( $properties ) ); … … 1122 1226 ); 1123 1227 1124 register_rest_field( 'attachment', 'my_custom_int', array( 1125 'schema' => $schema, 1126 'get_callback' => array( $this, 'additional_field_get_callback' ), 1127 ) ); 1228 register_rest_field( 1229 'attachment', 'my_custom_int', array( 1230 'schema' => $schema, 1231 'get_callback' => array( $this, 'additional_field_get_callback' ), 1232 ) 1233 ); 1128 1234 1129 1235 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' ); 1130 1236 1131 1237 $response = $this->server->dispatch( $request ); 1132 $data = $response->get_data();1238 $data = $response->get_data(); 1133 1239 $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] ); 1134 1240 $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] ); 1135 1241 1136 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1137 'post_mime_type' => 'image/jpeg', 1138 'post_excerpt' => 'A sample caption', 1139 ) ); 1242 $attachment_id = $this->factory->attachment->create_object( 1243 $this->test_file, 0, array( 1244 'post_mime_type' => 'image/jpeg', 1245 'post_excerpt' => 'A sample caption', 1246 ) 1247 ); 1140 1248 1141 1249 $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id ); … … 1156 1264 ); 1157 1265 1158 register_rest_field( 'attachment', 'my_custom_int', array( 1159 'schema' => $schema, 1160 'get_callback' => array( $this, 'additional_field_get_callback' ), 1161 'update_callback' => array( $this, 'additional_field_update_callback' ), 1162 ) ); 1266 register_rest_field( 1267 'attachment', 'my_custom_int', array( 1268 'schema' => $schema, 1269 'get_callback' => array( $this, 'additional_field_get_callback' ), 1270 'update_callback' => array( $this, 'additional_field_update_callback' ), 1271 ) 1272 ); 1163 1273 1164 1274 wp_set_current_user( self::$editor_id ); 1165 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1166 'post_mime_type' => 'image/jpeg', 1167 'post_excerpt' => 'A sample caption', 1168 'post_author' => self::$editor_id, 1169 ) ); 1275 $attachment_id = $this->factory->attachment->create_object( 1276 $this->test_file, 0, array( 1277 'post_mime_type' => 'image/jpeg', 1278 'post_excerpt' => 'A sample caption', 1279 'post_author' => self::$editor_id, 1280 ) 1281 ); 1170 1282 // Check for error on update. 1171 1283 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/media/%d', $attachment_id ) ); 1172 $request->set_body_params(array( 1173 'my_custom_int' => 'returnError', 1174 )); 1284 $request->set_body_params( 1285 array( 1286 'my_custom_int' => 'returnError', 1287 ) 1288 ); 1175 1289 1176 1290 $response = $this->server->dispatch( $request ); … … 1183 1297 1184 1298 public function test_search_item_by_filename() { 1185 $id = $this->factory->attachment->create_object( $this->test_file, 0, array( 1186 'post_mime_type' => 'image/jpeg', 1187 ) ); 1188 $id2 = $this->factory->attachment->create_object( $this->test_file2, 0, array( 1189 'post_mime_type' => 'image/png', 1190 ) ); 1299 $id = $this->factory->attachment->create_object( 1300 $this->test_file, 0, array( 1301 'post_mime_type' => 'image/jpeg', 1302 ) 1303 ); 1304 $id2 = $this->factory->attachment->create_object( 1305 $this->test_file2, 0, array( 1306 'post_mime_type' => 'image/png', 1307 ) 1308 ); 1191 1309 1192 1310 $filename = basename( $this->test_file2 ); … … 1195 1313 $request->set_param( 'search', $filename ); 1196 1314 $response = $this->server->dispatch( $request ); 1197 $data = $response->get_data();1315 $data = $response->get_data(); 1198 1316 1199 1317 $this->assertCount( 1, $data );
Note: See TracChangeset
for help on using the changeset viewer.