Make WordPress Core


Ignore:
Timestamp:
10/27/2016 02:56:28 AM (10 years ago)
Author:
boonebgorges
Message:

Share fixtures in REST API endpoint tests.

As sparrows' tears shed steadily
Make widest rivers filled,

setUp() routines run prodig'ly
Add minutes to a build.

So cull ye fixtures profligate!
Direct thine frugal gaze!

Our savings here - a half-minute -
When multiplied: Amaze!

See #30017.

File:
1 edited

Legend:

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

    r38968 r38975  
    1111 */
    1212class 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        }
    1339
    1440        public function setUp() {
    1541                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                 ) );
    2642
    2743                // Add an uploader role to test upload capabilities.
     
    3147                $role->add_cap( 'read' );
    3248                $role->add_cap( 'level_0' );
    33                 $this->uploader_id = $this->factory->user->create( array(
    34                         'role' => 'uploader',
    35                 ) );
    3649
    3750                $orig_file = DIR_TESTDATA . '/images/canola.jpg';
     
    180193
    181194        public function test_get_items_logged_in_editor() {
    182                 wp_set_current_user( $this->editor_id );
     195                wp_set_current_user( self::$editor_id );
    183196                $id1 = $this->factory->attachment->create_object( $this->test_file, 0, array(
    184197                        'post_mime_type' => 'image/jpeg',
     
    281294
    282295        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 );
    284297                $this->factory->attachment->create_object( $this->test_file, 0, array(
    285298                        'post_mime_type' => 'image/jpeg',
     
    308321                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    309322                // Properly authorized users can make the request
    310                 wp_set_current_user( $this->editor_id );
     323                wp_set_current_user( self::$editor_id );
    311324                $response = $this->server->dispatch( $request );
    312325                $this->assertEquals( 200, $response->get_status() );
     
    416429
    417430        public function test_create_item() {
    418                 wp_set_current_user( $this->author_id );
     431                wp_set_current_user( self::$author_id );
    419432                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    420433                $request->set_header( 'Content-Type', 'image/jpeg' );
     
    428441
    429442        public function test_create_item_default_filename_title() {
    430                 wp_set_current_user( $this->author_id );
     443                wp_set_current_user( self::$author_id );
    431444                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    432445                $request->set_file_params( array(
     
    446459
    447460        public function test_create_item_with_files() {
    448                 wp_set_current_user( $this->author_id );
     461                wp_set_current_user( self::$author_id );
    449462                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    450463                $request->set_file_params( array(
     
    462475
    463476        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 );
    465478                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    466479                $request->set_file_params( array(
     
    478491
    479492        public function test_create_item_empty_body() {
    480                 wp_set_current_user( $this->author_id );
     493                wp_set_current_user( self::$author_id );
    481494                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    482495                $response = $this->server->dispatch( $request );
     
    485498
    486499        public function test_create_item_missing_content_type() {
    487                 wp_set_current_user( $this->author_id );
     500                wp_set_current_user( self::$author_id );
    488501                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    489502                $request->set_body( file_get_contents( $this->test_file ) );
     
    493506
    494507        public function test_create_item_missing_content_disposition() {
    495                 wp_set_current_user( $this->author_id );
     508                wp_set_current_user( self::$author_id );
    496509                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    497510                $request->set_header( 'Content-Type', 'image/jpeg' );
     
    502515
    503516        public function test_create_item_bad_md5_header() {
    504                 wp_set_current_user( $this->author_id );
     517                wp_set_current_user( self::$author_id );
    505518                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    506519                $request->set_header( 'Content-Type', 'image/jpeg' );
     
    513526
    514527        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 );
    516529                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    517530                $request->set_file_params( array(
     
    529542
    530543        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 );
    532545                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    533546                $response = $this->server->dispatch( $request );
     
    536549
    537550        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 );
    540553                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    541554                $request->set_param( 'post', $post_id );
     
    545558
    546559        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 );
    549562                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    550563                $request->set_param( 'post', $post_id );
     
    555568        public function test_create_item_invalid_post_type() {
    556569                $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 );
    558571                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    559572                $request->set_header( 'Content-Type', 'image/jpeg' );
     
    566579
    567580        public function test_create_item_alt_text() {
    568                 wp_set_current_user( $this->author_id );
     581                wp_set_current_user( self::$author_id );
    569582                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    570583                $request->set_header( 'Content-Type', 'image/jpeg' );
     
    579592
    580593        public function test_create_item_unsafe_alt_text() {
    581                 wp_set_current_user( $this->author_id );
     594                wp_set_current_user( self::$author_id );
    582595                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    583596                $request->set_header( 'Content-Type', 'image/jpeg' );
     
    591604
    592605        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,
    598611                ) );
    599612                $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     
    616629
    617630        public function test_update_item_parent() {
    618                 wp_set_current_user( $this->editor_id );
     631                wp_set_current_user( self::$editor_id );
    619632                $original_parent = $this->factory->post->create( array() );
    620633                $attachment_id = $this->factory->attachment->create_object( $this->test_file, $original_parent, array(
     
    637650
    638651        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,
    644657                ) );
    645658                $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     
    651664        public function test_update_item_invalid_post_type() {
    652665                $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,
    658671                ) );
    659672                $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     
    664677
    665678        public function test_delete_item() {
    666                 wp_set_current_user( $this->editor_id );
     679                wp_set_current_user( self::$editor_id );
    667680                $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    668681                        'post_mime_type' => 'image/jpeg',
     
    676689
    677690        public function test_delete_item_no_trash() {
    678                 wp_set_current_user( $this->editor_id );
     691                wp_set_current_user( self::$editor_id );
    679692                $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
    680693                        'post_mime_type' => 'image/jpeg',
     
    693706
    694707        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,
    700713                ) );
    701714                $request = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id );
     
    708721                        'post_mime_type' => 'image/jpeg',
    709722                        'post_excerpt'   => 'A sample caption',
    710                         'post_author'    => $this->editor_id,
     723                        'post_author'    => self::$editor_id,
    711724                ) );
    712725
     
    799812                ) );
    800813
    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,
    806819                ) );
    807820                // Check for error on update.
Note: See TracChangeset for help on using the changeset viewer.