From 64b1e8f6ead94aa858e1177524e576f04bcca992 Mon Sep 17 00:00:00 2001 From: rejaulalomkhan Date: Mon, 19 Oct 2026 16:05:53 +0600 Subject: [PATCH 1/2] Tests: Add unit test coverage for `permalink_anchor()` Adds unit test coverage for the `permalink_anchor()` template tag introduced in WordPress 0.71, covering: - Default mode output ('id') - Explicit 'id' mode - Case-insensitivity for 'id' and 'title' modes - Mode 'title' with special characters and title sanitization - Fallback to 'id' mode for unrecognized or arbitrary mode arguments Follows up to #65819. --- tests/phpunit/tests/link/permalinkAnchor.php | 153 +++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 tests/phpunit/tests/link/permalinkAnchor.php diff --git a/tests/phpunit/tests/link/permalinkAnchor.php b/tests/phpunit/tests/link/permalinkAnchor.php new file mode 100644 index 0000000000000..abb2da073b15b --- /dev/null +++ b/tests/phpunit/tests/link/permalinkAnchor.php @@ -0,0 +1,153 @@ +post->create( + array( + 'post_title' => 'Test Post Title', + ) + ); + } + + /** + * Sets up the global $post object before each test method. + */ + public function set_up() { + parent::set_up(); + $GLOBALS['post'] = get_post( self::$post_id ); + } + + /** + * Tears down the global $post object after each test method. + */ + public function tear_down() { + unset( $GLOBALS['post'] ); + parent::tear_down(); + } + + /** + * Tests that permalink_anchor() outputs the post ID anchor by default when no argument is passed. + * + * @ticket 65819 + */ + public function test_permalink_anchor_default_mode_id() { + $expected = ''; + + $this->assertSame( $expected, get_echo( 'permalink_anchor' ) ); + } + + /** + * Tests that permalink_anchor() outputs the post ID anchor when explicitly passing 'id'. + * + * @ticket 65819 + */ + public function test_permalink_anchor_explicit_mode_id() { + $expected = ''; + + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'id' ) ) ); + } + + /** + * Tests that permalink_anchor() handles mode case-insensitively for 'id'. + * + * @ticket 65819 + */ + public function test_permalink_anchor_mode_id_case_insensitive() { + $expected = ''; + + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'ID' ) ) ); + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'Id' ) ) ); + } + + /** + * Tests that permalink_anchor() outputs the sanitized title anchor when passing 'title'. + * + * @ticket 65819 + */ + public function test_permalink_anchor_mode_title() { + $expected = ''; + + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'title' ) ) ); + } + + /** + * Tests that permalink_anchor() handles mode case-insensitively for 'title'. + * + * @ticket 65819 + */ + public function test_permalink_anchor_mode_title_case_insensitive() { + $expected = ''; + + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'TITLE' ) ) ); + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'Title' ) ) ); + } + + /** + * Tests that permalink_anchor() properly sanitizes titles containing special characters. + * + * @ticket 65819 + */ + public function test_permalink_anchor_mode_title_with_special_characters() { + $special_post_id = self::factory()->post->create( + array( + 'post_title' => 'Special Characters & Symbols: 100% Valid!', + ) + ); + + $GLOBALS['post'] = get_post( $special_post_id ); + + $expected_slug = sanitize_title( 'Special Characters & Symbols: 100% Valid!' ) . '-' . $special_post_id; + $expected = ''; + + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'title' ) ) ); + } + + /** + * Tests that permalink_anchor() falls back to id mode for unrecognized mode arguments. + * + * @ticket 65819 + * + * @dataProvider data_unrecognized_modes + * + * @param string $mode Mode to pass. + */ + public function test_permalink_anchor_unrecognized_mode_falls_back_to_id( $mode ) { + $expected = ''; + + $this->assertSame( $expected, get_echo( 'permalink_anchor', array( $mode ) ) ); + } + + /** + * Data provider for test_permalink_anchor_unrecognized_mode_falls_back_to_id(). + * + * @return array[] + */ + public static function data_unrecognized_modes() { + return array( + 'empty string' => array( '' ), + 'arbitrary string' => array( 'custom' ), + 'invalid mode slug' => array( 'slug' ), + 'numeric string mode' => array( '123' ), + ); + } +} From 311e0761047446d07e65d56bbe445334fe926b64 Mon Sep 17 00:00:00 2001 From: rejaulalomkhan Date: Tue, 20 Oct 2026 06:57:42 +0600 Subject: [PATCH 2/2] Tests: Use a literal expected slug in the permalink_anchor() special characters test. Building the expectation with sanitize_title() repeated the production dependency, so a change in its punctuation handling would change both sides together. See #65819. --- tests/phpunit/tests/link/permalinkAnchor.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/link/permalinkAnchor.php b/tests/phpunit/tests/link/permalinkAnchor.php index abb2da073b15b..5d9d05091804d 100644 --- a/tests/phpunit/tests/link/permalinkAnchor.php +++ b/tests/phpunit/tests/link/permalinkAnchor.php @@ -116,8 +116,7 @@ public function test_permalink_anchor_mode_title_with_special_characters() { $GLOBALS['post'] = get_post( $special_post_id ); - $expected_slug = sanitize_title( 'Special Characters & Symbols: 100% Valid!' ) . '-' . $special_post_id; - $expected = ''; + $expected = ''; $this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'title' ) ) ); }