Merge pull request #28801 from liquidweb/refactor/shipping-zone-read

Refactor the WC_Shipping_Zone_Data_Store::read() method
This commit is contained in:
Claudio Sanches 2021-01-15 12:58:36 -03:00 committed by GitHub
commit e698ed72fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 19 deletions

View File

@ -74,33 +74,41 @@ class WC_Shipping_Zone_Data_Store extends WC_Data_Store_WP implements WC_Shippin
public function read( &$zone ) {
global $wpdb;
$zone_data = false;
if ( 0 !== $zone->get_id() || '0' !== $zone->get_id() ) {
$zone_data = $wpdb->get_row(
$wpdb->prepare(
"SELECT zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1",
$zone->get_id()
)
);
}
// Zone 0 is used as a default if no other zones fit.
if ( 0 === $zone->get_id() || '0' === $zone->get_id() ) {
$this->read_zone_locations( $zone );
$zone->set_zone_name( __( 'Locations not covered by your other zones', 'woocommerce' ) );
$zone->read_meta_data();
$zone->set_object_read( true );
/**
* Indicate that the WooCommerce shipping zone has been loaded.
*
* @param WC_Shipping_Zone $zone The shipping zone that has been loaded.
*/
do_action( 'woocommerce_shipping_zone_loaded', $zone );
} elseif ( $zone_data ) {
$zone->set_zone_name( $zone_data->zone_name );
$zone->set_zone_order( $zone_data->zone_order );
$this->read_zone_locations( $zone );
$zone->read_meta_data();
$zone->set_object_read( true );
do_action( 'woocommerce_shipping_zone_loaded', $zone );
} else {
return;
}
$zone_data = $wpdb->get_row(
$wpdb->prepare(
"SELECT zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1",
$zone->get_id()
)
);
if ( ! $zone_data ) {
throw new Exception( __( 'Invalid data store.', 'woocommerce' ) );
}
$zone->set_zone_name( $zone_data->zone_name );
$zone->set_zone_order( $zone_data->zone_order );
$this->read_zone_locations( $zone );
$zone->read_meta_data();
$zone->set_object_read( true );
/** This action is documented in includes/datastores/class-wc-shipping-zone-data-store.php. */
do_action( 'woocommerce_shipping_zone_loaded', $zone );
}
/**

View File

@ -0,0 +1,48 @@
<?php
/**
* Class WC_Shipping_Zone_Data_Store_CPT_Test.
*/
class WC_Shipping_Zone_Data_Store_CPT_Test extends WC_Unit_Test_Case {
/**
* @testdox read() sets properties for normal, non-zero shipping zones.
*/
public function test_read_for_normal_shipping_zones() {
$zone = new WC_Shipping_Zone();
$zone->set_zone_name( 'California' );
$zone->set_zone_order( 3 );
$zone->add_location( 'US:CA', 'state' );
$zone->save();
$datastore = new WC_Shipping_Zone_Data_Store();
$datastore->read( $zone );
$this->assertSame( 'California', $zone->get_zone_name() );
$this->assertSame( 3, $zone->get_zone_order() );
$this->assertGreaterThan( 0, did_action( 'woocommerce_shipping_zone_loaded' ) );
}
/**
* @testdox read() sets default properties for shipping zone with ID 0.
*/
public function test_read_for_shipping_zone_zero() {
$zone = new WC_Shipping_Zone( 0 );
$datastore = new WC_Shipping_Zone_Data_Store();
$datastore->read( $zone );
$this->assertSame( 0, $zone->get_zone_order() );
$this->assertGreaterThan( 0, did_action( 'woocommerce_shipping_zone_loaded' ) );
}
/**
* @testdox read() throws an exception if the zone ID cannot be found.
*/
public function test_read_with_invalid_zone_id() {
$this->expectException( \Exception::class );
$zone = new WC_Shipping_Zone( -1 );
$datastore = new WC_Shipping_Zone_Data_Store();
$datastore->read( $zone );
}
}