Make WordPress Core

Opened 10 days ago

Last modified 10 days ago

#66545 new enhancement

Tests: Add unit tests for the Post Time To Read and Post Terms blocks

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

Description

The functions of the Post Time To Read and Post Terms blocks are not referenced by any test: block_core_post_time_to_read_word_count(), render_block_core_post_time_to_read() and register_block_core_post_time_to_read() (post-time-to-read.php), and render_block_core_post_terms(), block_core_post_terms_build_variations() and register_block_core_post_terms() (post-terms.php) in src/wp-includes/blocks/.

This ticket adds tests for the count of words and characters, the minutes to read (also as a range) and the number of words, the list of terms with its separator, prefix, suffix and classes, the variations of the taxonomies, and the registration of the blocks.

Change History (2)

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


10 days ago
#1

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).

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


10 days ago
#2

Adds unit tests for render_block_core_post_terms(), block_core_post_terms_build_variations() and register_block_core_post_terms() in src/wp-includes/blocks/post-terms.php, which were not referenced by any test. No behavior is changed. This is the second part of the ticket: the Post Time To Read block is covered in a separate pull request.

The new test class tests/phpunit/tests/blocks/renderPostTerms.php (23 tests) covers:

  • The terms of the post as links (rel="tag") separated by the separator of the block (a space by default, escaped).
  • Nothing is rendered without a post or a taxonomy, for a taxonomy that does not exist or is not viewable (also when the post has terms of it), or for a post without terms.
  • The prefix and the suffix (cleaned with wp_kses_post(), and not added when they are empty).
  • The classes: taxonomy-<term>, has-text-align-*, has-link-color, both together, and for another taxonomy.
  • The variations: one for each taxonomy that is publicly queryable and in the REST API (with its name, title, description, attributes, isActive and scope), none for the others, the built-in taxonomies first, and only the category as the default.
  • The block is registered with the render and the variation callbacks.

Testing:

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

I also temporarily broke the functions (each check of the post, the taxonomy and its view, each class, the separator and its escaping, the prefix and the suffix and their cleaning and order, the post of the list, the check of an empty list, the conditions of the variations, their fields and order, and the two callbacks) and confirmed that a new test fails for each one, except for the check of the built-in taxonomies, which does not change the order because the built-in taxonomies are always registered first.

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.