Media: Add unit tests for wp_get_registered_image_subsizes() - #12749
Media: Add unit tests for wp_get_registered_image_subsizes()#12749irozum wants to merge 2 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
Adds PHPUnit coverage for wp_get_registered_image_subsizes() (core media helper) by asserting its normalization and filtering behavior for registered image sizes.
Changes:
- Adds a test ensuring custom sizes registered via
add_image_size()appear with expected width/height and defaultcropvalue. - Adds a test ensuring crop position arrays (e.g.
array( 'center', 'top' )) are preserved rather than coerced to bool. - Adds tests ensuring sizes with no dimensions are omitted and that the
intermediate_image_sizesfilter can exclude sizes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Clean up. | ||
| remove_image_size( 'test-size' ); | ||
|
|
||
| $this->assertSame( array( 'center', 'top' ), $sizes['test-size']['crop'] ); |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
wp_get_registered_image_subsizes()(wp-includes/media.php, since 5.3.0) had no PHPUnit coverage at all. It's a pure function that composesget_intermediate_image_sizes()andwp_get_additional_image_sizes()into a normalized{width, height, crop}array per registered image size, so it's straightforward to pin down. This adds 4 tests toTests_Media:add_image_size()shows up with the correct width/height andcropasfalse.array( 'center', 'top' )) preserves that array rather than coercing it to a bool.intermediate_image_sizesfilter can exclude a size from the result.This is a test-only change — no production behavior is added or modified. Each test was verified to actually constrain the code: I temporarily broke the corresponding logic in
wp_get_registered_image_subsizes()(inverting the crop bool cast, disabling the zero-dimension skip, disabling the filter application) and confirmed the relevant test went red, then restored the original and confirmed green again.Ran the full
Tests_Mediaclass (453 tests) after adding these — no regressions. Lint and PHPStan clean.Trac ticket: https://core.trac.wordpress.org/ticket/65751
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Identifying the coverage gap, implementation, and mutation-testing verification of each assertion. Reviewed by @irozum.
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.