Formatting functions unit tests
This commit is contained in:
parent
8cd754d8b6
commit
c0197c639e
|
@ -1,120 +1,156 @@
|
|||
<?php
|
||||
/**
|
||||
* Formatting functions
|
||||
*
|
||||
* @package WooCommerce\Tests\Formatting
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class Functions.
|
||||
* @package WooCommerce\Tests\Formatting
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
$this->assertEquals( array( 'yes', 'no' ), array( wc_bool_to_string( true ), wc_bool_to_string( false ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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', '|' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_sanitize_taxonomy_name().
|
||||
*
|
||||
* @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 ಠ_ಠ' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_get_filename_from_url().
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_get_filename_from_url() {
|
||||
|
||||
$this->assertEquals( 'woocommerce.pdf', wc_get_filename_from_url( 'https://woocommerce.com/woocommerce.pdf' ) );
|
||||
$this->assertEmpty( wc_get_filename_from_url( 'ftp://wc' ) );
|
||||
$this->assertEmpty( wc_get_filename_from_url( 'http://www.skyverge.com' ) );
|
||||
$this->assertEquals( 'woocommerce', wc_get_filename_from_url( 'https://woocommerce.com/woocommerce' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_wc_get_dimension().
|
||||
*
|
||||
* @since 2.2.0
|
||||
*/
|
||||
public function data_provider_wc_get_dimension() {
|
||||
|
||||
// save default
|
||||
$default_unit = get_option( 'woocommerce_dimension_unit' );
|
||||
|
||||
$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' ) ),
|
||||
);
|
||||
|
||||
update_option( 'woocommerce_dimension_unit', 'in' );
|
||||
$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' ) ),
|
||||
);
|
||||
|
||||
update_option( 'woocommerce_dimension_unit', 'm' );
|
||||
$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' ) ),
|
||||
);
|
||||
|
||||
update_option( 'woocommerce_dimension_unit', 'mm' );
|
||||
$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' ) ),
|
||||
);
|
||||
|
||||
update_option( 'woocommerce_dimension_unit', 'yd' );
|
||||
$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' ) ),
|
||||
);
|
||||
|
||||
$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' ) ),
|
||||
);
|
||||
|
||||
// restore default
|
||||
update_option( 'woocommerce_dimension_unit', $default_unit );
|
||||
|
||||
return array_merge( $cm, $in, $m, $mm, $yd, $n, $custom );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_get_dimension().
|
||||
*
|
||||
* @dataProvider data_provider_wc_get_dimension
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_get_dimension( $assert, $value ) {
|
||||
public function test_wc_get_dimension() {
|
||||
// Save default.
|
||||
$default_unit = get_option( 'woocommerce_dimension_unit' );
|
||||
|
||||
$this->assertEquals( $assert, $value );
|
||||
// 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' ),
|
||||
) );
|
||||
|
||||
// in.
|
||||
update_option( 'woocommerce_dimension_unit', 'in' );
|
||||
$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.
|
||||
update_option( 'woocommerce_dimension_unit', 'm' );
|
||||
$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.
|
||||
update_option( 'woocommerce_dimension_unit', 'mm' );
|
||||
$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.
|
||||
update_option( 'woocommerce_dimension_unit', 'yd' );
|
||||
$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.
|
||||
update_option( 'woocommerce_dimension_unit', $default_unit );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,47 +159,46 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_get_weight() {
|
||||
|
||||
// save default
|
||||
// Save default.
|
||||
$default_unit = get_option( 'woocommerce_weight_unit' );
|
||||
|
||||
// kg (default unit)
|
||||
// kg (default unit).
|
||||
$this->assertEquals( 10, wc_get_weight( 10, 'kg' ) );
|
||||
$this->assertEquals( 10000, wc_get_weight( 10, 'g' ) );
|
||||
$this->assertEquals( 22.0462, wc_get_weight( 10, 'lbs' ) );
|
||||
$this->assertEquals( 352.74, wc_get_weight( 10, 'oz' ) );
|
||||
|
||||
// g
|
||||
// g.
|
||||
update_option( 'woocommerce_weight_unit', 'g' );
|
||||
$this->assertEquals( 0.01, wc_get_weight( 10, 'kg' ) );
|
||||
$this->assertEquals( 10, wc_get_weight( 10, 'g' ) );
|
||||
$this->assertEquals( 0.0220462, wc_get_weight( 10, 'lbs' ) );
|
||||
$this->assertEquals( 0.35274, wc_get_weight( 10, 'oz' ) );
|
||||
|
||||
// lbs
|
||||
// lbs.
|
||||
update_option( 'woocommerce_weight_unit', 'lbs' );
|
||||
$this->assertEquals( 4.53592, wc_get_weight( 10, 'kg' ) );
|
||||
$this->assertEquals( 4535.92, wc_get_weight( 10, 'g' ) );
|
||||
$this->assertEquals( 10, wc_get_weight( 10, 'lbs' ) );
|
||||
$this->assertEquals( 160.00004208, wc_get_weight( 10, 'oz' ) );
|
||||
|
||||
// oz
|
||||
// oz.
|
||||
update_option( 'woocommerce_weight_unit', 'oz' );
|
||||
$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' ) );
|
||||
$this->assertEquals( 10, wc_get_weight( 10, 'oz' ) );
|
||||
|
||||
// custom from unit
|
||||
// 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' ) );
|
||||
|
||||
// negative
|
||||
// Negative.
|
||||
$this->assertEquals( 0, wc_get_weight( -10, 'g' ) );
|
||||
|
||||
// restore default
|
||||
// Restore default.
|
||||
update_option( 'woocommerce_weight_unit', $default_unit );
|
||||
}
|
||||
|
||||
|
@ -173,7 +208,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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' ) );
|
||||
}
|
||||
|
@ -187,7 +221,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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 ) );
|
||||
|
@ -199,7 +232,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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 ) );
|
||||
}
|
||||
|
@ -210,56 +242,56 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_format_decimal() {
|
||||
// given string
|
||||
// Given string.
|
||||
$this->assertEquals( '9.99', wc_format_decimal( '9.99' ) );
|
||||
|
||||
// float
|
||||
// Float.
|
||||
$this->assertEquals( '9.99', wc_format_decimal( 9.99 ) );
|
||||
|
||||
// dp = false, no rounding
|
||||
// DP false, no rounding.
|
||||
$this->assertEquals( '9.9999', wc_format_decimal( 9.9999 ) );
|
||||
|
||||
// dp = use default (2)
|
||||
// DP when empty string uses default as 2.
|
||||
$this->assertEquals( '9.99', wc_format_decimal( 9.9911, '' ) );
|
||||
|
||||
// dp = use default (2) and round
|
||||
// DP use default as 2 and round.
|
||||
$this->assertEquals( '10.00', wc_format_decimal( 9.9999, '' ) );
|
||||
|
||||
// dp = use custom
|
||||
// DP use custom.
|
||||
$this->assertEquals( '9.991', wc_format_decimal( 9.9912, 3 ) );
|
||||
|
||||
// trim zeros
|
||||
// Trim zeros.
|
||||
$this->assertEquals( '9', wc_format_decimal( 9.00, false, true ) );
|
||||
|
||||
// trim zeros and round
|
||||
// Trim zeros and round.
|
||||
$this->assertEquals( '10', wc_format_decimal( 9.9999, '', true ) );
|
||||
|
||||
// given string with thousands in german format.
|
||||
// Given string with thousands in german format.
|
||||
update_option( 'woocommerce_price_decimal_sep', ',' );
|
||||
update_option( 'woocommerce_price_thousand_sep', '.' );
|
||||
|
||||
// given string
|
||||
// Given string.
|
||||
$this->assertEquals( '9.99', wc_format_decimal( '9.99' ) );
|
||||
|
||||
// float
|
||||
// Float.
|
||||
$this->assertEquals( '9.99', wc_format_decimal( 9.99 ) );
|
||||
|
||||
// dp = false, no rounding
|
||||
// DP false, no rounding.
|
||||
$this->assertEquals( '9.9999', wc_format_decimal( 9.9999 ) );
|
||||
|
||||
// dp = use default (2)
|
||||
// DP when empty string uses default as 2.
|
||||
$this->assertEquals( '9.99', wc_format_decimal( 9.9911, '' ) );
|
||||
|
||||
// dp = use default (2) and round
|
||||
// DP use default as 2 and round.
|
||||
$this->assertEquals( '10.00', wc_format_decimal( 9.9999, '' ) );
|
||||
|
||||
// dp = use custom
|
||||
// DP use custom.
|
||||
$this->assertEquals( '9.991', wc_format_decimal( 9.9912, 3 ) );
|
||||
|
||||
// trim zeros
|
||||
// Trim zeros.
|
||||
$this->assertEquals( '9', wc_format_decimal( 9.00, false, true ) );
|
||||
|
||||
// trim zeros and round
|
||||
// Trim zeros and round.
|
||||
$this->assertEquals( '10', wc_format_decimal( 9.9999, '', true ) );
|
||||
|
||||
update_option( 'woocommerce_price_decimal_sep', '.' );
|
||||
|
@ -272,10 +304,8 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_float_to_string() {
|
||||
|
||||
// given string, return 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 ) );
|
||||
}
|
||||
|
||||
|
@ -285,14 +315,13 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_format_localized_price() {
|
||||
|
||||
// save default
|
||||
// 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
|
||||
// Restore default.
|
||||
update_option( 'woocommerce_price_decimal_sep', $decimal_sep );
|
||||
}
|
||||
|
||||
|
@ -302,30 +331,46 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_format_localized_decimal() {
|
||||
|
||||
$this->assertEquals( '1.17', wc_format_localized_decimal( '1.17' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_clean() - note this is a basic type test as WP core already.
|
||||
* has coverage for sanitized_text_field().
|
||||
* 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().
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_clean() {
|
||||
|
||||
$this->assertEquals( 'cleaned', wc_clean( '<script>alert();</script>cleaned' ) );
|
||||
$this->assertEquals( array( 'cleaned', 'foo' ), wc_clean( array( '<script>alert();</script>cleaned', 'foo' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_sanitize_tooltip() - note this is a basic type test as WP core already.
|
||||
* has coverage for wp_kses().
|
||||
* Test wc_sanitize_textarea().
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function test_wc_sanitize_textarea() {
|
||||
$this->assertEquals( "foo\ncleaned\nbar", wc_sanitize_textarea( "foo\n<script>alert();</script>cleaned\nbar" ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_sanitize_tooltip().
|
||||
*
|
||||
* Note this is a basic type test as WP core already has coverage for wp_kses().
|
||||
*
|
||||
* @since 2.4
|
||||
*/
|
||||
public function test_wc_sanitize_tooltip() {
|
||||
|
||||
$this->assertEquals( 'alert();cleaned', wc_sanitize_tooltip( '<script>alert();</script>cleaned' ) );
|
||||
$this->assertEquals( 'alert();cleaned<p>foo</p><span>bar</span>', wc_sanitize_tooltip( '<script>alert();</script>cleaned<p>foo</p><span>bar</span>' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -334,7 +379,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_array_overlay() {
|
||||
|
||||
$a1 = array(
|
||||
'apple' => 'banana',
|
||||
'pear' => 'grape',
|
||||
|
@ -368,7 +412,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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 ) );
|
||||
|
@ -380,26 +423,25 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_get_woocommerce_price_format() {
|
||||
|
||||
// save default
|
||||
// Save default.
|
||||
$currency_pos = get_option( 'woocommerce_currency_pos' );
|
||||
|
||||
// default format (left)
|
||||
// Default format (left).
|
||||
$this->assertEquals( '%1$s%2$s', get_woocommerce_price_format() );
|
||||
|
||||
// right
|
||||
// Right.
|
||||
update_option( 'woocommerce_currency_pos', 'right' );
|
||||
$this->assertEquals( '%2$s%1$s', get_woocommerce_price_format() );
|
||||
|
||||
// left space
|
||||
// Left space.
|
||||
update_option( 'woocommerce_currency_pos', 'left_space' );
|
||||
$this->assertEquals( '%1$s %2$s', get_woocommerce_price_format() );
|
||||
|
||||
// right space
|
||||
// Right space.
|
||||
update_option( 'woocommerce_currency_pos', 'right_space' );
|
||||
$this->assertEquals( '%2$s %1$s', get_woocommerce_price_format() );
|
||||
|
||||
// restore default
|
||||
// Restore default.
|
||||
update_option( 'woocommerce_currency_pos', $currency_pos );
|
||||
}
|
||||
|
||||
|
@ -411,7 +453,7 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
public function test_wc_get_price_thousand_separator() {
|
||||
$separator = get_option( 'woocommerce_price_thousand_sep' );
|
||||
|
||||
// default value
|
||||
// Default value.
|
||||
$this->assertEquals( ',', wc_get_price_thousand_separator() );
|
||||
|
||||
update_option( 'woocommerce_price_thousand_sep', '.' );
|
||||
|
@ -431,7 +473,7 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
public function test_wc_get_price_decimal_separator() {
|
||||
$separator = get_option( 'woocommerce_price_decimal_sep' );
|
||||
|
||||
// default value
|
||||
// Default value.
|
||||
$this->assertEquals( '.', wc_get_price_decimal_separator() );
|
||||
|
||||
update_option( 'woocommerce_price_decimal_sep', ',' );
|
||||
|
@ -451,7 +493,7 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
public function test_wc_get_price_decimals() {
|
||||
$decimals = get_option( 'woocommerce_price_num_decimals' );
|
||||
|
||||
// default value
|
||||
// Default value.
|
||||
$this->assertEquals( 2, wc_get_price_decimals() );
|
||||
|
||||
update_option( 'woocommerce_price_num_decimals', '1' );
|
||||
|
@ -472,31 +514,30 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_price() {
|
||||
|
||||
// common prices
|
||||
// Common prices.
|
||||
$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 ) );
|
||||
|
||||
// different currency
|
||||
// Different currency.
|
||||
$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' ) ) );
|
||||
|
||||
// negative price
|
||||
// Negative price.
|
||||
$this->assertEquals( '<span class="woocommerce-Price-amount amount">-<span class="woocommerce-Price-currencySymbol">£</span>1.17</span>', wc_price( -1.17 ) );
|
||||
|
||||
// bogus prices
|
||||
// Bogus prices.
|
||||
$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( 'ಠ_ಠ' ) );
|
||||
|
||||
// trim zeros
|
||||
// Trim zeros.
|
||||
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
|
||||
$this->assertEquals( '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>1</span>', wc_price( 1.00 ) );
|
||||
remove_filter( 'woocommerce_price_trim_zeros', '__return_true' );
|
||||
|
||||
// ex tax label
|
||||
// Ex tax label.
|
||||
$calc_taxes = get_option( 'woocommerce_calc_taxes' );
|
||||
update_option( 'woocommerce_calc_taxes', 'yes' );
|
||||
$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 ) ) );
|
||||
|
@ -509,18 +550,13 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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 ) );
|
||||
}
|
||||
$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' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -529,7 +565,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_date_format() {
|
||||
|
||||
$this->assertEquals( get_option( 'date_format' ), wc_date_format() );
|
||||
}
|
||||
|
||||
|
@ -542,43 +577,72 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
$this->assertEquals( get_option( 'time_format' ), wc_time_format() );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_timezone_string().
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_timezone_string() {
|
||||
|
||||
// test when timezone string exists
|
||||
// Test when timezone string exists.
|
||||
update_option( 'timezone_string', 'America/New_York' );
|
||||
$this->assertEquals( 'America/New_York', wc_timezone_string() );
|
||||
|
||||
// restore default
|
||||
// Restore default.
|
||||
update_option( 'timezone_string', '' );
|
||||
|
||||
// test with missing UTC offset
|
||||
// Test with missing UTC offset.
|
||||
delete_option( 'gmt_offset' );
|
||||
$this->assertEquals( 'UTC', wc_timezone_string() );
|
||||
|
||||
// test with manually set UTC offset
|
||||
// Test with manually set UTC offset.
|
||||
update_option( 'gmt_offset', -4 );
|
||||
$this->assertNotEquals( 'UTC', wc_timezone_string() );
|
||||
|
||||
// test with invalid offset
|
||||
// Test with invalid offset.
|
||||
update_option( 'gmt_offset', 99 );
|
||||
$this->assertEquals( 'UTC', wc_timezone_string() );
|
||||
|
||||
// restore default
|
||||
// Restore default.
|
||||
update_option( 'gmt_offset', '0' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_timezone_offset().
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function test_wc_timezone_offset() {
|
||||
$this->assertEquals( 0.0, wc_timezone_offset() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rgb_from_hex().
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_rgb_from_hex() {
|
||||
|
||||
$rgb = array(
|
||||
'R' => 0,
|
||||
'G' => 93,
|
||||
|
@ -595,7 +659,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_hex_darker() {
|
||||
|
||||
$this->assertEquals( '#004178', wc_hex_darker( '005dab' ) );
|
||||
$this->assertEquals( '#004178', wc_hex_darker( '#005dab' ) );
|
||||
}
|
||||
|
@ -606,7 +669,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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 ) );
|
||||
|
@ -618,7 +680,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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' ) );
|
||||
}
|
||||
|
@ -629,7 +690,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @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 ) );
|
||||
|
@ -641,15 +701,29 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_format_postcode() {
|
||||
|
||||
// Generic postcode
|
||||
// Generic postcode.
|
||||
$this->assertEquals( '02111', wc_format_postcode( ' 02111 ', 'US' ) );
|
||||
|
||||
// US 9-digit postcode
|
||||
// US 9-digit postcode.
|
||||
$this->assertEquals( '02111-9999', wc_format_postcode( ' 021119999 ', 'US' ) );
|
||||
|
||||
// UK postcode
|
||||
// UK postcode.
|
||||
$this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) );
|
||||
|
||||
// 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' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -658,7 +732,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
* @since 2.2
|
||||
*/
|
||||
public function test_wc_format_phone_number() {
|
||||
|
||||
$this->assertEquals( '1-610-385-0000', wc_format_phone_number( '1.610.385.0000' ) );
|
||||
}
|
||||
|
||||
|
@ -673,4 +746,184 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
|
|||
$this->assertEquals( 'st.', wc_trim_string( 'string', 3, '.' ) );
|
||||
$this->assertEquals( 'string¥', wc_trim_string( 'string¥', 7, '' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
$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' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_format_price_range().
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function test_wc_format_price_range() {
|
||||
$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' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
$this->assertEquals( 'https://wordpress.tv/2015/10/19/mike-jolley-user-onboarding-for-wordpress-plugins/', wc_do_oembeds( 'https://wordpress.tv/2015/10/19/mike-jolley-user-onboarding-for-wordpress-plugins/' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue