Skip to content

Tests: Add unit tests for the server-side rendering of the Post Time To Read block - #15676

Closed
hmbashar wants to merge 2 commits into
WordPress:trunkfrom
hmbashar:66545-post-time-to-read-block-tests
Closed

hmbashar wants to merge 2 commits into
WordPress:trunkfrom
hmbashar:66545-post-time-to-read-block-tests

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Adds unit tests for block_core_post_time_to_read_word_count(), render_block_core_post_time_to_read() and register_block_core_post_time_to_read() in src/wp-includes/blocks/post-time-to-read.php, which were not referenced by any test. No behavior is changed. This is the first part of the ticket: the Post Terms block is covered in a separate pull request.

The new test class tests/phpunit/tests/blocks/renderPostTimeToRead.php (45 tests) covers:

  • The count of words (punctuation, HTML tags, comments, non-breaking spaces, entities and connectors are handled, accented letters count) and of characters with or without spaces (entities count as one, and so do astral characters), and the count of words for a type that is not valid.
  • Nothing is rendered without a post.
  • The minutes to read: at least one minute, the default reading speed of 189 words per minute, a speed of the block, and a range (4–6 minutes, and a minimum of one with the maximum at least one more).
  • The number of words (1 word, 12 words, 1,200 words) and the number of characters for a language that counts them. An empty text is rendered for a display mode that is not known.
  • The has-text-align-* class and the registration of the block.

Testing:

  • vendor/bin/phpunit --filter 'Tests_Blocks_RenderPostTimeToRead::'
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Blocks_RenderPostTimeToRead::'
  • vendor/bin/phpcs tests/phpunit/tests/blocks/renderPostTimeToRead.php

I also temporarily broke the functions (each regular expression and step of the count, the check of the type, the check of the post, the default speed and display mode, each limit and calculation of the minutes and of the range, the texts of the words and the characters, the class and the registration) and confirmed that a new test fails for each one, except for two mutants that do not change the result: the early return for an empty text (the count of an empty text is zero anyway) and the replacement of astral characters (the regular expressions already count a code point as one).

Trac ticket: https://core.trac.wordpress.org/ticket/66545

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 Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

github-actions Bot commented Nov 6, 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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props hmbashar.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Test cleanup leaks an altered block schema into subsequent tests instead of restoring the original object, and fractional non-range rounding lacks coverage.

2 open issues
What changed in this PR

Adds PHPUnit coverage for the Post Time To Read block’s server-side functions, addressing the first part of ticket #66545 without changing production code.

Changes:

  • Tests word and character counting across HTML, whitespace, punctuation, entities, and Unicode.
  • Covers reading-time output, ranges, custom speeds, count displays, missing post context, and text alignment.
  • Verifies block registration and its render callback.
File Description
tests/​phpunit/​tests/​blocks/​renderPostTimeToRead.php Adds 45 test cases for counting, server-side rendering, and block registration.

Review effort: Balanced

@hmbashar hmbashar closed this Nov 7, 2026
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.

2 participants