Make WordPress Core


Ignore:
Timestamp:
10/04/2016 08:32:40 PM (8 years ago)
Author:
peterwilsoncc
Message:

HTTP API: Simplify wp_parse_url() to ensure consistent results.

[38694] revealed some URL formats were been parsed incorrectly, including those used by Google Fonts. This change simplifies the function to use placeholder values which cause PHP's parsing to behave consistently.

Props jrf, peterwilsoncc.
Fixes #36356.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/http/http.php

    r38694 r38726  
    108108            array( '/://example.com/', array( 'path' => '/://example.com/' ) ),
    109109
     110            // Schemeless URL containing colons cause parse errors in PHP 7+.
     111            array(
     112                '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin',
     113                array(
     114                    'host'  => 'fonts.googleapis.com',
     115                    'path'  => '/css',
     116                    'query' => 'family=Open+Sans:400&subset=latin',
     117                ),
     118            ),
     119            array(
     120                '//fonts.googleapis.com/css?family=Open+Sans:400',
     121                array(
     122                    'host'  => 'fonts.googleapis.com',
     123                    'path'  => '/css',
     124                    'query' => 'family=Open+Sans:400',
     125                ),
     126            ),
     127
     128            array( 'filenamefound', array( 'path' => 'filenamefound' ) ),
     129
     130            // Empty string or non-string passed in.
     131            array( '', array( 'path' => '' ) ),
     132            array( 123, array( 'path' => '123' ) ),
    110133        );
    111134        /*
     
    118141    /**
    119142     * @ticket 36356
    120     */
     143    */
    121144    function test_wp_parse_url_with_default_component() {
    122145        $actual = wp_parse_url( self::FULL_TEST_URL, -1 );
     
    176199            array( '/://example.com/', PHP_URL_PATH, '/://example.com/' ),
    177200
     201            // Schemeless URL containing colons cause parse errors in PHP 7+.
     202            array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_HOST, 'fonts.googleapis.com' ),
     203            array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PORT, null ),
     204            array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PATH, '/css' ),
     205            array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_QUERY, 'family=Open+Sans:400&subset=latin' ),
     206            array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_HOST, 'fonts.googleapis.com' ), // 25
     207            array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PORT, null ),
     208            array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PATH, '/css' ), //27
     209            array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_QUERY, 'family=Open+Sans:400' ), //28
     210
     211            // Empty string or non-string passed in.
     212            array( '', PHP_URL_PATH, '' ),
     213            array( '', PHP_URL_QUERY, null ),
     214            array( 123, PHP_URL_PORT, null ),
     215            array( 123, PHP_URL_PATH, '123' ),
    178216        );
    179217    }
     
    225263        }
    226264    }
     265
     266    /**
     267     * @ticket 36356
     268     *
     269     * @dataProvider get_component_from_parsed_url_array_testcases
     270     */
     271    function test_get_component_from_parsed_url_array( $url, $component, $expected ) {
     272        $parts  = wp_parse_url( $url );
     273        $actual = _get_component_from_parsed_url_array( $parts, $component );
     274        $this->assertSame( $expected, $actual );
     275    }
     276
     277    function get_component_from_parsed_url_array_testcases() {
     278        // 0: A URL, 1: PHP URL constant, 2: The expected result.
     279        return array(
     280            array( 'http://example.com/', -1, array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ),
     281            array( 'http://example.com/', -1, array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ),
     282            array( 'http://example.com/', PHP_URL_HOST, 'example.com' ),
     283            array( 'http://example.com/', PHP_URL_USER, null ),
     284            array( 'http:///example.com', -1, false ), // Malformed.
     285            array( 'http:///example.com', PHP_URL_HOST, null ), // Malformed.
     286        );
     287    }
     288
     289    /**
     290     * @ticket 36356
     291     *
     292     * @dataProvider wp_translate_php_url_constant_to_key_testcases
     293     */
     294    function test_wp_translate_php_url_constant_to_key( $input, $expected ) {
     295        $actual = _wp_translate_php_url_constant_to_key( $input );
     296        $this->assertSame( $expected, $actual );
     297    }
     298
     299    function wp_translate_php_url_constant_to_key_testcases() {
     300        // 0: PHP URL constant, 1: The expected result.
     301        return array(
     302            array( PHP_URL_SCHEME, 'scheme' ),
     303            array( PHP_URL_HOST, 'host' ),
     304            array( PHP_URL_PORT, 'port' ),
     305            array( PHP_URL_USER, 'user' ),
     306            array( PHP_URL_PASS, 'pass' ),
     307            array( PHP_URL_PATH, 'path' ),
     308            array( PHP_URL_QUERY, 'query' ),
     309            array( PHP_URL_FRAGMENT, 'fragment' ),
     310
     311            // Test with non-PHP_URL_CONSTANT parameter.
     312            array( 'something', false ),
     313            array( ABSPATH, false ),
     314        );
     315    }
     316
    227317}
Note: See TracChangeset for help on using the changeset viewer.