Skip to content

Commit 31a8ce4

Browse files
Tests: Add unit tests for wp_doc_link_parse().
Follow-up to [10607]. Props pbearne. Fixes #65182. git-svn-id: https://develop.svn.wordpress.org/trunk@62369 602fd350-edb4-49c9-b593-d223f7449a82
1 parent aa60d9d commit 31a8ce4

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* @group admin
5+
*
6+
* @covers ::wp_doc_link_parse
7+
*/
8+
class Tests_Admin_Includes_Misc_WpDocLinkParse_Test extends WP_UnitTestCase {
9+
10+
/**
11+
* Tests wp_doc_link_parse() with various PHP content.
12+
*
13+
* @dataProvider data_wp_doc_link_parse
14+
* @ticket 65182
15+
*
16+
* @param string $content The PHP content to parse.
17+
* @param array $expected The expected array of function names.
18+
*/
19+
public function test_wp_doc_link_parse( $content, $expected ) {
20+
$this->assertSame( $expected, wp_doc_link_parse( $content ) );
21+
}
22+
23+
/**
24+
* Data provider for test_wp_doc_link_parse().
25+
*
26+
* @return array<string, array{
27+
* content: string,
28+
* expected: string[],
29+
* }>
30+
*/
31+
public function data_wp_doc_link_parse(): array {
32+
return array(
33+
'empty string' => array(
34+
'content' => '',
35+
'expected' => array(),
36+
),
37+
'null (invalid type)' => array(
38+
'content' => null,
39+
'expected' => array(),
40+
),
41+
'simple function call' => array(
42+
'content' => '<?php get_header(); ?>',
43+
'expected' => array( 'get_header' ),
44+
),
45+
'multiple unique functions' => array(
46+
'content' => '<?php get_header(); wp_footer(); ?>',
47+
'expected' => array( 'get_header', 'wp_footer' ),
48+
),
49+
'duplicate functions' => array(
50+
'content' => '<?php _e( "test" ); _e( "again" ); ?>',
51+
'expected' => array( '_e' ),
52+
),
53+
'function call with space' => array(
54+
'content' => '<?php is_array ( $val ); ?>',
55+
'expected' => array( 'is_array' ),
56+
),
57+
'sorted output' => array(
58+
'content' => '<?php zeta(); alpha(); ?>',
59+
'expected' => array( 'alpha', 'zeta' ),
60+
),
61+
'local function definition' => array(
62+
'content' => '<?php function my_local_func() {} my_local_func(); ?>',
63+
'expected' => array(),
64+
),
65+
'class method call' => array(
66+
'content' => '<?php $obj->my_method(); ?>',
67+
'expected' => array(),
68+
),
69+
'static class method call' => array(
70+
'content' => '<?php MyClass::my_static_method(); ?>',
71+
'expected' => array( 'my_static_method' ), // token_get_all() handles :: differently.
72+
),
73+
'mixed content' => array(
74+
'content' => '<?php
75+
function local_f() {}
76+
local_f();
77+
wp_remote_get();
78+
$o->method();
79+
esc_html( "test" );
80+
?>',
81+
'expected' => array( 'esc_html', 'wp_remote_get' ),
82+
),
83+
);
84+
}
85+
86+
/**
87+
* Tests the `documentation_ignore_functions` filter.
88+
*
89+
* @ticket 65182
90+
*/
91+
public function test_wp_doc_link_parse_filter() {
92+
$filter = function ( $ignore ) {
93+
$ignore[] = 'wp_remote_get';
94+
return $ignore;
95+
};
96+
97+
add_filter( 'documentation_ignore_functions', $filter );
98+
$result = wp_doc_link_parse( '<?php wp_remote_get(); esc_html(); ?>' );
99+
remove_filter( 'documentation_ignore_functions', $filter );
100+
101+
$this->assertSame( array( 'esc_html' ), $result );
102+
}
103+
}

0 commit comments

Comments
 (0)