Build/Test Tools: Mock plugin API response in WP_REST_Plugins_Controller_Test.

Avoid false test failures due to network conditions in the `WP_REST_Plugins_Controller_Test` class. This mocks HTTP responses from the plugin information endpoint for the link-manager plugin.

Props: peterwilsoncc, costdev.
See #59647.



git-svn-id: https://develop.svn.wordpress.org/trunk@57531 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2024-02-04 22:02:02 +00:00
parent 629d4c5753
commit ac240f068d
2 changed files with 33 additions and 3 deletions
@@ -0,0 +1 @@
{"name":"Link Manager","slug":"link-manager","version":"0.1-beta","author":"WordPress","author_profile":"https:\/\/profiles.wordpress.org\/nacin\/","contributors":{"nacin":{"profile":"https:\/\/profiles.wordpress.org\/nacin\/","avatar":"https:\/\/secure.gravatar.com\/avatar\/01cfe9feaafb068590891bbd1f6a7f5a?s=96&d=monsterid&r=g","display_name":"Andrew Nacin"}},"requires":"3.5","tested":"6.1.5","requires_php":false,"requires_plugins":[],"rating":90,"ratings":{"5":27,"4":3,"3":0,"2":0,"1":3},"num_ratings":33,"support_url":"https:\/\/wordpress.org\/support\/plugin\/link-manager\/","support_threads":1,"support_threads_resolved":1,"active_installs":30000,"last_updated":"2012-08-16 10:57pm GMT","added":"2012-08-16","homepage":"","download_link":"https:\/\/downloads.wordpress.org\/plugin\/link-manager.zip","upgrade_notice":[],"screenshots":[],"tags":{"blogroll":"blogroll","link-manager":"link manager","links":"links"},"versions":[],"business_model":false,"repository_url":"","commercial_support_url":"","donate_link":"","banners":[],"preview_link":"","language_packs":[]}
@@ -40,6 +40,13 @@ class WP_REST_Plugins_Controller_Test extends WP_Test_REST_Controller_Testcase {
*/
private static $admin;
/**
* JSON decoded response from the WordPress.org plugin API.
*
* @var stdClass
*/
private static $plugin_api_decoded_response;
/**
* Set up class test fixtures.
*
@@ -67,6 +74,8 @@ class WP_REST_Plugins_Controller_Test extends WP_Test_REST_Controller_Testcase {
if ( is_multisite() ) {
grant_super_admin( self::$super_admin );
}
self::$plugin_api_decoded_response = json_decode( file_get_contents( DIR_TESTDATA . '/plugins/link-manager.json' ) );
}
/**
@@ -1017,7 +1026,12 @@ class WP_REST_Plugins_Controller_Test extends WP_Test_REST_Controller_Testcase {
}
/**
* Sets up the plugin download to come locally instead of downloading it from .org
* Sets up the plugin repository requests to use local data.
*
* Requests to the plugin repository are mocked to avoid external HTTP requests so
* the test suite does not produce false negatives due to network failures.
*
* Both the plugin ZIP file and the plugin API response are mocked.
*
* @since 5.5.0
*/
@@ -1036,8 +1050,23 @@ class WP_REST_Plugins_Controller_Test extends WP_Test_REST_Controller_Testcase {
3
);
// Remove upgrade hooks which are not required for plugin installation tests
// and may interfere with the results due to a timeout in external HTTP requests.
add_filter(
'plugins_api',
function ( $bypass, $action, $args ) {
// Only mock the plugin_information (link-manager) request.
if ( 'plugin_information' !== $action || 'link-manager' !== $args->slug ) {
return $bypass;
}
return self::$plugin_api_decoded_response;
},
10,
3
);
/*
* Remove upgrade hooks which are not required for plugin installation tests
* and may interfere with the results due to a timeout in external HTTP requests.
*/
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
remove_action( 'upgrader_process_complete', 'wp_version_check' );
remove_action( 'upgrader_process_complete', 'wp_update_plugins' );