2016-01-08 14:53:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Shipping_Zone.
|
|
|
|
* @package WooCommerce\Tests\Shipping_Zone
|
|
|
|
*/
|
2016-03-23 12:14:13 +00:00
|
|
|
class WC_Tests_Shipping_Zone extends WC_Unit_Test_Case {
|
2016-01-08 14:53:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test: WC_Shipping_Zone::get_data
|
|
|
|
*/
|
|
|
|
public function test_get_data() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
|
|
|
$data = $zone->get_data();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertTrue( is_array( $data ) );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zones::get_zone_id
|
|
|
|
*/
|
2016-07-11 14:56:35 +00:00
|
|
|
public function test_get_zone_id() {
|
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_id(), 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
|
|
|
}
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zones::get_zone_name
|
|
|
|
*/
|
2016-07-11 14:56:35 +00:00
|
|
|
public function test_get_zone_name() {
|
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_name(), 'Local' );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
|
|
|
}
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zones::get_zone_order
|
|
|
|
*/
|
2016-07-11 14:56:35 +00:00
|
|
|
public function test_get_zone_order() {
|
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_order(), 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
|
|
|
}
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zones::get_zone_locations
|
|
|
|
*/
|
2016-07-11 14:56:35 +00:00
|
|
|
public function test_get_zone_locations() {
|
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertTrue( is_array( $zone->get_zone_locations() ) );
|
|
|
|
$this->assertTrue( 2 === sizeof( $zone->get_zone_locations() ) );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
|
|
|
}
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zones::get_formatted_location
|
|
|
|
*/
|
2016-07-11 14:56:35 +00:00
|
|
|
public function test_get_formatted_location() {
|
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_formatted_location(), 'United Kingdom (UK), CB*' );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 2 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_formatted_location(), 'Europe' );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 3 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_formatted_location(), 'California' );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 4 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_formatted_location(), 'United States (US)' );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
|
|
|
}
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::get_shipping_methods
|
|
|
|
*/
|
|
|
|
public function test_get_shipping_methods() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
$zone->add_shipping_method( 'flat_rate' );
|
|
|
|
$methods = $zone->get_shipping_methods();
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertTrue( 1 === sizeof( $methods ) );
|
2016-01-08 14:53:24 +00:00
|
|
|
$this->assertInstanceOf( 'WC_Shipping_Method', current( $methods ) );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::set_zone_name
|
|
|
|
*/
|
|
|
|
public function test_set_zone_name() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
$zone->set_zone_name( 'I am a fish' );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_name(), 'I am a fish' );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::set_zone_order
|
|
|
|
*/
|
|
|
|
public function test_set_zone_order() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
$zone->set_zone_order( 100 );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_order(), 100 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::is_valid_location_type
|
|
|
|
*/
|
|
|
|
public function test_is_valid_location_type() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_order(), 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::add_location
|
|
|
|
*/
|
|
|
|
public function test_add_location() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertTrue( $zone->is_valid_location_type( 'state' ) );
|
2016-01-08 14:53:24 +00:00
|
|
|
$this->assertTrue( $zone->is_valid_location_type( 'country' ) );
|
|
|
|
$this->assertTrue( $zone->is_valid_location_type( 'continent' ) );
|
|
|
|
$this->assertTrue( $zone->is_valid_location_type( 'postcode' ) );
|
|
|
|
$this->assertFalse( $zone->is_valid_location_type( 'poop' ) );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::clear_locations
|
|
|
|
*/
|
|
|
|
public function test_clear_locations() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
$zone->clear_locations();
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$zone_locations = $zone->get_zone_locations();
|
|
|
|
$this->assertTrue( empty( $zone_locations ) );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::set_locations
|
|
|
|
*/
|
|
|
|
public function test_set_locations() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
$zone->clear_locations();
|
|
|
|
$zone->set_locations( array(
|
|
|
|
array(
|
|
|
|
'code' => 'US',
|
2016-08-27 01:46:45 +00:00
|
|
|
'type' => 'country',
|
2016-01-08 14:53:24 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'code' => '90210',
|
2016-08-27 01:46:45 +00:00
|
|
|
'type' => 'postcode',
|
2016-01-08 14:53:24 +00:00
|
|
|
)
|
|
|
|
) );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_locations(), array(
|
2016-01-08 14:53:24 +00:00
|
|
|
2 => (object) array(
|
|
|
|
'code' => 'US',
|
2016-08-27 01:46:45 +00:00
|
|
|
'type' => 'country',
|
2016-01-08 14:53:24 +00:00
|
|
|
),
|
|
|
|
3 => (object) array(
|
|
|
|
'code' => '90210',
|
2016-08-27 01:46:45 +00:00
|
|
|
'type' => 'postcode',
|
|
|
|
),
|
2016-01-08 14:53:24 +00:00
|
|
|
) );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::save
|
|
|
|
*/
|
|
|
|
public function test_save() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
|
|
|
// Test
|
2016-07-11 14:56:35 +00:00
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
$zone->set_zone_name( 'I am a fish' );
|
|
|
|
$zone->save();
|
2016-03-23 12:14:13 +00:00
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
|
|
|
$this->assertEquals( $zone->get_zone_name(), 'I am a fish' );
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
/**
|
2016-01-08 14:53:24 +00:00
|
|
|
* Test: WC_Shipping_Zone::add_shipping_method
|
|
|
|
*/
|
|
|
|
public function test_add_shipping_method() {
|
2016-07-11 14:56:35 +00:00
|
|
|
// Setup
|
|
|
|
WC_Helper_Shipping_Zones::create_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Test
|
|
|
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
2016-01-08 14:53:24 +00:00
|
|
|
$zone->add_shipping_method( 'flat_rate' );
|
|
|
|
$zone->add_shipping_method( 'free_shipping' );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Assert
|
2016-01-08 14:53:24 +00:00
|
|
|
$methods = $zone->get_shipping_methods();
|
|
|
|
|
|
|
|
// Assert
|
2016-03-23 14:28:45 +00:00
|
|
|
$this->assertTrue( 2 === sizeof( $methods ) );
|
2016-01-08 14:53:24 +00:00
|
|
|
$this->assertInstanceOf( 'WC_Shipping_Method', current( $methods ) );
|
|
|
|
|
2016-07-11 14:56:35 +00:00
|
|
|
// Clean
|
|
|
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
2016-01-08 14:53:24 +00:00
|
|
|
}
|
|
|
|
}
|