Skip to content

Commit 880efab

Browse files
committed
Tests: Cover the_author_posts_link().
Verify echoed author links, permalink formats, empty author context, deprecated arguments, and filtered output. Developed in: #14431 Props rejaulalomkhan. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64524 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ffa4c0a commit 880efab

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
/**
4+
* Tests for the_author_posts_link().
5+
*
6+
* @group author
7+
* @group user
8+
*
9+
* @covers ::the_author_posts_link
10+
*/
11+
class Tests_User_TheAuthorPostsLink extends WP_UnitTestCase {
12+
13+
/**
14+
* Author user ID.
15+
*
16+
* @var int
17+
*/
18+
private static $author_id = 0;
19+
20+
/**
21+
* Post ID authored by self::$author_id.
22+
*
23+
* @var int
24+
*/
25+
private static $post_id = 0;
26+
27+
/**
28+
* Creates shared author and post fixtures.
29+
*
30+
* @param WP_UnitTest_Factory $factory Test factory.
31+
*/
32+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
33+
self::$author_id = $factory->user->create(
34+
array(
35+
'role' => 'author',
36+
'user_login' => 'test_author',
37+
'display_name' => 'Test Author',
38+
'description' => 'test_author',
39+
'user_url' => 'http://example.com',
40+
)
41+
);
42+
43+
self::$post_id = $factory->post->create(
44+
array(
45+
'post_author' => self::$author_id,
46+
'post_status' => 'publish',
47+
)
48+
);
49+
}
50+
51+
/**
52+
* Sets up the global post data before each test.
53+
*/
54+
public function set_up() {
55+
parent::set_up();
56+
57+
setup_postdata( get_post( self::$post_id ) );
58+
}
59+
60+
/**
61+
* Cleans up global state after each test.
62+
*/
63+
public function tear_down() {
64+
unset( $GLOBALS['authordata'] );
65+
66+
parent::tear_down();
67+
}
68+
69+
/**
70+
* Tests that the_author_posts_link() outputs the author posts link.
71+
*
72+
* @ticket 65819
73+
*/
74+
public function test_outputs_author_posts_link() {
75+
$output = get_echo( 'the_author_posts_link' );
76+
77+
$this->assertSame( get_the_author_posts_link(), $output );
78+
$this->assertStringContainsString( 'rel="author"', $output );
79+
$this->assertStringContainsString( '>Test Author</a>', $output );
80+
$this->assertStringContainsString( 'author=' . self::$author_id, $output );
81+
}
82+
83+
/**
84+
* Tests that the_author_posts_link() outputs pretty permalinks when enabled.
85+
*
86+
* @ticket 65819
87+
*/
88+
public function test_outputs_author_posts_link_with_pretty_permalinks() {
89+
$this->set_permalink_structure( '/%postname%/' );
90+
91+
$user = get_userdata( self::$author_id );
92+
$output = get_echo( 'the_author_posts_link' );
93+
$expected = get_the_author_posts_link();
94+
95+
$this->set_permalink_structure( '' );
96+
97+
$this->assertSame( $expected, $output );
98+
$this->assertStringContainsString( '/author/' . $user->user_nicename . '/', $output );
99+
}
100+
101+
/**
102+
* Tests that the_author_posts_link() outputs nothing when authordata is not set.
103+
*
104+
* @ticket 65819
105+
*/
106+
public function test_outputs_empty_string_when_authordata_is_not_set() {
107+
unset( $GLOBALS['authordata'] );
108+
109+
$this->assertSame( '', get_echo( 'the_author_posts_link' ) );
110+
}
111+
112+
/**
113+
* Tests that passing a deprecated argument triggers a deprecation notice.
114+
*
115+
* @ticket 65819
116+
* @expectedDeprecated the_author_posts_link
117+
*/
118+
public function test_deprecated_argument() {
119+
get_echo( 'the_author_posts_link', array( 'deprecated' ) );
120+
}
121+
122+
/**
123+
* Tests that the_author_posts_link() reflects the 'the_author_posts_link' filter.
124+
*
125+
* @ticket 65819
126+
*/
127+
public function test_applies_the_author_posts_link_filter() {
128+
$filter = static function () {
129+
return '<a href="https://example.org/custom-author">Custom Author Link</a>';
130+
};
131+
132+
add_filter( 'the_author_posts_link', $filter );
133+
134+
$output = get_echo( 'the_author_posts_link' );
135+
136+
remove_filter( 'the_author_posts_link', $filter );
137+
138+
$this->assertSame( '<a href="https://example.org/custom-author">Custom Author Link</a>', $output );
139+
}
140+
}

0 commit comments

Comments
 (0)