Make WordPress Core

Opened 99 minutes ago

Last modified 81 minutes ago

#66600 new enhancement

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

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

  • the small functions: block_core_latest_posts_get_excerpt_length() and block_core_latest_posts_migrate_categories() (the render_block_data filter);
  • render_block_core_latest_posts(): the query, the title, the featured image, the author, the date, the excerpt and the full content, the layout classes, and the restoring of the global post.

Change History (2)

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


97 minutes ago
#1

  • Keywords has-patch has-unit-tests added

Summary

Adds tests for the small functions of the Latest Posts block, which had no tests. This is the first pull request for the ticket; the rendering of the block follows in a separate one.

  • block_core_latest_posts_get_excerpt_length() gives the excerpt length that the block sets.
  • block_core_latest_posts_migrate_categories(): a categories attribute that is a string is changed to a list of category IDs for the Latest Posts block. Other blocks, blocks without a name or without categories, and categories that are already a list, a number or empty are left as they are.
  • The function is hooked to render_block_data, and the posts of a category given as a string are listed when the block is rendered.

Testing

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

Mutation check: 7 changes to the two functions and the hook were each caught by a test.

Not asserted: a string that is not an ID (for example a slug) gives the category ID 0.

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 #15814 on ​WordPress/wordpress-develop by ​@hmbashar.


81 minutes ago
#2

Summary

Adds tests for render_block_core_latest_posts(), the server-side rendering of the Latest Posts block, which had no tests. This is the second and last pull request for the ticket; the first one covers the small functions.

  • The query: the posts from the newest, the order and the order by, the number of posts, only published posts, sticky posts not first, the categories (empty and not a list are ignored) and the author.
  • The title with its link (and the fallback), the author, the date, and the featured image: the size slug, the alignment, the maximum width and height, and the link. The alignment and the title are escaped.
  • The excerpt (trimmed with the link to read more, not trimmed when a filter changes the length) and the full content (filtered, and a post is not rendered inside itself), and the message for a post that is protected by a password. The excerpt_length filter of the block is removed afterwards.
  • The classes of the layout (grid, columns, the minimum width, the old layout attributes), the dates, the author and the link color, and an empty list without posts.
  • The global post is restored with its post data.

Testing

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

Mutation check: 52 changes to the function were each caught by a test, except two that do not change the output: the priming of the cache of the thumbnails (a speed-up) and the replacement of ]]> in the content (the filters of the content already do it).

Note: a post whose content has the block is listed in the block and once more in its own content, and its content is empty the second time.

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.