From 902e4f00b4f50863c8cdc0ee6d035772e82d1abd Mon Sep 17 00:00:00 2001 From: Fernando Jorge Mota Date: Wed, 20 Sep 2023 20:01:16 -0300 Subject: [PATCH 1/4] Use new endpoint for deactivating licenses --- .../class-wp-job-manager-helper-api.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/includes/helper/class-wp-job-manager-helper-api.php b/includes/helper/class-wp-job-manager-helper-api.php index 5092826ef..d0644cba1 100644 --- a/includes/helper/class-wp-job-manager-helper-api.php +++ b/includes/helper/class-wp-job-manager-helper-api.php @@ -144,10 +144,24 @@ public function bulk_activate( $license_key, $product_slugs ) { * @return array|false JSON response or false if failed. */ public function deactivate( $args ) { - $args = wp_parse_args( $args ); - $args['wc-api'] = 'wp_plugin_licencing_activation_api'; - $args['request'] = 'deactivate'; - return $this->request( $args, false ); + $args = wp_parse_args( $args ); + $response = $this->request_endpoint( + 'wp-json/wpjmcom-licensing/v1/deactivate', + [ + 'method' => 'POST', + 'body' => wp_json_encode( + [ + 'product_slug' => $args['api_product_id'], + 'license_key' => $args['license_key'], + 'site_url' => $this->get_site_url(), + ] + ), + ] + ); + if ( ! is_array( $response ) || ! array_key_exists( 'success', $response ) ) { + return false; + } + return $response; } /** From d29bb66dba0dde3dd5827a2780e345012a996e31 Mon Sep 17 00:00:00 2001 From: Fernando Jorge Mota Date: Wed, 20 Sep 2023 20:06:40 -0300 Subject: [PATCH 2/4] Add support for mocking HTTP Status on tests for WPJM Helper API --- .../helper/test_class.wp-job-manager-helper-api.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php index 3323f8b7c..1ecfaef82 100644 --- a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php +++ b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php @@ -162,18 +162,19 @@ public function test_deactivate_invalid() { * * @param string $endpoint The request URI/path of the endpoint to mock the response for. * @param mixed $body The response body to return for the specified endpoint. + * @param int $status The HTTP status code to return for the specified endpoint. * * @return void */ - protected function mock_http_request($endpoint, $body) { - add_filter('pre_http_request', function($preempt, $args, $url) use ($endpoint, $body) { + protected function mock_http_request($endpoint, $body, $status = 200) { + add_filter('pre_http_request', function($preempt, $args, $url) use ($endpoint, $body, $status) { if ($endpoint === wp_parse_url($url, PHP_URL_PATH)) { return array( 'headers' => array(), 'body' => wp_json_encode($body), 'response' => array( - 'code' => 200, - 'message' => 'OK', + 'code' => $status, + 'message' => 200 === $status ? 'OK' : 'Error' ), 'cookies' => array(), ); From 89c81621e4739c660bd7910d1cfe0c3e71035a62 Mon Sep 17 00:00:00 2001 From: Fernando Jorge Mota Date: Wed, 20 Sep 2023 20:06:54 -0300 Subject: [PATCH 3/4] Change default valid response in WPJM Helper API --- .../includes/helper/test_class.wp-job-manager-helper-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php index 1ecfaef82..5e293ef0b 100644 --- a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php +++ b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php @@ -214,7 +214,7 @@ protected function set_expected_response( $test_data ) { } protected function default_valid_response() { - return [ 'status' => 1 ]; + return [ 'success' => true ]; } protected function default_invalid_response() { From aced50fd7d113b39117278d4e308d23f0dd17484 Mon Sep 17 00:00:00 2001 From: Fernando Jorge Mota Date: Wed, 20 Sep 2023 20:07:18 -0300 Subject: [PATCH 4/4] Mock new endpoint for deactivating licenses on tests for WPJM Helper API --- .../test_class.wp-job-manager-helper-api.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php index 5e293ef0b..622c972ab 100644 --- a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php +++ b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php @@ -127,16 +127,8 @@ public function test_activate_invalid() { */ public function test_deactivate_valid() { $base_args = $this->get_base_args(); - $this->set_expected_response( - [ - 'args' => wp_parse_args( - [ - 'wc-api' => 'wp_plugin_licencing_activation_api', - 'request' => 'deactivate', - ], - $base_args - ), - ] + $this->mock_http_request( '/wp-json/wpjmcom-licensing/v1/deactivate', + $this->default_valid_response() ); $instance = new WP_Job_Manager_Helper_API(); $response = $instance->deactivate( $base_args ); @@ -151,6 +143,12 @@ public function test_deactivate_valid() { */ public function test_deactivate_invalid() { $base_args = $this->get_base_args(); + $this->mock_http_request( '/wp-json/wpjmcom-licensing/v1/deactivate', + [ + 'error_code' => 'license_not_found' + ], + 404 + ); $instance = new WP_Job_Manager_Helper_API(); $response = $instance->deactivate( $base_args );