Bug 18736: (QA follow-up) Change to signed, add large int test
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 28 Sep 2018 09:20:49 +0000 (11:20 +0200)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Mon, 25 Mar 2019 14:29:31 +0000 (14:29 +0000)
[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 <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
(cherry picked from commit 2f5e839bf780955d228c9672abce9d6e5ad1aedf)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

C4/Acquisition.pm
t/Number/Price.t

index ed09360..802a82c 100644 (file)
@@ -2012,7 +2012,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;
 }
index 6eed734..141f00f 100644 (file)
@@ -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
+};