2017-06-07 21:31:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-01-09 18:31:51 +00:00
|
|
|
* Unit tests for the WP_Countries class.
|
|
|
|
*
|
2017-06-07 21:31:32 +00:00
|
|
|
* @package WooCommerce\Tests\Countries
|
|
|
|
*/
|
2019-01-09 18:31:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Countries tests.
|
|
|
|
*/
|
2017-06-07 21:31:32 +00:00
|
|
|
class WC_Tests_Countries extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getters.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_getters() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
$this->assertEquals( $countries->get_countries(), $countries->countries );
|
|
|
|
$this->assertEquals( $countries->get_states(), $countries->states );
|
|
|
|
}
|
|
|
|
|
2019-01-09 18:31:30 +00:00
|
|
|
/**
|
|
|
|
* Test get_shipping_continents.
|
|
|
|
*
|
|
|
|
* @since 3.6.0
|
|
|
|
*/
|
|
|
|
public function test_get_shipping_continents() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$countries = new WC_Countries();
|
2019-01-09 18:31:30 +00:00
|
|
|
$all_continents = $countries->get_continents();
|
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', 'all' );
|
|
|
|
$this->assertSame( $all_continents, $countries->get_shipping_continents() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', 'specific' );
|
|
|
|
update_option( 'woocommerce_specific_ship_to_countries', array( 'CA', 'JP' ) );
|
|
|
|
$expected = array(
|
|
|
|
'AS' => $all_continents['AS'],
|
|
|
|
'NA' => $all_continents['NA'],
|
|
|
|
);
|
|
|
|
$this->assertSame( $expected, $countries->get_shipping_continents() );
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:31:32 +00:00
|
|
|
/**
|
|
|
|
* Test get_allowed_countries.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_get_allowed_countries() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_allowed_countries', 'specific' );
|
|
|
|
update_option( 'woocommerce_specific_allowed_countries', array( 'RO', 'SI' ) );
|
2019-01-09 18:31:51 +00:00
|
|
|
$expected = array(
|
|
|
|
'RO' => 'Romania',
|
|
|
|
'SI' => 'Slovenia',
|
|
|
|
);
|
2017-06-07 21:31:32 +00:00
|
|
|
$this->assertEquals( $expected, $countries->get_allowed_countries() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_allowed_countries', 'all' );
|
|
|
|
$this->assertEquals( $countries->get_countries(), $countries->get_allowed_countries() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_allowed_countries', 'all_except' );
|
|
|
|
update_option( 'woocommerce_all_except_countries', array( 'RO', 'SI' ) );
|
|
|
|
$allowed_countries = $countries->get_allowed_countries();
|
|
|
|
$this->assertEquals( count( $countries->get_countries() ) - 2, count( $allowed_countries ) );
|
|
|
|
$this->assertFalse( isset( $allowed_countries['RO'], $allowed_countries['SI'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test get_shipping_countries.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_get_shipping_countries() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', '' );
|
|
|
|
$this->assertEquals( $countries->get_allowed_countries(), $countries->get_shipping_countries() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_allowed_countries', 'specific' );
|
|
|
|
update_option( 'woocommerce_specific_allowed_countries', array( 'RO', 'SI' ) );
|
|
|
|
$this->assertEquals( $countries->get_allowed_countries(), $countries->get_shipping_countries() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', 'all' );
|
|
|
|
$this->assertEquals( $countries->get_countries(), $countries->get_shipping_countries() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', 'specific' );
|
|
|
|
update_option( 'woocommerce_specific_ship_to_countries', array( 'RO', 'SI' ) );
|
2019-01-09 18:31:51 +00:00
|
|
|
$expected = array(
|
|
|
|
'RO' => 'Romania',
|
|
|
|
'SI' => 'Slovenia',
|
|
|
|
);
|
2017-06-07 21:31:32 +00:00
|
|
|
$this->assertEquals( $expected, $countries->get_shipping_countries() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test get_allowed_country_states.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_get_allowed_country_states() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_allowed_countries', 'all' );
|
|
|
|
$this->assertEquals( $countries->get_states(), $countries->get_allowed_country_states() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_allowed_countries', 'specific' );
|
|
|
|
update_option( 'woocommerce_specific_allowed_countries', array( 'US' ) );
|
|
|
|
|
|
|
|
$all_states = $countries->get_allowed_country_states();
|
2019-05-01 22:05:00 +00:00
|
|
|
$us_states = $all_states['US'];
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'Oregon', $us_states['OR'] );
|
|
|
|
$this->assertGreaterThanOrEqual( 50, count( $us_states ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test get_shipping_country_states.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_get_shipping_country_states() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', '' );
|
|
|
|
$this->assertEquals( $countries->get_allowed_country_states(), $countries->get_shipping_country_states() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', 'all' );
|
|
|
|
$this->assertEquals( $countries->get_states(), $countries->get_shipping_country_states() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_ship_to_countries', 'specific' );
|
|
|
|
update_option( 'woocommerce_specific_ship_to_countries', array( 'US' ) );
|
|
|
|
|
|
|
|
$all_states = $countries->get_shipping_country_states();
|
2019-05-01 22:05:00 +00:00
|
|
|
$us_states = $all_states['US'];
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'Oregon', $us_states['OR'] );
|
|
|
|
$this->assertGreaterThanOrEqual( 50, count( $us_states ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test shipping_to_prefix.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_shipping_to_prefix() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'to', $countries->shipping_to_prefix( 'RO' ) );
|
|
|
|
$this->assertEquals( 'to the', $countries->shipping_to_prefix( 'US' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test estimated_for_prefix.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_estimated_for_prefix() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'the ', $countries->estimated_for_prefix( 'GB' ) );
|
|
|
|
$this->assertEquals( '', $countries->estimated_for_prefix( 'RO' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test tax_or_vat.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_tax_or_vat() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_default_country', 'CZ' );
|
|
|
|
$this->assertEquals( 'VAT', $countries->tax_or_vat() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_default_country', 'NO' );
|
|
|
|
$this->assertEquals( 'VAT', $countries->tax_or_vat() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_default_country', 'US' );
|
|
|
|
$this->assertEquals( 'Tax', $countries->tax_or_vat() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test get_country_locale.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_get_country_locale() {
|
2019-01-09 18:31:51 +00:00
|
|
|
$countries = new WC_Countries();
|
2017-06-07 21:31:32 +00:00
|
|
|
update_option( 'woocommerce_allowed_countries', 'specific' );
|
|
|
|
update_option( 'woocommerce_specific_allowed_countries', array( 'RO', 'SI' ) );
|
|
|
|
|
|
|
|
$locales = $countries->get_country_locale();
|
|
|
|
$this->assertArrayHasKey( 'RO', $locales );
|
|
|
|
$this->assertArrayHasKey( 'SI', $locales );
|
|
|
|
$this->assertArrayNotHasKey( 'AU', $locales );
|
|
|
|
$this->assertArrayHasKey( 'default', $locales );
|
|
|
|
}
|
2019-10-31 07:43:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test get_european_union_countries.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_get_european_union_countries() {
|
|
|
|
// After Brexit there should be 27 countries in the EU.
|
|
|
|
$countries = new WC_Countries();
|
2019-10-31 09:28:19 +00:00
|
|
|
$this->assertCount( 27, $countries->get_european_union_countries() );
|
2019-10-31 07:43:05 +00:00
|
|
|
}
|
2020-02-05 14:16:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test get_vat_countries.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_get_vat_countries() {
|
|
|
|
$countries = new WC_Countries();
|
|
|
|
$this->assertCount( 80, $countries->get_vat_countries() );
|
|
|
|
}
|
2017-06-07 21:31:32 +00:00
|
|
|
}
|