|
| 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