Make WordPress Core


Ignore:
Timestamp:
01/19/2023 09:46:58 PM (2 years 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.