From 2f5e839bf780955d228c9672abce9d6e5ad1aedf Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 28 Sep 2018 11:20:49 +0200 Subject: [PATCH] Bug 18736: (QA follow-up) Change to signed, add large int test [1] Add trivial subtest in t/Number/Price.t Checking a negative number and a large number for the MAX_INT change. Note: Confusing to have t/Prices.t too. [2] Change UNSIGNED to SIGNED in get_rounding_sql. Although I did not spot problems with negative prices, we theoretically could while casting. cast(-2 as unsigned) == 18446744073709551614 Signed-off-by: Marcel de Rooy Signed-off-by: Nick Clemens --- C4/Acquisition.pm | 2 +- t/Number/Price.t | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 2123ea32ea..dfea465a5f 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -2013,7 +2013,7 @@ sub get_rounding_sql { my ( $round_string ) = @_; my $rounding_pref = C4::Context->preference('OrderPriceRounding') // q{}; if ( $rounding_pref eq "nearest_cent" ) { - return "CAST($round_string*100 AS UNSIGNED)/100"; + return "CAST($round_string*100 AS SIGNED)/100"; } return $round_string; } diff --git a/t/Number/Price.t b/t/Number/Price.t index 6eed734443..141f00ffa2 100644 --- a/t/Number/Price.t +++ b/t/Number/Price.t @@ -1,6 +1,6 @@ use Modern::Perl; -use Test::More tests => 33; +use Test::More tests => 34; use Test::MockModule; use t::lib::Mocks; @@ -134,3 +134,17 @@ is( Koha::Number::Price->new->unformat, '0', 'CHF: unformat 0' ); is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' ); is( Koha::Number::Price->new(1234567890)->unformat, '1234567890', 'CHF: unformat 1234567890' ); + +subtest 'Changes for format' => sub { # See also bug 18736 + plan tests => 3; + + t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' ); + + is( Koha::Number::Price->new(-2.125)->format, "-2.13", "Check negative value" ); + my $large_number = 2**53; # MAX_INT + my $price = Koha::Number::Price->new($large_number); + is( $price->format, $price->value, 'Format '.$price->value.' returns value' ); + like( Koha::Number::Price->new( 2**53/100 )->format, + qr/\d\.\d{2}$/, 'This price still seems to be formatted' ); + # Note that the comparison with MAX_INT is already subject to rounding +}; -- 2.39.5