Tests: Add test coverage for media_sideload_image() - #13397
Draft
mukeshpanchal27 wants to merge 2 commits into
Draft
Tests: Add test coverage for media_sideload_image()#13397mukeshpanchal27 wants to merge 2 commits into
mukeshpanchal27 wants to merge 2 commits into
Conversation
Adds unit tests for `media_sideload_image()` in the existing `Tests_Admin_IncludesMedia` test case, covering: * The `_source_url` post meta introduced in [47251], including URLs with a query string. * Each `$return_type`: the default image tag, `src`, `id`, and an unrecognized value falling back to the image tag. * The description being used as escaped alt text. * The attachment being attached to the given post. * URLs without an allowed extension being rejected before any HTTP request is made, plus an empty URL. * The `image_sideload_extensions` filter adding and removing extensions, and receiving the URL being sideloaded. * Download failures (transport errors and non-200 responses) being returned to the caller without creating an attachment. The HTTP request made by `download_url()` is short-circuited through `pre_http_request`, which returns a complete response array and writes a local fixture into the temporary file the request would have streamed to. Props killua99, joemcgill, mukesh27. See #49631, #48164. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Trac ticket: https://core.trac.wordpress.org/ticket/49631
Adds unit test coverage for
media_sideload_image(), which has none today. This picks up where #190 left off and addresses the review feedback left there.What is covered
The tests live in the existing
Tests_Admin_IncludesMediatest case (tests/phpunit/tests/admin/includesMedia.php) rather than a new file, since that is where the rest of thewp-admin/includes/media.phpcoverage already sits._source_urlpost meta (the ticket's original focus, added in [47251] / #48164), including that the meta stores the URL as passed when it carries a query string, while the file name has the query string stripped.$return_type, via a data provider: the default image tag,'html','src','id', and an unrecognized value falling back to the image tag.$descbeing used as escapedalttext on the returned tag..jpgx, exercising the\bin the regex) each return animage_sideload_failed/ "Invalid image URL." error before any HTTP request is made. An empty$fileis covered separately.image_sideload_extensionsfilter — adding an extension (bmp) lets a matching URL through and still records_source_url, removingjpg/jpeg/jperejects one, and the filter receives the URL being sideloaded as its second argument.WP_Errorand a non-200 response are both returned to the caller, and no attachment is created.Addressing the feedback on #190
wpTearDownAfterClass()is gone; posts are created per test with the factory and rolled back automatically.tear_down()now only callsremove_added_uploads(), which is still required (WP_UnitTestCasesnapshots the uploads directory inset_up()but does not clear it), and matchesTests_Post_Attachments,Tests_Image_SiteIconand others. Theremove_filter()call is dropped — hooks are restored between tests.pre_http_requestmock now returns a complete response array (headers,body,response,cookies,filename) sodownload_url()'sContent-Dispositionand response-message handling operate on a realistic shape rather than a bare status code.test_media_sideload_image()is now 12 focused test methods, three of which use data providers.assertSame( $expected, $actual, $message )with a failure message.Also modernised along the way:
set_up()/tear_down()instead ofsetUp()/tearDown(),assertSame()instead ofassertEquals(), noassertRegExp()/assertInternalType()(removed in PHPUnit 9), array syntax per WPCS, and@covers ::media_sideload_imageannotations.Testing instructions
42 tests, 129 assertions pass. Commenting out
add_post_meta( $id, '_source_url', $file );inmedia_sideload_image()fails 3 of the new tests, confirming they exercise the behaviour the ticket is about. The full--group mediasuite (876 tests) also passes.🤖 Generated with Claude Code