Repository navigation
Tests: Add unit test coverage for permalink_anchor() - #14486
rejaulalomkhan wants to merge 2 commits into
Conversation
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.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
🟢 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
left a comment
There was a problem hiding this comment.
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' ) ) ); | ||
| } |
There was a problem hiding this comment.
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.
|
@lancewillett Thanks for the review! Addressed in 311e076:
|
Trac ticket: https://core.trac.wordpress.org/ticket/65819
Description
Adds dedicated unit tests for
permalink_anchor()intests/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 thatpermalink_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
vendor/bin/phpcs tests/phpunit/tests/link/permalinkAnchor.php).src/wp-includes/link-template.php; verified 7 tests failed as expected; source restored cleanly.Use of AI Tools