Skip to content

Commit

Permalink
Merge pull request #2585 from Automattic/change/deactivate-license
Browse files Browse the repository at this point in the history
  • Loading branch information
fjorgemota authored Oct 3, 2023
2 parents f2ff89e + aced50f commit e15265f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
22 changes: 18 additions & 4 deletions includes/helper/class-wp-job-manager-helper-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );

Expand All @@ -162,18 +160,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(),
);
Expand Down Expand Up @@ -213,7 +212,7 @@ protected function set_expected_response( $test_data ) {
}

protected function default_valid_response() {
return [ 'status' => 1 ];
return [ 'success' => true ];
}

protected function default_invalid_response() {
Expand Down

0 comments on commit e15265f

Please sign in to comment.