Skip to content

Commit

Permalink
Add some missing API pieces:
Browse files Browse the repository at this point in the history
* Value was missing from the settings schema (but was still present in the response).
* Added delete endpoint for shipping zones.
* Corrected permissions check/error for shipping zone methods.
  • Loading branch information
justinshreve committed Aug 29, 2016
1 parent 53f3b35 commit de4296d
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
19 changes: 19 additions & 0 deletions includes/abstracts/abstract-wc-rest-shipping-zones-controller.php
Expand Up @@ -105,4 +105,23 @@ public function update_items_permissions_check( $request ) {

return true;
}

/**
* Check whether a given request has permission to delete Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function delete_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.', 'woocommerce' ), array( 'status' => 404 ) );
}

if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}

return true;
}

}
4 changes: 4 additions & 0 deletions includes/api/class-wc-rest-settings-options-controller.php
Expand Up @@ -386,6 +386,10 @@ public function get_item_schema() {
'sanitize_callback' => 'sanitize_text_field',
),
),
'value' => array(
'description' => __( 'Setting value.', 'woocommerce' ),
'type' => 'mixed',
),
'default' => array(
'description' => __( 'Default value for the setting.', 'woocommerce' ),
'type' => 'mixed',
Expand Down
Expand Up @@ -56,7 +56,7 @@ public function register_routes() {
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'update_items_permissions_check' ),
'permission_callback' => array( $this, 'delete_items_permissions_check' ),
'args' => array(
'force' => array(
'default' => false,
Expand Down
37 changes: 37 additions & 0 deletions includes/api/class-wc-rest-shipping-zones-controller.php
Expand Up @@ -53,6 +53,17 @@ public function register_routes() {
'permission_callback' => array( $this, 'update_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_items_permissions_check' ),
'args' => array(
'force' => array(
'default' => false,
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
}
Expand Down Expand Up @@ -161,6 +172,32 @@ public function update_item( $request ) {
return $this->get_item( $request );
}

/**
* Delete a single Shipping Zone.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Request|WP_Error
*/
public function delete_item( $request ) {
$zone = $this->get_zone( $request->get_param( 'id' ) );

if ( is_wp_error( $zone ) ) {
return $zone;
}

$force = $request['force'];

$response = $this->get_item( $request );

if ( $force ) {
$zone->delete();
} else {
return new WP_Error( 'rest_trash_not_supported', __( 'Shipping zones do not support trashing.' ), array( 'status' => 501 ) );
}

return $response;
}

/**
* Prepare the Shipping Zone for the REST response.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/unit-tests/api/settings.php
Expand Up @@ -137,10 +137,11 @@ public function test_get_setting_schema() {
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 8, count( $properties ) );
$this->assertEquals( 9, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'label', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'value', $properties );
$this->assertArrayHasKey( 'default', $properties );
$this->assertArrayHasKey( 'tip', $properties );
$this->assertArrayHasKey( 'placeholder', $properties );
Expand Down
41 changes: 41 additions & 0 deletions tests/unit-tests/api/shipping-zones.php
Expand Up @@ -296,6 +296,47 @@ public function test_update_shipping_zone_invalid_id() {
$this->assertEquals( 404, $response->get_status() );
}

/**
* Test Shipping Zone delete endpoint.
* @since 2.7.0
*/
public function test_delete_shipping_zone() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Zone 1' );

$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/' . $zone->get_id() );
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$data = $response->get_data();

$this->assertEquals( 200, $response->get_status() );
}

/**
* Test Shipping Zone delete endpoint without permissions.
* @since 2.7.0
*/
public function test_delete_shipping_zone_without_permission() {
wp_set_current_user( 0 );
$zone = $this->create_shipping_zone( 'Zone 1' );

$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/' . $zone->get_id() );
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}

/**
* Test Shipping Zone delete endpoint with a bad zone ID.
* @since 2.7.0
*/
public function test_delete_shipping_zone_invalid_id() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/0' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}

/**
* Test getting a single Shipping Zone.
* @since 2.7.0
Expand Down

0 comments on commit de4296d

Please sign in to comment.