Add tests for wc_sanitize_order_id()

This commit adds four test cases (via a PHPUnit data provider) for the `wc_sanitize_order_id()` function.

While writing the tests, it was also discovered that the function was returning a *string*, not an integer as indicated by the DocBlock, so explicit type-casting was added.
This commit is contained in:
Steve Grunwell 2018-10-04 13:53:55 +00:00
parent 96005a51c4
commit 65cda44fe6
2 changed files with 13 additions and 1 deletions

View File

@ -886,7 +886,7 @@ add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
* @param int $order_id Order ID.
*/
function wc_sanitize_order_id( $order_id ) {
return filter_var( $order_id, FILTER_SANITIZE_NUMBER_INT );
return (int) filter_var( $order_id, FILTER_SANITIZE_NUMBER_INT );
}
add_filter( 'woocommerce_shortcode_order_tracking_order_id', 'wc_sanitize_order_id' );

View File

@ -1271,6 +1271,18 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case {
$this->assertEquals( $expected, $orders );
}
/**
* Test wc_sanitize_order_id().
*
* @testWith ["123", 123]
* ["#123", 123]
* ["# 123", 123]
* ["Order #123", 123]
*/
public function test_wc_sanitize_order_id( $id, $expected ) {
$this->assertSame( $expected, wc_sanitize_order_id( $id ) );
}
/**
* Test wc_get_order_note().
*