From eb533d0263b7beef395cf04bb42f3afc336d87a7 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 11 Jul 2018 14:25:42 +0200 Subject: [PATCH 01/10] Update test_get_woocommerce_currencies to test for both cached and uncached currencies, gives us more code coverage --- tests/unit-tests/util/class-wc-tests-core-functions.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 5c7a9602cf9..3b516c319af 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -27,6 +27,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { * @since 2.2 */ public function test_get_woocommerce_currencies() { + static $currencies; $expected_currencies = array( 'AED' => 'United Arab Emirates dirham', @@ -194,6 +195,10 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_currencies, get_woocommerce_currencies() ); + + // Unset cached currencies and test again. + unset( $currencies ); + $this->assertEquals( $expected_currencies, get_woocommerce_currencies() ); } /** From 2f7e42bb2f0bab68267e923d2ab41923a407e696 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 11 Jul 2018 14:38:33 +0200 Subject: [PATCH 02/10] Add wc_get_theme_support unit test --- .../util/class-wc-tests-core-functions.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 3b516c319af..de4c5ee3649 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -220,6 +220,32 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { } } + /** + * Test wc_get_theme_support() + * + * @return void + */ + public function test_wc_get_theme_support() { + $this->assertEquals( 'default', wc_get_theme_support( '', 'default' ) ); + + $theme_support_options = array( + 'thumbnail_image_width' => 150, + 'single_image_width' => 300, + 'product_grid' => array( + 'default_rows' => 3, + 'min_rows' => 2, + 'max_rows' => 8, + 'default_columns' => 4, + 'min_columns' => 2, + 'max_columns' => 5, + ), + ); + add_theme_support( 'woocommerce', $theme_support_options ); + + $this->assertEquals( $theme_support_options, wc_get_theme_support() ); + $this->assertEquals( $theme_support_options['product_grid'], wc_get_theme_support( 'product_grid' ) ); + } + /** * Test get_woocommerce_api_url(). * From 1d5605c9dc2df483b220db94db32aa501022f25b Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 11 Jul 2018 14:59:05 +0200 Subject: [PATCH 03/10] Add wc_array_cartesian unit test --- .../util/class-wc-tests-core-functions.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index de4c5ee3649..1b3f5b1839a 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -725,4 +725,59 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { $this->assertEquals( wc_selected( $value, $options ), $actual_result ); } } + + /** + * Test wc_array_cartesian. + * + * @return void + */ + public function test_wc_array_cartesian() { + $product = new WC_Product_Variable(); + $attributes = array(); + + $attribute = new \WC_Product_Attribute(); + $attribute->set_id( 0 ); + $attribute->set_name( 'Attr1' ); + $attribute->set_options( array( 'Attr1 Value 1', 'Attr1 Value 2' ) ); + $attribute->set_position( 0 ); + $attribute->set_visible( true ); + $attribute->set_variation( true ); + $attributes[] = $attribute; + + $attribute = new \WC_Product_Attribute(); + $attribute->set_id( 0 ); + $attribute->set_name( 'Attr2' ); + $attribute->set_options( array( 'Attr2 Value 1', 'Attr2 Value 2' ) ); + $attribute->set_position( 0 ); + $attribute->set_visible( true ); + $attribute->set_variation( true ); + $attributes[] = $attribute; + + $product->set_props( array( + 'attributes' => $attributes, + ) ); + $product->save(); + + $variation_attributes = wc_list_pluck( array_filter( $product->get_attributes(), 'wc_attributes_array_filter_variation' ), 'get_slugs' ); + $expected_combinations = array( + array( + 'attr2' => 'Attr2 Value 1', + 'attr1' => 'Attr1 Value 1', + ), + array( + 'attr2' => 'Attr2 Value 2', + 'attr1' => 'Attr1 Value 1', + ), + array( + 'attr2' => 'Attr2 Value 1', + 'attr1' => 'Attr1 Value 2', + ), + array( + 'attr2' => 'Attr2 Value 2', + 'attr1' => 'Attr1 Value 2', + ), + ); + + $this->assertEquals( $expected_combinations, array_reverse( wc_array_cartesian( $variation_attributes ) ) ); + } } From 0c01b6c3bbbe04e3b33110127d92bee0b5c26e58 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 11 Jul 2018 15:05:18 +0200 Subject: [PATCH 04/10] Add wc_get_credit_card_type_label unit test --- .../util/class-wc-tests-core-functions.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 1b3f5b1839a..7ec7ef0f109 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -780,4 +780,17 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { $this->assertEquals( $expected_combinations, array_reverse( wc_array_cartesian( $variation_attributes ) ) ); } + + /** + * Test wc_get_credit_card_type_label. + * + * @return void + */ + public function test_wc_get_credit_card_type_label() { + $this->assertEquals( 'Visa', wc_get_credit_card_type_label( 'visa' ) ); + $this->assertEquals( 'JCB', wc_get_credit_card_type_label( 'jCb' ) ); + $this->assertEquals( 'MasterCard', wc_get_credit_card_type_label( 'Mastercard' ) ); + $this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american_express' ) ); + $this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american-express' ) ); + } } From a28253739f9634a36244399647590dfdbd75aea0 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 11 Jul 2018 15:10:55 +0200 Subject: [PATCH 05/10] Add wc_get_permalink_structure unit test --- .../util/class-wc-tests-core-functions.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 7ec7ef0f109..f80e15ff911 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -793,4 +793,24 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { $this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american_express' ) ); $this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american-express' ) ); } + + /** + * Test wc_get_permalink_structure. + * + * @return void + */ + public function test_wc_get_permalink_structure() { + $expected_structure = array( + 'product_base' => 'product', + 'category_base' => 'product-category', + 'tag_base' => 'product-tag', + 'attribute_base' => '', + 'use_verbose_page_rules' => '', + 'product_rewrite_slug' => 'product', + 'category_rewrite_slug' => 'product-category', + 'tag_rewrite_slug' => 'product-tag', + 'attribute_rewrite_slug' => '', + ); + $this->assertEquals( $expected_structure, wc_get_permalink_structure() ); + } } From 9e495478f47dfbccf61e237bad508a17403aed8c Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 11 Jul 2018 15:17:00 +0200 Subject: [PATCH 06/10] Add unit test for wc_decimal_to_fraction --- tests/unit-tests/util/class-wc-tests-core-functions.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index f80e15ff911..bc86a2f7f55 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -813,4 +813,13 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_structure, wc_get_permalink_structure() ); } + + /** + * Test wc_decimal_to_fraction. + * + * @return void + */ + public function test_wc_decimal_to_fraction() { + $this->assertEquals( array( 7, 2 ), wc_decimal_to_fraction( '3.5' ) ); + } } From 370c20007681076e2da42b3b319665f1e3139b38 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Mon, 16 Jul 2018 12:59:01 +0200 Subject: [PATCH 07/10] Make wc_array_cartesian test use predefined array for testing against instead of relying on product attributes. --- .../util/class-wc-tests-core-functions.php | 38 ++++++------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index bc86a2f7f55..20c1c7185b5 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -732,33 +732,17 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { * @return void */ public function test_wc_array_cartesian() { - $product = new WC_Product_Variable(); - $attributes = array(); + $array = array( + 'attr1' => array( + 'Attr1 Value 1', + 'Attr1 Value 2', + ), + 'attr2' => array( + 'Attr2 Value 1', + 'Attr2 Value 2', + ), + ); - $attribute = new \WC_Product_Attribute(); - $attribute->set_id( 0 ); - $attribute->set_name( 'Attr1' ); - $attribute->set_options( array( 'Attr1 Value 1', 'Attr1 Value 2' ) ); - $attribute->set_position( 0 ); - $attribute->set_visible( true ); - $attribute->set_variation( true ); - $attributes[] = $attribute; - - $attribute = new \WC_Product_Attribute(); - $attribute->set_id( 0 ); - $attribute->set_name( 'Attr2' ); - $attribute->set_options( array( 'Attr2 Value 1', 'Attr2 Value 2' ) ); - $attribute->set_position( 0 ); - $attribute->set_visible( true ); - $attribute->set_variation( true ); - $attributes[] = $attribute; - - $product->set_props( array( - 'attributes' => $attributes, - ) ); - $product->save(); - - $variation_attributes = wc_list_pluck( array_filter( $product->get_attributes(), 'wc_attributes_array_filter_variation' ), 'get_slugs' ); $expected_combinations = array( array( 'attr2' => 'Attr2 Value 1', @@ -778,7 +762,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { ), ); - $this->assertEquals( $expected_combinations, array_reverse( wc_array_cartesian( $variation_attributes ) ) ); + $this->assertEquals( $expected_combinations, array_reverse( wc_array_cartesian( $array ) ) ); } /** From 1f46caaa4ca7433ce7cf291350cdca81b2dd86a3 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Mon, 16 Jul 2018 13:02:24 +0200 Subject: [PATCH 08/10] Test some edge cases where CC name could be empty or contain a non standard value. --- tests/unit-tests/util/class-wc-tests-core-functions.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 20c1c7185b5..500f4d38c10 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -776,6 +776,8 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { $this->assertEquals( 'MasterCard', wc_get_credit_card_type_label( 'Mastercard' ) ); $this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american_express' ) ); $this->assertEquals( 'American Express', wc_get_credit_card_type_label( 'american-express' ) ); + $this->assertEquals( '', wc_get_credit_card_type_label( '' ) ); + $this->assertEquals( 'Random name', wc_get_credit_card_type_label( 'random-name' ) ); } /** From 5a0d3c8bf72014a12c6da9eb47200f296627caab Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 25 Jul 2018 14:07:57 +0200 Subject: [PATCH 09/10] Fix indentation issues caused by github conflict tool --- tests/unit-tests/util/class-wc-tests-core-functions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 1d63e4b69e4..9da0f86c478 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -807,9 +807,9 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { */ public function test_wc_decimal_to_fraction() { $this->assertEquals( array( 7, 2 ), wc_decimal_to_fraction( '3.5' ) ); - } - - /* + } + + /*** * Test wc_get_user_agent function. * * @return void From 73b80c9241a2fcc9f06df8bbda8d476448ee5ebb Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 25 Jul 2018 14:09:04 +0200 Subject: [PATCH 10/10] Remove extra asterisk --- tests/unit-tests/util/class-wc-tests-core-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 9da0f86c478..f8dcc0f6a51 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -809,7 +809,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case { $this->assertEquals( array( 7, 2 ), wc_decimal_to_fraction( '3.5' ) ); } - /*** + /** * Test wc_get_user_agent function. * * @return void