3 use Test::More tests => 31;
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
21 p_sep_by_space => 0, # Force to not add a space between the symbol and the number
23 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
24 $currency = Koha::Acquisition::Currency->new({
31 is( Koha::Number::Price->new->format( $format ), '0.00', 'US: format 0' );
32 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'US: format 3' );
33 is( Koha::Number::Price->new(1234567890)->format( $format ),
34 '1,234,567,890.00', 'US: format 1234567890' );
36 is( Koha::Number::Price->new(100000000000000)->format, '100000000000000', 'Numbers too big are not formatted');
38 # FIXME This should be display symbol, but it was the case before the creation of this module
39 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
40 '0.00', 'US: format 0 with symbol' );
41 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
42 '3.00', 'US: format 3 with symbol' );
44 Koha::Number::Price->new(1234567890)
45 ->format( { %$format, with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
49 is( Koha::Number::Price->new->unformat, '0', 'US: unformat 0' );
50 is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
51 is( Koha::Number::Price->new(1234567890)->unformat,
52 '1234567890', 'US: unformat 1234567890' );
55 # Bug 18900 - Check params are not from system environement
56 setlocale(LC_NUMERIC, "fr_FR.UTF-8");
57 my $current_locale = setlocale(LC_NUMERIC);
59 skip "fr_FR.UTF-8 locale required for tests and missing", 2
60 unless $current_locale eq 'fr_FR.UTF-8';
62 is( Koha::Number::Price->new(12345678.9)->format( { %$format, with_symbol => 1 } ),
63 '12,345,678.90', 'US: format 12,345,678.90 with symbol' );
64 is( Koha::Number::Price->new('12,345,678.90')->unformat,
65 '12345678.9', 'US: unformat 12345678.9' );
66 setlocale(LC_NUMERIC, $orig_locale);
69 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
70 $currency = Koha::Acquisition::Currency->new({
77 # Actually,the price formating for France is 3,00€
78 # How put the symbol at the end with Number::Format?
79 is( Koha::Number::Price->new->format( $format ), '0,00', 'FR: format 0' );
80 is( Koha::Number::Price->new(3)->format( $format ), '3,00', 'FR: format 3' );
82 Koha::Number::Price->new(1234567890)->format( $format ),
84 'FR: format 1234567890'
86 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
87 '€0,00', 'FR: format 0 with symbol' );
88 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
89 '€3,00', 'FR: format 3 with symbol' );
91 Koha::Number::Price->new(1234567890)
92 ->format( { %$format, with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
96 is( Koha::Number::Price->new->unformat, '0', 'FR: unformat 0' );
97 is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
98 is( Koha::Number::Price->new(1234567890)->unformat,
99 '1234567890', 'FR: unformat 1234567890' );
101 # Price formatting for Switzerland: 1'234'567.89
102 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'CH' );
103 $currency = Koha::Acquisition::Currency->new({
110 is( Koha::Number::Price->new->format( $format ), '0.00', 'CH: format 0' );
111 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'CH: format 3' );
113 Koha::Number::Price->new(1234567890)->format( $format ),
114 '1\'234\'567\'890.00',
115 'CHF: format 1234567890'
117 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
118 'CHF0.00', 'CH: format 0 with symbol' );
119 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
120 'CHF3.00', 'CH: format 3 with symbol' );
122 Koha::Number::Price->new(1234567890)
123 ->format( { %$format, with_symbol => 1 }, 'CH: format 123567890 with symbol' ),
124 'CHF1\'234\'567\'890.00'
127 is( Koha::Number::Price->new->unformat, '0', 'CHF: unformat 0' );
128 is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' );
129 is( Koha::Number::Price->new(1234567890)->unformat,
130 '1234567890', 'CHF: unformat 1234567890' );