From 206ee73bdd04ee515ff861f1d5cd8ac9b49ba1c0 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Thu, 11 Nov 2021 16:58:27 +0000 Subject: [PATCH] Build/Test Tools: Mock remote request for unknown plugin in `WP_REST_Plugins_Controller::create_item()`. Instead of hitting the live API, this commit mocks the remote request when testing creating an item that's an unknown plugin. Follow-up to [48242]. Props hellofromTonya, noisysocks, sergeybiryukov, TimothyBlynJacobs. See #54420. git-svn-id: https://develop.svn.wordpress.org/trunk@52138 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/rest-plugins-controller.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-plugins-controller.php b/tests/phpunit/tests/rest-api/rest-plugins-controller.php index e2a61d605b..3cea054301 100644 --- a/tests/phpunit/tests/rest-api/rest-plugins-controller.php +++ b/tests/phpunit/tests/rest-api/rest-plugins-controller.php @@ -546,8 +546,26 @@ class WP_REST_Plugins_Controller_Test extends WP_Test_REST_Controller_Testcase { */ public function test_create_item_unknown_plugin() { wp_set_current_user( self::$super_admin ); + add_filter( + 'pre_http_request', + static function() { + /* + * Mocks the request to: + * https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&request%5Bslug%5D=alex-says-this-block-definitely-doesnt-exist&request%5Bfields%5D%5Bsections%5D=0&request%5Bfields%5D%5Blanguage_packs%5D=1&request%5Blocale%5D=en_US&request%5Bwp_version%5D=5.9 + */ + return array( + 'headers' => array(), + 'response' => array( + 'code' => 404, + 'message' => 'Not Found', + ), + 'body' => '{"error":"Plugin not found."}', + 'cookies' => array(), + 'filename' => null, + ); + } + ); - // This will hit the live API. $request = new WP_REST_Request( 'POST', self::BASE ); $request->set_body_params( array( 'slug' => 'alex-says-this-block-definitely-doesnt-exist' ) ); $response = rest_do_request( $request );