From a5d3bde7a0a198f3a1b30161e80aa8ebb66df41a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 5 Sep 2013 17:28:52 +0000 Subject: [PATCH] Avoids incorrect results when `url_to_postid()` checking is not strict enough. Adds a bunch of Unit Tests for `url_to_postid()', which is currently only tested lightly in some XML-RPC tests. Props gcorne, gradyetc. Fixes #20560. git-svn-id: https://develop.svn.wordpress.org/trunk@25258 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rewrite.php | 8 +-- tests/phpunit/tests/rewrite.php | 108 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/tests/rewrite.php diff --git a/src/wp-includes/rewrite.php b/src/wp-includes/rewrite.php index e2dbc802a4..54239ce00c 100644 --- a/src/wp-includes/rewrite.php +++ b/src/wp-includes/rewrite.php @@ -324,14 +324,14 @@ function url_to_postid($url) { if ( !$wp_rewrite->using_index_permalinks() ) $url = str_replace( $wp_rewrite->index . '/', '', $url ); - if ( false !== strpos($url, home_url()) ) { - // Chop off http://domain.com + if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) { + // Chop off http://domain.com/[path] $url = str_replace(home_url(), '', $url); } else { // Chop off /path/to/blog - $home_path = parse_url(home_url()); + $home_path = parse_url( home_url( '/' ) ); $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ; - $url = str_replace($home_path, '', $url); + $url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) ); } // Trim leading and lagging slashes diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php new file mode 100644 index 0000000000..323076604e --- /dev/null +++ b/tests/phpunit/tests/rewrite.php @@ -0,0 +1,108 @@ +init(); + flush_rewrite_rules(); + } + + function tearDown() { + parent::tearDown(); + $GLOBALS['wp_rewrite']->init(); + } + + function test_url_to_postid() { + + $id = $this->factory->post->create(); + $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); + + $id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); + } + + function test_url_to_postid_hierarchical() { + + $parent_id = $this->factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) ); + $child_id = $this->factory->post->create( array( 'post_title' => 'Child', 'post_type' => 'page', 'post_parent' => $parent_id ) ); + + $this->assertEquals( $parent_id, url_to_postid( get_permalink( $parent_id ) ) ); + $this->assertEquals( $child_id, url_to_postid( get_permalink( $child_id ) ) ); + } + + function test_url_to_postid_home_has_path() { + + update_option( 'home', home_url( '/example/' ) ); + + $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'examp' ) ); + $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); + $this->assertEquals( $id, url_to_postid( site_url('/example/examp' ) ) ); + $this->assertEquals( $id, url_to_postid( '/example/examp/' ) ); + $this->assertEquals( $id, url_to_postid( '/example/examp' ) ); + + $this->assertEquals( 0, url_to_postid( site_url( '/example/ex' ) ) ); + $this->assertEquals( 0, url_to_postid( '/example/ex' ) ); + $this->assertEquals( 0, url_to_postid( '/example/ex/' ) ); + $this->assertEquals( 0, url_to_postid( '/example-page/example/' ) ); + $this->assertEquals( 0, url_to_postid( '/example-page/ex/' ) ); + } + + function test_url_to_postid_dupe_path() { + update_option( 'home', home_url('/example/') ); + + $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'example' ) ); + + $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); + $this->assertEquals( $id, url_to_postid( site_url( '/example/example/' ) ) ); + $this->assertEquals( $id, url_to_postid( '/example/example/' ) ); + $this->assertEquals( $id, url_to_postid( '/example/example' ) ); + } + + /** + * Reveals bug introduced in WP 3.0 + */ + function test_url_to_postid_home_url_collision() { + update_option( 'home', home_url( '/example' ) ); + + $this->factory->post->create( array( 'post_title' => 'Collision', 'post_type' => 'page', 'post_name' => 'collision' ) ); + + // This url should NOT return a post ID + $badurl = site_url( '/example-collision' ); + $this->assertEquals( 0, url_to_postid( $badurl ) ); + } + + /** + * Reveals bug introduced in WP 3.0 + * + * Run tests using multisite `phpunit -c multisite` + */ + function test_url_to_postid_ms_home_url_collision() { + + if ( ! is_multisite() ) { + $this->markTestSkipped( 'test_url_to_postid_ms_home_url_collision requires multisite' ); + return false; + } + + $blog_id = $this->factory->blog->create( array( 'path' => '/example' ) ); + switch_to_blog( $blog_id ); + + $this->factory->post->create( array( 'post_title' => 'Collision ', 'post_type' => 'page' ) ); + + // This url should NOT return a post ID + $badurl = network_home_url( '/example-collision' ); + $this->assertEquals( 0, url_to_postid( $badurl ) ); + + restore_current_blog(); + } +} \ No newline at end of file