Skip to content

Commit

Permalink
Cleaning up the API and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leogermani committed Jan 24, 2022
1 parent 2324db2 commit 4a5acd7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 60 deletions.
27 changes: 0 additions & 27 deletions projects/packages/my-jetpack/src/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,6 @@ public static function get_anti_spam_data() {
);
}

/**
* Activate Backup product
*/
public static function activate_backup_product() {
/*
* @todo: implement
* suggestion: when it enables, return an success array:
* array( 'status' => 'activated' );
* Otherwise, an WP_Error instance will be nice.
*/
return array(
'status' => 'active',
);
}

/**
* Deactivate Backup product
*/
public static function deactivate_backup_product() {
/*
* @todo: implement
*/
return array(
'status' => 'inactive',
);
}

/**
* Returns information about the Backup product
*
Expand Down
47 changes: 21 additions & 26 deletions projects/packages/my-jetpack/src/class-rest-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ public function __construct() {
);
}

/**
* Get the schema for the products endpoint
*
* @return array
*/
public function get_products_schema() {
return array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'products',
'type' => 'object',
'properties' => Products::get_product_data_schema(),
);
}

/**
* Check user capability to access the endpoint.
*
Expand Down Expand Up @@ -107,9 +121,7 @@ public static function check_product_argument( $value ) {
* @return array of site products list.
*/
public static function get_products() {
$response = array(
'products' => Products::get_products(),
);
$response = Products::get_products();
return rest_ensure_response( $response, 200 );
}

Expand Down Expand Up @@ -161,7 +173,7 @@ public static function activate_product( $request ) {
return $activate_product_result;
}

return rest_ensure_response( $activate_product_result, 200 );
return rest_ensure_response( Products::get_product( $product_slug ), 200 );
}

/**
Expand All @@ -174,11 +186,10 @@ public static function deactivate_product( $request ) {
$product_slug = $request->get_param( 'product' );
$product = Products::get_product( $product_slug );
if ( ! isset( $product['class'] ) ) {
return new \WP_REST_Response(
array(
'error_message' => 'not_implemented',
),
400
return new \WP_Error(
'not_implemented',
esc_html__( 'The product class handler is not implemented', 'jetpack-my-jetpack' ),
array( 'status' => 400 )
);
}

Expand All @@ -187,23 +198,7 @@ public static function deactivate_product( $request ) {
return $deactivate_product_result;
}

return rest_ensure_response( $deactivate_product_result, 200 );
return rest_ensure_response( Products::get_product( $product_slug ), 200 );
}

/**
* Set site product state.
*
* @param \WP_REST_Request $request The request object.
* @return array of site products list.
*/
public static function set_product_state( $request ) {
$product_slug = $request->get_param( 'product' );
$activate = $request->get_param( 'activate' );

if ( $activate ) {
return rest_ensure_response( Products::activate_backup_product( $product_slug ), 200 );
}

return rest_ensure_response( Products::deactivate_backup_product( $product_slug ), 200 );
}
}
15 changes: 8 additions & 7 deletions projects/packages/my-jetpack/tests/php/test-products-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function test_get_products() {
$data = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $products, $data['products'] );
$this->assertEquals( $products, $data );
}

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ public function test_get_product() {
$data = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $product, $data['products'][0] );
$this->assertEquals( $product, $data );
}

/**
Expand All @@ -196,7 +196,8 @@ public function test_activate_boost() {
$response = $this->server->dispatch( $this->request );
$data = $response->get_data();

$this->assertEquals( 'active', $data['products'][0]['status'] );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'active', $data['status'] );
$this->assertTrue( is_plugin_active( $this->boost_mock_filename ) );
}

Expand All @@ -214,7 +215,8 @@ public function test_deactivate_boost() {
$response = $this->server->dispatch( $this->request );
$data = $response->get_data();

$this->assertEquals( 'inactive', $data['products'][0]['status'] );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'inactive', $data['status'] );
$this->assertFalse( is_plugin_active( $this->boost_mock_filename ) );
}

Expand All @@ -233,9 +235,8 @@ public function test_activate_uninstallable() {
$response = $this->server->dispatch( $this->request );
$data = $response->get_data();

$this->assertEquals( 'inactive', $data['products'][0]['status'] );
$this->assertEquals( 'plugin_php_incompatible', $data['error_code'] );
$this->assertFalse( $data['success'] );
$this->assertEquals( 500, $response->get_status() );
$this->assertEquals( 'plugin_php_incompatible', $data['code'] );
$this->assertFalse( is_plugin_active( $this->boost_mock_filename ) );
}

Expand Down

0 comments on commit 4a5acd7

Please sign in to comment.