Make WordPress Core

Opened 2 hours ago

Last modified 2 hours ago

#66598 new enhancement

Tests: Add unit tests for the server-side rendering of the Calendar block

Reported by: hmbashar Owned by:
Priority: normal Milestone: Awaiting Review
Component: Editor Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The Calendar block (core/calendar) is rendered on the server by the functions in src/wp-includes/blocks/calendar.php. They have no tests.

This ticket adds tests for the functions of the file, in separate pull requests:

  • render_block_core_calendar(): nothing for a site without published posts (and the notice for a logged in user), the colors, the link color, the month and the year of the attributes with the permalink structure, and the restoring of the globals;
  • block_core_calendar_has_published_posts() and the functions that keep its flag up to date on single sites: block_core_calendar_update_has_published_posts(), block_core_calendar_update_has_published_post_on_delete() and block_core_calendar_update_has_published_post_on_transition_post_status().

Change History (2)

This ticket was mentioned in ​PR #15810 on ​WordPress/wordpress-develop by ​@hmbashar.


2 hours ago
#1

  • Keywords has-patch has-unit-tests added

Summary

Adds tests for render_block_core_calendar() and the check for published posts, block_core_calendar_has_published_posts() and block_core_calendar_update_has_published_posts(), which had no tests. This is the first pull request for the ticket; the handlers that keep the flag up to date on single sites follow in a separate one.

  • Nothing is rendered without published posts for a visitor, and a notice is rendered for a user who is logged in.
  • The calendar in the wrapper, the preset and custom colors of the text and the background (the preset wins) as classes and as a style of the table, and the class of the link color.
  • The month and the year of the attributes, only when the permalink structure has both, and the restoring of the $monthnum and $year globals.
  • block_core_calendar_has_published_posts(): the post_count option on a multisite, the cached option on a single site, and the query and the saved flag when the option is missing.
  • block_core_calendar_update_has_published_posts(): only published posts count, not drafts or pages.

The tests that depend on a single site or on a multisite use the ms-excluded and ms-required groups.

Testing

vendor/bin/phpunit --filter 'Tests_Blocks_RenderCalendar::'
vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Blocks_RenderCalendar::'
vendor/bin/phpcs tests/phpunit/tests/blocks/renderCalendar.php

Mutation check: 34 changes to the functions were each caught by a test (the two for the multisite branch with the multisite configuration).

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 ticket was mentioned in ​PR #15811 on ​WordPress/wordpress-develop by ​@hmbashar.


2 hours ago
#2

Summary

Adds tests for the functions of the Calendar block that keep the flag of the published posts up to date on single sites. This is the second pull request for the ticket; the first one covers the rendering of the block and the check for published posts. The functions are only defined on single sites, so the test class is in the ms-excluded group.

  • block_core_calendar_update_has_published_post_on_delete(): the flag is updated for a published post, and not for a draft, a private post, a page or a post that does not exist.
  • block_core_calendar_update_has_published_post_on_transition_post_status(): the flag is updated when a post is published or unpublished (including from or to private), and not when the status is the same, when neither status is publish, or for a page.
  • The functions are hooked to delete_post and transition_post_status, and the flag follows a post that is created as published, published and unpublished.

Testing

vendor/bin/phpunit --filter 'Tests_Blocks_CalendarPublishedPostsFlag::'
vendor/bin/phpcs tests/phpunit/tests/blocks/calendarPublishedPostsFlag.php

Mutation check: 13 changes to the two functions and their hooks were each caught by a test.

Not asserted: block_core_calendar_update_has_published_post_on_delete() runs on delete_post, before the post is deleted, so the post is still counted when the flag is updated. The tests call the function directly.

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.

Note: See TracTickets for help on using tickets.