Skip to content

Conversation

sarthak-19
Copy link
Contributor

@sarthak-19 sarthak-19 commented Feb 18, 2025

Summary

This is part of #1789:

  • Ignore Coverage for Non-Critical Code Blocks
  • Add Missing @covers Annotations
  • Add Missing Tests
Before : 79.00% ⚠️ After: 95.00% ✅
image  image

cc : @westonruter

@sarthak-19 sarthak-19 changed the title Improve Test Coverage for Web Worker Offloading Plugin Improve Test Coverage for Auto Sizes Plugin Feb 18, 2025
Copy link

codecov bot commented Feb 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.53%. Comparing base (1fc81a3) to head (3a983f2).
⚠️ Report is 19 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1879      +/-   ##
==========================================
+ Coverage   68.21%   68.53%   +0.32%     
==========================================
  Files          93       91       -2     
  Lines        7947     7930      -17     
==========================================
+ Hits         5421     5435      +14     
+ Misses       2526     2495      -31     
Flag Coverage Δ
multisite 68.53% <ø> (+0.32%) ⬆️
single 35.78% <ø> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sarthak-19 sarthak-19 marked this pull request as ready for review February 18, 2025 19:49
Copy link

github-actions bot commented Feb 18, 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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: [email protected].

Co-authored-by: sarthak-19 <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: pravinparmar2404 <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: phanduynam <[email protected]>

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

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick

@westonruter westonruter added [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) [Type] Enhancement A suggestion for improvement of an existing feature skip changelog PRs that should not be mentioned in changelogs labels Feb 21, 2025
@westonruter westonruter added this to the auto-sizes n.e.x.t milestone Feb 21, 2025
Comment on lines 723 to 730
public function test_auto_sizes_prime_attachment_caches_with_empty_class(): void {
$img_tag = '<img src="https://example.com/foo.png">';

$result = auto_sizes_prime_attachment_caches( $img_tag );

// Assert that the output is the same as the input since no processing should occur.
$this->assertSame( $img_tag, $result );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this test doesn't actually test that the attachment caches were primed. Ideally there would be a data provider for this, one which tells the test function to create an attachment and get the image HTML for it via wp_get_attachment_image(). In this case, it should assert somehow that _prime_post_caches() was called. Then there could be another case in the data provider for an external image without a class attribute as you have here, and in this case it should assert that _prime_post_caches() was not called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @westonruter thanks for the feedback however I'm a bit lost here in implementing this, could you help me out in this test implementation.

  • Also what's the recommended approach to verify that _prime_post_caches() was called inside our function? Should I use a mock, a filter, or is there another WordPress-specific testing pattern I should follow?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good question. It boils down to testing for the side effects of the _prime_post_caches() function. In the case here, there would be no side effects. In particular, there should be no DB query. So you can add a callback for the query filter to increment whenever it is called. In the case here, since there is no class attribute, it would be expected that the query would never be made. However, if there was another IMG tag for an actual attachment then it would have a class attribute that would reference the attachment post ID, in which case the query filter should apply.

Note that you probably need to call flush_cache() right before calling auto_sizes_prime_attachment_caches() to ensure that none of the posts are cached so that the query will occur.

I think this test should have a data provider that has two entries:

  1. Test the case where there is no class attribute and thus the expected query count is zero (0).
  2. Test the case where the IMG has a comes from an actual attachment, and in this case the expected query count is one (1).

@phanduynam

This comment was marked as spam.

@mukeshpanchal27
Copy link
Member

@sarthak-19 Could you please resolve the conflict and address the feedbacks?

@sarthak-19
Copy link
Contributor Author

@mukeshpanchal27 Will do today 😅

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sarthak-19 Thanks for the PR!

Overall, it looks solid to me. I’ve left a few suggestions. Also, could you please re-run the coverage report? I recently merged some accurate sizes PRs that might impact the results.

@westonruter When you have a moment, could you please take a look as well?

@mukeshpanchal27
Copy link
Member

@sarthak-19 @westonruter I’m going to merge these PR.

Since they don’t affect production code and only improve test coverage, any remaining items can be addressed in a separate follow-up PR.

@mukeshpanchal27 mukeshpanchal27 merged commit 434fb10 into WordPress:trunk Aug 19, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) skip changelog PRs that should not be mentioned in changelogs [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants