Make WordPress Core


Ignore:
Timestamp:
12/15/2021 07:59:32 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Mock the HTTP request response in download_url() tests.

This aims to speed up the tests and minimize unrelated failures by avoiding an unnecessary external HTTP request, while still performing the intended functionality checks.

Update similar helpers in some other tests to use more consistent terminology.

Follow-up to [37907], [46175], [51626].

See #54420, #53363.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesFile.php

    r52242 r52382  
    292292     */
    293293    public function test_download_url_no_warning_for_url_without_path() {
     294        // Hook a mocked HTTP request response.
     295        add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
     296
    294297        $result = download_url( 'https://example.com' );
    295298
     
    307310     */
    308311    public function test_download_url_no_warning_for_url_without_path_with_signature_verification() {
     312        // Hook a mocked HTTP request response.
     313        add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
     314
    309315        add_filter(
    310316            'wp_signature_hosts',
     
    327333        $this->assertSame( 'signature_verification_no_signature', $error->get_error_code() );
    328334    }
     335
     336    /**
     337     * Mock the HTTP request response.
     338     *
     339     * @param bool   $false     False.
     340     * @param array  $arguments Request arguments.
     341     * @param string $url       Request URL.
     342     * @return array|bool
     343     */
     344    public function mock_http_request( $false, $arguments, $url ) {
     345        if ( 'https://example.com' === $url ) {
     346            return array(
     347                'response' => array(
     348                    'code' => 200,
     349                ),
     350            );
     351        }
     352
     353        return $false;
     354    }
    329355}
Note: See TracChangeset for help on using the changeset viewer.