Ticket #21602: 21602.diff
File 21602.diff, 7.3 KB (added by , 8 years ago) |
---|
-
src/wp-includes/canonical.php
function redirect_canonical( $requested_ 438 438 // Always trailing slash the Front Page URL 439 439 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) 440 440 $redirect['path'] = trailingslashit($redirect['path']); 441 441 442 442 // Ignore differences in host capitalization, as this can lead to infinite redirects 443 443 // Only redirect no-www <=> yes-www 444 444 if ( strtolower($original['host']) == strtolower($redirect['host']) || 445 445 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) 446 446 $redirect['host'] = $original['host']; 447 447 448 448 $compare_original = array( $original['host'], $original['path'] ); 449 449 450 450 if ( !empty( $original['port'] ) ) 451 451 $compare_original[] = $original['port']; 452 452 453 if ( !empty($original['path']) ) 454 $compare_original[] = $original['path']; 455 453 456 if ( !empty( $original['query'] ) ) 454 457 $compare_original[] = $original['query']; 455 458 456 459 $compare_redirect = array( $redirect['host'], $redirect['path'] ); 457 460 458 461 if ( !empty( $redirect['port'] ) ) 459 462 $compare_redirect[] = $redirect['port']; 460 463 464 if ( !empty($redirect['path']) ) 465 $compare_redirect[] = $redirect['path']; 466 461 467 if ( !empty( $redirect['query'] ) ) 462 468 $compare_redirect[] = $redirect['query']; 463 469 464 470 if ( $compare_original !== $compare_redirect ) { 465 471 $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; 466 472 if ( !empty($redirect['port']) ) 467 473 $redirect_url .= ':' . $redirect['port']; 468 474 $redirect_url .= $redirect['path']; 469 475 if ( !empty($redirect['query']) ) 470 476 $redirect_url .= '?' . $redirect['query']; 477 } else { 478 // No changes between the two URLs were detected. 479 return; 471 480 } 472 481 473 482 if ( ! $redirect_url || $redirect_url == $requested_url ) { 474 483 return; 475 484 } 476 485 477 486 // Hex encoded octets are case-insensitive. 478 487 if ( false !== strpos($requested_url, '%') ) { 479 488 if ( !function_exists('lowercase_octets') ) { 480 489 function lowercase_octets($matches) { 481 490 return strtolower( $matches[0] ); 482 491 } 483 492 } 484 493 $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url); 485 494 } -
tests/phpunit/includes/testcase-canonical.php
class WP_Canonical_UnitTestCase extends 169 169 $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong ); 170 170 171 171 if ( $ticket ) 172 172 $this->knownWPBug( $ticket ); 173 173 174 174 $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null; 175 175 176 176 if ( is_string($expected) ) 177 177 $expected = array('url' => $expected); 178 178 elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) ) 179 179 $expected = array( 'qv' => $expected ); 180 180 181 181 if ( !isset($expected['url']) && !isset($expected['qv']) ) 182 182 $this->markTestSkipped('No valid expected output was provided'); 183 183 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 ); 185 188 186 189 // Does the redirect match what's expected? 187 190 $can_url = $this->get_canonical( $test_url ); 188 191 $parsed_can_url = parse_url($can_url); 189 192 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. 191 205 if ( isset($expected['url']) ) { 206 $this->assertEquals( WP_TESTS_DOMAIN, $parsed_can_url['host'] ); 192 207 $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref ); 193 208 } 194 209 195 210 if ( ! isset($expected['qv']) ) 196 211 return; 197 212 198 213 // "make" that the request and check the query is correct 199 214 $this->go_to( $can_url ); 200 215 201 216 // Are all query vars accounted for, And correct? 202 217 global $wp; 203 218 204 219 $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars); 205 220 if ( !empty($parsed_can_url['query']) ) { 206 221 parse_str($parsed_can_url['query'], $_qv); 207 222 208 223 // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite) 209 224 $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref ); 210 225 211 226 $query_vars = array_merge($query_vars, $_qv); 212 227 } 213 228 214 229 $this->assertEquals( $expected['qv'], $query_vars ); 215 230 } 216 231 217 232 /** 218 233 * Get the canonical URL given a raw URL. 219 234 * 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/ 222 236 * @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns 223 237 * the fully-qualified URL as generated by redirect_canonical(). 224 238 */ 225 239 public function get_canonical( $test_url ) { 226 $test_url = home_url( $test_url );227 228 240 $can_url = redirect_canonical( $test_url, false ); 229 241 if ( ! $can_url ) 230 242 return $test_url; // No redirect will take place for this request 231 243 232 244 return $can_url; 233 245 } 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 234 255 } -
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 }