WordPress.org

Make WordPress Core

Ticket #21602: 21602.diff

File 21602.diff, 7.3 KB (added by dd32, 5 years ago)
  • src/wp-includes/canonical.php

    function redirect_canonical( $requested_ 
    438438        // Always trailing slash the Front Page URL
    439439        if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
    440440                $redirect['path'] = trailingslashit($redirect['path']);
    441441
    442442        // Ignore differences in host capitalization, as this can lead to infinite redirects
    443443        // Only redirect no-www <=> yes-www
    444444        if ( strtolower($original['host']) == strtolower($redirect['host']) ||
    445445                ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
    446446                $redirect['host'] = $original['host'];
    447447
    448448        $compare_original = array( $original['host'], $original['path'] );
    449449
    450450        if ( !empty( $original['port'] ) )
    451451                $compare_original[] = $original['port'];
    452452
     453        if ( !empty($original['path']) )
     454                $compare_original[] = $original['path'];
     455
    453456        if ( !empty( $original['query'] ) )
    454457                $compare_original[] = $original['query'];
    455458
    456459        $compare_redirect = array( $redirect['host'], $redirect['path'] );
    457460
    458461        if ( !empty( $redirect['port'] ) )
    459462                $compare_redirect[] = $redirect['port'];
    460463
     464        if ( !empty($redirect['path']) )
     465                $compare_redirect[] = $redirect['path'];
     466
    461467        if ( !empty( $redirect['query'] ) )
    462468                $compare_redirect[] = $redirect['query'];
    463469
    464470        if ( $compare_original !== $compare_redirect ) {
    465471                $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
    466472                if ( !empty($redirect['port']) )
    467473                        $redirect_url .= ':' . $redirect['port'];
    468474                $redirect_url .= $redirect['path'];
    469475                if ( !empty($redirect['query']) )
    470476                        $redirect_url .= '?' . $redirect['query'];
     477        } else {
     478                // No changes between the two URLs were detected.
     479                return;
    471480        }
    472481
    473482        if ( ! $redirect_url || $redirect_url == $requested_url ) {
    474483                return;
    475484        }
    476485
    477486        // Hex encoded octets are case-insensitive.
    478487        if ( false !== strpos($requested_url, '%') ) {
    479488                if ( !function_exists('lowercase_octets') ) {
    480489                        function lowercase_octets($matches) {
    481490                                return strtolower( $matches[0] );
    482491                        }
    483492                }
    484493                $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
    485494        }
  • tests/phpunit/includes/testcase-canonical.php

    class WP_Canonical_UnitTestCase extends  
    169169                $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
    170170
    171171                if ( $ticket )
    172172                        $this->knownWPBug( $ticket );
    173173
    174174                $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
    175175
    176176                if ( is_string($expected) )
    177177                        $expected = array('url' => $expected);
    178178                elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
    179179                        $expected = array( 'qv' => $expected );
    180180
    181181                if ( !isset($expected['url']) && !isset($expected['qv']) )
    182182                        $this->markTestSkipped('No valid expected output was provided');
    183183
    184                 $this->go_to( home_url( $test_url ) );
     184                // FQDN it.
     185                $test_url = home_url( $test_url );
     186
     187                $this->go_to( $test_url );
    185188
    186189                // Does the redirect match what's expected?
    187190                $can_url = $this->get_canonical( $test_url );
    188191                $parsed_can_url = parse_url($can_url);
    189192
    190                 // Just test the Path and Query if present
     193                // Test the url components individually.
     194                if ( isset($expected['url']) ) {
     195                        $this->assertEquals( WP_TESTS_DOMAIN, $parsed_can_url['host'] );
     196                        $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
     197                }
     198
     199                // Try the request again, but this time with a capitalised hostname.
     200                add_filter( 'home_url', array( $this, '_filter_url_to_uppercase' ) );
     201                $can_url = $this->get_canonical( $test_url );
     202                $parsed_can_url = parse_url($can_url);
     203
     204                // Test the url components individually.
    191205                if ( isset($expected['url']) ) {
     206                        $this->assertEquals( WP_TESTS_DOMAIN, $parsed_can_url['host'] );
    192207                        $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
    193208                }
    194209
    195210                if ( ! isset($expected['qv']) )
    196211                        return;
    197212
    198213                // "make" that the request and check the query is correct
    199214                $this->go_to( $can_url );
    200215
    201216                // Are all query vars accounted for, And correct?
    202217                global $wp;
    203218
    204219                $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
    205220                if ( !empty($parsed_can_url['query']) ) {
    206221                        parse_str($parsed_can_url['query'], $_qv);
    207222
    208223                        // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
    209224                        $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
    210225
    211226                        $query_vars = array_merge($query_vars, $_qv);
    212227                }
    213228
    214229                $this->assertEquals( $expected['qv'], $query_vars );
    215230        }
    216231
    217232        /**
    218233         * Get the canonical URL given a raw URL.
    219234         *
    220          * @param string $test_url Should be relative to the site "front", ie /category/uncategorized/
    221          *                         as opposed to http://example.com/category/uncategorized/
     235         * @param string $test_url Should be an absolute url ie. http://example.com/category/uncategorized/
    222236         * @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns
    223237         *                  the fully-qualified URL as generated by redirect_canonical().
    224238         */
    225239        public function get_canonical( $test_url ) {
    226                 $test_url = home_url( $test_url );
    227 
    228240                $can_url = redirect_canonical( $test_url, false );
    229241                if ( ! $can_url )
    230242                        return $test_url; // No redirect will take place for this request
    231243
    232244                return $can_url;
    233245        }
     246
     247        /**
     248         * Filters the Site URL to uppercase.
     249         * Used to
     250         */
     251        public function _filter_url_to_uppercase( $url ) {
     252                return str_replace( 'http://' . WP_TESTS_DOMAIN . '/', 'http://' . strtoupper( WP_TESTS_DOMAIN ) . '/', $url );
     253        }
     254
    234255}
  • tests/phpunit/tests/canonical/pageOnFront.php

    class Tests_Canonical_PageOnFront extend 
    3232        function data() {
    3333                /* Format:
    3434                 * [0]: $test_url,
    3535                 * [1]: expected results: Any of the following can be used
    3636                 *      array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET );
    3737                 *      array( expected query vars to be set, same as 'qv' above )
    3838                 *      (string) expected redirect location
    3939                 * [3]: (optional) The ticket the test refers to, Can be skipped if unknown.
    4040                 */
    4141                 return array(
    4242                        // Check against an odd redirect
    4343                        array( '/page/2/', '/page/2/' ),
    4444                        array( '/?page=2', '/page/2/' ),
    4545                        array( '/page/1/', '/' ),
    4646                        array( '/?page=1', '/' ),
     47                        array( '/', '/', 21602 ),
    4748
    4849                        // The page designated as the front page should redirect to the front of the site
    4950                        array( '/front-page/', '/' ),
    5051                        array( '/front-page/2/', '/page/2/' ),
    5152                        array( '/front-page/?page=2', '/page/2/' ),
    5253                        array( '/blog-page/?paged=2', '/blog-page/page/2/' ),
    5354                 );
    5455        }
    5556}