-
Notifications
You must be signed in to change notification settings - Fork 138
Fix incorrect image size selection in PICTURE element
#1885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix incorrect image size selection in PICTURE element
#1885
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## trunk #1885 +/- ##
==========================================
+ Coverage 70.99% 71.01% +0.01%
==========================================
Files 85 85
Lines 6958 6959 +1
==========================================
+ Hits 4940 4942 +2
+ Misses 2018 2017 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @1ucay. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
felixarntz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@b1ink0 The production code changes look good to me, but it would be great if you could add a unit test to verify that it fixes the originally reported bug.
| /** | ||
| * Test handling of case when wp_get_attachment_image_src returns false. | ||
| */ | ||
| public function test_wrap_image_in_picture_with_false_image_src(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is good to have, but it's not actually testing the more important fix that this PR implements. It would be great if you could add a test that verifies that what is reported in #1882 gets fixed by this PR. You could probably use the code snippet you have in the PR description in that test itself.
A great way to do that is test-driven development:
- Work on the test based on the codebase without this fix. (For example temporarily comment out or revert the changes in
webp_uploads_wrap_image_in_picture()locally.) - Once you implemented the test, it should fail.
- Re-enable the changes in
webp_uploads_wrap_image_in_picture(). - Now the same test should pass without any changes to it.
This way you verify the bug gets fixed by this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed guidance! I will make sure to integrate the step provided for test-driven development into my workflow from now on.
felixarntz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@b1ink0 Thank you for the updates, the test looks great! Just a few minor points of feedback on following some best practices for PHPUnit tests in WordPress.
| wp_delete_attachment( $attachment_id, true ); | ||
|
|
||
| if ( $tear_down instanceof Closure ) { | ||
| $tear_down(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having this cleanup logic called here is not reliable, since any assertion failing would result in this code not being reached. Can we instead incorporate this in an actual tear_down() method on the class that will always get invoked after each test?
You could store the attachment IDs generated in a class property, so that after each check any new IDs result in their attachment being deleted.
| 'tear_down' => static function (): void { | ||
| remove_image_size( 'square' ); | ||
| remove_all_filters( 'pre_option_medium_large_size_w' ); | ||
| remove_all_filters( 'pre_option_medium_large_size_h' ); | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is the only usage of the tear_down closure, we can probably work without them:
- Filters added in a test are removed automatically after the test, so I don't think we need to clean them up here.
- For the image size, maybe you can handle it in a
tear_down()method on the class? Alternatively if it makes sense to keep it, we need to move when thistear_downclosure is invoked in the test (see above comment).
felixarntz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@b1ink0 This looks great, thank you for the updates!
I have one more feedback, which would be great to address, but I'm already going to approve it as it overall is good.
|
Thanks for the updates @b1ink0, looks good! @westonruter Did you already create a branch for the next release set on Monday, or can this still be merged into |
|
@felixarntz No, I didn't create branch yet. There are still several PRs pending merge. |
Summary
Fixes #1882
Relevant technical choices
The
srcsetis generated differently for theSOURCEandIMGtags when there are two image sizes with the same width but different heights. This discrepancy occurs because thewp_image_matches_ratiocheck is applied duringwp_calculate_image_srcsetfor generating thesrcsetof theIMGtag. However, for theSOURCEtag, thesrcsetis generated within thewebp_uploads_wrap_image_in_picturefunction, which then overrides the sources created by thewp_get_attachment_image_srcsetfunction usingwp_calculate_image_srcsetfilter. The issue was caused by the absence of thewp_image_matches_ratiocheck inwebp_uploads_wrap_image_in_picture, leading to incorrect image size selection.Below is the code snippet to reproduce the bug.