From d759c0ef236aaa6db4206593e6a9b1c364793a9e Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Mon, 12 Jan 2015 02:23:43 +0000 Subject: [PATCH] Check for existence of data from `get_blogaddress_by_id()` before returning a URL * Prevent a notice when an invalid ID is used with `get_blogaddres_by_id()`. * Return a falsy empty string rather than the previous "http://". * Add unit tests for `get_blogaddress_by_id()`. Props nerrad. Fixes #30566. git-svn-id: https://develop.svn.wordpress.org/trunk@31157 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/ms-blogs.php | 4 ++-- tests/phpunit/tests/multisite/site.php | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 72ebbaa3a8..d291f169db 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -33,11 +33,11 @@ function wpmu_update_blogs_date() { * @since MU * * @param int $blog_id Blog ID - * @return string + * @return string Full URL of the blog if found. Empty string if not. */ function get_blogaddress_by_id( $blog_id ) { $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details! - return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ); + return isset( $bloginfo->domain ) && isset( $bloginfo->path ) ? esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ) : ''; } /** diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 73cbd2872b..603546c3c7 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -332,7 +332,7 @@ class Tests_Multisite_Site extends WP_UnitTestCase { $result = update_blog_details( 999, array( 'domain' => 'example.com' ) ); $this->assertFalse( $result ); } - + function test_update_blog_details() { $blog_id = $this->factory->blog->create(); @@ -1461,6 +1461,22 @@ class Tests_Multisite_Site extends WP_UnitTestCase { remove_filter( 'domain_exists', array( $this, '_domain_exists_cb' ), 10, 4 ); } + + /** + * Tests returning an address for a given valid id. + */ + function test_get_blogaddress_by_id_with_valid_id() { + $blogaddress = get_blogaddress_by_id( 1 ); + $this->assertEquals( 'http://example.org/', $blogaddress ); + } + + /** + * Tests returning the appropriate response for a invalid id given. + */ + function test_get_blogaddress_by_id_with_invalid_id() { + $blogaddress = get_blogaddress_by_id( 42 ); + $this->assertEquals( '', $blogaddress ); + } } endif; \ No newline at end of file