Ticket #21602: 21602.3.diff
File 21602.3.diff, 7.8 KB (added by , 9 years ago) |
---|
-
tests/phpunit/includes/testcase-canonical.php
class WP_Canonical_UnitTestCase extends 146 146 147 147 foreach ( self::$term_ids as $tid => $tax ) { 148 148 wp_delete_term( $tid, $tax ); 149 149 } 150 150 151 151 self::$author_id = null; 152 152 self::$post_ids = array(); 153 153 self::$comment_ids = array(); 154 154 self::$term_ids = array(); 155 155 self::$terms = array(); 156 156 } 157 157 158 158 /** 159 159 * Assert that a given URL is the same a the canonical URL generated by WP. 160 160 * 161 * This runs the canonical tests with the home_url hostname being forced to uppercase in addition to standard. 162 * 161 163 * @since 4.1.0 162 164 * 163 165 * @param string $test_url Raw URL that will be run through redirect_canonical(). 164 166 * @param string $expected Expected string. 165 167 * @param int $ticket Optional. Trac ticket number. 166 168 * @param array $expected_doing_it_wrong Array of class/function names expected to throw _doing_it_wrong() notices. 167 169 */ 168 170 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() ) { 169 195 $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong ); 170 196 171 197 if ( $ticket ) 172 198 $this->knownWPBug( $ticket ); 173 199 174 200 $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null; 175 201 176 202 if ( is_string($expected) ) 177 203 $expected = array('url' => $expected); 178 204 elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) ) 179 205 $expected = array( 'qv' => $expected ); 180 206 181 207 if ( !isset($expected['url']) && !isset($expected['qv']) ) 182 208 $this->markTestSkipped('No valid expected output was provided'); 183 209 184 $this->go_to( home_url( $test_url ));210 $this->go_to( $test_url ); 185 211 186 212 // 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; 188 215 $parsed_can_url = parse_url($can_url); 189 216 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 } 193 224 } 194 225 195 226 if ( ! isset($expected['qv']) ) 196 227 return; 197 228 198 229 // "make" that the request and check the query is correct 199 230 $this->go_to( $can_url ); 200 231 201 232 // Are all query vars accounted for, And correct? 202 233 global $wp; 203 234 204 235 $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars); 205 236 if ( !empty($parsed_can_url['query']) ) { 206 237 parse_str($parsed_can_url['query'], $_qv); 207 238 208 239 // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite) 209 240 $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref ); 210 241 211 242 $query_vars = array_merge($query_vars, $_qv); 212 243 } 213 244 214 245 $this->assertEquals( $expected['qv'], $query_vars ); 215 246 } 216 247 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 returns223 * 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 request231 232 return $can_url;233 }234 248 } -
tests/phpunit/tests/canonical/pageOnFront.php
class Tests_Canonical_PageOnFront extend 32 32 function data() { 33 33 /* Format: 34 34 * [0]: $test_url, 35 35 * [1]: expected results: Any of the following can be used 36 36 * array( 'url': expected redirection location, 'qv': expected query vars to be set via the rewrite AND $_GET ); 37 37 * array( expected query vars to be set, same as 'qv' above ) 38 38 * (string) expected redirect location 39 39 * [3]: (optional) The ticket the test refers to, Can be skipped if unknown. 40 40 */ 41 41 return array( 42 42 // Check against an odd redirect 43 43 array( '/page/2/', '/page/2/' ), 44 44 array( '/?page=2', '/page/2/' ), 45 45 array( '/page/1/', '/' ), 46 46 array( '/?page=1', '/' ), 47 array( '/', '/' /*, 21602 */ ), 47 48 48 49 // The page designated as the front page should redirect to the front of the site 49 50 array( '/front-page/', '/' ), 50 51 array( '/front-page/2/', '/page/2/' ), 51 52 array( '/front-page/?page=2', '/page/2/' ), 52 53 array( '/blog-page/?paged=2', '/blog-page/page/2/' ), 53 54 ); 54 55 } 55 56 } -
src/wp-includes/canonical.php
function wp_redirect_admin_locations() { 634 634 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) { 635 635 wp_redirect( admin_url() ); 636 636 exit; 637 637 } 638 638 639 639 $logins = array( 640 640 home_url( 'wp-login.php', 'relative' ), 641 641 home_url( 'login', 'relative' ), 642 642 site_url( 'login', 'relative' ), 643 643 ); 644 644 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) { 645 645 wp_redirect( wp_login_url() ); 646 646 exit; 647 647 } 648 648 } 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 */ 660 function 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 */ 672 function force_lowercase_hostname_in_url_cb( $host ) { 673 return $host[1] . strtolower( $host[2] ); 674 } 675 add_filter( 'home_url', 'force_lowercase_hostname_in_url', 100, 3 ); 676 add_filter( 'site_url', 'force_lowercase_hostname_in_url', 100, 3 ); 677 No newline at end of file