Skip to content

Tests: Add unit tests for the shortcode registry and helper functions - #15610

Closed
hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66500-shortcodes-tests
Closed

hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66500-shortcodes-tests

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Adds unit tests for four functions in src/wp-includes/shortcodes.php that were not referenced by any test. No behavior is changed.

Functions covered, each in its own test class in tests/phpunit/tests/shortcodes/:

  • remove_all_shortcodes()
    • Removes every registered shortcode, including the ones of core, and the content is no longer processed afterwards.
    • Shortcodes can be registered again afterwards, and calling it with no shortcodes does not fail.
  • get_shortcode_tags_in_content()
    • Returns the registered tags in the order they appear, repeated tags are repeated, and unregistered tags, tags that only start like a registered one and brackets that are not shortcodes are ignored.
    • Returns the tags nested in the content of an enclosing shortcode, at several levels, and does not return the shortcodes that are inside an attribute.
    • Uses the shortcodes registered at the time of the call.
  • apply_shortcodes()
    • Returns the same result as do_shortcode() for self closing, enclosing, escaped, unregistered and empty content.
    • Passes $ignore_html on, so shortcodes inside HTML elements are skipped only when it is true.
  • unescape_invalid_shortcodes()
    • Replaces [ and ] with the square brackets, in text and inside HTML, and leaves any other content and other character references as they are.

The tests change the global $shortcode_tags, so they save and restore it. Escaped shortcodes ([[tag]]) are not covered for get_shortcode_tags_in_content(), because the function currently returns their tag too and that behavior is not asserted.

Testing:

  • vendor/bin/phpunit --filter 'Tests_Shortcodes_'
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Shortcodes_'
  • vendor/bin/phpcs tests/phpunit/tests/shortcodes/

I also temporarily broke the functions in src/wp-includes/shortcodes.php (not clearing the list, not looking at nested content, returning unique tags, not passing $ignore_html on, apply_shortcodes() returning the content as it is, and each of the two bracket replacements) and confirmed that a new test fails for each one. Removing the early return for content without a bracket in get_shortcode_tags_in_content() does not change the result, because the regular expression finds nothing in that case.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Written with AI and manually reviewed by me.


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.

Covers `remove_all_shortcodes()`, `get_shortcode_tags_in_content()`, `apply_shortcodes()` and `unescape_invalid_shortcodes()`, which had no tests.

See #66500.
@github-actions

github-actions Bot commented Nov 6, 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.

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

Props hmbashar.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

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 shortcode unit-test additions have no active review issues requiring resolution.

No open issues 🎉

What changed in this PR

Adds dedicated unit tests for four shortcode registry and helper functions in src/wp-includes/shortcodes.php, without changing production behavior.

Changes:

  • Covers shortcode removal, re-registration, and tag discovery, including nested and repeated tags.
  • Tests apply_shortcodes() output, equivalence to do_shortcode(), and HTML-handling options.
  • Verifies bracket-placeholder replacement and preservation of unrelated content.
File Description
tests/​phpunit/​tests/​shortcodes/​unescapeInvalidShortcodes.php Tests bracket unescaping in text and HTML.
tests/​phpunit/​tests/​shortcodes/​removeAllShortcodes.php Tests clearing the registry and registering shortcodes again.
tests/​phpunit/​tests/​shortcodes/​getShortcodeTagsInContent.php Tests tag discovery, nesting, ordering, and registry changes.
tests/​phpunit/​tests/​shortcodes/​applyShortcodes.php Tests shortcode processing and forwarding of HTML-handling options.

Review effort: Balanced

@hmbashar hmbashar closed this Nov 12, 2026
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.

2 participants