Skip to content

Commit

Permalink
Build/Test Tools: Mock plugin API response in `WP_REST_Plugins_Contro…
Browse files Browse the repository at this point in the history
…ller_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
  • Loading branch information
peterwilsoncc committed Feb 4, 2024
1 parent 629d4c5 commit ac240f0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions tests/phpunit/data/plugins/link-manager.json
Original file line number Diff line number Diff line change
@@ -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":[]}
35 changes: 32 additions & 3 deletions tests/phpunit/tests/rest-api/rest-plugins-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -67,6 +74,8 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
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' ) );
}

/**
Expand Down Expand Up @@ -1017,7 +1026,12 @@ protected function check_get_plugin_data( $data, $network_only = false ) {
}

/**
* 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
*/
Expand All @@ -1036,8 +1050,23 @@ static function ( $reply, $package, $upgrader ) {
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' );
Expand Down

0 comments on commit ac240f0

Please sign in to comment.