- Timestamp:
- 10/27/2016 02:56:28 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r38968 r38975 11 11 */ 12 12 class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Controller_Testcase { 13 protected static $editor_id; 14 protected static $author_id; 15 protected static $contributor_id; 16 protected static $uploader_id; 17 18 public static function wpSetUpBeforeClass( $factory ) { 19 self::$editor_id = $factory->user->create( array( 20 'role' => 'editor', 21 ) ); 22 self::$author_id = $factory->user->create( array( 23 'role' => 'author', 24 ) ); 25 self::$contributor_id = $factory->user->create( array( 26 'role' => 'contributor', 27 ) ); 28 self::$uploader_id = $factory->user->create( array( 29 'role' => 'uploader', 30 ) ); 31 } 32 33 public static function wpTearDownAfterClass() { 34 self::delete_user( self::$editor_id ); 35 self::delete_user( self::$author_id ); 36 self::delete_user( self::$contributor_id ); 37 self::delete_user( self::$uploader_id ); 38 } 13 39 14 40 public function setUp() { 15 41 parent::setUp(); 16 17 $this->editor_id = $this->factory->user->create( array(18 'role' => 'editor',19 ) );20 $this->author_id = $this->factory->user->create( array(21 'role' => 'author',22 ) );23 $this->contributor_id = $this->factory->user->create( array(24 'role' => 'contributor',25 ) );26 42 27 43 // Add an uploader role to test upload capabilities. … … 31 47 $role->add_cap( 'read' ); 32 48 $role->add_cap( 'level_0' ); 33 $this->uploader_id = $this->factory->user->create( array(34 'role' => 'uploader',35 ) );36 49 37 50 $orig_file = DIR_TESTDATA . '/images/canola.jpg'; … … 180 193 181 194 public function test_get_items_logged_in_editor() { 182 wp_set_current_user( $this->editor_id );195 wp_set_current_user( self::$editor_id ); 183 196 $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array( 184 197 'post_mime_type' => 'image/jpeg', … … 281 294 282 295 public function test_get_items_invalid_status_param_is_error_response() { 283 wp_set_current_user( $this->editor_id );296 wp_set_current_user( self::$editor_id ); 284 297 $this->factory->attachment->create_object( $this->test_file, 0, array( 285 298 'post_mime_type' => 'image/jpeg', … … 308 321 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 309 322 // Properly authorized users can make the request 310 wp_set_current_user( $this->editor_id );323 wp_set_current_user( self::$editor_id ); 311 324 $response = $this->server->dispatch( $request ); 312 325 $this->assertEquals( 200, $response->get_status() ); … … 416 429 417 430 public function test_create_item() { 418 wp_set_current_user( $this->author_id );431 wp_set_current_user( self::$author_id ); 419 432 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 420 433 $request->set_header( 'Content-Type', 'image/jpeg' ); … … 428 441 429 442 public function test_create_item_default_filename_title() { 430 wp_set_current_user( $this->author_id );443 wp_set_current_user( self::$author_id ); 431 444 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 432 445 $request->set_file_params( array( … … 446 459 447 460 public function test_create_item_with_files() { 448 wp_set_current_user( $this->author_id );461 wp_set_current_user( self::$author_id ); 449 462 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 450 463 $request->set_file_params( array( … … 462 475 463 476 public function test_create_item_with_upload_files_role() { 464 wp_set_current_user( $this->uploader_id );477 wp_set_current_user( self::$uploader_id ); 465 478 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 466 479 $request->set_file_params( array( … … 478 491 479 492 public function test_create_item_empty_body() { 480 wp_set_current_user( $this->author_id );493 wp_set_current_user( self::$author_id ); 481 494 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 482 495 $response = $this->server->dispatch( $request ); … … 485 498 486 499 public function test_create_item_missing_content_type() { 487 wp_set_current_user( $this->author_id );500 wp_set_current_user( self::$author_id ); 488 501 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 489 502 $request->set_body( file_get_contents( $this->test_file ) ); … … 493 506 494 507 public function test_create_item_missing_content_disposition() { 495 wp_set_current_user( $this->author_id );508 wp_set_current_user( self::$author_id ); 496 509 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 497 510 $request->set_header( 'Content-Type', 'image/jpeg' ); … … 502 515 503 516 public function test_create_item_bad_md5_header() { 504 wp_set_current_user( $this->author_id );517 wp_set_current_user( self::$author_id ); 505 518 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 506 519 $request->set_header( 'Content-Type', 'image/jpeg' ); … … 513 526 514 527 public function test_create_item_with_files_bad_md5_header() { 515 wp_set_current_user( $this->author_id );528 wp_set_current_user( self::$author_id ); 516 529 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 517 530 $request->set_file_params( array( … … 529 542 530 543 public function test_create_item_invalid_upload_files_capability() { 531 wp_set_current_user( $this->contributor_id );544 wp_set_current_user( self::$contributor_id ); 532 545 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 533 546 $response = $this->server->dispatch( $request ); … … 536 549 537 550 public function test_create_item_invalid_edit_permissions() { 538 $post_id = $this->factory->post->create( array( 'post_author' => $this->editor_id ) );539 wp_set_current_user( $this->author_id );551 $post_id = $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); 552 wp_set_current_user( self::$author_id ); 540 553 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 541 554 $request->set_param( 'post', $post_id ); … … 545 558 546 559 public function test_create_item_invalid_upload_permissions() { 547 $post_id = $this->factory->post->create( array( 'post_author' => $this->editor_id ) );548 wp_set_current_user( $this->uploader_id );560 $post_id = $this->factory->post->create( array( 'post_author' => self::$editor_id ) ); 561 wp_set_current_user( self::$uploader_id ); 549 562 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 550 563 $request->set_param( 'post', $post_id ); … … 555 568 public function test_create_item_invalid_post_type() { 556 569 $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) ); 557 wp_set_current_user( $this->editor_id );570 wp_set_current_user( self::$editor_id ); 558 571 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 559 572 $request->set_header( 'Content-Type', 'image/jpeg' ); … … 566 579 567 580 public function test_create_item_alt_text() { 568 wp_set_current_user( $this->author_id );581 wp_set_current_user( self::$author_id ); 569 582 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 570 583 $request->set_header( 'Content-Type', 'image/jpeg' ); … … 579 592 580 593 public function test_create_item_unsafe_alt_text() { 581 wp_set_current_user( $this->author_id );594 wp_set_current_user( self::$author_id ); 582 595 $request = new WP_REST_Request( 'POST', '/wp/v2/media' ); 583 596 $request->set_header( 'Content-Type', 'image/jpeg' ); … … 591 604 592 605 public function test_update_item() { 593 wp_set_current_user( $this->editor_id );594 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 595 'post_mime_type' => 'image/jpeg', 596 'post_excerpt' => 'A sample caption', 597 'post_author' => $this->editor_id,606 wp_set_current_user( self::$editor_id ); 607 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 608 'post_mime_type' => 'image/jpeg', 609 'post_excerpt' => 'A sample caption', 610 'post_author' => self::$editor_id, 598 611 ) ); 599 612 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); … … 616 629 617 630 public function test_update_item_parent() { 618 wp_set_current_user( $this->editor_id );631 wp_set_current_user( self::$editor_id ); 619 632 $original_parent = $this->factory->post->create( array() ); 620 633 $attachment_id = $this->factory->attachment->create_object( $this->test_file, $original_parent, array( … … 637 650 638 651 public function test_update_item_invalid_permissions() { 639 wp_set_current_user( $this->author_id );640 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 641 'post_mime_type' => 'image/jpeg', 642 'post_excerpt' => 'A sample caption', 643 'post_author' => $this->editor_id,652 wp_set_current_user( self::$author_id ); 653 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 654 'post_mime_type' => 'image/jpeg', 655 'post_excerpt' => 'A sample caption', 656 'post_author' => self::$editor_id, 644 657 ) ); 645 658 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); … … 651 664 public function test_update_item_invalid_post_type() { 652 665 $attachment_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent' => 0 ) ); 653 wp_set_current_user( $this->editor_id );654 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 655 'post_mime_type' => 'image/jpeg', 656 'post_excerpt' => 'A sample caption', 657 'post_author' => $this->editor_id,666 wp_set_current_user( self::$editor_id ); 667 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 668 'post_mime_type' => 'image/jpeg', 669 'post_excerpt' => 'A sample caption', 670 'post_author' => self::$editor_id, 658 671 ) ); 659 672 $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id ); … … 664 677 665 678 public function test_delete_item() { 666 wp_set_current_user( $this->editor_id );679 wp_set_current_user( self::$editor_id ); 667 680 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 668 681 'post_mime_type' => 'image/jpeg', … … 676 689 677 690 public function test_delete_item_no_trash() { 678 wp_set_current_user( $this->editor_id );691 wp_set_current_user( self::$editor_id ); 679 692 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 680 693 'post_mime_type' => 'image/jpeg', … … 693 706 694 707 public function test_delete_item_invalid_delete_permissions() { 695 wp_set_current_user( $this->author_id );696 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 697 'post_mime_type' => 'image/jpeg', 698 'post_excerpt' => 'A sample caption', 699 'post_author' => $this->editor_id,708 wp_set_current_user( self::$author_id ); 709 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 710 'post_mime_type' => 'image/jpeg', 711 'post_excerpt' => 'A sample caption', 712 'post_author' => self::$editor_id, 700 713 ) ); 701 714 $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id ); … … 708 721 'post_mime_type' => 'image/jpeg', 709 722 'post_excerpt' => 'A sample caption', 710 'post_author' => $this->editor_id,723 'post_author' => self::$editor_id, 711 724 ) ); 712 725 … … 799 812 ) ); 800 813 801 wp_set_current_user( $this->editor_id );802 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 803 'post_mime_type' => 'image/jpeg', 804 'post_excerpt' => 'A sample caption', 805 'post_author' => $this->editor_id,814 wp_set_current_user( self::$editor_id ); 815 $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array( 816 'post_mime_type' => 'image/jpeg', 817 'post_excerpt' => 'A sample caption', 818 'post_author' => self::$editor_id, 806 819 ) ); 807 820 // Check for error on update.
Note: See TracChangeset
for help on using the changeset viewer.