Skip to content

Add AJAX image cropping tests and update MIME type handling#8117

Open
pbearne wants to merge 7 commits intoWordPress:trunkfrom
pbearne:62729-stream_preview_image()-should-stream-with-right-mime-type
Open

Add AJAX image cropping tests and update MIME type handling#8117
pbearne wants to merge 7 commits intoWordPress:trunkfrom
pbearne:62729-stream_preview_image()-should-stream-with-right-mime-type

Conversation

@pbearne
Copy link

@pbearne pbearne commented Jan 13, 2025

Introduced detailed PHPUnit tests for AJAX image cropping preview functionality. Made get_mime_type a public method and adjusted MIME type handling in stream_preview_image for better compatibility with image editors. These changes ensure more robustness in image handling and testing.

Trac ticket:

Introduced detailed PHPUnit tests for AJAX image cropping preview functionality. Made `get_mime_type` a public method and adjusted MIME type handling in `stream_preview_image` for better compatibility with image editors. These changes ensure more robustness in image handling and testing.
@github-actions
Copy link

github-actions bot commented Jan 13, 2025

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Props pbearne.

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

@github-actions
Copy link

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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

Paul Bearne and others added 6 commits January 13, 2025 13:44
…ated to image handling:

1. **Eager Loading:** The addition of `$this->load();` in the constructor of the test image class forces the image to be loaded immediately upon instantiation.  Previously, the image loading likely happened later during the test execution. This change ensures the image data is available right from the start, potentially addressing issues where the image wasn't loaded when expected in the original code.  This could have been the root cause of a bug where image processing operations were failing because the image data wasn't yet present.

2. **Removal of Content-Type Assertion:** The removal of `$this->assertContains( 'image/jpeg', xdebug_get_headers() );` suggests a shift in testing strategy.  The original test verified that the correct content-type header (`image/jpeg`) was being sent.  The updated test no longer checks this. This might be because the content-type is now handled elsewhere, or because this specific test's focus has narrowed to just verifying the image data itself (the JPEG file signature) rather than the HTTP response headers. This removal could indicate a refactor where header handling is tested separately.

This change likely improves the test's reliability by ensuring the image is loaded before any operations are performed on it.  The removal of the content-type check might signal a more focused test or a shift in responsibility for header management.  Without further context on the overall testing strategy, it's hard to say definitively whether removing the content-type check is a positive change. However, the eager loading addresses a potential source of test flakiness.
…orrect MIME type is used when saving and streaming edited images.

Key changes:

* **Saving Edited Images:** The original code streamed the image using the `$mime_type` variable, which was potentially outdated. The updated code retrieves the MIME type *after* applying the `image_editor_save_pre` filter.  This is important because the filter might modify the image, thus changing its MIME type. The addition of `method_exists` check ensures this only happens if the image editor object supports `get_mime_type`. This prevents fatal errors if a custom image editor doesn't implement that method.

* **Streaming Images (wp_stream_image):** The original code retrieved the MIME type from the image editor object (`$img->get_mime_type()`). The updated code removes this and relies solely on the `$post->post_mime_type` for the MIME type. This simplifies the logic and potentially avoids inconsistencies if the image editor's MIME type differs from what WordPress has stored.  This relies on the MIME type being correctly updated during the save operation (addressed in the previous change).

The changes improve reliability by ensuring that the correct MIME type is used consistently, especially when filters modify the image during saving.  It also simplifies the streaming logic by removing a potentially redundant MIME type retrieval.
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.

1 participant