|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the `permalink_anchor()` function. |
| 4 | + * |
| 5 | + * @group link |
| 6 | + * @group post |
| 7 | + * |
| 8 | + * @covers ::permalink_anchor |
| 9 | + */ |
| 10 | +class Tests_Link_PermalinkAnchor extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Test post ID. |
| 14 | + * |
| 15 | + * @var int |
| 16 | + */ |
| 17 | + protected static $post_id; |
| 18 | + |
| 19 | + /** |
| 20 | + * Sets up shared fixtures for the test class. |
| 21 | + * |
| 22 | + * @param WP_UnitTest_Factory $factory Unit test factory. |
| 23 | + */ |
| 24 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 25 | + self::$post_id = $factory->post->create( |
| 26 | + array( |
| 27 | + 'post_title' => 'Test Post Title', |
| 28 | + ) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Sets up the global $post object before each test method. |
| 34 | + */ |
| 35 | + public function set_up() { |
| 36 | + parent::set_up(); |
| 37 | + $GLOBALS['post'] = get_post( self::$post_id ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Tears down the global $post object after each test method. |
| 42 | + */ |
| 43 | + public function tear_down() { |
| 44 | + unset( $GLOBALS['post'] ); |
| 45 | + parent::tear_down(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Tests that permalink_anchor() outputs the post ID anchor by default when no argument is passed. |
| 50 | + * |
| 51 | + * @ticket 65819 |
| 52 | + */ |
| 53 | + public function test_permalink_anchor_default_mode_id() { |
| 54 | + $expected = '<a id="post-' . self::$post_id . '"></a>'; |
| 55 | + |
| 56 | + $this->assertSame( $expected, get_echo( 'permalink_anchor' ) ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Tests that permalink_anchor() outputs the post ID anchor when explicitly passing 'id'. |
| 61 | + * |
| 62 | + * @ticket 65819 |
| 63 | + */ |
| 64 | + public function test_permalink_anchor_explicit_mode_id() { |
| 65 | + $expected = '<a id="post-' . self::$post_id . '"></a>'; |
| 66 | + |
| 67 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'id' ) ) ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Tests that permalink_anchor() handles mode case-insensitively for 'id'. |
| 72 | + * |
| 73 | + * @ticket 65819 |
| 74 | + */ |
| 75 | + public function test_permalink_anchor_mode_id_case_insensitive() { |
| 76 | + $expected = '<a id="post-' . self::$post_id . '"></a>'; |
| 77 | + |
| 78 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'ID' ) ) ); |
| 79 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'Id' ) ) ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Tests that permalink_anchor() outputs the sanitized title anchor when passing 'title'. |
| 84 | + * |
| 85 | + * @ticket 65819 |
| 86 | + */ |
| 87 | + public function test_permalink_anchor_mode_title() { |
| 88 | + $expected = '<a id="test-post-title-' . self::$post_id . '"></a>'; |
| 89 | + |
| 90 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'title' ) ) ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Tests that permalink_anchor() handles mode case-insensitively for 'title'. |
| 95 | + * |
| 96 | + * @ticket 65819 |
| 97 | + */ |
| 98 | + public function test_permalink_anchor_mode_title_case_insensitive() { |
| 99 | + $expected = '<a id="test-post-title-' . self::$post_id . '"></a>'; |
| 100 | + |
| 101 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'TITLE' ) ) ); |
| 102 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'Title' ) ) ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Tests that permalink_anchor() properly sanitizes titles containing special characters. |
| 107 | + * |
| 108 | + * @ticket 65819 |
| 109 | + */ |
| 110 | + public function test_permalink_anchor_mode_title_with_special_characters() { |
| 111 | + $special_post_id = self::factory()->post->create( |
| 112 | + array( |
| 113 | + 'post_title' => 'Special Characters & Symbols: 100% Valid!', |
| 114 | + ) |
| 115 | + ); |
| 116 | + |
| 117 | + $GLOBALS['post'] = get_post( $special_post_id ); |
| 118 | + |
| 119 | + $expected = '<a id="special-characters-symbols-100-valid-' . $special_post_id . '"></a>'; |
| 120 | + |
| 121 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'title' ) ) ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Tests that permalink_anchor() falls back to id mode for unrecognized mode arguments. |
| 126 | + * |
| 127 | + * @ticket 65819 |
| 128 | + * |
| 129 | + * @dataProvider data_unrecognized_modes |
| 130 | + * |
| 131 | + * @param string $mode Mode to pass. |
| 132 | + */ |
| 133 | + public function test_permalink_anchor_unrecognized_mode_falls_back_to_id( $mode ) { |
| 134 | + $expected = '<a id="post-' . self::$post_id . '"></a>'; |
| 135 | + |
| 136 | + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( $mode ) ) ); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Data provider for test_permalink_anchor_unrecognized_mode_falls_back_to_id(). |
| 141 | + * |
| 142 | + * @return array[] |
| 143 | + */ |
| 144 | + public static function data_unrecognized_modes() { |
| 145 | + return array( |
| 146 | + 'empty string' => array( '' ), |
| 147 | + 'arbitrary string' => array( 'custom' ), |
| 148 | + 'invalid mode slug' => array( 'slug' ), |
| 149 | + 'numeric string mode' => array( '123' ), |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments