diff --git a/plugins/woocommerce-beta-tester/api/api.php b/plugins/woocommerce-beta-tester/api/api.php index 2d4a1fbd355..16c1ef5e7cc 100644 --- a/plugins/woocommerce-beta-tester/api/api.php +++ b/plugins/woocommerce-beta-tester/api/api.php @@ -64,3 +64,4 @@ require 'live-branches/install.php'; require 'remote-spec-validator/class-wca-test-helper-remote-spec-validator.php'; require 'remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php'; require 'remote-logging/remote-logging.php'; +require 'tools/wccom-request-errors.php'; diff --git a/plugins/woocommerce-beta-tester/api/tools/wccom-request-errors.php b/plugins/woocommerce-beta-tester/api/tools/wccom-request-errors.php new file mode 100644 index 00000000000..4f2662763c3 --- /dev/null +++ b/plugins/woocommerce-beta-tester/api/tools/wccom-request-errors.php @@ -0,0 +1,48 @@ + 'POST', + 'args' => array( + 'mode' => array( + 'description' => 'wccom request error mode', + 'type' => 'enum', + 'enum' => array( 'timeout', 'error', 'disabled' ), + ), + ), + ) +); + +register_woocommerce_admin_test_helper_rest_route( + '/tools/get-wccom-request-errors/v1', + 'tools_get_wccom_request_errors', + array( + 'methods' => 'GET', + ) +); + +/** + * A tool to set wccom request errors mode. + * + * @param WP_REST_Request $request Request object. + */ +function tools_set_wccom_request_errors( $request ) { + $mode = $request->get_param( 'mode' ); + + update_option( 'wc_admin_test_helper_modify_wccom_request_responses', $mode ); + + return new WP_REST_Response( $mode, 200 ); +} + +/** + * A tool to get wccom request mode. + */ +function tools_get_wccom_request_errors() { + $mode = get_option( 'wc_admin_test_helper_modify_wccom_request_responses', 'disabled' ); + + return new WP_REST_Response( $mode, 200 ); +} diff --git a/plugins/woocommerce-beta-tester/changelog/50654-add-beta-tester-wccom-endpoints-mods b/plugins/woocommerce-beta-tester/changelog/50654-add-beta-tester-wccom-endpoints-mods new file mode 100644 index 00000000000..c5e7e461085 --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/50654-add-beta-tester-wccom-endpoints-mods @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add a tool to simulate server problems with woocommerce.com \ No newline at end of file diff --git a/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-wccom-requests.php b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-wccom-requests.php new file mode 100644 index 00000000000..d7a581aa43c --- /dev/null +++ b/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-wccom-requests.php @@ -0,0 +1,74 @@ +wc_beta_tester_modify_wccom_responses(); + } + + /** + * Modify responses based on option value. + */ + public function wc_beta_tester_modify_wccom_responses() { + $mode = get_option( 'wc_admin_test_helper_modify_wccom_request_responses', 'disabled' ); + + if ( 'disabled' === $mode ) { + return; + } + + if ( 'timeout' === $mode ) { + add_filter( 'pre_http_request', array( $this, 'override_http_request_timeout' ), 10, 3 ); + } + + if ( 'error' === $mode ) { + add_filter( 'pre_http_request', array( $this, 'override_http_request_error' ), 10, 3 ); + } + } + + /** + * Override the http request with a timeout. + */ + public function override_http_request_timeout( $response, $args, $url ) { + if ( strpos( $url, 'https://woocommerce.com/wp-json/' ) !== false || strpos( $url, 'woocommerce.test/wp-json/' ) !== false ) { + sleep( 6 ); // 6 seconds + return new WP_Error( 'http_request_timeout', 'Mock timeout error' ); + } + + return false; + } + + /** + * Override the http request with an error. + */ + public function override_http_request_error( $response, $args, $url ) { + if ( strpos( $url, 'https://woocommerce.com/wp-json/' ) !== false ) { + return array( + 'response' => array( + 'code' => 500, + 'message' => 'Internal Server Error', + ), + 'body' => 'Server Error', + 'headers' => array(), + 'cookies' => array(), + 'filename' => null, + ); + } + + return false; + } +} + +new WC_Beta_Tester_WCCOM_Requests(); diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/index.js b/plugins/woocommerce-beta-tester/src/tools/commands/index.js index e077ae389bf..6283f0e35a8 100644 --- a/plugins/woocommerce-beta-tester/src/tools/commands/index.js +++ b/plugins/woocommerce-beta-tester/src/tools/commands/index.js @@ -16,6 +16,11 @@ import { UPDATE_COMING_SOON_MODE_ACTION_NAME, } from './set-coming-soon-mode'; +import { + UPDATE_WCCOM_REQUEST_ERRORS_MODE, + SetWccomRequestErrros, +} from './set-wccom-request-errors'; + export default [ { command: 'Trigger WCA Install', @@ -87,4 +92,9 @@ export default [ description: , action: UPDATE_COMING_SOON_MODE_ACTION_NAME, }, + { + command: 'Force errors on woocommerce.com requests', + description: , + action: UPDATE_WCCOM_REQUEST_ERRORS_MODE, + }, ]; diff --git a/plugins/woocommerce-beta-tester/src/tools/commands/set-wccom-request-errors.js b/plugins/woocommerce-beta-tester/src/tools/commands/set-wccom-request-errors.js new file mode 100644 index 00000000000..d4c108665a6 --- /dev/null +++ b/plugins/woocommerce-beta-tester/src/tools/commands/set-wccom-request-errors.js @@ -0,0 +1,48 @@ +/** + * External dependencies + */ +import { SelectControl } from '@wordpress/components'; +import { useDispatch, useSelect } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import { STORE_KEY } from '../data/constants'; + +export const UPDATE_WCCOM_REQUEST_ERRORS_MODE = 'updateWccomRequestErrorsMode'; + +const OPTIONS = [ + { label: 'Timeout after 6 seconds', value: 'timeout' }, + { label: '500', value: 'error' }, + { label: 'Disabled', value: 'disabled' }, +]; + +export const SetWccomRequestErrros = () => { + const errorsMode = useSelect( + ( select ) => select( STORE_KEY ).getWccomRequestErrorsMode(), + [] + ); + const { updateCommandParams } = useDispatch( STORE_KEY ); + + function onChange( mode ) { + updateCommandParams( UPDATE_WCCOM_REQUEST_ERRORS_MODE, { + mode, + } ); + } + + return ( +
+ { ! errorsMode ? ( +

Loading ...

+ ) : ( + + ) } +
+ ); +}; diff --git a/plugins/woocommerce-beta-tester/src/tools/data/actions.js b/plugins/woocommerce-beta-tester/src/tools/data/actions.js index 9e34802dd2c..b0703a08c70 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/actions.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/actions.js @@ -272,3 +272,13 @@ export function* updateComingSoonMode( params ) { } ); } ); } + +export function* updateWccomRequestErrorsMode( params ) { + yield runCommand( 'Update wccom request errors mode', function* () { + yield apiFetch( { + path: API_NAMESPACE + '/tools/set-wccom-request-errors/v1', + method: 'POST', + data: params, + } ); + } ); +} diff --git a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js index 9a15d468c83..2545784a6db 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/reducer.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/reducer.js @@ -13,6 +13,7 @@ const DEFAULT_STATE = { updateComingSoonMode: {}, updateBlockTemplateLoggingThreshold: {}, runSelectedUpdateCallbacks: {}, + updateWccomRequestErrorsMode: {}, }, status: '', dbUpdateVersions: [], diff --git a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js index 5f55565925d..81065ce3170 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/resolvers.js @@ -17,6 +17,7 @@ import { import { UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME } from '../commands/update-block-template-logging-threshold'; import { UPDATE_COMING_SOON_MODE_ACTION_NAME } from '../commands/set-coming-soon-mode'; import { TRIGGER_UPDATE_CALLBACKS_ACTION_NAME } from '../commands/trigger-update-callbacks'; +import { UPDATE_WCCOM_REQUEST_ERRORS_MODE } from '../commands/set-wccom-request-errors'; export function* getCronJobs() { const path = `${ API_NAMESPACE }/tools/get-cron-list/v1`; @@ -117,3 +118,20 @@ export function* getComingSoonMode() { throw new Error( error ); } } + +export function* getWccomRequestErrorsMode() { + const path = `${ API_NAMESPACE }/tools/get-wccom-request-errors/v1`; + + try { + const mode = yield apiFetch( { + path, + method: 'GET', + } ); + + yield updateCommandParams( UPDATE_WCCOM_REQUEST_ERRORS_MODE, { + mode: mode || 'disabled', + } ); + } catch ( error ) { + throw new Error( error ); + } +} diff --git a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js index 1ac46fc713e..b53025ff4b1 100644 --- a/plugins/woocommerce-beta-tester/src/tools/data/selectors.js +++ b/plugins/woocommerce-beta-tester/src/tools/data/selectors.js @@ -37,3 +37,7 @@ export function getBlockTemplateLoggingThreshold( state ) { export function getComingSoonMode( state ) { return state.params.updateComingSoonMode.mode; } + +export function getWccomRequestErrorsMode( state ) { + return state.params.updateWccomRequestErrorsMode.mode; +} diff --git a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php index 5c89cefc75a..bc1d9a76d72 100644 --- a/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php +++ b/plugins/woocommerce-beta-tester/woocommerce-beta-tester.php @@ -64,6 +64,7 @@ function _wc_beta_tester_bootstrap() { // Tools. include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-version-picker.php'; include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-override-coming-soon-options.php'; + include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-wccom-requests.php'; register_activation_hook( __FILE__, array( 'WC_Beta_Tester', 'activate' ) );