From 9f5c78b676413b3224996a4d786a533c2df24db8 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Fri, 17 Jan 2014 07:46:33 +0000 Subject: [PATCH] Unit tests for get_url_in_content(). Return false when no content is passed, to match the return value of no links being found. props mdbitz. #26171. git-svn-id: https://develop.svn.wordpress.org/trunk@26972 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 8 +-- .../tests/formatting/GetUrlInContent.php | 49 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/tests/formatting/GetUrlInContent.php diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index c56d82017e..27d64e1dc6 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3770,11 +3770,13 @@ function wp_unslash( $value ) { * @return string The found URL. */ function get_url_in_content( $content ) { - if ( empty( $content ) ) - return ''; + if ( empty( $content ) ) { + return false; + } - if ( preg_match( '/]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) + if ( preg_match( '/]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) { return esc_url_raw( $matches[2] ); + } return false; } diff --git a/tests/phpunit/tests/formatting/GetUrlInContent.php b/tests/phpunit/tests/formatting/GetUrlInContent.php new file mode 100644 index 0000000000..f34bdd855f --- /dev/null +++ b/tests/phpunit/tests/formatting/GetUrlInContent.php @@ -0,0 +1,49 @@ +NO URL CONTENT", + false + ), //no URLs + array ( + '
NO URL CONTENT
', + false + ), // ignore none link elements + array ( + 'ABC
LINK CONTENT
', + "/relative.php" + ), // single link + array ( + 'ABC
LINK CONTENT LINK
', + "/relative.php" + ), // multiple links + array ( + 'ABC
LINK CONTENT
', + "http://example.com/Mr%20WordPress2" + ), // escape link + ); + } + + /** + * Validate the get_url_in_content function + * @dataProvider get_input_output + */ + function test_get_url_in_content( $in_str, $exp_str ) { + $this->assertEquals($exp_str, get_url_in_content( $in_str ) ); + } +}