Repository navigation
Tests: Add unit test coverage for WP_Widget_Calendar - #14186
rejaulalomkhan wants to merge 3 commits into
Conversation
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.
|
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 Unlinked AccountsThe 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: 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 ); |
There was a problem hiding this comment.
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:
| $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(); |
There was a problem hiding this comment.
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:
| $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 ); | ||
| } |
There was a problem hiding this comment.
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
|
@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
Description
Adds a test class for
WP_Widget_Calendar, which had no tests. The tests cover the registration settings, the output fromwidget()(with and without a title), thewidget_titlefilter arguments, the private static instance counter omitting thecalendar_wrapID attribute on subsequent calls, title sanitization inupdate(), and the settings form output inform().This is a test-only change; no production code is modified.
Testing Instructions
npm run test:php -- --filter 'Tests_Widgets_wpWidgetCalendar'npm run test:php -- --filter 'Tests_Widgets_wpWidgetCalendar' -c tests/phpunit/multisite.xmlnpm run test:php -- --filter 'Tests_Widgets_wpWidgetCalendar' --order-by=randomnpm run test:php -- --group widgetsnode ./tools/local-env/scripts/docker.js run --rm php ./vendor/bin/phpcs tests/phpunit/tests/widgets/wpWidgetCalendar.phpUse of AI Tools
Yes, an AI assistant was used for scaffolding and initial test generation. All tests, assertions, and coding standards were manually reviewed, verified, and run locally.
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.