Skip to content

Tests: Add unit test coverage for permalink_anchor() - #14486

Open
rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/permalink-anchor
Open

rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/permalink-anchor

Conversation

@rejaulalomkhan

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/65819

Description

Adds dedicated unit tests for permalink_anchor() in tests/phpunit/tests/link/permalinkAnchor.php. Prior to this PR, permalink_anchor() (introduced in WordPress 0.71) had no direct unit test coverage in WordPress Core.

Tests added

  • test_permalink_anchor_default_mode_id(): Verifies that permalink_anchor() outputs the post ID anchor (<a id="post-{ID}"></a>) by default when called without arguments.
  • test_permalink_anchor_explicit_mode_id(): Verifies output when explicitly passing mode 'id'.
  • test_permalink_anchor_mode_id_case_insensitive(): Verifies that mode 'id' is handled case-insensitively ('ID', 'Id').
  • test_permalink_anchor_mode_title(): Verifies output when passing mode 'title', asserting the sanitized post title anchor format (<a id="{slug}-{ID}"></a>).
  • test_permalink_anchor_mode_title_case_insensitive(): Verifies that mode 'title' is handled case-insensitively ('TITLE', 'Title').
  • test_permalink_anchor_mode_title_with_special_characters(): Verifies title sanitization when the post title contains special characters, ampersands, and punctuation.
  • test_permalink_anchor_unrecognized_mode_falls_back_to_id(): Uses a data provider (data_unrecognized_modes) covering empty string '', arbitrary string 'custom', invalid slug 'slug', and numeric string '123', verifying fallback to 'id' mode.

Quality Gate & Verification

  • PHPUnit (single site): 10 tests, 12 assertions passed (0.33s).
  • PHPUnit (multisite): 10 tests, 12 assertions passed (0.45s).
  • PHPUnit (randomized order): 10 tests, 12 assertions passed (seed 1792404252).
  • PHPCS: 0 errors, 0 warnings against WordPress standards (vendor/bin/phpcs tests/phpunit/tests/link/permalinkAnchor.php).
  • Mutation check: Temporarily modified anchor prefix in src/wp-includes/link-template.php; verified 7 tests failed as expected; source restored cleanly.

Use of AI Tools

Question Answer
Did you use AI tools to find the bug or create the patch? Yes
Which AI tools did you use? Antigravity with Gemini 2.5 Pro
How much of the code was generated by AI? Most of the code
Did you verify the patch locally? Yes, verified through single-site, multisite, randomized PHPUnit execution, mutation verification, and PHPCS.

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.
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props rejaulalomkhan, mukesh27, lancewillett.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused test-only changes match existing behavior, and no blocking issues were identified.

No open issues 🎉

What changed in this PR

Adds dedicated unit coverage for permalink_anchor() without changing production behavior.

Changes:

  • Tests default, explicit, and case-insensitive anchor modes.
  • Covers title sanitization and fallback for unrecognized modes.
File Description
tests/​phpunit/​tests/​link/​permalinkAnchor.php Adds anchor-output tests with shared fixtures and invalid-mode data cases.

Review effort: Balanced

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local tests and PHPCS pass. Please make the small test-quality adjustment below before landing.

AI review · gpt-6

$expected = '<a id="' . $expected_slug . '"></a>';

$this->assertSame( $expected, get_echo( 'permalink_anchor', array( 'title' ) ) );
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a literal expected slug for this fixed special-character title, appending only the generated post ID. Calling sanitize_title() to construct the expectation repeats the production dependency: a change in its handling of punctuation would change both actual and expected output together. The literal expectation will make this particular case protect the output described by its name.

…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.
@rejaulalomkhan

Copy link
Copy Markdown
Author

@lancewillett Thanks for the review! Addressed in 311e076:

  • The special-characters test now expects the literal special-characters-symbols-100-valid-<post ID> instead of building it with sanitize_title().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

4 participants