WordPress.org

Make WordPress Core

Changeset 29864


Ignore:
Timestamp:
10/09/2014 03:00:16 AM (5 years ago)
Author:
dd32
Message:

Add some unit tests for WP_HTTP::parse_url() to cover the <PHP 5.4.7 compatibility alterations.

These unit tests cover the expected vehaviour of certain combinations of URL's, but makes no attempt to test invalid URL structures, as PHP's behavious for invalid URL's is undefined (Some will be treated as paths, others fail, and it varies between PHP 5.4.7+ and <5.4.7).
This change also makes WP_HTTP::parse_url() protected in order to allow unit testing.
See #28001, #29886

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-http.php

    r29861 r29864  
    683683     * @since 4.1.0
    684684     *
    685      * @access private
     685     * @access protected
    686686     * @param  string $url The URL to parse
    687687     * @return bool|array False on failure; Array of URL components on success; See parse_url()'s return values.
    688688     */
    689     private static function parse_url( $url ) {
     689    protected static function parse_url( $url ) {
    690690        $parts = @parse_url( $url );
    691691        if ( ! $parts ) {
  • trunk/tests/phpunit/tests/http/http.php

    r29851 r29864  
    6767        );
    6868    }
     69
     70    /**
     71     * @dataProvider parse_url_testcases
     72     */
     73    function test_parse_url( $url, $expected ) {
     74        if ( ! is_callable( array( 'WP_HTTP_Testable', 'parse_url' ) ) ) {
     75            $this->markTestSkipped( "This version of WP_HTTP doesn't support WP_HTTP::parse_url()" );
     76            return;
     77        }
     78        $actual = WP_HTTP_Testable::parse_url( $url );
     79        $this->assertEquals( $expected, $actual );
     80    }
     81
     82    function parse_url_testcases() {
     83        // 0: The URL, 1: The expected resulting structure
     84        return array(
     85            array( 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ),
     86
     87            // < PHP 5.4.7: Schemeless URL
     88            array( '//example.com/path/', array( 'host' => 'example.com', 'path' => '/path/' ) ),
     89            array( '//example.com/', array( 'host' => 'example.com', 'path' => '/' ) ),
     90            array( 'http://example.com//path/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '//path/' ) ),
     91
     92            // < PHP 5.4.7: Scheme seperator in the URL
     93            array( 'http://example.com/http://example.net/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/http://example.net/' ) ),
     94            array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ),
     95            // PHP's parse_url() calls this an invalid url, we handle it as a path
     96            array( '/://example.com/', array( 'path' => '/://example.com/' ) ),
     97
     98        );
     99        /*
     100        Untestable edge cases in various PHP:
     101          - ///example.com - Fails in PHP >= 5.4.7, assumed path in <5.4.7
     102          - ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7
     103        */
     104    }
    69105}
     106
     107/**
     108 * A Wrapper of WP_HTTP to make parse_url() publicaly accessible for testing purposes.
     109 */
     110class WP_HTTP_Testable extends WP_HTTP {
     111    public static function parse_url( $url ) {
     112        return parent::parse_url( $url );
     113    }
     114}
Note: See TracChangeset for help on using the changeset viewer.