Skip to content

Tests: Improve coverage for url_shorten() and test the previously unexercised $length parameter#12558

Open
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:tests/url-shorten-length-param
Open

Tests: Improve coverage for url_shorten() and test the previously unexercised $length parameter#12558
Soean wants to merge 1 commit into
WordPress:trunkfrom
Soean:tests/url-shorten-length-param

Conversation

@Soean

@Soean Soean commented Jul 16, 2026

Copy link
Copy Markdown
Member

This refactors the tests in tests/phpunit/tests/formatting/urlShorten.php and adds coverage that was missing.

Misplaced test argument

The case meant to test the optional $length parameter passed the length as the third argument to assertSame():

$this->assertSame( 'wordpress.org/about/philosophy/#…', url_shorten( 'http://wordpress.org/about/philosophy/#decisions' ), 31 );

That third argument is assertSame()'s $message, not the URL length. So url_shorten() ran with its default length of 35, the expected value happened to match the default output, and the assertion passed — while the $length parameter was never actually tested.

Changes

  • Convert the tests to @dataProvider data sets with descriptive keys, so a failure points to the exact case.
  • Separate the default-behaviour cases from the custom-length cases.
  • Add real coverage for $length: the URL is kept when the length exceeds or equals the cleaned URL (pinning the strict > comparison) and shortened when it is smaller.
  • Add a comment clarifying that a shortened URL is cut to ( $length - 3 ) characters before … is appended.

No production code is changed — this is a test-only improvement.

All tests pass locally via npm run test:php -- --filter Tests_Formatting_UrlShorten (9 tests, 9 assertions).

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

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 soean.

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

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant