woocommerce/tests/unit-tests/shipping-zones/shipping-zone.php

362 lines
7.9 KiB
PHP
Raw Normal View History

2016-01-08 14:53:24 +00:00
<?php
/**
* Class Shipping_Zone.
* @package WooCommerce\Tests\Shipping_Zone
*/
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
/**
* Test: WC_Shipping_Zones::get_id
2016-01-08 14:53:24 +00:00
*/
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_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',
'type' => 'country',
2016-01-08 14:53:24 +00:00
),
array(
'code' => '90210',
'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-11-15 21:53:11 +00:00
0 => (object) array(
2016-01-08 14:53:24 +00:00
'code' => 'US',
'type' => 'country',
2016-01-08 14:53:24 +00:00
),
2016-11-15 21:53:11 +00:00
1 => (object) array(
2016-01-08 14:53:24 +00:00
'code' => '90210',
'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();
$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
}
2016-11-15 21:53:11 +00:00
/**
* Test legacy zone functions.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-23 16:27:18 +00:00
*
* @expectedDeprecated WC_Shipping_Zone::read
* @expectedDeprecated WC_Shipping_Zone::create
* @expectedDeprecated WC_Shipping_Zone::update
2016-11-15 21:53:11 +00:00
*/
public function test_wc_shipping_zone_legacy() {
// Create a single zone.
$zone = new WC_Shipping_Zone();
$zone->set_zone_name( 'Local' );
$zone->set_zone_order( 1 );
$zone->add_location( 'GB', 'country' );
$zone->add_location( 'CB*', 'postcode' );
$zone->save();
$zone_id = $zone->get_id();
$zone_read = new WC_Shipping_Zone();
$zone_read->read( $zone_id );
$this->assertEquals( $zone_id, $zone_read->get_id() );
$zone = new WC_Shipping_Zone();
$zone->set_zone_name( 'Test' );
$zone->set_zone_order( 2 );
$zone->create();
$this->assertEquals( 'Test', $zone->get_zone_name() );
$this->assertNotEmpty( $zone->get_id() );
$zone->set_zone_name( 'Test 2' );
$zone->update();
$this->assertEquals( 'Test 2', $zone->get_zone_name() );
}
2016-01-08 14:53:24 +00:00
}