Skip to content

Tests: Add unit test coverage for WP_Widget_Archives - #14185

Open
rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/widget-archives
Open

rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/widget-archives

Conversation

@rejaulalomkhan

Copy link
Copy Markdown

Description

Adds a test class for WP_Widget_Archives, which had no tests. The tests cover the registration settings, default and custom title output, the widget_title filter arguments, the HTML5 navigation wrapper vs. XHTML format, dropdown mode output and the widget_archives_dropdown_args filter, settings sanitization in update(), and the settings form output in form().

This is a test-only change; no production code is modified.

Testing Instructions

  1. Run the tests on single site:
    npm run test:php -- --filter 'Tests_Widgets_wpWidgetArchives'
  2. Run them on multisite:
    npm run test:php -- --filter 'Tests_Widgets_wpWidgetArchives' -c tests/phpunit/multisite.xml
  3. Run them in random order:
    npm run test:php -- --filter 'Tests_Widgets_wpWidgetArchives' --order-by=random
  4. Run the whole group to confirm nothing else is affected:
    npm run test:php -- --group widgets
  5. Run PHPCS on the test file:
    node ./tools/local-env/scripts/docker.js run --rm php ./vendor/bin/phpcs tests/phpunit/tests/widgets/wpWidgetArchives.php

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request outside of WordPress Core Trac.

Adds a test class for WP_Widget_Archives, which had no tests. The tests cover the registration settings, the default and custom title output, the widget_title filter arguments, the HTML5 navigation wrapper vs. XHTML format, dropdown mode output and the widget_archives_dropdown_args filter, settings sanitization in update(), and the settings form output in form().

This is a test-only change; no production code is modified.

See #65819.
@github-actions

github-actions Bot commented Oct 11, 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: @claude.

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 rejaulalomkhan, huzaifaalmesbah.

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

)
);
$this->assertSame( 'checkbox', $processor->get_attribute( 'type' ) );
$this->assertTrue( $processor->get_attribute( 'checked' ) );

@huzaifaalmesbah huzaifaalmesbah Oct 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is what's failing CI: checked() prints checked='checked', so get_attribute() returns the string 'checked' rather than true.

Suggested change
$this->assertTrue( $processor->get_attribute( 'checked' ) );
$this->assertSame( 'checked', $processor->get_attribute( 'checked' ) );

AI Review: Opus 5.5

)
);
$this->assertSame( 'checkbox', $processor->get_attribute( 'type' ) );
$this->assertTrue( $processor->get_attribute( 'checked' ) );

@huzaifaalmesbah huzaifaalmesbah Oct 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here, it only didn't show up in CI because the assertion above fails first.

Suggested change
$this->assertTrue( $processor->get_attribute( 'checked' ) );
$this->assertSame( 'checked', $processor->get_attribute( 'checked' ) );

$this->assertStringContainsString( '<select id="archives-dropdown-2" name="archive-dropdown">', $output );
$this->assertStringContainsString( '<label class="screen-reader-text" for="archives-dropdown-2">Select Archive</label>', $output );
$this->assertStringContainsString( '<option value="">Select Month</option>', $output );
$this->assertStringContainsString( 'archives-dropdown-2', $output );

@huzaifaalmesbah huzaifaalmesbah Oct 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This one is already covered by the <select id="archives-dropdown-2" assertion a few lines up, so I think it can go.

Comment on lines +245 to +249
$old_instance = array(
'title' => 'Old Title',
'count' => 0,
'dropdown' => 0,
);

@huzaifaalmesbah huzaifaalmesbah Oct 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unchecked checkboxes aren't submitted, so the real-world case is count and dropdown missing from $new_instance. Could you add a data set for that?

With the old instance at 0/0 it wouldn't prove much though, so starting it at 1/1 would show the values are reset rather than carried over.

`checked()` outputs `checked='checked'`, so the attribute value read by the HTML API is the string `'checked'`, not `true`.

`WP_HTML_Tag_Processor::next_tag()` does not support an `id` query key, so those queries silently matched the next `INPUT` regardless of its ID. Assert the `id` attribute of each input explicitly instead.

Also remove a redundant dropdown ID assertion, and add an `update()` data set where the unchecked checkboxes are absent from the submitted instance, starting from an old instance with both options enabled, so that the reset to `0` is actually verified.

See #65819.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9qLmSDTekkoHLL78tqtBH
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