Skip to content

Tests: Add unit tests for the functions that find and assign the widgets of the sidebars - #15614

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

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

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Adds unit tests for four more functions in src/wp-includes/widgets.php that were not referenced by any test. This is the second group of the work described in the ticket, and is independent from the first one (the functions that read the registered sidebars and widgets, in #15613). No behavior is changed.

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

  • is_active_sidebar()
    • True for a sidebar with widgets, false for one without widgets and for an unknown one, and it follows the widgets that are assigned.
    • An integer is the number of the sidebar (sidebar-N), a string is sanitized like a title.
    • The is_active_sidebar filter receives the result and the index, and its result is returned.
  • is_dynamic_sidebar()
    • True when a registered sidebar has a registered widget, also among widgets that are not registered.
    • False when no sidebar is registered, when the registered sidebars have no widgets, and when the widgets are not registered. The widgets of sidebars that are not registered, and the inactive ones, are ignored.
  • wp_find_widgets_sidebar()
    • Returns the ID of the sidebar of a widget as a string, including the inactive widgets sidebar, the first one when the widget is assigned twice, and null when it is not assigned or there are no sidebars.
    • The ID of the widget is matched exactly, and the sidebars_widgets filter is used.
  • wp_assign_widget_to_sidebar()
    • Adds a widget to a sidebar, also to one that does not have widgets yet, and moves it from the sidebar it was assigned to, or from all of the sidebars when it is assigned to several.
    • Removes the widget from its sidebar when the new sidebar is empty ('', null or false).
    • Does not change the other widgets and sidebars, saves the version of the array, and the functions that read the sidebars use the change.

Assigning a widget to the sidebar it already is in is not covered, because it currently adds the widget a second time and that behavior is not asserted.

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

Testing:

  • vendor/bin/phpunit --filter 'Tests_Widgets_(IsActiveSidebar|IsDynamicSidebar|WpFindWidgetsSidebar|WpAssignWidgetToSidebar)'
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Widgets_(IsActiveSidebar|IsDynamicSidebar|WpFindWidgetsSidebar|WpAssignWidgetToSidebar)'
  • vendor/bin/phpcs tests/phpunit/tests/widgets/

I also temporarily broke these functions in src/wp-includes/widgets.php (the prefix of numeric sidebars, the sanitizing of the ID, the filter, the check for the widgets, the registered widgets and sidebars in is_dynamic_sidebar(), stopping at the first widget, the strict comparison and the string cast in wp_find_widgets_sidebar(), its order, and in wp_assign_widget_to_sidebar() the removal of the old assignment, the loop over duplicates, adding to an empty sidebar and saving the result) and confirmed that a new test fails for each one. Removing the $sidebar_id !== $maybe_sidebar_id condition of wp_assign_widget_to_sidebar() is not caught on purpose, because it only changes the case that is not asserted.

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.

…ets of the sidebars.

Covers `is_active_sidebar()`, `is_dynamic_sidebar()`, `wp_find_widgets_sidebar()` and `wp_assign_widget_to_sidebar()`, 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 identified for this test-only expansion of sidebar and widget coverage.

No open issues 🎉

What changed in this PR

Adds unit tests for four sidebar and widget functions in src/wp-includes/widgets.php, extending the coverage tracked in ticket #66502 and complementing #15613. No production behavior changes.

Changes:

  • Covers sidebar activity checks, widget registration requirements, ID handling, and filters.
  • Tests widget lookup, assignment, movement, removal, and persistence.
  • Isolates test state by resetting sidebar/widget registries and clearing the sidebar-widget cache.
File Description
tests/​phpunit/​tests/​widgets/​wpFindWidgetsSidebar.php Tests exact widget lookup, inactive widgets, duplicate assignments, and filtering.
tests/​phpunit/​tests/​widgets/​wpAssignWidgetToSidebar.php Tests assignment, movement, removal, saved data, and subsequent reads.
tests/​phpunit/​tests/​widgets/​isDynamicSidebar.php Tests dynamic sidebar detection based on registered sidebars and widgets.
tests/​phpunit/​tests/​widgets/​isActiveSidebar.php Tests activity detection, ID normalization, assignment changes, and filters.

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