Make WordPress Core

Opened 102 minutes ago

Last modified 93 minutes ago

#66599 new enhancement

Tests: Add unit tests for the server-side rendering of the Button 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 Button block (core/button) is rendered on the server by render_block_core_button() in src/wp-includes/blocks/button.php. The function has no tests.

This ticket adds tests that cover:

  • an empty button (or content without a link or button) being handled: nothing is rendered for an empty button, and the content is returned as it is without a link or button;
  • the background: the image, size, position, repeat, attachment and gradient being added to the link, merged with its style, with the defaults for the size and the position;
  • the width: a percentage (with the legacy classes), a fixed value and a preset, as the classes and the style of the wrapper.

Change History (1)

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


93 minutes ago
#1

  • Keywords has-patch has-unit-tests added

Summary

Adds tests for render_block_core_button(), the server-side rendering of the Button block, which had no tests.

  • An empty button (a link or a button with nothing or only comments) is not rendered; a button with only a space is. Content without any tag is returned as it is.
  • The background on the link or the button element, not on the wrapper: the image with the default size cover, the position of a contained image, the repeat, the attachment and the gradient, merged with the style of the element, and the class has-background. Only the first link is changed, and a background without styles changes nothing.
  • The width on the wrapper: a percentage with its legacy classes and without, a length, the order with the existing style, and widths that are empty or not a string.
  • The width presets: a default one, one of the theme that wins over the default one, a preset that is a length (with the custom property) and a preset that is not found.

The test removes the presets it adds from the cached theme data.

Testing

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

Mutation check: 46 changes to the function (every branch of the empty check, the background and the width) were each caught by a test.

Not asserted: content that has tags but no link or button (for example a div with a span) gives an empty string today, while the comment in the code says that content without a button is returned as it is. Only content without any tag is returned as it is.

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.