Skip to content

Tests: Add unit tests for the server-side rendering of the Post Excerpt block - #15672

Closed
hmbashar wants to merge 3 commits into
WordPress:trunkfrom
hmbashar:66543-post-excerpt-block-tests
Closed

hmbashar wants to merge 3 commits into
WordPress:trunkfrom
hmbashar:66543-post-excerpt-block-tests

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Adds unit tests for render_block_core_post_excerpt(), block_core_post_excerpt_excerpt_length() and register_block_core_post_excerpt() in src/wp-includes/blocks/post-excerpt.php, which were not referenced by any test. No behavior is changed. This is the second part of the ticket: the Post Date block is covered in a separate pull request.

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

  • Nothing is rendered without a post in the context.
  • The excerpt from the content of the post, the written excerpt, and the excerptLength of the block for both (a text that is shorter is not changed).
  • The link to read more: on a new line by default (wp-block-post-excerpt__more-text), in the same paragraph with showMoreOnNewLine false (without a space for an empty excerpt), with its text cleaned by wp_kses_post() and its URL escaped.
  • Without a text for the link, the excerpt_more filter is used for the end of the excerpt, and with a link it is replaced by nothing. The block raises the length of the excerpt to 101 words while it renders, and both filters that it adds are removed afterwards.
  • block_core_post_excerpt_excerpt_length() returns 101.
  • The classes: has-text-align-*, has-link-color, both together, and none for another element.
  • The block is registered with the render callback.

Testing:

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

I also temporarily broke the functions (each check, the link and its text, the filters that are added and removed, the post of the excerpt, the length, the position of the link, the separator, each class and the render callback) and confirmed that a new test fails for each one.

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

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, huzaifaalmesbah.

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

The new excerpt test incorrectly expects script contents to be removed, but wp_kses_post() retains them, causing the assertion to fail against the unchanged renderer.

1 open issue
What changed in this PR

Adds unit tests for server-side rendering of the Post Excerpt block and its helper functions, without changing production behavior.

Changes:

  • Covers excerpt generation, length limits, and read-more link rendering and sanitization.
  • Checks temporary filter cleanup and attribute-driven CSS classes.
  • Verifies the excerpt-length helper and block registration callback.
File Description
tests/​phpunit/​tests/​blocks/​renderPostExcerpt.php Adds fixtures and tests for Post Excerpt rendering, filters, classes, and registration.

Review effort: Balanced

@huzaifaalmesbah huzaifaalmesbah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for working on this, @hmbashar! I really appreciate you taking the time to write comprehensive server-side rendering unit tests for the Post Excerpt block (render_block_core_post_excerpt(), block_core_post_excerpt_excerpt_length(), and registration). Local PHPUnit suite (23 tests, 30 assertions) and PHPCS/ESLint both pass cleanly.

I left a few small notes inline.

AI Review: Gemini 3.8 Flash

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

3 participants