Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions tests/phpunit/tests/admin/includes/misc/wpDocLinkParse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Test wp_doc_link_parse().
*
* @group admin
*
* @covers ::wp_doc_link_parse
*/
class Tests_wp_doc_link_parse extends WP_UnitTestCase {

/**
* Tests wp_doc_link_parse() with various PHP content.
*
* @dataProvider data_wp_doc_link_parse
* @ticket 65182
*
* @param string $content The PHP content to parse.
* @param array $expected The expected array of function names.
*/
public function test_wp_doc_link_parse( $content, $expected ) {
$this->assertSame( $expected, wp_doc_link_parse( $content ) );
}

/**
* Data provider for test_wp_doc_link_parse.
*
* @return array<string, array{
* content: string,
* expected: string[],
* }>
*/
public function data_wp_doc_link_parse(): array {
return array(
'empty string' => array(
'content' => '',
'expected' => array(),
),
'null (invalid type)' => array(
'content' => null,
'expected' => array(),
),
'simple function call' => array(
'content' => '<?php get_header(); ?>',
'expected' => array( 'get_header' ),
),
'multiple unique functions' => array(
'content' => '<?php get_header(); wp_footer(); ?>',
'expected' => array( 'get_header', 'wp_footer' ),
),
'duplicate functions' => array(
'content' => '<?php _e("test"); _e("again"); ?>',
'expected' => array( '_e' ),
),
'sorted output' => array(
'content' => '<?php zeta(); alpha(); ?>',
'expected' => array( 'alpha', 'zeta' ),
),
'local function definition' => array(
'content' => '<?php function my_local_func() {} my_local_func(); ?>',
'expected' => array(),
),
'class method call' => array(
'content' => '<?php $obj->my_method(); ?>',
'expected' => array(),
),
'static class method call' => array(
'content' => '<?php MyClass::my_static_method(); ?>',
'expected' => array( 'my_static_method' ), // token_get_all handles :: differently.
),
'mixed content' => array(
'content' => '<?php
function local_f() {}
local_f();
wp_remote_get();
$o->method();
esc_html( "test" );
?>',
'expected' => array( 'esc_html', 'wp_remote_get' ),
),
'function call with space' => array(
'content' => '<?php is_array ( $val ); ?>',
'expected' => array( 'is_array' ),
),
);
}

/**
* Tests the documentation_ignore_functions filter.
*
* @ticket 65182
*/
public function test_wp_doc_link_parse_filter() {
$filter = function ( $ignore ) {
$ignore[] = 'wp_remote_get';
return $ignore;
};

add_filter( 'documentation_ignore_functions', $filter );
$result = wp_doc_link_parse( '<?php wp_remote_get(); esc_html(); ?>' );
remove_filter( 'documentation_ignore_functions', $filter );

$this->assertSame( array( 'esc_html' ), $result );
}
}
Loading