2014-09-05 18:35:02 +00:00
< ? php
2017-10-05 14:20:52 +00:00
/**
* Formatting functions
*
* @ package WooCommerce\Tests\Formatting
*/
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 .
2017-10-05 14:20:52 +00:00
*
2014-09-05 18:35:02 +00:00
* @ since 2.2
*/
2018-07-19 13:26:52 +00:00
class WC_Tests_Formatting_Functions extends WP_HTTP_TestCase {
public function setUp () {
parent :: setUp ();
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
$this -> http_responder = array ( $this , 'mock_http_responses' );
}
2017-10-05 14:20:52 +00:00
/**
* Test wc_string_to_bool () .
*
* @ since 3.3 . 0
*/
public function test_wc_string_to_bool () {
$this -> assertTrue ( wc_string_to_bool ( 1 ) );
$this -> assertTrue ( wc_string_to_bool ( 'yes' ) );
$this -> assertTrue ( wc_string_to_bool ( 'true' ) );
$this -> assertFalse ( wc_string_to_bool ( 0 ) );
$this -> assertFalse ( wc_string_to_bool ( 'no' ) );
$this -> assertFalse ( wc_string_to_bool ( 'false' ) );
}
/**
* Test wc_bool_to_string () .
*
* @ since 3.3 . 0
*/
public function test_wc_bool_to_string () {
2018-06-27 07:37:44 +00:00
$this -> assertEquals ( array ( 'yes' , 'no' ), array ( wc_bool_to_string ( true ), wc_bool_to_string ( false ) ) );
2017-10-05 14:20:52 +00:00
}
/**
* Test wc_string_to_array () .
*
* @ since 3.3 . 0
*/
public function test_wc_string_to_array () {
$this -> assertEquals ( array (
'foo' ,
'bar' ,
), wc_string_to_array ( 'foo|bar' , '|' ) );
}
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 ಠ_ಠ' ) );
}
2017-10-05 14:20:52 +00:00
/**
* Test wc_sanitize_permalink () .
*
* @ since 3.3 . 0
*/
public function test_wc_sanitize_permalink () {
$this -> assertEquals ( 'foo.com/bar' , wc_sanitize_permalink ( 'http://foo.com/bar' ) );
}
2014-09-05 18:35:02 +00:00
/**
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-07-05 22:52:16 +00:00
$this -> assertEquals ( 'woocommerce.pdf' , wc_get_filename_from_url ( 'https://woocommerce.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' ) );
2018-06-27 07:37:44 +00:00
$this -> assertEquals ( 'woocommerce' , wc_get_filename_from_url ( 'https://woocommerce.com/woocommerce' ) );
2014-09-05 18:35:02 +00:00
}
/**
2017-10-05 14:20:52 +00:00
* Test wc_get_dimension () .
2014-09-05 18:35:02 +00:00
*
2017-10-05 14:20:52 +00:00
* @ since 2.2
2014-09-05 18:35:02 +00:00
*/
2017-10-05 14:20:52 +00:00
public function test_wc_get_dimension () {
// Save default.
2014-09-05 18:35:02 +00:00
$default_unit = get_option ( 'woocommerce_dimension_unit' );
2017-10-05 14:20:52 +00:00
// cm (default unit).
$this -> assertEquals ( array ( 10 , 3.937 , 0.10936133 , 100 , 0.1 ), array (
wc_get_dimension ( 10 , 'cm' ),
wc_get_dimension ( 10 , 'in' ),
wc_get_dimension ( 10 , 'yd' ),
wc_get_dimension ( 10 , 'mm' ),
wc_get_dimension ( 10 , 'm' ),
) );
2014-09-05 18:35:02 +00:00
2017-10-05 14:20:52 +00:00
// in.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_dimension_unit' , 'in' );
2017-10-05 14:20:52 +00:00
$this -> assertEquals ( array ( 25.4 , 10 , 0.2777777782 , 254 , 0.254 ), array (
wc_get_dimension ( 10 , 'cm' ),
wc_get_dimension ( 10 , 'in' ),
wc_get_dimension ( 10 , 'yd' ),
wc_get_dimension ( 10 , 'mm' ),
wc_get_dimension ( 10 , 'm' ),
) );
// m.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_dimension_unit' , 'm' );
2017-10-05 14:20:52 +00:00
$this -> assertEquals ( array ( 1000 , 393.7 , 10.936133 , 10000 , 10 ), array (
wc_get_dimension ( 10 , 'cm' ),
wc_get_dimension ( 10 , 'in' ),
wc_get_dimension ( 10 , 'yd' ),
wc_get_dimension ( 10 , 'mm' ),
wc_get_dimension ( 10 , 'm' ),
) );
// mm.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_dimension_unit' , 'mm' );
2017-10-05 14:20:52 +00:00
$this -> assertEquals ( array ( 1 , 0.3937 , 0.010936133 , 10 , 0.01 ), array (
wc_get_dimension ( 10 , 'cm' ),
wc_get_dimension ( 10 , 'in' ),
wc_get_dimension ( 10 , 'yd' ),
wc_get_dimension ( 10 , 'mm' ),
wc_get_dimension ( 10 , 'm' ),
) );
// yd.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_dimension_unit' , 'yd' );
2017-10-05 14:20:52 +00:00
$this -> assertEquals ( array ( 914.4 , 359.99928 , 10 , 9144 , 9.144 ), array (
wc_get_dimension ( 10 , 'cm' ),
wc_get_dimension ( 10 , 'in' ),
wc_get_dimension ( 10 , 'yd' ),
wc_get_dimension ( 10 , 'mm' ),
wc_get_dimension ( 10 , 'm' ),
) );
// Negative.
$this -> assertEquals ( 0 , wc_get_dimension ( - 10 , 'mm' ) );
// Custom.
$this -> assertEquals ( array ( 25.4 , 914.4 , 393.7 , 0.010936133 ), array (
wc_get_dimension ( 10 , 'cm' , 'in' ),
wc_get_dimension ( 10 , 'cm' , 'yd' ),
wc_get_dimension ( 10 , 'in' , 'm' ),
wc_get_dimension ( 10 , 'yd' , 'mm' ),
) );
// Restore default.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_dimension_unit' , $default_unit );
}
/**
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 () {
2017-10-05 14:20:52 +00:00
// Save default.
2014-09-05 18:35:02 +00:00
$default_unit = get_option ( 'woocommerce_weight_unit' );
2017-10-05 14:20:52 +00:00
// kg (default unit).
2014-09-05 18:35:02 +00:00
$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' ) );
2017-10-05 14:20:52 +00:00
// g.
2014-09-05 18:35:02 +00:00
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' ) );
2017-10-05 14:20:52 +00:00
// lbs.
2014-09-05 18:35:02 +00:00
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
2017-10-05 14:20:52 +00:00
// oz.
2014-09-05 18:35:02 +00:00
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' ) );
2017-10-05 14:20:52 +00:00
// Custom from unit.
2016-01-13 06:45:05 +00:00
$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' ) );
2017-10-05 14:20:52 +00:00
// Negative.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( 0 , wc_get_weight ( - 10 , 'g' ) );
2017-10-05 14:20:52 +00:00
// Restore default.
2014-09-05 18:35:02 +00:00
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
*
* @ since 2.2
*/
public function test_wc_round_tax_total () {
2017-12-05 12:10:23 +00:00
update_option ( 'woocommerce_prices_include_tax' , 'no' );
$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 ) );
$this -> assertEquals ( 19.99 , wc_round_tax_total ( 19.99 ) );
$this -> assertEquals ( 83 , wc_round_tax_total ( 82.5 , 0 ) );
$this -> assertEquals ( 83 , wc_round_tax_total ( 82.54 , 0 ) );
$this -> assertEquals ( 83 , wc_round_tax_total ( 82.546 , 0 ) );
update_option ( 'woocommerce_prices_include_tax' , 'yes' );
2014-09-05 18:35:02 +00:00
$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 ) );
2017-12-05 12:10:23 +00:00
$this -> assertEquals ( 19.99 , wc_round_tax_total ( 19.99 ) );
$this -> assertEquals ( 82 , wc_round_tax_total ( 82.5 , 0 ) );
$this -> assertEquals ( 83 , wc_round_tax_total ( 82.54 , 0 ) );
$this -> assertEquals ( 83 , wc_round_tax_total ( 82.546 , 0 ) );
// Default.
update_option ( 'woocommerce_prices_include_tax' , 'no' );
2014-09-05 18:35:02 +00:00
}
/**
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 () {
2017-10-05 14:20:52 +00:00
// Given string.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '9.99' , wc_format_decimal ( '9.99' ) );
2017-10-05 14:20:52 +00:00
// Float.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '9.99' , wc_format_decimal ( 9.99 ) );
2017-10-05 14:20:52 +00:00
// DP false, no rounding.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '9.9999' , wc_format_decimal ( 9.9999 ) );
2017-10-05 14:20:52 +00:00
// DP when empty string uses default as 2.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '9.99' , wc_format_decimal ( 9.9911 , '' ) );
2017-10-05 14:20:52 +00:00
// DP use default as 2 and round.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '10.00' , wc_format_decimal ( 9.9999 , '' ) );
2017-10-05 14:20:52 +00:00
// DP use custom.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '9.991' , wc_format_decimal ( 9.9912 , 3 ) );
2017-10-05 14:20:52 +00:00
// Trim zeros.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '9' , wc_format_decimal ( 9.00 , false , true ) );
2017-10-05 14:20:52 +00:00
// Trim zeros and round.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '10' , wc_format_decimal ( 9.9999 , '' , true ) );
2017-05-25 10:51:45 +00:00
2017-10-05 14:20:52 +00:00
// Given string with thousands in german format.
2017-05-30 10:54:31 +00:00
update_option ( 'woocommerce_price_decimal_sep' , ',' );
update_option ( 'woocommerce_price_thousand_sep' , '.' );
2017-10-05 14:20:52 +00:00
// Given string.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '9.99' , wc_format_decimal ( '9.99' ) );
2017-10-05 14:20:52 +00:00
// Float.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '9.99' , wc_format_decimal ( 9.99 ) );
2017-10-05 14:20:52 +00:00
// DP false, no rounding.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '9.9999' , wc_format_decimal ( 9.9999 ) );
2017-10-05 14:20:52 +00:00
// DP when empty string uses default as 2.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '9.99' , wc_format_decimal ( 9.9911 , '' ) );
2017-10-05 14:20:52 +00:00
// DP use default as 2 and round.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '10.00' , wc_format_decimal ( 9.9999 , '' ) );
2017-10-05 14:20:52 +00:00
// DP use custom.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '9.991' , wc_format_decimal ( 9.9912 , 3 ) );
2017-10-05 14:20:52 +00:00
// Trim zeros.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '9' , wc_format_decimal ( 9.00 , false , true ) );
2017-10-05 14:20:52 +00:00
// Trim zeros and round.
2017-06-06 11:44:58 +00:00
$this -> assertEquals ( '10' , wc_format_decimal ( 9.9999 , '' , true ) );
2017-05-30 10:54:31 +00:00
2017-11-06 18:21:14 +00:00
update_option ( 'woocommerce_price_num_decimals' , '8' );
2017-11-06 18:00:47 +00:00
// Floats.
2017-11-06 18:21:14 +00:00
$this -> assertEquals ( '0.00001' , wc_format_decimal ( 0.00001 ) );
$this -> assertEquals ( '0.22222222' , wc_format_decimal ( 0.22222222 ) );
2017-11-06 18:00:47 +00:00
2017-11-06 18:21:14 +00:00
update_option ( 'woocommerce_price_num_decimals' , '2' );
2017-05-30 10:54:31 +00:00
update_option ( 'woocommerce_price_decimal_sep' , '.' );
update_option ( 'woocommerce_price_thousand_sep' , ',' );
2014-09-05 18:35:02 +00:00
}
/**
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 () {
2017-10-05 14:20:52 +00:00
// Given string, return string.
2014-09-05 18:35:02 +00:00
$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 () {
2017-10-05 14:20:52 +00:00
// Save default.
2014-09-05 18:35:02 +00:00
$decimal_sep = get_option ( 'woocommerce_price_decimal_sep' );
update_option ( 'woocommerce_price_decimal_sep' , ',' );
$this -> assertEquals ( '1,17' , wc_format_localized_price ( '1.17' ) );
2017-10-05 14:20:52 +00:00
// Restore default.
2014-09-05 18:35:02 +00:00
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' ) );
}
/**
2017-10-05 14:20:52 +00:00
* Test wc_format_coupon_code () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_coupon_code () {
$this -> assertEquals ( 'foo#bar' , wc_format_coupon_code ( 'FOO#bar<script>alert();</script>' ) );
}
/**
* Test wc_clean () .
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' ) );
2017-10-05 14:20:52 +00:00
$this -> assertEquals ( array ( 'cleaned' , 'foo' ), wc_clean ( array ( '<script>alert();</script>cleaned' , 'foo' ) ) );
}
/**
* Test wc_sanitize_textarea () .
*
* @ since 3.3 . 0
*/
public function test_wc_sanitize_textarea () {
$this -> assertEquals ( " foo \n cleaned \n bar " , wc_sanitize_textarea ( " foo \n <script>alert();</script>cleaned \n bar " ) );
2015-04-09 15:24:58 +00:00
}
/**
2017-10-05 14:20:52 +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 () {
2017-10-05 14:20:52 +00:00
$this -> assertEquals ( 'alert();cleaned<p>foo</p><span>bar</span>' , wc_sanitize_tooltip ( '<script>alert();</script>cleaned<p>foo</p><span>bar</span>' ) );
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' ,
2016-08-27 01:46:45 +00:00
),
2014-09-05 18:35:02 +00:00
);
$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 () {
2017-10-05 14:20:52 +00:00
// Save default.
2014-09-05 18:35:02 +00:00
$currency_pos = get_option ( 'woocommerce_currency_pos' );
2017-10-05 14:20:52 +00:00
// Default format (left).
2018-01-12 14:20:39 +00:00
$this -> assertEquals ( '%1$s%2$s' , get_woocommerce_price_format () );
2014-09-05 18:35:02 +00:00
2017-10-05 14:20:52 +00:00
// Right.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_currency_pos' , 'right' );
2018-01-12 14:20:39 +00:00
$this -> assertEquals ( '%2$s%1$s' , get_woocommerce_price_format () );
2014-09-05 18:35:02 +00:00
2017-10-05 14:20:52 +00:00
// Left space.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_currency_pos' , 'left_space' );
2018-01-12 14:20:39 +00:00
$this -> assertEquals ( '%1$s %2$s' , get_woocommerce_price_format () );
2014-09-05 18:35:02 +00:00
2017-10-05 14:20:52 +00:00
// Right space.
2014-09-05 18:35:02 +00:00
update_option ( 'woocommerce_currency_pos' , 'right_space' );
2018-01-12 14:20:39 +00:00
$this -> assertEquals ( '%2$s %1$s' , get_woocommerce_price_format () );
2014-09-05 18:35:02 +00:00
2017-10-05 14:20:52 +00:00
// Restore default.
2014-09-05 18:35:02 +00:00
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' );
2017-10-05 14:20:52 +00:00
// Default value.
2015-04-09 15:35:35 +00:00
$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' );
2017-10-05 14:20:52 +00:00
// Default value.
2015-04-09 15:35:35 +00:00
$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' );
2017-10-05 14:20:52 +00:00
// Default value.
2015-04-09 15:35:35 +00:00
$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 () {
2017-10-05 14:20:52 +00:00
// Common prices.
2018-01-12 14:20:39 +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
2017-10-05 14:20:52 +00:00
// Different currency.
2018-01-12 14:20:39 +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
2017-10-05 14:20:52 +00:00
// Negative price.
2018-01-12 14:20:39 +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
2017-10-05 14:20:52 +00:00
// Bogus prices.
2018-01-12 14:20:39 +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
2017-10-05 14:20:52 +00:00
// Trim zeros.
2014-09-05 18:35:02 +00:00
add_filter ( 'woocommerce_price_trim_zeros' , '__return_true' );
2018-01-12 14:20:39 +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' );
2017-10-05 14:20:52 +00:00
// Ex tax label.
2014-09-05 18:35:02 +00:00
$calc_taxes = get_option ( 'woocommerce_calc_taxes' );
update_option ( 'woocommerce_calc_taxes' , 'yes' );
2018-01-12 14:20:39 +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 () {
2017-10-05 14:20:52 +00:00
$this -> assertEquals ( array ( 10240 , 10485760 , 10737418240 , 10995116277760 , 11258999068426240 ), array (
wc_let_to_num ( '10K' ),
wc_let_to_num ( '10M' ),
wc_let_to_num ( '10G' ),
wc_let_to_num ( '10T' ),
wc_let_to_num ( '10P' ),
) );
2014-09-05 18:35:02 +00:00
}
/**
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 () );
}
2017-10-05 14:20:52 +00:00
/**
* Test wc_string_to_timestamp () .
*
* @ since 3.3 . 0
*/
public function test_wc_string_to_timestamp () {
$this -> assertEquals ( 1507075200 , wc_string_to_timestamp ( '2017-10-04' ) );
$this -> assertEquals ( 1507075200 , wc_string_to_timestamp ( '2017-10-04' , strtotime ( '3000-10-04' ) ) );
}
/**
* Test wc_string_to_datetime () .
*
* @ since 3.3 . 0
*/
public function test_wc_string_to_datetime () {
$data = wc_string_to_datetime ( '2014-10-04' );
$this -> assertInstanceOf ( 'WC_DateTime' , $data );
$this -> assertEquals ( 1412380800 , $data -> getTimestamp () );
}
2014-09-05 18:35:02 +00:00
/**
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 () {
2017-10-05 14:20:52 +00:00
// Test when timezone string exists.
2014-09-05 18:35:02 +00:00
update_option ( 'timezone_string' , 'America/New_York' );
$this -> assertEquals ( 'America/New_York' , wc_timezone_string () );
2017-10-05 14:20:52 +00:00
// Restore default.
2014-09-05 18:35:02 +00:00
update_option ( 'timezone_string' , '' );
2017-10-05 14:20:52 +00:00
// Test with missing UTC offset.
2014-09-05 18:35:02 +00:00
delete_option ( 'gmt_offset' );
$this -> assertEquals ( 'UTC' , wc_timezone_string () );
2017-10-05 14:20:52 +00:00
// Test with manually set UTC offset.
2014-09-05 18:35:02 +00:00
update_option ( 'gmt_offset' , - 4 );
2017-03-23 00:11:21 +00:00
$this -> assertNotEquals ( 'UTC' , wc_timezone_string () );
2014-09-05 18:35:02 +00:00
2017-10-05 14:20:52 +00:00
// Test with invalid offset.
2014-09-05 18:35:02 +00:00
update_option ( 'gmt_offset' , 99 );
$this -> assertEquals ( 'UTC' , wc_timezone_string () );
2017-10-05 14:20:52 +00:00
// Restore default.
2014-09-05 18:35:02 +00:00
update_option ( 'gmt_offset' , '0' );
}
2017-10-05 14:20:52 +00:00
/**
* Test wc_timezone_offset () .
*
* @ since 3.3 . 0
*/
public function test_wc_timezone_offset () {
$this -> assertEquals ( 0.0 , wc_timezone_offset () );
}
2014-09-05 18:35:02 +00:00
/**
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 () {
2017-10-05 14:20:52 +00:00
// Generic postcode.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( '02111' , wc_format_postcode ( ' 02111 ' , 'US' ) );
2017-10-05 14:20:52 +00:00
// US 9-digit postcode.
2016-09-09 18:05:44 +00:00
$this -> assertEquals ( '02111-9999' , wc_format_postcode ( ' 021119999 ' , 'US' ) );
2017-10-05 14:20:52 +00:00
// UK postcode.
2014-09-05 18:35:02 +00:00
$this -> assertEquals ( 'PCRN 1ZZ' , wc_format_postcode ( 'pcrn1zz' , 'GB' ) );
2017-10-05 14:20:52 +00:00
// BR/PL postcode.
$this -> assertEquals ( '99999-999' , wc_format_postcode ( '99999999' , 'BR' ) );
// JP postcode.
$this -> assertEquals ( '999-9999' , wc_format_postcode ( '9999999' , 'JP' ) );
}
/**
* Test wc_normalize_postcode () .
*
* @ since 3.3 . 0
*/
public function test_wc_normalize_postcode () {
$this -> assertEquals ( '99999999' , wc_normalize_postcode ( '99999-999' ) );
2014-09-05 18:35:02 +00:00
}
/**
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' ) );
2018-06-27 07:37:44 +00:00
$this -> assertEquals ( '+47 0000 00003' , wc_format_phone_number ( ' +47 0000 00003 ' ) ); // This number contains non-visible unicode chars at the beginning and end of string, should remove all those.
$this -> assertEquals ( '27 00 00 0000' , wc_format_phone_number ( '27 00 00 0000' ) );
2014-09-05 18:35:02 +00:00
}
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' ) );
2018-06-27 07:37:44 +00:00
$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
}
2017-10-05 14:20:52 +00:00
/**
* Test wc_format_content () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_content () {
$this -> assertEquals ( " <p>foo</p> \n " , wc_format_content ( 'foo' ) );
}
/**
* Test wc_sanitize_term_text_based () .
*
* @ since 3.3 . 0
*/
public function test_wc_sanitize_term_text_based () {
$this -> assertEquals ( 'foo' , wc_sanitize_term_text_based ( " <p>foo</p> \n " ) );
}
/**
* Test wc_make_numeric_postcode () .
*
* @ since 3.3 . 0
*/
public function test_wc_make_numeric_postcode () {
$this -> assertEquals ( '16050300' , wc_make_numeric_postcode ( 'PE30' ) );
}
/**
* Test wc_format_stock_for_display () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_stock_for_display () {
$product = WC_Helper_Product :: create_simple_product ();
$product -> set_stock_quantity ( '10' );
$this -> assertEquals ( '10 in stock' , wc_format_stock_for_display ( $product ) );
$product -> set_stock_quantity ( '1' );
$default = get_option ( 'woocommerce_stock_format' );
update_option ( 'woocommerce_stock_format' , 'low_amount' );
$this -> assertEquals ( 'Only 1 left in stock' , wc_format_stock_for_display ( $product ) );
update_option ( 'woocommerce_stock_format' , $default );
$product -> set_stock_quantity ( '-1' );
$product -> set_manage_stock ( true );
$product -> set_backorders ( 'notify' );
$this -> assertEquals ( '-1 in stock (can be backordered)' , wc_format_stock_for_display ( $product ) );
$product -> delete ( true );
}
/**
* Test wc_format_stock_quantity_for_display () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_stock_quantity_for_display () {
$product = WC_Helper_Product :: create_simple_product ();
$product -> set_stock_quantity ( '10' );
$this -> assertEquals ( '10' , wc_format_stock_quantity_for_display ( $product -> get_stock_quantity (), $product ) );
$product -> delete ( true );
}
/**
* Test wc_format_sale_price () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_sale_price () {
2018-01-12 14:20:39 +00:00
$this -> assertEquals ( '<del><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>10.00</span></del> <ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>5.00</span></ins>' , wc_format_sale_price ( '10' , '5' ) );
2017-10-05 14:20:52 +00:00
}
/**
* Test wc_format_price_range () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_price_range () {
2018-01-12 14:20:39 +00:00
$this -> assertEquals ( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>10.00</span> – <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>5.00</span>' , wc_format_price_range ( '10' , '5' ) );
2017-10-05 14:20:52 +00:00
}
/**
* Test wc_format_weight () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_weight () {
$this -> assertEquals ( '10 kg' , wc_format_weight ( '10' ) );
}
/**
* Test wc_format_dimensions () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_dimensions () {
$this -> assertEquals ( '10 x 10 x 10 cm' , wc_format_dimensions ( array ( 10 , 10 , 10 ) ) );
}
/**
* Test wc_format_datetime () .
*
* @ since 3.3 . 0
*/
public function test_wc_format_datetime () {
$date = new WC_DateTime ( '2017-10-05' , new DateTimeZone ( 'UTC' ) );
$this -> assertEquals ( 'October 5, 2017' , wc_format_datetime ( $date ) );
$this -> assertEquals ( '' , wc_format_datetime ( 'foo' ) );
}
/**
* Test wc_do_oembeds () .
*
* @ since 3.3 . 0
*/
public function test_wc_do_oembeds () {
// In this case should only return the URL back, since oEmbed will run other actions on frontend.
2018-07-19 13:26:52 +00:00
$this -> assertEquals (
" <iframe width='500' height='281' src='https://videopress.com/embed/9sRCUigm?hd=0' frameborder='0' allowfullscreen></iframe><script src='https://v0.wordpress.com/js/next/videopress-iframe.js?m=1435166243'></script> " , // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
wc_do_oembeds ( 'https://wordpress.tv/2015/10/19/mike-jolley-user-onboarding-for-wordpress-plugins/' )
);
}
/**
* Provides a mocked response for the oembed test . This way it is not necessary to perform
* a regular request to an external server which would significantly slow down the tests .
*
* This function is called by WP_HTTP_TestCase :: http_request_listner () .
*
* @ param array $request Request arguments .
* @ param string $url URL of the request .
*
* @ return array | false mocked response or false to let WP perform a regular request .
*/
protected function mock_http_responses ( $request , $url ) {
$mocked_response = false ;
if ( false !== strpos ( $url , 'https://wordpress.tv/oembed/' ) ) {
$mocked_response = array (
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
'body' => '{"type":"video","version":"1.0","title":null,"width":500,"height":281,"html":"<iframe width=\'500\' height=\'281\' src=\'https:\/\/videopress.com\/embed\/9sRCUigm?hd=0\' frameborder=\'0\' allowfullscreen><\/iframe><script src=\'https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1435166243\'><\/script>"}' ,
'response' => array ( 'code' => 200 ),
);
}
return $mocked_response ;
2017-10-05 14:20:52 +00:00
}
/**
* Test wc_get_string_before_colon () .
*
* @ since 3.3 . 0
*/
public function test_wc_get_string_before_colon () {
$this -> assertEquals ( 'foo' , wc_get_string_before_colon ( 'foo:1' ) );
}
/**
* Test wc_array_merge_recursive_numeric () .
*
* @ since 3.3 . 0
*/
public function test_wc_array_merge_recursive_numeric () {
$a = array (
'A' => 'bob' ,
'sum' => 10 ,
'C' => array (
'x' ,
'y' ,
'z' => 50 ,
),
);
$b = array (
'A' => 'max' ,
'sum' => 12 ,
'C' => array (
'x' ,
'y' ,
'z' => 45 ,
),
);
$c = array (
'A' => 'tom' ,
'sum' => 8 ,
'C' => array (
'x' ,
'y' ,
'z' => 50 ,
'w' => 1 ,
),
);
$this -> assertEquals ( array (
'A' => 'tom' ,
'sum' => 30 ,
'C' => array (
'x' ,
'y' ,
'z' => 145 ,
'w' => 1 ,
),
), wc_array_merge_recursive_numeric ( $a , $b , $c ) );
}
2014-09-05 18:35:02 +00:00
}