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

309 lines
7.1 KiB
PHP
Raw Normal View History

2016-01-08 14:53:24 +00:00
<?php
2019-01-09 18:28:01 +00:00
/**
* Tests for the WC_Shopping_Zone class.
2019-01-09 18:28:01 +00:00
*
* @package WooCommerce\Tests\Shipping
2019-01-09 18:28:01 +00:00
*/
2016-01-08 14:53:24 +00:00
/**
* Class Shipping_Zone.
* @package WooCommerce\Tests\Shipping
2016-01-08 14:53:24 +00:00
*/
class WC_Tests_Shipping_Zone extends WC_Unit_Test_Case {
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
/**
* Set up tests.
*/
public function setUp() {
parent::setUp();
WC_Helper_Shipping_Zones::create_mock_zones();
}
2016-01-08 14:53:24 +00:00
/**
* Test: WC_Shipping_Zone::get_data
*/
public function test_get_data() {
2019-01-09 18:28:01 +00:00
// Test.
2016-07-11 14:56:35 +00:00
$zone = WC_Shipping_Zones::get_zone( 1 );
$data = $zone->get_data();
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertTrue( is_array( $data ) );
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() {
2019-01-09 18:28:01 +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
2019-01-09 18:28:01 +00:00
// Assert.
$this->assertEquals( $zone->get_id(), 1 );
2016-07-11 14:56:35 +00:00
}
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() {
2019-01-09 18:28:01 +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
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertEquals( $zone->get_zone_name(), 'Local' );
}
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() {
2019-01-09 18:28:01 +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
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertEquals( $zone->get_zone_order(), 1 );
}
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() {
2019-01-09 18:28:01 +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
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertTrue( is_array( $zone->get_zone_locations() ) );
$this->assertTrue( 2 === count( $zone->get_zone_locations() ) );
2016-07-11 14:56:35 +00:00
}
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() {
2019-01-09 18:28:01 +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
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertEquals( $zone->get_formatted_location(), 'United Kingdom (UK), CB*' );
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Test.
2016-07-11 14:56:35 +00:00
$zone = WC_Shipping_Zones::get_zone( 2 );
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertEquals( $zone->get_formatted_location(), 'Europe' );
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Test.
2016-07-11 14:56:35 +00:00
$zone = WC_Shipping_Zones::get_zone( 3 );
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertEquals( $zone->get_formatted_location(), 'California' );
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Test.
2016-07-11 14:56:35 +00:00
$zone = WC_Shipping_Zones::get_zone( 4 );
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$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
/**
2016-01-08 14:53:24 +00:00
* Test: WC_Shipping_Zone::get_shipping_methods
*/
public function test_get_shipping_methods() {
2019-01-09 18:28:01 +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->add_shipping_method( 'flat_rate' );
$methods = $zone->get_shipping_methods();
2019-01-09 18:28:01 +00:00
// Assert.
$this->assertTrue( 1 === count( $methods ) );
2016-01-08 14:53:24 +00:00
$this->assertInstanceOf( 'WC_Shipping_Method', current( $methods ) );
}
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() {
2019-01-09 18:28:01 +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' );
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$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
/**
2016-01-08 14:53:24 +00:00
* Test: WC_Shipping_Zone::set_zone_order
*/
public function test_set_zone_order() {
2019-01-09 18:28:01 +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_order( 100 );
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertEquals( $zone->get_zone_order(), 100 );
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() {
2019-01-09 18:28:01 +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
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$this->assertEquals( $zone->get_zone_order(), 1 );
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() {
// Test.
2016-07-11 14:56:35 +00:00
$zone = WC_Shipping_Zones::get_zone( 1 );
2016-01-08 14:53:24 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$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' ) );
add_filter( 'woocommerce_valid_location_types', array( $this, 'add_valid_zone_location' ) );
$this->assertTrue( $zone->is_valid_location_type( 'poop' ) );
remove_filter( 'woocommerce_valid_location_types', array( $this, 'add_valid_zone_location' ), 10 );
}
/**
* Add a custom zone location.
*
* @param array $locations Valid zone locations.
* @return array New list of valid zone locations.
*/
public function add_valid_zone_location( $locations ) {
return array_merge( $locations, array( 'poop' ) );
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() {
// 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->clear_locations();
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$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
/**
2016-01-08 14:53:24 +00:00
* Test: WC_Shipping_Zone::set_locations
*/
public function test_set_locations() {
2019-01-09 18:28:01 +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->clear_locations();
$zone->set_locations(
2016-01-08 14:53:24 +00:00
array(
array(
'code' => 'US',
'type' => 'country',
),
array(
'code' => '90210',
'type' => 'postcode',
),
)
);
2016-01-08 14:53:24 +00:00
2019-01-09 18:28:01 +00:00
// Assert.
$this->assertEquals(
2019-01-09 18:28:01 +00:00
$zone->get_zone_locations(),
array(
0 => (object) array(
'code' => 'US',
'type' => 'country',
),
1 => (object) array(
'code' => '90210',
'type' => 'postcode',
),
)
);
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() {
2019-01-09 18:28:01 +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
2019-01-09 18:28:01 +00:00
// Assert.
2016-07-11 14:56:35 +00:00
$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
/**
2016-01-08 14:53:24 +00:00
* Test: WC_Shipping_Zone::add_shipping_method
*/
public function test_add_shipping_method() {
2019-01-09 18:28:01 +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' );
2019-01-09 18:28:01 +00:00
// Assert.
2016-01-08 14:53:24 +00:00
$methods = $zone->get_shipping_methods();
2019-01-09 18:28:01 +00:00
// Assert.
$this->assertTrue( 2 === count( $methods ) );
2016-01-08 14:53:24 +00:00
$this->assertInstanceOf( 'WC_Shipping_Method', current( $methods ) );
}
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' );
// Test invalid zone location.
$zone->add_location( '1234', 'custom_type' );
2016-11-15 21:53:11 +00:00
$zone->save();
$zone_id = $zone->get_id();
// Count zone locations, there should only be 2 as one is invalid.
$this->assertSame( 2, count( $zone->get_zone_locations() ) );
2016-11-15 21:53:11 +00:00
$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
}