From 23a325ca8d7f975fe739d1ca16c41b44b0217fff Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Tue, 15 Nov 2016 15:16:04 -0800 Subject: [PATCH] Make sure we error out on read if a zone is not found. --- includes/class-wc-shipping-zones.php | 13 +++++++++---- .../class-wc-shipping-zone-data-store-table.php | 2 ++ tests/unit-tests/api/shipping-zones.php | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/includes/class-wc-shipping-zones.php b/includes/class-wc-shipping-zones.php index 7c87177781a..af18d820847 100644 --- a/includes/class-wc-shipping-zones.php +++ b/includes/class-wc-shipping-zones.php @@ -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; } diff --git a/includes/data-stores/class-wc-shipping-zone-data-store-table.php b/includes/data-stores/class-wc-shipping-zone-data-store-table.php index 98dfed70568..120f07d633e 100644 --- a/includes/data-stores/class-wc-shipping-zone-data-store-table.php +++ b/includes/data-stores/class-wc-shipping-zone-data-store-table.php @@ -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' ) ); } } diff --git a/tests/unit-tests/api/shipping-zones.php b/tests/unit-tests/api/shipping-zones.php index 1a424650745..710b8694297 100644 --- a/tests/unit-tests/api/shipping-zones.php +++ b/tests/unit-tests/api/shipping-zones.php @@ -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() ); }