Make WordPress Core

Opened 93 minutes ago

Last modified 84 minutes ago

#66601 new enhancement

Tests: Add unit tests for the server-side rendering of the Comments 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 Comments block (core/comments) is rendered on the server by the functions in src/wp-includes/blocks/comments.php. They have no tests.

This ticket adds tests that cover:

  • render_block_core_comments(): nothing without the post ID in the context, or for a post with closed comments and no comments; the static version of the block; and the legacy version with the comments template, its wrapper classes (also for the core/post-comments block), the restoring of the global post, and the script of the reply link;
  • comments_block_form_defaults(): the button and the field of the comment form, for block themes and for other themes;
  • register_legacy_post_comments_block(): the core/post-comments block type that is registered again with the render callback.

Change History (1)

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


84 minutes ago
#1

  • Keywords has-patch has-unit-tests added

Summary

Adds tests for the server-side rendering of the Comments block (render_block_core_comments(), comments_block_form_defaults() and register_legacy_post_comments_block()), which had no tests.

  • render_block_core_comments(): nothing without the post ID in the context or for a post with closed comments and no comments (closed comments with comments, and open comments without comments, are rendered); the static version with the inner blocks; the legacy version with the comments of the post of the context, the wrapper classes (the legacy class and the text alignment), the core/post-comments block always being legacy, the script of the reply link (only for the legacy version) and the restoring of the global post.
  • comments_block_form_defaults(): unchanged for a theme that is not a block theme, and the button and the field of the form for a block theme. It is hooked to comment_form_defaults.
  • register_legacy_post_comments_block(): the core/post-comments block type with its render callback, context, attributes and supports, registered again when it exists or not, and the block_type_metadata filter.

The tests restore the core/post-comments block type and the script queue.

Testing

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

Mutation check: 30 changes to the three functions were each caught by a test, except the setup_postdata() call of the legacy version, which does not change the output of the comments template.

enqueue_legacy_post_comments_block_styles() is not tested: it keeps a static flag, so it can only run once in a process.

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.