3 use Test::More tests => 35;
8 # Number formating depends by default on system environement
9 # See http://search.cpan.org/~wrw/Number-Format/Format.pm
10 use POSIX qw(setlocale LC_NUMERIC);
12 use Koha::Acquisition::Currencies;
13 my $budget_module = Test::MockModule->new('Koha::Acquisition::Currencies');
15 $budget_module->mock( 'get_active', sub { return $currency; } );
16 use_ok('Koha::Number::Price');
18 my $orig_locale = setlocale(LC_NUMERIC);
20 p_cs_precedes => 1, # Force to place the symbol at the beginning
23 is( Koha::Number::Price->new->format( $format ), '0.00', 'There is no currency defined yet, do not explode!' );
25 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
26 $currency = Koha::Acquisition::Currency->new({
31 p_sep_by_space => 0, # Force to not add a space between the symbol and the number. This is the default behaviour
34 is( Koha::Number::Price->new->format( $format ), '0.00', 'US: format 0' );
35 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'US: format 3' );
36 is( Koha::Number::Price->new(1234567890)->format( $format ),
37 '1,234,567,890.00', 'US: format 1234567890' );
39 is( Koha::Number::Price->new(100000000000000)->format, '100000000000000', 'Numbers too big are not formatted');
40 is( Koha::Number::Price->new(-100000000000000)->format, '-100000000000000', 'Negative numbers too big are not formatted');
42 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
43 '$0.00', 'US: format 0 with symbol' );
44 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
45 '$3.00', 'US: format 3 with symbol' );
47 Koha::Number::Price->new(1234567890)
48 ->format( { %$format, with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
52 $currency->p_sep_by_space(1);
53 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
54 '$ 3.00', 'US: format 3 with symbol and a space' );
56 is( Koha::Number::Price->new->unformat, '0', 'US: unformat 0' );
57 is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
58 is( Koha::Number::Price->new(1234567890)->unformat,
59 '1234567890', 'US: unformat 1234567890' );
62 # Bug 18900 - Check params are not from system environement
63 setlocale(LC_NUMERIC, "fr_FR.UTF-8");
64 my $current_locale = setlocale(LC_NUMERIC);
66 skip "fr_FR.UTF-8 locale required for tests and missing", 2
67 unless $current_locale eq 'fr_FR.UTF-8';
69 is( Koha::Number::Price->new(12345678.9)->format( { %$format, with_symbol => 1 } ),
70 '$ 12,345,678.90', 'US: format 12,345,678.90 with symbol' );
71 is( Koha::Number::Price->new('12,345,678.90')->unformat,
72 '12345678.9', 'US: unformat 12345678.9' );
73 setlocale(LC_NUMERIC, $orig_locale);
76 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
77 $currency = Koha::Acquisition::Currency->new({
84 # Actually,the price formating for France is 3,00€
85 # How put the symbol at the end with Number::Format?
86 is( Koha::Number::Price->new->format( $format ), '0,00', 'FR: format 0' );
87 is( Koha::Number::Price->new(3)->format( $format ), '3,00', 'FR: format 3' );
89 Koha::Number::Price->new(1234567890)->format( $format ),
91 'FR: format 1234567890'
93 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
94 '€0,00', 'FR: format 0 with symbol' );
95 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
96 '€3,00', 'FR: format 3 with symbol' );
98 Koha::Number::Price->new(1234567890)
99 ->format( { %$format, with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
103 is( Koha::Number::Price->new->unformat, '0', 'FR: unformat 0' );
104 is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
105 is( Koha::Number::Price->new(1234567890)->unformat,
106 '1234567890', 'FR: unformat 1234567890' );
108 # Price formatting for Switzerland: 1'234'567.89
109 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'CH' );
110 $currency = Koha::Acquisition::Currency->new({
117 is( Koha::Number::Price->new->format( $format ), '0.00', 'CH: format 0' );
118 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'CH: format 3' );
120 Koha::Number::Price->new(1234567890)->format( $format ),
121 '1\'234\'567\'890.00',
122 'CHF: format 1234567890'
124 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
125 'CHF0.00', 'CH: format 0 with symbol' );
126 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
127 'CHF3.00', 'CH: format 3 with symbol' );
129 Koha::Number::Price->new(1234567890)
130 ->format( { %$format, with_symbol => 1 }, 'CH: format 123567890 with symbol' ),
131 'CHF1\'234\'567\'890.00'
134 is( Koha::Number::Price->new->unformat, '0', 'CHF: unformat 0' );
135 is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' );
136 is( Koha::Number::Price->new(1234567890)->unformat,
137 '1234567890', 'CHF: unformat 1234567890' );
139 subtest 'Changes for format' => sub { # See also bug 18736
142 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
144 is( Koha::Number::Price->new(-2.125)->format, "-2.13", "Check negative value" );
145 my $large_number = 2**53; # MAX_INT
146 my $price = Koha::Number::Price->new($large_number);
147 is( $price->format, $price->value, 'Format '.$price->value.' returns value' );
148 like( Koha::Number::Price->new( 2**53/100 )->format,
149 qr/\d\.\d{2}$/, 'This price still seems to be formatted' );
150 # Note that the comparison with MAX_INT is already subject to rounding