Add more core-functions unit tests (#18190)

* Pass through additional args to wc_create_order in test

* Pass extra args to wc_create_order to improve coverage, test WP_Error check of wc_create_order

* wc_get_template_part test

* wc_enqueue_js test

* wc_get_log_file_name test

* wc_get_page_children test

* hash_equals test

* wc_rand_hash test

* wc_transaction_query test

* Fix script test and phpcs stuff

* Fix widget tests

* Move tests to correct file and fix phpcs
This commit is contained in:
Gerhard Potgieter 2017-12-18 15:58:40 +02:00 committed by Rodrigo Primo
parent 906efe1ffc
commit 25c4a84491
3 changed files with 288 additions and 82 deletions

View File

@ -1,67 +0,0 @@
<?php
/**
* Class Functions.
*
* @package WooCommerce\Tests\Core
* @since 3.2.0
*/
class WC_Tests_WooCommerce_Functions extends WC_Unit_Test_Case {
/**
* Tests wc_maybe_define_constant().
*
* @since 3.2.0
*/
public function test_wc_maybe_define_constant() {
$this->assertFalse( defined( 'WC_TESTING_DEFINE_FUNCTION' ) );
// Check if defined.
wc_maybe_define_constant( 'WC_TESTING_DEFINE_FUNCTION', true );
$this->assertTrue( defined( 'WC_TESTING_DEFINE_FUNCTION' ) );
// Check value.
wc_maybe_define_constant( 'WC_TESTING_DEFINE_FUNCTION', false );
$this->assertTrue( WC_TESTING_DEFINE_FUNCTION );
}
/**
* Tests wc_create_order() and wc_update_order() currency handling.
*
* @since 3.2.0
*/
public function test_wc_create_update_order_currency() {
$old_currency = get_woocommerce_currency();
$new_currency = 'BGN';
update_option( 'woocommerce_currency', $new_currency );
// New order should be created using shop currency.
$order = wc_create_order( array(
'status' => 'pending',
'customer_id' => 1,
) );
$this->assertEquals( $new_currency, $order->get_currency() );
update_option( 'woocommerce_currency', $old_currency );
// Currency should not change when order is updated.
$order = wc_update_order( array(
'customer_id' => 2,
'order_id' => $order->get_id(),
) );
$this->assertEquals( $new_currency, $order->get_currency() );
}
/**
* Test the wc_is_active_theme function.
*
* @return void
*/
public function test_wc_is_active_theme() {
$current_theme = get_template();
$this->assertTrue( wc_is_active_theme( $current_theme ) );
$this->assertFalse( wc_is_active_theme( 'somegiberish' ) );
$this->assertTrue( wc_is_active_theme( array( $current_theme, 'somegiberish' ) ) );
}
}

View File

@ -1,10 +1,14 @@
<?php
/**
* Class Core_Functions.
* Unit tests for the core functions.
*
* @package WooCommerce\Tests\Util
* @since 2.2
*/
/**
* Core function unit tests.
*/
class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
/**
@ -199,13 +203,13 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
*/
public function test_get_woocommerce_currency_symbol() {
// default currency
// Default currency.
$this->assertEquals( '&pound;', get_woocommerce_currency_symbol() );
// given specific currency
// Given specific currency.
$this->assertEquals( '&#36;', get_woocommerce_currency_symbol( 'USD' ) );
// each case
// Each case.
foreach ( array_keys( get_woocommerce_currencies() ) as $currency_code ) {
$this->assertInternalType( 'string', get_woocommerce_currency_symbol( $currency_code ) );
}
@ -220,10 +224,10 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
$base_uri = get_home_url();
// base uri
// Base URI.
$this->assertEquals( "$base_uri/wc-api/v3/", get_woocommerce_api_url( null ) );
// path
// Path.
$this->assertEquals( "$base_uri/wc-api/v3/orders", get_woocommerce_api_url( 'orders' ) );
}
@ -245,7 +249,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
* @since 3.0.0
*/
public function test_wc_get_logger() {
// This filter should have no effect because the class does not implement WC_Logger_Interface
// This filter should have no effect because the class does not implement WC_Logger_Interface.
add_filter( 'woocommerce_logging_class', array( $this, 'return_bad_logger' ) );
$this->setExpectedIncorrectUsage( 'wc_get_logger' );
@ -287,9 +291,22 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
*/
public function test_wc_format_country_state_string() {
// Test with correct values.
$this->assertEquals( array( 'country' => 'US', 'state' => 'CA' ), wc_format_country_state_string( 'US:CA' ) );
$this->assertEquals(
array(
'country' => 'US',
'state' => 'CA',
),
wc_format_country_state_string( 'US:CA' )
);
// Test what happens when we pass an incorrect value.
$this->assertEquals( array( 'country' => 'US-CA', 'state' => '' ), wc_format_country_state_string( 'US-CA' ) );
$this->assertEquals(
array(
'country' => 'US-CA',
'state' => '',
),
wc_format_country_state_string( 'US-CA' )
);
}
/**
@ -311,7 +328,13 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
* @since 3.0.0
*/
public function test_wc_print_r() {
$arr = array( 1, 2, 'a', 'b', 'c' => 'd' );
$arr = array(
1,
2,
'a',
'b',
'c' => 'd',
);
// This filter will sequentially remove handlers, allowing us to test as though our
// functions were accumulatively blacklisted, adding one on each call.
@ -352,7 +375,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
$this->assertFalse( $return_value );
ob_clean();
// Reset filter to include all handlers
// Reset filter to include all handlers.
$this->filter_wc_print_r_alternatives( array(), true );
$this->assertEquals( print_r( $arr, true ), wc_print_r( $arr, true ) );
@ -372,7 +395,7 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
* Eventually, no handlers are returned.
*
* @param array $alternatives Input array of alternatives.
* @param bool $reset Optional. Default false. True to reset excluded alternatives.
* @param bool $reset Optional. Default false. True to reset excluded alternatives.
* @return array|bool Alternatives. True on reset.
*/
public function filter_wc_print_r_alternatives( $alternatives, $reset = false ) {
@ -398,4 +421,228 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
$wildcards = array( 'GIJóN', 'GIJÓN', 'GIJÓN*', 'GIJÓ*', 'GIJ*', 'GI*', 'G*', '*' );
$this->assertEquals( $wildcards, wc_get_wildcard_postcodes( $postcode, $country ) );
}
/**
* Tests wc_maybe_define_constant().
*
* @since 3.2.0
*/
public function test_wc_maybe_define_constant() {
$this->assertFalse( defined( 'WC_TESTING_DEFINE_FUNCTION' ) );
// Check if defined.
wc_maybe_define_constant( 'WC_TESTING_DEFINE_FUNCTION', true );
$this->assertTrue( defined( 'WC_TESTING_DEFINE_FUNCTION' ) );
// Check value.
wc_maybe_define_constant( 'WC_TESTING_DEFINE_FUNCTION', false );
$this->assertTrue( WC_TESTING_DEFINE_FUNCTION );
}
/**
* Tests wc_create_order() and wc_update_order() currency handling.
*
* @since 3.2.0
*/
public function test_wc_create_update_order_currency() {
$old_currency = get_woocommerce_currency();
$new_currency = 'BGN';
update_option( 'woocommerce_currency', $new_currency );
// New order should be created using shop currency.
$order = wc_create_order( array(
'status' => 'pending',
'customer_id' => 1,
'created_via' => 'unit tests',
'cart_hash' => '',
) );
$this->assertEquals( $new_currency, $order->get_currency() );
update_option( 'woocommerce_currency', $old_currency );
// Currency should not change when order is updated.
$order = wc_update_order( array(
'customer_id' => 2,
'order_id' => $order->get_id(),
) );
$this->assertEquals( $new_currency, $order->get_currency() );
$order = wc_update_order( array(
'customer_id' => 2,
) );
$this->assertInstanceOf( 'WP_Error', $order );
}
/**
* Test the wc_is_active_theme function.
*
* @return void
*/
public function test_wc_is_active_theme() {
$current_theme = get_template();
$this->assertTrue( wc_is_active_theme( $current_theme ) );
$this->assertFalse( wc_is_active_theme( 'somegiberish' ) );
$this->assertTrue( wc_is_active_theme( array( $current_theme, 'somegiberish' ) ) );
}
/**
* Test the wc_get_template function.
*
* @return void
*/
public function test_wc_get_template_part() {
$this->assertEmpty( wc_get_template_part( 'nothinghere' ) );
}
/**
* Test wc_get_image_size function.
*
* @return void
*/
public function test_wc_get_image_size() {
$this->assertArrayHasKey( 'width', wc_get_image_size( array( 100, 100, 1 ) ) );
$this->assertArrayHasKey( 'height', wc_get_image_size( 'shop_single' ) );
update_option( 'woocommerce_thumbnail_cropping', 'uncropped' );
$this->assertArrayHasKey( 'crop', wc_get_image_size( 'shop_thumbnail' ) );
update_option( 'woocommerce_thumbnail_cropping', 'custom' );
$this->assertArrayHasKey( 'crop', wc_get_image_size( 'shop_thumbnail' ) );
}
/**
* Test wc_enqueue_js and wc_print_js functions.
*
* @return void
*/
public function test_wc_enqueue_js_wc_print_js() {
$js = 'alert( "test" );';
ob_start();
wc_print_js();
$printed_js = ob_get_clean();
$this->assertNotContains( $js, $printed_js );
wc_enqueue_js( $js );
ob_start();
wc_print_js();
$printed_js = ob_get_clean();
$this->assertContains( $js, $printed_js );
}
/**
* Test wc_get_log_file_name function.
*
* @return void
*/
public function test_wc_get_log_file_name() {
$this->assertNotEmpty( wc_get_log_file_name( 'test' ) );
}
/**
* Test wc_get_page_children function.
*
* @return void
*/
public function test_wc_get_page_children() {
$page_id = wp_insert_post( array(
'post_title' => 'Parent Page',
'post_type' => 'page',
'post_name' => 'parent-page',
'post_status' => 'publish',
'post_author' => 1,
'menu_order' => 0,
) );
$child_page_id = wp_insert_post( array(
'post_parent' => $page_id,
'post_title' => 'Parent Page',
'post_type' => 'page',
'post_name' => 'parent-page',
'post_status' => 'publish',
'post_author' => 1,
'menu_order' => 0,
) );
$children = wc_get_page_children( $page_id );
$this->assertEquals( $child_page_id, $children[0] );
wp_delete_post( $page_id, true );
wp_delete_post( $child_page_id, true );
}
/**
* Test hash_equals function.
*
* @return void
*/
public function test_hash_equals() {
$this->assertTrue( hash_equals( 'abc', 'abc' ) ); // @codingStandardsIgnoreLine.
$this->assertFalse( hash_equals( 'abcd', 'abc' ) ); // @codingStandardsIgnoreLine.
}
/**
* Test wc_rand_hash function.
*
* @return void
*/
public function test_wc_rand_hash() {
$this->assertNotEquals( wc_rand_hash(), wc_rand_hash() );
}
/**
* Test wc_transaction_query function.
*/
public function test_wc_transaction_query() {
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'options',
array(
'option_name' => 'transaction_test',
'option_value' => '1',
),
array(
'%s',
'%s',
)
);
wc_transaction_query( 'start' );
$this->assertTrue( WC_USE_TRANSACTIONS );
$wpdb->update(
$wpdb->prefix . 'options',
array(
'option_value' => '0',
),
array(
'option_name' => 'transaction_test',
)
);
$col = $wpdb->get_col( "SElECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'transaction_test'" );
$this->assertEquals( '0', $col[0] );
wc_transaction_query( 'rollback' );
$col = $wpdb->get_col( "SElECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'transaction_test'" );
$this->assertEquals( '1', $col[0] );
wc_transaction_query( 'start' );
$wpdb->update(
$wpdb->prefix . 'options',
array(
'option_value' => '0',
),
array(
'option_name' => 'transaction_test',
)
);
wc_transaction_query( 'commit' );
$col = $wpdb->get_col( "SElECT option_value FROM {$wpdb->prefix}options WHERE option_name = 'transaction_test'" );
$this->assertEquals( '0', $col[0] );
$wpdb->delete(
$wpdb->prefix . 'options',
array(
'option_name' => 'transaction_test',
)
);
}
}

View File

@ -20,17 +20,43 @@ class WC_Tests_Widget extends WC_Unit_Test_Case {
$this->assertTrue( property_exists( $dummy_widget, 'widget_id' ) );
}
/**
* Test widget caching.
*
* @return void
*/
public function test_caching() {
global $wp_widget_factory;
require_once 'class-dummy-widget.php';
register_widget( 'Dummy_Widget' );
$dummy_widget = $wp_widget_factory->widgets['Dummy_Widget'];
$this->assertFalse( $dummy_widget->get_cached_widget( array( 'widget_id' => $dummy_widget->widget_id ) ) );
// Uncached widget.
ob_start();
$cache_hit = $dummy_widget->get_cached_widget( array( 'widget_id' => $dummy_widget->widget_id ) );
$output = ob_get_clean();
$this->assertFalse( $cache_hit );
$this->assertEmpty( $output );
// Render widget to prime the cache.
ob_start();
$dummy_widget->widget( array( 'widget_id' => $dummy_widget->widget_id ), array() );
$this->assertTrue( $dummy_widget->get_cached_widget( array( 'widget_id' => $dummy_widget->widget_id ) ) );
ob_get_clean();
// Cached widget.
ob_start();
$cache_hit = $dummy_widget->get_cached_widget( array( 'widget_id' => $dummy_widget->widget_id ) );
$output = ob_get_clean();
$this->assertTrue( $cache_hit );
$this->assertEquals( 'Dummy', $output );
}
/**
* Test widget form.
*
* @return void
*/
public function test_form() {
global $wp_widget_factory;
require_once 'class-dummy-widget.php';