2014-09-05 18:35:02 +00:00
|
|
|
<?php
|
2015-03-06 15:32:40 +00:00
|
|
|
|
2014-09-05 18:35:02 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Class Functions.
|
2015-03-06 15:32:40 +00:00
|
|
|
* @package WooCommerce\Tests\Formatting
|
2014-09-05 18:35:02 +00:00
|
|
|
* @since 2.2
|
2015-03-06 15:32:40 +00:00
|
|
|
*
|
|
|
|
* @todo Split formatting class into smaller classes
|
2014-09-05 18:35:02 +00:00
|
|
|
*/
|
2016-03-23 12:14:13 +00:00
|
|
|
class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_sanitize_taxonomy_name().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_sanitize_taxonomy_name() {
|
|
|
|
|
|
|
|
$this->assertEquals( 'name-with-spaces', wc_sanitize_taxonomy_name( 'Name With Spaces' ) );
|
|
|
|
$this->assertEquals( 'namewithtabs', wc_sanitize_taxonomy_name( 'Name With Tabs' ) );
|
|
|
|
$this->assertEquals( 'specialchars', wc_sanitize_taxonomy_name( 'special!@#$%^&*()chars' ) );
|
|
|
|
$this->assertEquals( 'look-of-ಠ_ಠ', wc_sanitize_taxonomy_name( 'Look Of ಠ_ಠ' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_filename_from_url().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_get_filename_from_url() {
|
|
|
|
|
2016-05-27 01:48:49 +00:00
|
|
|
$this->assertEquals( 'woocommerce.pdf', wc_get_filename_from_url( 'https://www.woothemes.com/woocommerce.pdf' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
$this->assertEmpty( wc_get_filename_from_url( 'ftp://wc' ) );
|
|
|
|
$this->assertEmpty( wc_get_filename_from_url( 'http://www.skyverge.com' ) );
|
2016-05-27 01:48:49 +00:00
|
|
|
$this->assertEquals( 'woocommerce', wc_get_filename_from_url( 'https://www.woothemes.com/woocommerce' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Data provider for test_wc_get_dimension().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
2014-12-21 20:57:30 +00:00
|
|
|
* @since 2.2.0
|
2014-09-05 18:35:02 +00:00
|
|
|
*/
|
2014-12-21 20:57:30 +00:00
|
|
|
public function data_provider_wc_get_dimension() {
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
// save default
|
|
|
|
$default_unit = get_option( 'woocommerce_dimension_unit' );
|
|
|
|
|
2014-12-21 20:57:30 +00:00
|
|
|
$cm = array(
|
|
|
|
array( 10, wc_get_dimension( 10, 'cm' ) ),
|
|
|
|
array( 3.937, wc_get_dimension( 10, 'in' ) ),
|
|
|
|
array( 0.10936133, wc_get_dimension( 10, 'yd' ) ),
|
|
|
|
array( 100, wc_get_dimension( 10, 'mm' ) ),
|
|
|
|
array( 0.1, wc_get_dimension( 10, 'm' ) ),
|
|
|
|
);
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_dimension_unit', 'in' );
|
2014-12-21 20:57:30 +00:00
|
|
|
$in = array(
|
|
|
|
array( 25.4, wc_get_dimension( 10, 'cm' ) ),
|
|
|
|
array( 10, wc_get_dimension( 10, 'in' ) ),
|
|
|
|
array( 0.2777777782, wc_get_dimension( 10, 'yd' ) ),
|
|
|
|
array( 254, wc_get_dimension( 10, 'mm' ) ),
|
|
|
|
array( 0.254, wc_get_dimension( 10, 'm' ) ),
|
|
|
|
);
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_dimension_unit', 'm' );
|
2014-12-21 20:57:30 +00:00
|
|
|
$m = array(
|
|
|
|
array( 1000, wc_get_dimension( 10, 'cm' ) ),
|
|
|
|
array( 393.7, wc_get_dimension( 10, 'in' ) ),
|
|
|
|
array( 10.936133, wc_get_dimension( 10, 'yd' ) ),
|
|
|
|
array( 10000, wc_get_dimension( 10, 'mm' ) ),
|
|
|
|
array( 10, wc_get_dimension( 10, 'm' ) ),
|
|
|
|
);
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_dimension_unit', 'mm' );
|
2014-12-21 20:57:30 +00:00
|
|
|
$mm = array(
|
|
|
|
array( 1, wc_get_dimension( 10, 'cm' ) ),
|
|
|
|
array( 0.3937, wc_get_dimension( 10, 'in' ) ),
|
|
|
|
array( 0.010936133, wc_get_dimension( 10, 'yd' ) ),
|
|
|
|
array( 10, wc_get_dimension( 10, 'mm' ) ),
|
|
|
|
array( 0.01, wc_get_dimension( 10, 'm' ) ),
|
|
|
|
);
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_dimension_unit', 'yd' );
|
2014-12-21 20:57:30 +00:00
|
|
|
$yd = array(
|
|
|
|
array( 914.4, wc_get_dimension( 10, 'cm' ) ),
|
|
|
|
array( 359.99928, wc_get_dimension( 10, 'in' ) ),
|
|
|
|
array( 10, wc_get_dimension( 10, 'yd' ) ),
|
|
|
|
array( 9144, wc_get_dimension( 10, 'mm' ) ),
|
|
|
|
array( 9.144, wc_get_dimension( 10, 'm' ) ),
|
|
|
|
);
|
|
|
|
|
|
|
|
$n = array(
|
|
|
|
array( 0, wc_get_dimension( -10, 'mm' ) ),
|
|
|
|
);
|
2014-09-05 18:35:02 +00:00
|
|
|
|
2016-01-13 06:45:05 +00:00
|
|
|
$custom = array(
|
|
|
|
array( 25.4, wc_get_dimension( 10, 'cm', 'in' ) ),
|
|
|
|
array( 914.4, wc_get_dimension( 10, 'cm', 'yd' ) ),
|
|
|
|
array( 393.7, wc_get_dimension( 10, 'in', 'm' ) ),
|
|
|
|
array( 0.010936133, wc_get_dimension( 10, 'yd', 'mm' ) )
|
|
|
|
);
|
|
|
|
|
2014-09-05 18:35:02 +00:00
|
|
|
// restore default
|
|
|
|
update_option( 'woocommerce_dimension_unit', $default_unit );
|
2014-12-21 20:57:30 +00:00
|
|
|
|
2016-01-13 06:45:05 +00:00
|
|
|
return array_merge( $cm, $in, $m, $mm, $yd, $n, $custom );
|
2014-12-21 20:57:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_dimension().
|
2014-12-21 20:57:30 +00:00
|
|
|
*
|
|
|
|
* @dataProvider data_provider_wc_get_dimension
|
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_get_dimension( $assert, $value ) {
|
|
|
|
|
|
|
|
$this->assertEquals( $assert, $value );
|
|
|
|
|
2014-09-05 18:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_weight().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_get_weight() {
|
|
|
|
|
|
|
|
// save default
|
|
|
|
$default_unit = get_option( 'woocommerce_weight_unit' );
|
|
|
|
|
|
|
|
// kg (default unit)
|
|
|
|
$this->assertEquals( 10, wc_get_weight( 10, 'kg' ) );
|
|
|
|
$this->assertEquals( 10000, wc_get_weight( 10, 'g' ) );
|
2015-03-11 17:23:47 +00:00
|
|
|
$this->assertEquals( 22.0462, wc_get_weight( 10, 'lbs' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
$this->assertEquals( 352.74, wc_get_weight( 10, 'oz' ) );
|
|
|
|
|
|
|
|
// g
|
|
|
|
update_option( 'woocommerce_weight_unit', 'g' );
|
|
|
|
$this->assertEquals( 0.01, wc_get_weight( 10, 'kg' ) );
|
|
|
|
$this->assertEquals( 10, wc_get_weight( 10, 'g' ) );
|
2015-03-11 17:23:47 +00:00
|
|
|
$this->assertEquals( 0.0220462, wc_get_weight( 10, 'lbs' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
$this->assertEquals( 0.35274, wc_get_weight( 10, 'oz' ) );
|
|
|
|
|
|
|
|
// lbs
|
|
|
|
update_option( 'woocommerce_weight_unit', 'lbs' );
|
2015-03-11 17:23:47 +00:00
|
|
|
$this->assertEquals( 4.53592, wc_get_weight( 10, 'kg' ) );
|
|
|
|
$this->assertEquals( 4535.92, wc_get_weight( 10, 'g' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
$this->assertEquals( 10, wc_get_weight( 10, 'lbs' ) );
|
2015-03-11 17:23:47 +00:00
|
|
|
$this->assertEquals( 160.00004208, wc_get_weight( 10, 'oz' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
// oz
|
|
|
|
update_option( 'woocommerce_weight_unit', 'oz' );
|
2015-03-11 17:23:47 +00:00
|
|
|
$this->assertEquals( 0.283495, wc_get_weight( 10, 'kg' ) );
|
|
|
|
$this->assertEquals( 283.495, wc_get_weight( 10, 'g' ) );
|
|
|
|
$this->assertEquals( 0.6249987469, wc_get_weight( 10, 'lbs' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
$this->assertEquals( 10, wc_get_weight( 10, 'oz' ) );
|
|
|
|
|
2016-01-13 06:45:05 +00:00
|
|
|
// custom from unit
|
|
|
|
$this->assertEquals( 0.283495, wc_get_weight( 10, 'kg', 'oz' ) );
|
|
|
|
$this->assertEquals( 0.01, wc_get_weight( 10, 'kg', 'g' ) );
|
|
|
|
$this->assertEquals( 4.53592, wc_get_weight( 10, 'kg', 'lbs' ) );
|
|
|
|
$this->assertEquals( 10, wc_get_weight( 10, 'kg', 'kg' ) );
|
|
|
|
|
2014-09-05 18:35:02 +00:00
|
|
|
// negative
|
|
|
|
$this->assertEquals( 0, wc_get_weight( -10, 'g' ) );
|
|
|
|
|
|
|
|
// restore default
|
|
|
|
update_option( 'woocommerce_weight_unit', $default_unit );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_trim_zeros().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_trim_zeros() {
|
|
|
|
|
|
|
|
$this->assertEquals( '$1', wc_trim_zeros( '$1.00' ) );
|
|
|
|
$this->assertEquals( '$1.10', wc_trim_zeros( '$1.10' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_round_tax_total().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Note the PHP 5.2 section of wc_round_tax_total() is excluded from test.
|
|
|
|
* coverage.
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_round_tax_total() {
|
|
|
|
|
|
|
|
$this->assertEquals( 1.25, wc_round_tax_total( 1.246 ) );
|
|
|
|
$this->assertEquals( 20, wc_round_tax_total( 19.9997 ) );
|
|
|
|
$this->assertEquals( 19.99, wc_round_tax_total( 19.99 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_format_refund_total().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_format_refund_total() {
|
|
|
|
|
|
|
|
$this->assertEquals( -10, wc_format_refund_total( 10 ) );
|
|
|
|
$this->assertEquals( 10, wc_format_refund_total( -10 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_format_decimal().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_format_decimal() {
|
|
|
|
|
|
|
|
// given string
|
|
|
|
$this->assertEquals( '9.99', wc_format_decimal( '9.99' ) );
|
|
|
|
|
|
|
|
// float
|
|
|
|
$this->assertEquals( '9.99', wc_format_decimal( 9.99 ) );
|
|
|
|
|
|
|
|
// dp = false, no rounding
|
|
|
|
$this->assertEquals( '9.9999', wc_format_decimal( 9.9999 ) );
|
|
|
|
|
|
|
|
// dp = use default (2)
|
|
|
|
$this->assertEquals( '9.99', wc_format_decimal( 9.9911, '' ) );
|
|
|
|
|
|
|
|
// dp = use default (2) and round
|
|
|
|
$this->assertEquals( '10.00', wc_format_decimal( 9.9999, '' ) );
|
|
|
|
|
|
|
|
// dp = use custom
|
|
|
|
$this->assertEquals( '9.991', wc_format_decimal( 9.9912, 3 ) );
|
|
|
|
|
|
|
|
// trim zeros
|
|
|
|
$this->assertEquals( '9', wc_format_decimal( 9.00, false, true ) );
|
|
|
|
|
|
|
|
// trim zeros and round
|
|
|
|
$this->assertEquals( '10', wc_format_decimal( 9.9999, '', true ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_float_to_string().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_float_to_string() {
|
|
|
|
|
|
|
|
// given string, return string
|
|
|
|
$this->assertEquals( '1.99', wc_float_to_string( '1.99' ) );
|
|
|
|
|
|
|
|
$this->assertEquals( '1.17', wc_float_to_string( 1.17 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_format_localized_price().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_format_localized_price() {
|
|
|
|
|
|
|
|
// save default
|
|
|
|
$decimal_sep = get_option( 'woocommerce_price_decimal_sep' );
|
|
|
|
update_option( 'woocommerce_price_decimal_sep', ',' );
|
|
|
|
|
|
|
|
$this->assertEquals( '1,17', wc_format_localized_price( '1.17' ) );
|
|
|
|
|
|
|
|
// restore default
|
|
|
|
update_option( 'woocommerce_price_decimal_sep', $decimal_sep );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_format_localized_decimal().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_format_localized_decimal() {
|
|
|
|
|
|
|
|
$this->assertEquals( '1.17', wc_format_localized_decimal( '1.17' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_clean() - note this is a basic type test as WP core already.
|
|
|
|
* has coverage for sanitized_text_field().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_clean() {
|
|
|
|
|
2015-04-09 15:24:58 +00:00
|
|
|
$this->assertEquals( 'cleaned', wc_clean( '<script>alert();</script>cleaned' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_sanitize_tooltip() - note this is a basic type test as WP core already.
|
|
|
|
* has coverage for wp_kses().
|
2015-04-09 15:24:58 +00:00
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
public function test_wc_sanitize_tooltip() {
|
|
|
|
|
|
|
|
$this->assertEquals( 'alert();cleaned', wc_sanitize_tooltip( '<script>alert();</script>cleaned' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_array_overlay().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_array_overlay() {
|
|
|
|
|
|
|
|
$a1 = array(
|
|
|
|
'apple' => 'banana',
|
|
|
|
'pear' => 'grape',
|
|
|
|
'vegetables' => array(
|
|
|
|
'cucumber' => 'asparagus',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$a2 = array(
|
|
|
|
'strawberry' => 'orange',
|
|
|
|
'apple' => 'kiwi',
|
|
|
|
'vegetables' => array(
|
|
|
|
'cucumber' => 'peas',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$overlayed = array(
|
|
|
|
'apple' => 'kiwi',
|
|
|
|
'pear' => 'grape',
|
|
|
|
'vegetables' => array(
|
|
|
|
'cucumber' => 'peas',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $overlayed, wc_array_overlay( $a1, $a2 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_stock_amount().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_stock_amount() {
|
|
|
|
|
|
|
|
$this->assertEquals( 10, wc_stock_amount( 10 ) );
|
|
|
|
$this->assertEquals( 10, wc_stock_amount( '10' ) );
|
|
|
|
$this->assertEquals( 3, wc_stock_amount( 3.43 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_woocommerce_price_format().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_get_woocommerce_price_format() {
|
|
|
|
|
|
|
|
// save default
|
|
|
|
$currency_pos = get_option( 'woocommerce_currency_pos' );
|
|
|
|
|
|
|
|
// default format (left)
|
|
|
|
$this->assertEquals( '%1$s%2$s', get_woocommerce_price_format() );
|
|
|
|
|
|
|
|
// right
|
|
|
|
update_option( 'woocommerce_currency_pos', 'right' );
|
|
|
|
$this->assertEquals( '%2$s%1$s', get_woocommerce_price_format() );
|
|
|
|
|
|
|
|
// left space
|
|
|
|
update_option( 'woocommerce_currency_pos', 'left_space' );
|
|
|
|
$this->assertEquals( '%1$s %2$s', get_woocommerce_price_format() );
|
|
|
|
|
|
|
|
// right space
|
|
|
|
update_option( 'woocommerce_currency_pos', 'right_space' );
|
|
|
|
$this->assertEquals( '%2$s %1$s', get_woocommerce_price_format() );
|
|
|
|
|
|
|
|
// restore default
|
|
|
|
update_option( 'woocommerce_currency_pos', $currency_pos );
|
|
|
|
}
|
|
|
|
|
2015-04-09 15:35:35 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_price_thousand_separator().
|
2015-04-09 15:35:35 +00:00
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
public function test_wc_get_price_thousand_separator() {
|
|
|
|
$separator = get_option( 'woocommerce_price_thousand_sep' );
|
|
|
|
|
|
|
|
// defualt value
|
|
|
|
$this->assertEquals( ',', wc_get_price_thousand_separator() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_thousand_sep', '.' );
|
|
|
|
$this->assertEquals( '.', wc_get_price_thousand_separator() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_thousand_sep', '<.>' );
|
2015-06-01 14:03:19 +00:00
|
|
|
$this->assertEquals( '<.>', wc_get_price_thousand_separator() );
|
2015-04-09 15:35:35 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_price_thousand_sep', $separator );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_price_decimal_separator().
|
2015-04-09 15:35:35 +00:00
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
public function test_wc_get_price_decimal_separator() {
|
|
|
|
$separator = get_option( 'woocommerce_price_decimal_sep' );
|
|
|
|
|
|
|
|
// defualt value
|
|
|
|
$this->assertEquals( '.', wc_get_price_decimal_separator() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_decimal_sep', ',' );
|
|
|
|
$this->assertEquals( ',', wc_get_price_decimal_separator() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_decimal_sep', '<.>' );
|
2015-06-01 14:03:19 +00:00
|
|
|
$this->assertEquals( '<.>', wc_get_price_decimal_separator() );
|
2015-04-09 15:35:35 +00:00
|
|
|
|
|
|
|
update_option( 'woocommerce_price_decimal_sep', $separator );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_get_price_decimals().
|
2015-04-09 15:35:35 +00:00
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
public function test_wc_get_price_decimals() {
|
|
|
|
$decimals = get_option( 'woocommerce_price_num_decimals' );
|
|
|
|
|
|
|
|
// defualt value
|
|
|
|
$this->assertEquals( 2, wc_get_price_decimals() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_num_decimals', '1' );
|
|
|
|
$this->assertEquals( 1, wc_get_price_decimals() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_num_decimals', '-2' );
|
|
|
|
$this->assertEquals( 2, wc_get_price_decimals() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_num_decimals', '2.50' );
|
|
|
|
$this->assertEquals( 2, wc_get_price_decimals() );
|
|
|
|
|
|
|
|
update_option( 'woocommerce_price_num_decimals', $decimals );
|
|
|
|
}
|
|
|
|
|
2014-09-05 18:35:02 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_price().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_price() {
|
|
|
|
|
|
|
|
// common prices
|
2016-05-16 10:14:31 +00:00
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>1.00</span>', wc_price( 1 ) );
|
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>1.10</span>', wc_price( 1.1 ) );
|
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>1.17</span>', wc_price( 1.17 ) );
|
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>1,111.17</span>', wc_price( 1111.17 ) );
|
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>0.00</span>', wc_price( 0 ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
// different currency
|
2016-05-16 10:38:22 +00:00
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>1,111.17</span>', wc_price( 1111.17, array( 'currency' => 'USD' ) ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
// negative price
|
2016-05-16 10:14:31 +00:00
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount">-<span class="woocommerce-Price-currencySymbol">£</span>1.17</span>', wc_price( -1.17 ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
// bogus prices
|
2016-05-16 10:14:31 +00:00
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>0.00</span>', wc_price( null ) );
|
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>0.00</span>', wc_price( 'Q' ) );
|
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>0.00</span>', wc_price( 'ಠ_ಠ' ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
// trim zeros
|
|
|
|
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
|
2016-05-16 10:14:31 +00:00
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>1</span>', wc_price( 1.00 ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
remove_filter( 'woocommerce_price_trim_zeros', '__return_true' );
|
|
|
|
|
|
|
|
// ex tax label
|
|
|
|
$calc_taxes = get_option( 'woocommerce_calc_taxes' );
|
|
|
|
update_option( 'woocommerce_calc_taxes', 'yes' );
|
2016-05-16 11:00:03 +00:00
|
|
|
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>1,111.17</span> <small class="woocommerce-Price-taxLabel tax_label">(ex. VAT)</small>', wc_price( '1111.17', array( 'ex_tax_label' => true ) ) );
|
2014-09-05 18:35:02 +00:00
|
|
|
update_option( 'woocommerce_calc_taxes', $calc_taxes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_let_to_num().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_let_to_num() {
|
|
|
|
|
|
|
|
$sizes = array(
|
|
|
|
'10K' => 10240,
|
|
|
|
'10M' => 10485760,
|
|
|
|
'10G' => 10737418240,
|
|
|
|
'10T' => 10995116277760,
|
|
|
|
'10P' => 11258999068426240,
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $sizes as $notation => $size ) {
|
|
|
|
$this->assertEquals( $size, wc_let_to_num( $notation ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_date_format().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_date_format() {
|
|
|
|
|
|
|
|
$this->assertEquals( get_option( 'date_format' ), wc_date_format() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_time_format().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_time_format() {
|
|
|
|
$this->assertEquals( get_option( 'time_format' ), wc_time_format() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_timezone_string().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_timezone_string() {
|
|
|
|
|
|
|
|
// test when timezone string exists
|
|
|
|
update_option( 'timezone_string', 'America/New_York' );
|
|
|
|
$this->assertEquals( 'America/New_York', wc_timezone_string() );
|
|
|
|
|
|
|
|
// restore default
|
|
|
|
update_option( 'timezone_string', '' );
|
|
|
|
|
|
|
|
// test with missing UTC offset
|
|
|
|
delete_option( 'gmt_offset' );
|
|
|
|
$this->assertEquals( 'UTC', wc_timezone_string() );
|
|
|
|
|
|
|
|
// test with manually set UTC offset
|
|
|
|
update_option( 'gmt_offset', -4 );
|
2014-12-10 18:09:01 +00:00
|
|
|
$this->assertEquals( 'America/Halifax', wc_timezone_string() );
|
2014-09-05 18:35:02 +00:00
|
|
|
|
|
|
|
// test with invalid offset
|
|
|
|
update_option( 'gmt_offset', 99 );
|
|
|
|
$this->assertEquals( 'UTC', wc_timezone_string() );
|
|
|
|
|
|
|
|
// restore default
|
|
|
|
update_option( 'gmt_offset', '0' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_rgb_from_hex().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_rgb_from_hex() {
|
|
|
|
|
|
|
|
$rgb = array(
|
|
|
|
'R' => 0,
|
|
|
|
'G' => 93,
|
|
|
|
'B' => 171,
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $rgb, wc_rgb_from_hex( '005dab' ) );
|
|
|
|
$this->assertEquals( $rgb, wc_rgb_from_hex( '#005dab' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_hex_darker().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_hex_darker() {
|
|
|
|
|
|
|
|
$this->assertEquals( '#004178', wc_hex_darker( '005dab' ) );
|
|
|
|
$this->assertEquals( '#004178', wc_hex_darker( '#005dab' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_hex_lighter().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_hex_lighter() {
|
|
|
|
|
|
|
|
$this->assertEquals( '#4d8ec4', wc_hex_lighter( '005dab' ) );
|
|
|
|
$this->assertEquals( '#4d8ec4', wc_hex_lighter( '#005dab' ) );
|
|
|
|
$this->assertEquals( '#0c3a3b', wc_hex_lighter( '0a3839', 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_light_or_dark().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_light_or_dark() {
|
|
|
|
|
|
|
|
$this->assertEquals( '#FFFFFF', wc_light_or_dark( '005dab' ) );
|
|
|
|
$this->assertEquals( '#FFFFFF', wc_light_or_dark( '#005dab' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_format_hex().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_format_hex() {
|
|
|
|
|
|
|
|
$this->assertEquals( '#CCCCCC', wc_format_hex( 'CCC' ) );
|
|
|
|
$this->assertEquals( '#CCCCCC', wc_format_hex( '#CCC' ) );
|
|
|
|
$this->assertEquals( null, wc_format_hex( null ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_format_postcode().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_format_postcode() {
|
|
|
|
|
|
|
|
// generic postcode
|
|
|
|
$this->assertEquals( '02111', wc_format_postcode( ' 02111 ', 'US' ) );
|
|
|
|
|
|
|
|
// UK postcode
|
|
|
|
$this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_format_phone_number().
|
2014-09-05 18:35:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_format_phone_number() {
|
|
|
|
|
|
|
|
$this->assertEquals( '1-610-385-0000', wc_format_phone_number( '1.610.385.0000' ) );
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:37:57 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test wc_trim_string().
|
2015-03-03 11:37:57 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
public function test_wc_trim_string() {
|
|
|
|
$this->assertEquals( 'string', wc_trim_string( 'string' ) );
|
|
|
|
$this->assertEquals( 's...', wc_trim_string( 'string', 4 ) );
|
|
|
|
$this->assertEquals( 'st.', wc_trim_string( 'string', 3, '.' ) );
|
2016-02-23 11:40:16 +00:00
|
|
|
$this->assertEquals( 'string¥', wc_trim_string( 'string¥', 7, '' ) );
|
2015-03-03 11:37:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-05 18:35:02 +00:00
|
|
|
}
|