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
This commit is contained in:
parent
238f9d2b0a
commit
9f5c78b676
|
@ -3770,11 +3770,13 @@ function wp_unslash( $value ) {
|
||||||
* @return string The found URL.
|
* @return string The found URL.
|
||||||
*/
|
*/
|
||||||
function get_url_in_content( $content ) {
|
function get_url_in_content( $content ) {
|
||||||
if ( empty( $content ) )
|
if ( empty( $content ) ) {
|
||||||
return '';
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) )
|
if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
|
||||||
return esc_url_raw( $matches[2] );
|
return esc_url_raw( $matches[2] );
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group formatting
|
||||||
|
*/
|
||||||
|
class Tests_Formatting_GetUrlInContent extends WP_UnitTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL Content Data Provider
|
||||||
|
*
|
||||||
|
* array ( input_txt, converted_output_txt )
|
||||||
|
*/
|
||||||
|
public function get_input_output() {
|
||||||
|
return array (
|
||||||
|
array (
|
||||||
|
"",
|
||||||
|
false
|
||||||
|
), //empty content
|
||||||
|
array (
|
||||||
|
"<div>NO URL CONTENT</div>",
|
||||||
|
false
|
||||||
|
), //no URLs
|
||||||
|
array (
|
||||||
|
'<div href="/relative.php">NO URL CONTENT</div>',
|
||||||
|
false
|
||||||
|
), // ignore none link elements
|
||||||
|
array (
|
||||||
|
'ABC<div><a href="/relative.php">LINK</a> CONTENT</div>',
|
||||||
|
"/relative.php"
|
||||||
|
), // single link
|
||||||
|
array (
|
||||||
|
'ABC<div><a href="/relative.php">LINK</a> CONTENT <a href="/suppress.php">LINK</a></div>',
|
||||||
|
"/relative.php"
|
||||||
|
), // multiple links
|
||||||
|
array (
|
||||||
|
'ABC<div><a href="http://example.com/Mr%20WordPress 2">LINK</a> CONTENT </div>',
|
||||||
|
"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 ) );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue