Skip to content

Tests: Add unit test coverage for WP_Widget_Calendar - #14186

Open
rejaulalomkhan wants to merge 3 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/widget-calendar
Open

rejaulalomkhan wants to merge 3 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/widget-calendar

Conversation

@rejaulalomkhan

Copy link
Copy Markdown

Description

Adds a test class for WP_Widget_Calendar, which had no tests. The tests cover the registration settings, the output from widget() (with and without a title), the widget_title filter arguments, the private static instance counter omitting the calendar_wrap ID attribute on subsequent calls, title 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_wpWidgetCalendar'
  2. Run them on multisite:
    npm run test:php -- --filter 'Tests_Widgets_wpWidgetCalendar' -c tests/phpunit/multisite.xml
  3. Run them in random order:
    npm run test:php -- --filter 'Tests_Widgets_wpWidgetCalendar' --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/wpWidgetCalendar.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_Calendar, which had no tests. The tests cover the registration settings, the output from widget() with and without a title, the widget_title filter arguments, the private static instance counter omitting the calendar_wrap id attribute on subsequent calls, title 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.

*/
private function reset_calendar_widget_instance_count() {
$property = new ReflectionProperty( 'WP_Widget_Calendar', 'instance' );
$property->setAccessible( true );

@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 fails every PHP 8.5 job: setAccessible() is deprecated there, and it's a no-op since 8.1. Other Core tests guard it like this:

Suggested change
$property->setAccessible( true );
if ( PHP_VERSION_ID < 80100 ) {
$property->setAccessible( true );
}

AI Review: Opus 5.5

*/
public function test_widget_output_without_title() {
$widget = new WP_Widget_Calendar();
$instance = array();

@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.

The multisite jobs fail on the wp-calendar assertion below because get_calendar() prints nothing when there are no published posts, so the wrapper comes out empty. Creating a post makes the test independent of what's in the database:

Suggested change
$instance = array();
$instance = array();
self::factory()->post->create();

$this->assertStringNotContainsString( 'widget-title', $output );
$this->assertStringContainsString( '<div id="calendar_wrap" class="calendar_wrap">', $output );
$this->assertStringContainsString( '<table id="wp-calendar"', $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.

Might be worth a separate small test for the no-posts case too, asserting the wrapper is rendered empty. That's the behaviour the multisite failure accidentally exposed.

`ReflectionProperty::setAccessible()` is deprecated as of PHP 8.5 and has had no effect since PHP 8.1, so only call it on older versions, matching the existing convention in the test suite.

`get_calendar()` outputs nothing when the site has no posts, so the assertion for the calendar table depended on posts leaking in from elsewhere. Create a post in that test and add a separate test for the no-posts case.

See #65819.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F9qLmSDTekkoHLL78tqtBH
@rejaulalomkhan

rejaulalomkhan commented Oct 11, 2026 •

Copy link
Copy Markdown
Author

@huzaifaalmesbah Thanks! Addressed in 145d5ec and 9d94223: guarded setAccessible() behind PHP_VERSION_ID < 80100, created a post for the calendar-table assertion, added a no-posts test, and unset the global $posts in that test, since get_calendar() skips its no-posts check when it is populated.

`get_calendar()` only runs its "no posts" check when the global `$posts` is empty, so the test failed when the full suite ran after a test that left that global populated. Unset it before rendering.

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