Make WordPress Core

Changeset 62205 for trunk


Ignore:
Timestamp:
04/03/2026 09:58:33 PM (6 days ago)
Author:
SergeyBiryukov
Message:

Tests: Move data providers and helpers in Tests_REST_Server for consistency.

This ensures that data providers or helper functions used by a single test are located next to the test, for consistency with the rest of the test suite.

Follow-up to [37905], [37943], [45809], [47239], [47260], [47351], [48947], [49252], [49257], [51960], [53110], [56096], [59032].

See #64225.

File:
1 edited

Legend:

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

    r62107 r62205  
    150150        $this->assertSame( $status, $enveloped['status'] );
    151151        $this->assertSame( $headers, $enveloped['headers'] );
     152    }
     153
     154    /**
     155     * Data provider.
     156     *
     157     * @return array
     158     */
     159    public function data_envelope_params() {
     160        return array(
     161            array( '1' ),
     162            array( 'true' ),
     163            array( false ),
     164            array( 'alternate' ),
     165            array( array( 'alternate' ) ),
     166        );
    152167    }
    153168
     
    17231738
    17241739    /**
     1740     * Helper to setup a users and auth cookie global for the
     1741     * rest_send_refreshed_nonce related tests.
     1742     */
     1743    protected function helper_setup_user_for_rest_send_refreshed_nonce_tests() {
     1744        $author = self::factory()->user->create( array( 'role' => 'author' ) );
     1745        wp_set_current_user( $author );
     1746
     1747        global $wp_rest_auth_cookie;
     1748
     1749        $wp_rest_auth_cookie = true;
     1750    }
     1751
     1752    /**
     1753     * Helper to make the request and get the headers for the
     1754     * rest_send_refreshed_nonce related tests.
     1755     *
     1756     * @return array
     1757     */
     1758    protected function helper_make_request_and_return_headers_for_rest_send_refreshed_nonce_tests() {
     1759        $request = new WP_REST_Request( 'GET', '/', array() );
     1760        $result  = rest_get_server()->serve_request( '/' );
     1761
     1762        return rest_get_server()->sent_headers;
     1763    }
     1764
     1765    /**
    17251766     * Refreshed nonce should be present in header when a valid nonce is
    17261767     * passed for logged in/anonymous user and not present when nonce is not
     
    17531794
    17541795    /**
     1796     * @return array {
     1797     *     @type array {
     1798     *         @type bool $has_logged_in_user Are we registering a user for the test.
     1799     *         @type bool $has_nonce          Is the nonce passed.
     1800     *     }
     1801     * }
     1802     */
     1803    public function data_rest_send_refreshed_nonce() {
     1804        return array(
     1805            array( true, true ),
     1806            array( true, false ),
     1807            array( false, true ),
     1808            array( false, false ),
     1809        );
     1810    }
     1811
     1812    /**
    17551813     * Make sure that a sanitization that transforms the argument type will not
    17561814     * cause the validation to fail.
     
    17891847
    17901848        $this->assertSame( 200, $response->get_status() );
     1849    }
     1850
     1851    public function _validate_as_integer_123( $value, $request, $key ) {
     1852        if ( ! is_int( $value ) ) {
     1853            return new WP_Error( 'some-error', 'This is not valid!' );
     1854        }
     1855
     1856        return true;
     1857    }
     1858
     1859    public function _validate_as_string_foo( $value, $request, $key ) {
     1860        if ( ! is_string( $value ) ) {
     1861            return new WP_Error( 'some-error', 'This is not valid!' );
     1862        }
     1863
     1864        return true;
    17911865    }
    17921866
     
    26382712        $this->assertSame( array( 'GET', 'PUT' ), $link['targetHints']['allow'] );
    26392713    }
    2640 
    2641     public function _validate_as_integer_123( $value, $request, $key ) {
    2642         if ( ! is_int( $value ) ) {
    2643             return new WP_Error( 'some-error', 'This is not valid!' );
    2644         }
    2645 
    2646         return true;
    2647     }
    2648 
    2649     public function _validate_as_string_foo( $value, $request, $key ) {
    2650         if ( ! is_string( $value ) ) {
    2651             return new WP_Error( 'some-error', 'This is not valid!' );
    2652         }
    2653 
    2654         return true;
    2655     }
    2656 
    2657     /**
    2658      * @return array {
    2659      *     @type array {
    2660      *         @type bool $has_logged_in_user Are we registering a user for the test.
    2661      *         @type bool $has_nonce          Is the nonce passed.
    2662      *     }
    2663      * }
    2664      */
    2665     public function data_rest_send_refreshed_nonce() {
    2666         return array(
    2667             array( true, true ),
    2668             array( true, false ),
    2669             array( false, true ),
    2670             array( false, false ),
    2671         );
    2672     }
    2673 
    2674     /**
    2675      * Helper to setup a users and auth cookie global for the
    2676      * rest_send_refreshed_nonce related tests.
    2677      */
    2678     protected function helper_setup_user_for_rest_send_refreshed_nonce_tests() {
    2679         $author = self::factory()->user->create( array( 'role' => 'author' ) );
    2680         wp_set_current_user( $author );
    2681 
    2682         global $wp_rest_auth_cookie;
    2683 
    2684         $wp_rest_auth_cookie = true;
    2685     }
    2686 
    2687     /**
    2688      * Helper to make the request and get the headers for the
    2689      * rest_send_refreshed_nonce related tests.
    2690      *
    2691      * @return array
    2692      */
    2693     protected function helper_make_request_and_return_headers_for_rest_send_refreshed_nonce_tests() {
    2694         $request = new WP_REST_Request( 'GET', '/', array() );
    2695         $result  = rest_get_server()->serve_request( '/' );
    2696 
    2697         return rest_get_server()->sent_headers;
    2698     }
    2699 
    2700     /**
    2701      * Data provider.
    2702      *
    2703      * @return array
    2704      */
    2705     public function data_envelope_params() {
    2706         return array(
    2707             array( '1' ),
    2708             array( 'true' ),
    2709             array( false ),
    2710             array( 'alternate' ),
    2711             array( array( 'alternate' ) ),
    2712         );
    2713     }
    27142714}
Note: See TracChangeset for help on using the changeset viewer.