Make WordPress Core

Ticket #21602: 21602.3.diff

File 21602.3.diff, 7.8 KB (added by dd32, 9 years ago)

a variant which forces home_url + site_url to have a lowercase hostname - same tests as 21602.2.diff

  • tests/phpunit/includes/testcase-canonical.php

    class WP_Canonical_UnitTestCase extends  
    146146
    147147                foreach ( self::$term_ids as $tid => $tax ) {
    148148                        wp_delete_term( $tid, $tax );
    149149                }
    150150
    151151                self::$author_id = null;
    152152                self::$post_ids = array();
    153153                self::$comment_ids = array();
    154154                self::$term_ids = array();
    155155                self::$terms = array();
    156156        }
    157157
    158158        /**
    159159         * Assert that a given URL is the same a the canonical URL generated by WP.
    160160         *
     161         * This runs the canonical tests with the home_url hostname being forced to uppercase in addition to standard.
     162         *
    161163         * @since 4.1.0
    162164         *
    163165         * @param string $test_url                Raw URL that will be run through redirect_canonical().
    164166         * @param string $expected                Expected string.
    165167         * @param int    $ticket                  Optional. Trac ticket number.
    166168         * @param array  $expected_doing_it_wrong Array of class/function names expected to throw _doing_it_wrong() notices.
    167169         */
    168170        public function assertCanonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
     171                $test_url = home_url( $test_url );
     172
     173                $this->_assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
     174
     175                // Re-test with the home_url hostname forced to uppercase.
     176                add_filter( 'home_url', array( $this, 'filter_uppercase_hostname' ) );
     177                $this->_assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
     178        }
     179
     180        /**
     181         * Internal helper for `::assertCanonical` for force an uppercase hostname in `home_url()`.
     182         *
     183         * @ignore
     184         */
     185        public function filter_uppercase_hostname( $url ) {
     186                return str_replace( '://' . WP_TESTS_DOMAIN, '://' . strtoupper( WP_TESTS_DOMAIN ), $url );
     187        }
     188
     189        /**
     190         * Internal helper for `::assertCanonical`.
     191         *
     192         * @ignore
     193         */
     194        public function _assertCanonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
    169195                $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
    170196
    171197                if ( $ticket )
    172198                        $this->knownWPBug( $ticket );
    173199
    174200                $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
    175201
    176202                if ( is_string($expected) )
    177203                        $expected = array('url' => $expected);
    178204                elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
    179205                        $expected = array( 'qv' => $expected );
    180206
    181207                if ( !isset($expected['url']) && !isset($expected['qv']) )
    182208                        $this->markTestSkipped('No valid expected output was provided');
    183209
    184                 $this->go_to( home_url( $test_url ) );
     210                $this->go_to( $test_url );
    185211
    186212                // Does the redirect match what's expected?
    187                 $can_url = $this->get_canonical( $test_url );
     213                $can_response = redirect_canonical( null, false );
     214                $can_url = $can_response ? $can_response : $test_url;
    188215                $parsed_can_url = parse_url($can_url);
    189216
    190                 // Just test the Path and Query if present
    191                 if ( isset($expected['url']) ) {
    192                         $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
     217                if ( isset( $expected['url'] ) ) {
     218                        if ( $test_url == $expected['url'] ) {
     219                                $this->assertNull( $can_response, $ticket_ref );
     220                        } else {
     221                                $this->assertEquals( WP_TESTS_DOMAIN, $parsed_can_url['host'] );
     222                                $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
     223                        }
    193224                }
    194225
    195226                if ( ! isset($expected['qv']) )
    196227                        return;
    197228
    198229                // "make" that the request and check the query is correct
    199230                $this->go_to( $can_url );
    200231
    201232                // Are all query vars accounted for, And correct?
    202233                global $wp;
    203234
    204235                $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
    205236                if ( !empty($parsed_can_url['query']) ) {
    206237                        parse_str($parsed_can_url['query'], $_qv);
    207238
    208239                        // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
    209240                        $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
    210241
    211242                        $query_vars = array_merge($query_vars, $_qv);
    212243                }
    213244
    214245                $this->assertEquals( $expected['qv'], $query_vars );
    215246        }
    216247
    217         /**
    218          * Get the canonical URL given a raw URL.
    219          *
    220          * @param string $test_url Should be relative to the site "front", ie /category/uncategorized/
    221          *                         as opposed to http://example.com/category/uncategorized/
    222          * @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns
    223          *                  the fully-qualified URL as generated by redirect_canonical().
    224          */
    225         public function get_canonical( $test_url ) {
    226                 $test_url = home_url( $test_url );
    227 
    228                 $can_url = redirect_canonical( $test_url, false );
    229                 if ( ! $can_url )
    230                         return $test_url; // No redirect will take place for this request
    231 
    232                 return $can_url;
    233         }
    234248}
  • 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}
  • src/wp-includes/canonical.php

    function wp_redirect_admin_locations() { 
    634634        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
    635635                wp_redirect( admin_url() );
    636636                exit;
    637637        }
    638638
    639639        $logins = array(
    640640                home_url( 'wp-login.php', 'relative' ),
    641641                home_url( 'login', 'relative' ),
    642642                site_url( 'login', 'relative' ),
    643643        );
    644644        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
    645645                wp_redirect( wp_login_url() );
    646646                exit;
    647647        }
    648648}
     649
     650/**
     651 * Force the hostname in the home_url & site_url to lowercase.
     652 *
     653 * Domain names are case insensitive, however pathnames are not.
     654 * WordPress makes comparisons based on the entire url, so this
     655 * forces the hostname to a conistent lowercase value.
     656 *
     657 * @param string $url The URL to force the hostname to lowercase on.
     658 * @return string The URL with the hostname lowercased.
     659 */
     660function force_lowercase_hostname_in_url( $url, $path = null, $scheme = null ) {
     661        if ( 'relative' != $scheme ) {
     662                $url = preg_replace_callback( '!^(\w+://)([^/]*[A-Z][^/]*/)!', 'force_lowercase_hostname_in_url_cb', $url );
     663        }
     664        return $url;
     665}
     666
     667/**
     668 * Callback for `force_lowercase_hostname_in_url()`.
     669 *
     670 * @ignore
     671 */
     672function force_lowercase_hostname_in_url_cb( $host ) {
     673        return $host[1] . strtolower( $host[2] );
     674}
     675add_filter( 'home_url', 'force_lowercase_hostname_in_url', 100, 3 );
     676add_filter( 'site_url', 'force_lowercase_hostname_in_url', 100, 3 );
     677 No newline at end of file