Skip to content

Media: Add unit tests for wp_get_registered_image_subsizes() - #12749

Open
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:task/wp-get-registered-image-subsizes-tests
Open

Media: Add unit tests for wp_get_registered_image_subsizes()#12749
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:task/wp-get-registered-image-subsizes-tests

Conversation

@irozum

@irozum irozum commented Jul 29, 2026

Copy link
Copy Markdown

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 composes get_intermediate_image_sizes() and wp_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 to Tests_Media:

  • A custom size registered via add_image_size() shows up with the correct width/height and crop as false.
  • A size registered with a crop position (e.g. array( 'center', 'top' )) preserves that array rather than coercing it to a bool.
  • A size with no width/height set is omitted from the result entirely.
  • The intermediate_image_sizes filter 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_Media class (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.

Copilot AI review requested due to automatic review settings July 29, 2026 02:05
@github-actions

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. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props irozum.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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.

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 default crop value.
  • 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_sizes filter 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'] );
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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