Skip to content

Commit c586979

Browse files
committed
Tests: Cover adjacent post links and relational output.
Verify previous and next post destinations, title formatting, and relational links on single-post views. Cover display wrappers and empty output at the ends of the post sequence. Developed in: #14649, #14705 Props devmuhib, huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65054 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6572d43 commit c586979

2 files changed

Lines changed: 194 additions & 0 deletions

File tree

‎tests/phpunit/tests/link/getAdjacentPostLink.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
/**
44
* @group link
5+
* @covers ::adjacent_post_link
56
* @covers ::get_adjacent_post_link
67
* @covers ::get_next_post_link
78
* @covers ::get_previous_post_link
9+
* @covers ::next_post_link
10+
* @covers ::previous_post_link
811
*/
912
class Tests_Link_GetAdjacentPostLink extends WP_UnitTestCase {
1013

@@ -97,4 +100,64 @@ public function test_get_previous_post_link_exclude_category() {
97100
$expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">' . $title . '</a>';
98101
$this->assertSame( $expected, $actual );
99102
}
103+
104+
/**
105+
* Tests that adjacent_post_link() echoes get_adjacent_post_link().
106+
*
107+
* @ticket 65819
108+
*/
109+
public function test_adjacent_post_link_echoes_get_adjacent_post_link() {
110+
$previous_title = get_post( $this->post_ids[3] )->post_title;
111+
$expected_prev = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">' . $previous_title . '</a>';
112+
$actual_prev = get_echo( 'adjacent_post_link', array( '&laquo; %link', '%title', false, '', true ) );
113+
114+
$this->assertSame( $expected_prev, $actual_prev );
115+
116+
$next_title = get_post( $this->post_ids[1] )->post_title;
117+
$expected_next = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">' . $next_title . '</a> &raquo;';
118+
$actual_next = get_echo( 'adjacent_post_link', array( '%link &raquo;', '%title', false, '', false ) );
119+
120+
$this->assertSame( $expected_next, $actual_next );
121+
}
122+
123+
/**
124+
* Tests that previous_post_link() echoes get_previous_post_link().
125+
*
126+
* @ticket 65819
127+
*/
128+
public function test_previous_post_link_echoes_get_previous_post_link() {
129+
$title = get_post( $this->post_ids[3] )->post_title;
130+
$expected = '&laquo; <a href="' . home_url( '?p=' . $this->post_ids[3] ) . '" rel="prev">' . $title . '</a>';
131+
$actual = get_echo( 'previous_post_link', array( '&laquo; %link', '%title' ) );
132+
133+
$this->assertSame( $expected, $actual );
134+
}
135+
136+
/**
137+
* Tests that next_post_link() echoes get_next_post_link().
138+
*
139+
* @ticket 65819
140+
*/
141+
public function test_next_post_link_echoes_get_next_post_link() {
142+
$title = get_post( $this->post_ids[1] )->post_title;
143+
$expected = '<a href="' . home_url( '?p=' . $this->post_ids[1] ) . '" rel="next">' . $title . '</a> &raquo;';
144+
$actual = get_echo( 'next_post_link', array( '%link &raquo;', '%title' ) );
145+
146+
$this->assertSame( $expected, $actual );
147+
}
148+
149+
/**
150+
* Tests that adjacent post link display functions output nothing when on terminal posts.
151+
*
152+
* @ticket 65819
153+
*/
154+
public function test_adjacent_post_links_output_nothing_on_terminal_posts() {
155+
// Move to first (most recent) post so there is no next post.
156+
$GLOBALS['post'] = get_post( $this->post_ids[0] );
157+
$this->assertSame( '', get_echo( 'next_post_link' ) );
158+
159+
// Move to last (oldest) post so there is no previous post.
160+
$GLOBALS['post'] = get_post( end( $this->post_ids ) );
161+
$this->assertSame( '', get_echo( 'previous_post_link' ) );
162+
}
100163
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_adjacent_post_rel_link(), adjacent_posts_rel_link(), and adjacent_posts_rel_link_wp_head().
5+
*
6+
* @group link
7+
* @covers ::get_adjacent_post_rel_link
8+
* @covers ::adjacent_posts_rel_link
9+
* @covers ::adjacent_posts_rel_link_wp_head
10+
*/
11+
class Tests_Link_GetAdjacentPostRelLink extends WP_UnitTestCase {
12+
13+
/**
14+
* Post IDs for testing.
15+
*
16+
* @var int[]
17+
*/
18+
protected static $post_ids = array();
19+
20+
/**
21+
* Set up test fixtures before class.
22+
*
23+
* @param WP_UnitTest_Factory $factory
24+
*/
25+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
26+
self::$post_ids[] = $factory->post->create(
27+
array(
28+
'post_title' => 'First Post',
29+
'post_date' => '2026-01-01 10:00:00',
30+
)
31+
);
32+
self::$post_ids[] = $factory->post->create(
33+
array(
34+
'post_title' => 'Second Post',
35+
'post_date' => '2026-01-02 10:00:00',
36+
)
37+
);
38+
self::$post_ids[] = $factory->post->create(
39+
array(
40+
'post_title' => 'Third Post',
41+
'post_date' => '2026-01-03 10:00:00',
42+
)
43+
);
44+
}
45+
46+
/**
47+
* Tests get_adjacent_post_rel_link() for previous post link.
48+
*
49+
* @ticket 65819
50+
*/
51+
public function test_get_adjacent_post_rel_link_previous() {
52+
$this->go_to( get_permalink( self::$post_ids[1] ) );
53+
54+
$link = get_adjacent_post_rel_link( '%title', false, '', true );
55+
56+
$this->assertStringContainsString( "rel='prev'", $link );
57+
$this->assertStringContainsString( "title='First Post'", $link );
58+
$this->assertStringContainsString( "href='" . get_permalink( self::$post_ids[0] ) . "'", $link );
59+
}
60+
61+
/**
62+
* Tests get_adjacent_post_rel_link() for next post link.
63+
*
64+
* @ticket 65819
65+
*/
66+
public function test_get_adjacent_post_rel_link_next() {
67+
$this->go_to( get_permalink( self::$post_ids[1] ) );
68+
69+
$link = get_adjacent_post_rel_link( '%title', false, '', false );
70+
71+
$this->assertStringContainsString( "rel='next'", $link );
72+
$this->assertStringContainsString( "title='Third Post'", $link );
73+
$this->assertStringContainsString( "href='" . get_permalink( self::$post_ids[2] ) . "'", $link );
74+
}
75+
76+
/**
77+
* Tests get_adjacent_post_rel_link() returns null when no adjacent post exists.
78+
*
79+
* @ticket 65819
80+
*/
81+
public function test_get_adjacent_post_rel_link_null_when_no_adjacent_post() {
82+
// First post has no previous post.
83+
$this->go_to( get_permalink( self::$post_ids[0] ) );
84+
$this->assertNull( get_adjacent_post_rel_link( '%title', false, '', true ) );
85+
86+
// Third post has no next post.
87+
$this->go_to( get_permalink( self::$post_ids[2] ) );
88+
$this->assertNull( get_adjacent_post_rel_link( '%title', false, '', false ) );
89+
}
90+
91+
/**
92+
* Tests get_adjacent_post_rel_link() with custom title format replacing %title and %date.
93+
*
94+
* @ticket 65819
95+
*/
96+
public function test_get_adjacent_post_rel_link_custom_title_format() {
97+
update_option( 'date_format', 'Y-m-d' );
98+
$this->go_to( get_permalink( self::$post_ids[1] ) );
99+
100+
$link = get_adjacent_post_rel_link( 'Pre: %title on %date', false, '', true );
101+
102+
$this->assertStringContainsString( "title='Pre: First Post on 2026-01-01'", $link );
103+
}
104+
105+
/**
106+
* Tests adjacent_posts_rel_link_wp_head() outputs relational links on a single post.
107+
*
108+
* @ticket 65819
109+
*/
110+
public function test_adjacent_posts_rel_link_wp_head_outputs_on_single_post() {
111+
$this->set_permalink_structure( '' );
112+
$this->go_to( get_permalink( self::$post_ids[1] ) );
113+
114+
$expected_prev = "<link rel='prev' title='First Post' href='" . home_url( '?p=' . self::$post_ids[0] ) . "' />\n";
115+
$expected_next = "<link rel='next' title='Third Post' href='" . home_url( '?p=' . self::$post_ids[2] ) . "' />\n";
116+
117+
$this->expectOutputString( $expected_prev . $expected_next );
118+
adjacent_posts_rel_link_wp_head();
119+
}
120+
121+
/**
122+
* Tests adjacent_posts_rel_link_wp_head() outputs nothing on the home page.
123+
*
124+
* @ticket 65819
125+
*/
126+
public function test_adjacent_posts_rel_link_wp_head_outputs_nothing_when_not_single() {
127+
$this->go_to( home_url( '/' ) );
128+
$this->expectOutputString( '' );
129+
adjacent_posts_rel_link_wp_head();
130+
}
131+
}

0 commit comments

Comments
 (0)