Bug 4078: Add the ability to display and configure the symbol for prices
[koha.git] / t / Number / Price.t
1 use Modern::Perl;
2
3 use Test::More tests => 31;
4
5 use Test::MockModule;
6 use t::lib::Mocks;
7
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);
11
12 use Koha::Acquisition::Currencies;
13 my $budget_module = Test::MockModule->new('Koha::Acquisition::Currencies');
14 my $currency;
15 $budget_module->mock( 'get_active', sub { return $currency; } );
16 use_ok('Koha::Number::Price');
17
18 my $orig_locale = setlocale(LC_NUMERIC);
19 my $format = {
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
22 };
23 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
24 $currency = Koha::Acquisition::Currency->new({
25     currency => 'USD',
26     symbol   => '$',
27     rate     => 1,
28     active   => 1,
29 });
30
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' );
35
36 is( Koha::Number::Price->new(100000000000000)->format, '100000000000000', 'Numbers too big are not formatted');
37
38 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
39     '$0.00', 'US: format 0 with symbol' );
40 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
41     '$3.00', 'US: format 3 with symbol' );
42 is(
43     Koha::Number::Price->new(1234567890)
44       ->format( { %$format, with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
45     '$1,234,567,890.00'
46 );
47
48 is( Koha::Number::Price->new->unformat,    '0', 'US: unformat 0' );
49 is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
50 is( Koha::Number::Price->new(1234567890)->unformat,
51     '1234567890', 'US: unformat 1234567890' );
52
53 SKIP: {
54     # Bug 18900 - Check params are not from system environement
55     setlocale(LC_NUMERIC, "fr_FR.UTF-8");
56     my $current_locale = setlocale(LC_NUMERIC);
57
58     skip "fr_FR.UTF-8 locale required for tests and missing", 2
59         unless $current_locale eq 'fr_FR.UTF-8';
60
61     is( Koha::Number::Price->new(12345678.9)->format( { %$format, with_symbol => 1 } ),
62         '12,345,678.90', 'US: format 12,345,678.90 with symbol' );
63     is( Koha::Number::Price->new('12,345,678.90')->unformat,
64         '12345678.9', 'US: unformat 12345678.9' );
65     setlocale(LC_NUMERIC, $orig_locale);
66 }
67
68 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
69 $currency = Koha::Acquisition::Currency->new({
70     currency => 'EUR',
71     symbol   => '€',
72     rate     => 1,
73     active   => 1,
74 });
75
76 # Actually,the price formating for France is 3,00€
77 # How put the symbol at the end with Number::Format?
78 is( Koha::Number::Price->new->format( $format ),    '0,00', 'FR: format 0' );
79 is( Koha::Number::Price->new(3)->format( $format ), '3,00', 'FR: format 3' );
80 is(
81     Koha::Number::Price->new(1234567890)->format( $format ),
82     '1 234 567 890,00',
83     'FR: format 1234567890'
84 );
85 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
86     '€0,00', 'FR: format 0 with symbol' );
87 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
88     '€3,00', 'FR: format 3 with symbol' );
89 is(
90     Koha::Number::Price->new(1234567890)
91       ->format( { %$format, with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
92     '€1 234 567 890,00'
93 );
94
95 is( Koha::Number::Price->new->unformat,    '0', 'FR: unformat 0' );
96 is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
97 is( Koha::Number::Price->new(1234567890)->unformat,
98     '1234567890', 'FR: unformat 1234567890' );
99
100 # Price formatting for Switzerland: 1'234'567.89
101 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'CH' );
102 $currency = Koha::Acquisition::Currency->new({
103     currency => 'nnn',
104     symbol   => 'CHF',
105     rate     => 1,
106     active   => 1,
107 });
108
109 is( Koha::Number::Price->new->format( $format ),    '0.00', 'CH: format 0' );
110 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'CH: format 3' );
111 is(
112     Koha::Number::Price->new(1234567890)->format( $format ),
113     '1\'234\'567\'890.00',
114     'CHF: format 1234567890'
115 );
116 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
117     'CHF0.00', 'CH: format 0 with symbol' );
118 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
119     'CHF3.00', 'CH: format 3 with symbol' );
120 is(
121     Koha::Number::Price->new(1234567890)
122       ->format( { %$format, with_symbol => 1 }, 'CH: format 123567890 with symbol' ),
123     'CHF1\'234\'567\'890.00'
124 );
125
126 is( Koha::Number::Price->new->unformat,    '0', 'CHF: unformat 0' );
127 is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' );
128 is( Koha::Number::Price->new(1234567890)->unformat,
129     '1234567890', 'CHF: unformat 1234567890' );