Make WordPress Core

Ticket #43329: 43329.2.diff

File 43329.2.diff, 2.6 KB (added by soulseekah, 6 years ago)

typo in tests filter cleanup section

  • src/wp-admin/includes/file.php

    diff --git src/wp-admin/includes/file.php src/wp-admin/includes/file.php
    index 2a0c879..aceeeb8 100644
    function download_url( $url, $timeout = 300 ) { 
    993993                return $response;
    994994        }
    995995
    996         if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
     996        $response_code = wp_remote_retrieve_response_code( $response );
     997        if ( 200 != $response_code ) {
     998                $data = array(
     999                        'code' => $response_code,
     1000                );
     1001
     1002                /**
     1003                 * Retrieve a sample of the actual body for debug purposes.
     1004                 */
     1005                if ( is_resource( $tmpf = fopen( $tmpfname, 'rb' ) ) ) {
     1006                        $data['body'] = fread( $tmpf, apply_filters( 'download_url_error_max_body_response_size', KB_IN_BYTES ) );
     1007                        fclose( $tmpf );
     1008                }
     1009
    9971010                unlink( $tmpfname );
    998                 return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
     1011                return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
    9991012        }
    10001013
    10011014        $content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
  • tests/phpunit/tests/admin/includesFile.php

    diff --git tests/phpunit/tests/admin/includesFile.php tests/phpunit/tests/admin/includesFile.php
    index 7ecbd0e..f3a25ff 100644
    class Tests_Admin_includesFile extends WP_UnitTestCase { 
    3131                update_option( 'siteurl', $siteurl );
    3232                $_SERVER['SCRIPT_FILENAME'] = $sfn;
    3333        }
     34
     35        /**
     36         * @ticket 43329
     37         */
     38        public function test_download_url_non_200() {
     39                add_filter( 'pre_http_request', array( $this, 'fake_download_url_non_200' ), 10, 3 );
     40
     41                $error = download_url( 'test_download_url_non_200' );
     42                $this->assertWPError( $error );
     43                $this->assertEquals( $error->get_error_data(), array(
     44                        'code' => 418,
     45                        'body' => 'This is an unexpected error message from your favorite server',
     46                ) );
     47
     48                add_filter( 'download_url_error_max_body_response_size', array( $this, '__return_5' ) );
     49
     50                $error = download_url( 'test_download_url_non_200' );
     51                $this->assertWPError( $error );
     52                $this->assertEquals( $error->get_error_data(), array(
     53                        'code' => 418,
     54                        'body' => 'This ',
     55                ) );
     56
     57                remove_filter( 'download_url_error_max_body_response_size', array( $this, '__return_5' ) );
     58                remove_filter( 'pre_http_request', array( $this, 'fake_download_url_non_200' ) );
     59        }
     60
     61        public function fake_download_url_non_200( $response, $args, $url ) {
     62                file_put_contents( $args['filename'], 'This is an unexpected error message from your favorite server' );
     63                return array(
     64                        'response'      => array(
     65                                'code'    => 418,
     66                                'message' => "I'm a teapot!",
     67                        ),
     68                );
     69        }
     70
     71        public function __return_5() {
     72                return 5;
     73        }
    3474}