diff --git a/tests/phpunit/data/plugins/link-manager.json b/tests/phpunit/data/plugins/link-manager.json new file mode 100644 index 0000000000..5752aac562 --- /dev/null +++ b/tests/phpunit/data/plugins/link-manager.json @@ -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":[]} \ No newline at end of file diff --git a/tests/phpunit/tests/rest-api/rest-plugins-controller.php b/tests/phpunit/tests/rest-api/rest-plugins-controller.php index 1da20c8315..2d93c9849b 100644 --- a/tests/phpunit/tests/rest-api/rest-plugins-controller.php +++ b/tests/phpunit/tests/rest-api/rest-plugins-controller.php @@ -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' );