Skip to content

Tests: Add unit tests for the sidebar and widget lookup functions - #15613

Closed
hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66502-widgets-lookup-tests
Closed

hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66502-widgets-lookup-tests

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Adds unit tests for five functions in src/wp-includes/widgets.php that were not referenced by any test. This is the first of two groups of the work described in the ticket: it covers the functions that read the registered sidebars and widgets. is_active_sidebar(), is_dynamic_sidebar(), wp_find_widgets_sidebar() and wp_assign_widget_to_sidebar() follow in a second pull request. No behavior is changed.

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

  • is_registered_sidebar()
    • True for a registered sidebar, false for another one, when none is registered and after the sidebar is unregistered.
    • The ID is matched exactly (case sensitive), and numeric IDs are accepted.
  • wp_widget_description()
    • Returns the description of the requested widget, escaped for HTML.
    • Returns an empty string for an empty description, and null when the widget has no description, is not registered, or when the ID is not a scalar.
  • wp_sidebar_description()
    • Returns the description of the requested sidebar, with the markup and attributes that are not allowed removed, and keeps the markup that the wp_kses_allowed_html filter allows for the sidebar_description context.
    • Returns an empty string for a sidebar without a description, and null when the sidebar is not registered or when the ID is not a scalar.
  • wp_get_sidebar()
    • Returns the registered data of the requested sidebar among several ones, and null for a sidebar that is not registered.
    • Returns the inactive widgets sidebar even when it is not registered, and the registered one when it is.
    • The ID is compared strictly.
  • wp_get_widget_defaults()
    • Returns an empty list for each registered sidebar, in the order they are registered, and an empty array when none is registered. The widgets assigned to the sidebars are not included.

The tests reset the registered sidebars and widgets, and restore them afterwards.

Testing:

  • vendor/bin/phpunit --filter 'Tests_Widgets_(IsRegisteredSidebar|WpWidgetDescription|WpSidebarDescription|WpGetSidebar|WpGetWidgetDefaults)'
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Widgets_(IsRegisteredSidebar|WpWidgetDescription|WpSidebarDescription|WpGetSidebar|WpGetWidgetDefaults)'
  • vendor/bin/phpcs tests/phpunit/tests/widgets/

I also temporarily broke these functions in src/wp-includes/widgets.php (always returning true, the escaping of the widget description, the check of a scalar ID in both description functions, the call to wp_kses() and its context, the strict comparison of the ID, the fallback for the inactive widgets sidebar and its priority over registered sidebars, and the contents and the order of the defaults) and confirmed that a new test fails for each one.

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

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 `is_registered_sidebar()`, `wp_widget_description()`, `wp_sidebar_description()`, `wp_get_sidebar()` and `wp_get_widget_defaults()`, which had no tests.

See #66502.
@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

No active review issues are reported for the sidebar and widget lookup test additions.

No open issues 🎉

What changed in this PR

Adds unit tests for five sidebar and widget lookup functions in src/wp-includes/widgets.php, covering the first group of work for ticket #66502. No production behavior changes.

Changes:

  • Covers registration lookups, description escaping and filtering, sidebar retrieval, and widget defaults.
  • Tests edge cases including missing registrations, non-scalar IDs, strict ID matching, and the inactive-sidebar fallback.
  • Saves and restores registered sidebar and widget state around each test.
File Description
tests/​phpunit/​tests/​widgets/​wpWidgetDescription.php Tests widget descriptions, HTML escaping, and missing or invalid inputs.
tests/​phpunit/​tests/​widgets/​wpSidebarDescription.php Tests sidebar descriptions, markup filtering, and missing or invalid inputs.
tests/​phpunit/​tests/​widgets/​wpGetWidgetDefaults.php Tests empty defaults, registration order, and exclusion of assigned widgets.
tests/​phpunit/​tests/​widgets/​wpGetSidebar.php Tests sidebar retrieval, strict ID matching, and inactive-sidebar fallback precedence.
tests/​phpunit/​tests/​widgets/​isRegisteredSidebar.php Tests registration status, unregistration, case sensitivity, and numeric IDs.

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