Merge pull request #29721 from woocommerce/fix/29525

Modify wc_get_low_stock_amount function to always return a number
This commit is contained in:
Roy Ho 2021-04-20 11:56:27 -07:00 committed by GitHub
commit e4fec5b914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 22 deletions

View File

@ -403,7 +403,7 @@ function wc_get_low_stock_amount( WC_Product $product ) {
$low_stock_amount = $product->get_low_stock_amount();
if ( '' === $low_stock_amount && $product->is_type( 'variation' ) ) {
$product = wc_get_product( $product->get_parent_id() );
$product = wc_get_product( $product->get_parent_id() );
$low_stock_amount = $product->get_low_stock_amount();
}
@ -411,5 +411,5 @@ function wc_get_low_stock_amount( WC_Product $product ) {
$low_stock_amount = get_option( 'woocommerce_notify_low_stock_amount', 2 );
}
return $low_stock_amount;
return (int) $low_stock_amount;
}

View File

@ -7,6 +7,7 @@
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\Testing\Tools\CodeHacking\CodeHacker;
use PHPUnit\Framework\Constraint\IsType;
/**
* WC Unit Test Case.
@ -246,4 +247,16 @@ class WC_Unit_Test_Case extends WP_HTTP_TestCase {
public function register_legacy_proxy_class_mocks( array $mocks ) {
wc_get_container()->get( LegacyProxy::class )->register_class_mocks( $mocks );
}
/**
* Asserts that a variable is of type int.
* TODO: After upgrading to PHPUnit 8 or newer, remove this method and replace calls with PHPUnit's built-in 'assertIsInt'.
*
* @param mixed $actual The value to check.
* @param mixed $message Error message to use if the assertion fails.
* @return bool mixed True if the value is of integer type, false otherwise.
*/
public static function assertIsInteger( $actual, $message = '' ) {
return self::assertInternalType( 'int', $actual, $message );
}
}

View File

@ -192,6 +192,17 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
}
}
/**
* Assert that a value is equal to another one and is of integer type.
*
* @param mixed $expected The value $actual must be equal to.
* @param mixed $actual The value to check for equality to $expected and for type.
*/
private function assertIsIntAndEquals( $expected, $actual ) {
$this->assertEquals( $expected, $actual );
self::assertIsInteger( $actual );
}
/**
* Test wc_get_low_stock_amount with a simple product which has low stock amount set.
*/
@ -200,7 +211,7 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
$site_wide_low_stock_amount = 3;
// Set the store-wide default.
update_option( 'woocommerce_notify_low_stock_amount', $site_wide_low_stock_amount );
update_option( 'woocommerce_notify_low_stock_amount', strval( $site_wide_low_stock_amount ) );
// Simple product, set low stock amount.
$product = WC_Helper_Product::create_simple_product(
@ -212,7 +223,7 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
)
);
$this->assertEquals( $product_low_stock_amount, wc_get_low_stock_amount( $product ) );
$this->assertIsIntAndEquals( $product_low_stock_amount, wc_get_low_stock_amount( $product ) );
}
/**
@ -222,18 +233,18 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
$site_wide_low_stock_amount = 3;
// Set the store-wide default.
update_option( 'woocommerce_notify_low_stock_amount', $site_wide_low_stock_amount );
update_option( 'woocommerce_notify_low_stock_amount', strval( $site_wide_low_stock_amount ) );
// Simple product, don't set low stock amount.
$product = WC_Helper_Product::create_simple_product(
true,
array(
'manage_stock' => true,
'stock_quantity' => 10,
'manage_stock' => true,
'stock_quantity' => 10,
)
);
$this->assertEquals( $site_wide_low_stock_amount, wc_get_low_stock_amount( $product ) );
$this->assertIsIntAndEquals( $site_wide_low_stock_amount, wc_get_low_stock_amount( $product ) );
}
/**
@ -245,7 +256,7 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
$variation_low_stock_amount = 7;
// Set the store-wide default.
update_option( 'woocommerce_notify_low_stock_amount', $site_wide_low_stock_amount );
update_option( 'woocommerce_notify_low_stock_amount', strval( $site_wide_low_stock_amount ) );
// Parent low stock amount NOT set.
$variable_product = WC_Helper_Product::create_variation_product();
@ -254,22 +265,22 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
// Set the variation low stock amount.
$variations = $variable_product->get_available_variations( 'objects' );
$var1 = $variations[0];
$var1 = $variations[0];
$var1->set_manage_stock( true );
$var1->set_low_stock_amount( $variation_low_stock_amount );
$var1->save();
$this->assertEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
$this->assertIsIntAndEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
// Even after turning on manage stock on the parent, but with no value.
$variable_product->set_manage_stock( true );
$variable_product->save();
$this->assertEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
$this->assertIsIntAndEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
// Ans also after turning the manage stock off again on the parent.
$variable_product->set_manage_stock( false );
$variable_product->save();
$this->assertEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
$this->assertIsIntAndEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
}
/**
@ -282,7 +293,7 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
$variation_low_stock_amount = 7;
// Set the store-wide default.
update_option( 'woocommerce_notify_low_stock_amount', $site_wide_low_stock_amount );
update_option( 'woocommerce_notify_low_stock_amount', strval( $site_wide_low_stock_amount ) );
// Set the parent low stock amount.
$variable_product = WC_Helper_Product::create_variation_product();
@ -292,12 +303,12 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
// Set the variation low stock amount.
$variations = $variable_product->get_available_variations( 'objects' );
$var1 = $variations[0];
$var1 = $variations[0];
$var1->set_manage_stock( true );
$var1->set_low_stock_amount( $variation_low_stock_amount );
$var1->save();
$this->assertEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
$this->assertIsIntAndEquals( $variation_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
}
/**
@ -309,7 +320,7 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
$parent_low_stock_amount = 5;
// Set the store-wide default.
update_option( 'woocommerce_notify_low_stock_amount', $site_wide_low_stock_amount );
update_option( 'woocommerce_notify_low_stock_amount', strval( $site_wide_low_stock_amount ) );
// Set the parent low stock amount.
$variable_product = WC_Helper_Product::create_variation_product();
@ -319,9 +330,9 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
// Don't set the variation low stock amount.
$variations = $variable_product->get_available_variations( 'objects' );
$var1 = $variations[0];
$var1 = $variations[0];
$this->assertEquals( $parent_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
$this->assertIsIntAndEquals( $parent_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
}
/**
@ -332,7 +343,7 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
$site_wide_low_stock_amount = 3;
// Set the store-wide default.
update_option( 'woocommerce_notify_low_stock_amount', $site_wide_low_stock_amount );
update_option( 'woocommerce_notify_low_stock_amount', strval( $site_wide_low_stock_amount ) );
// Set the parent low stock amount.
$variable_product = WC_Helper_Product::create_variation_product();
@ -340,10 +351,10 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
// Don't set the variation low stock amount.
$variations = $variable_product->get_available_variations( 'objects' );
$var1 = $variations[0];
$var1 = $variations[0];
$var1->set_manage_stock( false );
$this->assertEquals( $site_wide_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
$this->assertIsIntAndEquals( $site_wide_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
}
}