Make WordPress Core

Changeset 55102


Ignore:
Timestamp:
01/19/2023 09:46:58 PM (17 months ago)
Author:
SergeyBiryukov
Message:

Tests: Correct additional_field_get_callback() parameters in some REST API tests.

The second parameter passed to get_callback in WP_REST_Controller::add_additional_fields_to_object() is the field name, not the request details.

Includes moving the get_callback and update_callback helper functions next to the tests they are used in.

Follow-up to [38832], [43768].

See #56793.

Location:
trunk/tests/phpunit/tests/rest-api
Files:
8 edited

Legend:

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

    r54428 r55102  
    16791679    }
    16801680
    1681     public function test_search_item_by_filename() {
    1682         $id1 = self::factory()->attachment->create_object(
    1683             self::$test_file,
    1684             0,
    1685             array(
    1686                 'post_mime_type' => 'image/jpeg',
    1687             )
    1688         );
    1689         $id2 = self::factory()->attachment->create_object(
    1690             self::$test_file2,
    1691             0,
    1692             array(
    1693                 'post_mime_type' => 'image/png',
    1694             )
    1695         );
    1696 
    1697         $filename = wp_basename( self::$test_file2 );
    1698 
    1699         $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
    1700         $request->set_param( 'search', $filename );
    1701         $response = rest_get_server()->dispatch( $request );
    1702         $data     = $response->get_data();
    1703 
    1704         $this->assertCount( 1, $data );
    1705         $this->assertSame( $id2, $data[0]['id'] );
    1706         $this->assertSame( 'image/png', $data[0]['mime_type'] );
    1707     }
    1708 
    1709     public function additional_field_get_callback( $object, $request ) {
     1681    public function additional_field_get_callback( $object, $field_name ) {
    17101682        return 123;
    17111683    }
     
    17151687            return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
    17161688        }
     1689    }
     1690
     1691    public function test_search_item_by_filename() {
     1692        $id1 = self::factory()->attachment->create_object(
     1693            self::$test_file,
     1694            0,
     1695            array(
     1696                'post_mime_type' => 'image/jpeg',
     1697            )
     1698        );
     1699        $id2 = self::factory()->attachment->create_object(
     1700            self::$test_file2,
     1701            0,
     1702            array(
     1703                'post_mime_type' => 'image/png',
     1704            )
     1705        );
     1706
     1707        $filename = wp_basename( self::$test_file2 );
     1708
     1709        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     1710        $request->set_param( 'search', $filename );
     1711        $response = rest_get_server()->dispatch( $request );
     1712        $data     = $response->get_data();
     1713
     1714        $this->assertCount( 1, $data );
     1715        $this->assertSame( $id2, $data[0]['id'] );
     1716        $this->assertSame( 'image/png', $data[0]['mime_type'] );
    17171717    }
    17181718
  • trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php

    r54130 r55102  
    515515    }
    516516
    517     public function additional_field_get_callback( $object ) {
    518         return get_post_meta( $object['id'], 'my_custom_int', true );
    519     }
    520 
    521     public function additional_field_update_callback( $value, $post ) {
    522         update_post_meta( $post->ID, 'my_custom_int', $value );
     517    public function additional_field_get_callback( $object, $field_name ) {
     518        return get_post_meta( $object['id'], $field_name, true );
     519    }
     520
     521    public function additional_field_update_callback( $value, $post, $field_name ) {
     522        update_post_meta( $post->ID, $field_name, $value );
    523523    }
    524524
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r54891 r55102  
    11811181    }
    11821182
    1183     public function additional_field_get_callback( $object, $request ) {
     1183    public function additional_field_get_callback( $object, $field_name ) {
    11841184        return 123;
    11851185    }
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r54891 r55102  
    32993299    }
    33003300
    3301     public function additional_field_get_callback( $object ) {
    3302         return get_comment_meta( $object['id'], 'my_custom_int', true );
    3303     }
    3304 
    3305     public function additional_field_update_callback( $value, $comment ) {
     3301    public function additional_field_get_callback( $object, $field_name ) {
     3302        return get_comment_meta( $object['id'], $field_name, true );
     3303    }
     3304
     3305    public function additional_field_update_callback( $value, $comment, $field_name ) {
    33063306        if ( 'returnError' === $value ) {
    33073307            return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
    33083308        }
    3309         update_comment_meta( $comment->comment_ID, 'my_custom_int', $value );
     3309        update_comment_meta( $comment->comment_ID, $field_name, $value );
    33103310    }
    33113311
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r54855 r55102  
    45874587    }
    45884588
    4589     public function additional_field_get_callback( $object ) {
    4590         return get_post_meta( $object['id'], 'my_custom_int', true );
    4591     }
    4592 
    4593     public function additional_field_update_callback( $value, $post ) {
     4589    public function additional_field_get_callback( $object, $field_name ) {
     4590        return get_post_meta( $object['id'], $field_name, true );
     4591    }
     4592
     4593    public function additional_field_update_callback( $value, $post, $field_name ) {
    45944594        if ( 'returnError' === $value ) {
    45954595            return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
    45964596        }
    4597         update_post_meta( $post->ID, 'my_custom_int', $value );
     4597        update_post_meta( $post->ID, $field_name, $value );
    45984598    }
    45994599
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r53948 r55102  
    403403    }
    404404
    405     public function additional_field_get_callback( $object ) {
    406         return get_post_meta( $object['id'], 'my_custom_int', true );
    407     }
    408 
    409     public function additional_field_update_callback( $value, $post ) {
    410         update_post_meta( $post->ID, 'my_custom_int', $value );
     405    public function additional_field_get_callback( $object, $field_name ) {
     406        return get_post_meta( $object['id'], $field_name, true );
     407    }
     408
     409    public function additional_field_update_callback( $value, $post, $field_name ) {
     410        update_post_meta( $post->ID, $field_name, $value );
    411411    }
    412412
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r54891 r55102  
    13251325    }
    13261326
     1327    public function additional_field_get_callback( $object, $field_name ) {
     1328        return 123;
     1329    }
     1330
     1331    public function additional_field_update_callback( $value, $tag ) {
     1332        if ( 'returnError' === $value ) {
     1333            return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
     1334        }
     1335    }
     1336
    13271337    /**
    13281338     * @ticket 38504
     
    13951405        $this->assertArrayHasKey( $edit_field, $data );
    13961406        $this->assertArrayNotHasKey( $view_field, $data );
    1397     }
    1398 
    1399     public function additional_field_get_callback( $object, $request ) {
    1400         return 123;
    1401     }
    1402 
    1403     public function additional_field_update_callback( $value, $tag ) {
    1404         if ( 'returnError' === $value ) {
    1405             return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
    1406         }
    14071407    }
    14081408
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r54891 r55102  
    28262826    }
    28272827
     2828    public function additional_field_get_callback( $object, $field_name ) {
     2829        return get_user_meta( $object['id'], $field_name, true );
     2830    }
     2831
     2832    public function additional_field_update_callback( $value, $user, $field_name ) {
     2833        if ( 'returnError' === $value ) {
     2834            return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
     2835        }
     2836        update_user_meta( $user->ID, $field_name, $value );
     2837    }
     2838
    28282839    /**
    28292840     * @ticket 39701
     
    30733084    }
    30743085
    3075     public function additional_field_get_callback( $object ) {
    3076         return get_user_meta( $object['id'], 'my_custom_int', true );
    3077     }
    3078 
    3079     public function additional_field_update_callback( $value, $user ) {
    3080         if ( 'returnError' === $value ) {
    3081             return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
    3082         }
    3083         update_user_meta( $user->ID, 'my_custom_int', $value );
    3084     }
    3085 
    30863086    protected function check_user_data( $user, $data, $context, $links ) {
    30873087        $this->assertSame( $user->ID, $data['id'] );
Note: See TracChangeset for help on using the changeset viewer.