Build/Test Tools: Fail tests that make external HTTP requests outside the external-http group. - #13407
Conversation
… the `external-http` group. Add a `pre_http_request` filter in `WP_UnitTestCase_Base` that blocks an external HTTP request from a test outside the `external-http` group, then fails that test. The filter runs at `PHP_INT_MAX`, so a mock added by the test answers first. The check runs only when `WP_RUN_CORE_TESTS` is set. Add `@group external-http` to `Tests_oEmbed_WpEmbed::test_run_shortcode_url_only`, the only test in trunk that the check catches.
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. |
westonruter
left a comment
There was a problem hiding this comment.
This is looking really good.
|
@adimoldovan Want to take out of draft now? What's next to land this? |
Co-authored-by: Weston Ruter <westonruter@gmail.com>
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Fail core tests that make unmocked HTTP requests outside the external-http group, and block those requests before they reach the network. Preserve mocked responses and leave plugin and theme test suites unaffected. Add the missing external-http annotation to the oEmbed shortcode test. Developed in: #13407 Props peterwilsoncc, adrianmoldovanwp, westonruter. See #63083. git-svn-id: https://develop.svn.wordpress.org/trunk@63560 602fd350-edb4-49c9-b593-d223f7449a82
Fail core tests that make unmocked HTTP requests outside the external-http group, and block those requests before they reach the network. Preserve mocked responses and leave plugin and theme test suites unaffected. Add the missing external-http annotation to the oEmbed shortcode test. Developed in: WordPress/wordpress-develop#13407 Props peterwilsoncc, adrianmoldovanwp, westonruter. See #63083. Built from https://develop.svn.wordpress.org/trunk@63560 git-svn-id: http://core.svn.wordpress.org/trunk@62736 1a063a9b-81f0-0310-95a4-ce76da25c4cd
|
Neat! |
Trac ticket: https://core.trac.wordpress.org/ticket/63083
A test that makes an external HTTP request without
@group external-httpnow fails, and the request never leaves the machine:WP_UnitTestCase_Baseadds apre_http_requestfilter to each test outside the group, behind any mock the test adds itself. The check runs only whenWP_RUN_CORE_TESTSis set, so plugin and theme suites that use the core test suite keep working.Tests_oEmbed_WpEmbed::test_run_shortcode_url_onlygets the annotation it was missing. It is the only test in trunk that the check catches. Its own assertions pass either way, becauserun_shortcode()falls back tomaybe_make_link()and returns the same link, which is why the request went unnoticed.PHPUnit 10 removes
getGroups(), so that migration will have to touch this condition.Testing Instructions
Start the environment:
npm run env:start && npm run env:install.Run each of these. Expect no failures.
npm run test:phpnpm run test:php -- --group external-httpnpm run test:php -- --group ajaxnpm run test:php -- -c tests/phpunit/multisite.xmlnpm run test:php -- -c tests/phpunit/multisite.xml --group external-httpConfirm the check catches a new offender. Save this as
tests/phpunit/tests/guardDemo.php:Run
npm run test:php -- --filter Tests_Guard_Demo. Expect one failure that names the URL and tells you to add the group. Add@group external-httpto the test and run it again: the default run now excludes it. Delete the file.Confirm the annotation in this patch is what keeps the suite green. Remove
@group external-httpfromTests_oEmbed_WpEmbed::test_run_shortcode_url_only, then runnpm run test:php -- --filter Tests_oEmbed_WpEmbed::test_run_shortcode_url_only. Expect the same failure. Restore the annotation.Check the coding standards:
composer lint tests/phpunit/includes/abstract-testcase.php tests/phpunit/tests/oembed/WpEmbed.php.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: The report-only audit of the test suite, and assisted with the implementation.
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.