Make sure we error out on read if a zone is not found.

This commit is contained in:
Justin Shreve 2016-11-15 15:16:04 -08:00
parent 6f51fce88b
commit 23a325ca8d
3 changed files with 13 additions and 6 deletions

View File

@ -56,17 +56,22 @@ class WC_Shipping_Zones {
public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
switch ( $by ) {
case 'zone_id' :
return new WC_Shipping_Zone( $id );
$zone_id = $id;
break;
case 'instance_id' :
$data_store = WC_Data_Store::load( 'shipping-zone' );
$zone_id = $data_store->get_zone_id_by_instance_id( $id );
if ( false !== $zone_id ) {
return new WC_Shipping_Zone( $zone_id );
}
break;
}
if ( false !== $zone_id ) {
try {
return new WC_Shipping_Zone( $zone_id );
} catch ( Exception $e ) {
return false;
}
}
return false;
}

View File

@ -74,6 +74,8 @@ class WC_Shipping_Zone_Data_Store_Table implements WC_Shipping_Zone_Data_Store_I
$zone->read_meta_data();
$zone->set_object_read( true );
do_action( 'woocommerce_shipping_zone_loaded', $zone );
} else {
throw new Exception( __( 'Invalid data store.', 'woocommerce' ) );
}
}

View File

@ -286,7 +286,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
public function test_update_shipping_zone_invalid_id() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/1' );
$request = new WP_REST_Request( 'PUT', '/wc/v1/shipping/zones/555555' );
$request->set_body_params( array(
'name' => 'Zone Test',
'order' => 2,
@ -332,7 +332,7 @@ class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
*/
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' );
$request = new WP_REST_Request( 'DELETE', '/wc/v1/shipping/zones/555555' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}