Skip to content

test for wp_maybe_load_widgets - #3682

Open
pbearne wants to merge 10 commits into
WordPress:trunkfrom
pbearne:57201_wp_maybe_load_widgets
Open

pbearne wants to merge 10 commits into
WordPress:trunkfrom
pbearne:57201_wp_maybe_load_widgets

Conversation

@pbearne

@pbearne pbearne commented Nov 25, 2022

Copy link
Copy Markdown

@costdev costdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @pbearne and for working to implement standards early to save time in reviewing / implementing review feedback! Much appreciated and look forward to seeing that in future PRs!

I've left some thoughts below which should help with an unmet standard (message parameter) and test stability. 🙂

Comment thread tests/phpunit/tests/functions/wpMaybeLoadWidgets.php Outdated
* @ticket 57201
*/
public function test_wp_maybe_load_widgets() {
$this->assertFalse( class_exists( 'WP_Nav_Menu_Widget' ) );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of thoughts:

  1. Test methods with multiple assertions should have a message parameter to explain what went wrong.

For similar assertions (i.e. multiple assertion of class_exists()), the message can be:

  • WP_Nav_Menu_Widget should not exist before doing something.
  • WP_Nav_Menu_Widget should not exist after doing something.
  • WP_Nav_Menu_Widget should exist after doing something.
  1. It may be best to split this into two test methods:
  • test_wp_maybe_load_widgets_should_respect_falsy_load_default_widgets_filter() - Use a data provider with falsy values to send to the filter hook via static closure, then assertFalse() for class_exists() and has_action().
  • test_wp_maybe_load_widgets_should_load_widgets() - assertFalse() for class_exists() and has_action(), then call wp_maybe_load_widgets(), then assertTrue() on class_exists() and has_action() (all assertions should have the message parameter)

Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com>
@github-actions

github-actions Bot commented Jun 26, 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. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @pbearne@git.wordpress.org.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Props pbearne, costdev, rejaulalomkhan.

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.

@rejaulalomkhan

Copy link
Copy Markdown

Thanks for picking this up again and moving it to the new test structure, @pbearne! I ran it against trunk to check the open points from the earlier review:

Patch Testing Report

Patch tested: #3682 (head 1acb423)

Environment

  • WordPress: 7.2-alpha-63166-src
  • PHP: 8.3.35
  • Server: wordpress-develop local Docker environment (nginx), PHPUnit 9.6.38
  • Database: MySQL 9.7.2
  • Browser: N/A (unit tests)
  • OS: Windows 11
  • Theme: N/A
  • MU Plugins: None activated
  • Plugins: None activated

Steps taken

  1. Ran WpMaybeLoadWidgetsTest.php on single site and multisite: passes, but with 2 assertions. WP_Nav_Menu_Widget is already loaded during bootstrap, so the if ( ! class_exists( 'WP_Nav_Menu_Widget' ) ) branch never runs and the load_default_widgets path isn't covered.
  2. Removed the test's own wp_maybe_load_widgets() call: the test still passes, since the class and the _admin_menu hook were both set up during bootstrap.
  3. Made the function ignore the filter (if ( false ) instead of ! apply_filters( 'load_default_widgets', true )): the test still passes.
  4. ❌ Patch is failing: the test passes regardless of what wp_maybe_load_widgets() does at call time.

Expected result

  • The tests fail when the function stops hooking wp_widgets_add_menu, or when it ignores a falsy load_default_widgets.

Additional Notes

  • The hook part can be tested by removing it first, since hooks are restored after each test. This also follows the split suggested in the earlier review (a falsy data provider for the filter):

    public function test_should_hook_widgets_menu() {
    	// The widgets library is loaded during bootstrap, so reset the hook first.
    	remove_action( '_admin_menu', 'wp_widgets_add_menu' );
    
    	wp_maybe_load_widgets();
    
    	$this->assertSame( 10, has_action( '_admin_menu', 'wp_widgets_add_menu' ) );
    }
    
    /**
     * @dataProvider data_falsy_values
     */
    public function test_should_not_hook_widgets_menu_when_filter_is_falsy( $value ) {
    	remove_action( '_admin_menu', 'wp_widgets_add_menu' );
    	add_filter(
    		'load_default_widgets',
    		static function () use ( $value ) {
    			return $value;
    		}
    	);
    
    	wp_maybe_load_widgets();
    
    	$this->assertFalse( has_action( '_admin_menu', 'wp_widgets_add_menu' ) );
    }
    
    public static function data_falsy_values() {
    	return array(
    		'false'        => array( false ),
    		'0'            => array( 0 ),
    		'empty string' => array( '' ),
    		'null'         => array( null ),
    	);
    }

    These pass on single site, multisite and with --order-by=reverse. Ignoring the filter fails 4 of the 5, and dropping the add_action() fails the first one. Loading default-widgets.php itself can't be checked this way, since the class is always loaded first.

  • AI assistance: Claude Code (Claude Opus 5.5) was used to run the tests and draft the suggested tests; I reviewed the results.

@pbearne

pbearne commented Oct 14, 2026

Copy link
Copy Markdown
Author

@rejaulalomkhan I have updated the dcode with your suggestions

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.

3 participants