Repository navigation
Tests: Add unit test coverage for permalink_anchor() #14486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rejaulalomkhan
wants to merge
2
commits into
WordPress:trunk
Choose a base branch
from
rejaulalomkhan:tests/permalink-anchor
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the `permalink_anchor()` function. | ||
| * | ||
| * @group link | ||
| * @group post | ||
| * | ||
| * @covers ::permalink_anchor | ||
| */ | ||
| class Tests_Link_PermalinkAnchor extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Test post ID. | ||
| * | ||
| * @var int | ||
| */ | ||
| protected static $post_id; | ||
|
|
||
| /** | ||
| * Sets up shared fixtures for the test class. | ||
| * | ||
| * @param WP_UnitTest_Factory $factory Unit test factory. | ||
| */ | ||
| public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | ||
| self::$post_id = $factory->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 = '<a id="post-' . self::$post_id . '"></a>'; | ||
|
|
||
| $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 = '<a id="post-' . self::$post_id . '"></a>'; | ||
|
|
||
| $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 = '<a id="post-' . self::$post_id . '"></a>'; | ||
|
|
||
| $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 = '<a id="test-post-title-' . self::$post_id . '"></a>'; | ||
|
|
||
| $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 = '<a id="test-post-title-' . self::$post_id . '"></a>'; | ||
|
|
||
| $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 = '<a id="special-characters-symbols-100-valid-' . $special_post_id . '"></a>'; | ||
|
|
||
| $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 = '<a id="post-' . self::$post_id . '"></a>'; | ||
|
|
||
| $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' ), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.