Skip to content

Tests: Add unit tests for the server-side rendering of the Post Featured Image block - #15681

Open
hmbashar wants to merge 2 commits into
WordPress:trunkfrom
hmbashar:66546-post-featured-image-block-tests
Open

hmbashar wants to merge 2 commits into
WordPress:trunkfrom
hmbashar:66546-post-featured-image-block-tests

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Adds unit tests for render_block_core_post_featured_image() and register_block_core_post_featured_image() in src/wp-includes/blocks/post-featured-image.php, which were not referenced by any test. No behavior is changed. This is the second part of the ticket: the helper functions are covered in a separate pull request.

The new test class tests/phpunit/tests/blocks/renderPostFeaturedImage.php (33 tests) covers:

  • Nothing is rendered without a post or for a post without an image.
  • The featured image in a <figure> with the size of the block (post-thumbnail by default), and the classes of the border on the image.
  • isLink links the image to the post, with the target and the optional rel, and escaped URL, target and rel. The image is not linked for false, null or zero. The alternative text of a linked image is the title of the post without tags, or Untitled post <ID>.
  • The styles of the image: the aspect ratio (also auto), the height, the width (with height:auto without a height), the scale, the shadow, the border before them, and nothing for empty values.
  • useFirstImageFromPost: the first image of the content is used when the post has no featured image (with its attributes, and the styles of the block added to its own), not before the featured image, and nothing is rendered when there is no image.
  • The overlay is added after the image, inside the link when there is one, and only with a dim ratio.
  • The block is registered with the render callback.

Testing:

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

I also temporarily broke the function in about 35 ways (each check, the link and its attributes and escaping, the alternative text, each part of the styles, the first image and the merge of the styles, the overlay and the render callback) and confirmed that a new test fails for each one.

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

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

The test fails to restore the original block registration, leaving a replacement without bootstrap-added support attributes and altering the schema seen by subsequent tests.

1 open issue
What changed in this PR

Adds unit coverage for the Post Featured Image block’s server-side rendering and registration, supporting Trac ticket #66546 without changing production code.

Changes:

  • Tests image selection, links and escaping, alternative text, styles, and overlays.
  • Covers first-content-image fallback and featured-image precedence.
  • Verifies registration of the render callback.
File Description
tests/​phpunit/​tests/​blocks/​renderPostFeaturedImage.php Adds fixtures, helpers, and tests for rendering and block registration.

Review effort: Balanced

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