-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Build/Test Tools: Fail tests that make external HTTP requests outside the external-http group.
#13407
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
Draft
adimoldovan
wants to merge
1
commit into
WordPress:trunk
Choose a base branch
from
adimoldovan:63083-tests-making-external-requests
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−0
Draft
Build/Test Tools: Fail tests that make external HTTP requests outside the external-http group.
#13407
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,13 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { | |||||
| /** @var non-empty-string[] */ | ||||||
| protected $caught_doing_it_wrong = array(); | ||||||
|
|
||||||
| /** | ||||||
| * URLs of blocked external HTTP requests made during the current test. | ||||||
| * | ||||||
| * @var string[] | ||||||
| */ | ||||||
| protected $blocked_http_requests = array(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| protected static $hooks_saved = array(); | ||||||
| protected static $ignore_files; | ||||||
|
|
||||||
|
|
@@ -143,6 +150,12 @@ public function set_up() { | |||||
| $this->expectDeprecated(); | ||||||
| add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); | ||||||
| add_filter( 'wp_hash_password_options', array( $this, 'wp_hash_password_options' ), 1, 2 ); | ||||||
|
|
||||||
| if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS | ||||||
| && ! in_array( 'external-http', $this->getGroups(), true ) | ||||||
| ) { | ||||||
| add_filter( 'pre_http_request', array( $this, 'block_external_http_request' ), PHP_INT_MAX, 3 ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -685,6 +698,34 @@ public function expectedDeprecated() { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Blocks an external HTTP request that no other filter has answered. | ||||||
| * | ||||||
| * Added by set_up() for tests that are not in the `external-http` group, and | ||||||
| * runs last on the filter so any mock set up by the test itself gets the | ||||||
| * first say. Requests reaching this point are recorded and fail the test in | ||||||
| * assert_post_conditions(). | ||||||
| * | ||||||
| * @since 7.2.0 | ||||||
| * | ||||||
| * @param false|array|WP_Error $response A preemptive response, or false when none was given. | ||||||
| * @param array $args Request arguments. | ||||||
| * @param string $url The request URL. | ||||||
| * @return array|WP_Error The preemptive response, or an error for a blocked request. | ||||||
| */ | ||||||
| public function block_external_http_request( $response, $args, $url ) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if ( false !== $response ) { | ||||||
| return $response; | ||||||
| } | ||||||
|
|
||||||
| $this->blocked_http_requests[] = $url; | ||||||
|
|
||||||
| return new WP_Error( | ||||||
| 'test_external_http_blocked', | ||||||
| 'External HTTP requests are blocked in tests that are not in the `external-http` group.' | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Detects post-test failure conditions. | ||||||
| * | ||||||
|
|
@@ -694,6 +735,14 @@ public function expectedDeprecated() { | |||||
| */ | ||||||
| protected function assert_post_conditions() { | ||||||
| $this->expectedDeprecated(); | ||||||
|
|
||||||
| if ( $this->blocked_http_requests ) { | ||||||
| $this->fail( | ||||||
| "This test made an external HTTP request but is not in the `external-http` group.\n" | ||||||
| . "Add `@group external-http` to it, or mock the request with the `pre_http_request` filter.\n" | ||||||
| . '- ' . implode( "\n- ", array_unique( $this->blocked_http_requests ) ) | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
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
Oops, something went wrong.
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.
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.