Bug 18900: (QA followup) Make tests conditional to locale existence
[koha.git] / t / Number / Price.t
1 use Modern::Perl;
2
3 use Test::More tests => 30;
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 # FIXME This should be display symbol, but it was the case before the creation of this module
37 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
38     '0.00', 'US: format 0 with symbol' );
39 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
40     '3.00', 'US: format 3 with symbol' );
41 is(
42     Koha::Number::Price->new(1234567890)
43       ->format( { %$format, with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
44     '1,234,567,890.00'
45 );
46
47 is( Koha::Number::Price->new->unformat,    '0', 'US: unformat 0' );
48 is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
49 is( Koha::Number::Price->new(1234567890)->unformat,
50     '1234567890', 'US: unformat 1234567890' );
51
52 SKIP: {
53     # Bug 18900 - Check params are not from system environement
54     setlocale(LC_NUMERIC, "fr_FR.UTF-8");
55     my $current_locale = setlocale(LC_NUMERIC);
56
57     skip "fr_FR.UTF-8 locale required for tests and missing", 2
58         unless $current_locale eq 'fr_FR.UTF-8';
59
60     is( Koha::Number::Price->new(12345678.9)->format( { %$format, with_symbol => 1 } ),
61         '12,345,678.90', 'US: format 12,345,678.90 with symbol' );
62     is( Koha::Number::Price->new('12,345,678.90')->unformat,
63         '12345678.9', 'US: unformat 12345678.9' );
64     setlocale(LC_NUMERIC, $orig_locale);
65 }
66
67 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
68 $currency = Koha::Acquisition::Currency->new({
69     currency => 'EUR',
70     symbol   => '€',
71     rate     => 1,
72     active   => 1,
73 });
74
75 # Actually,the price formating for France is 3,00€
76 # How put the symbol at the end with Number::Format?
77 is( Koha::Number::Price->new->format( $format ),    '0,00', 'FR: format 0' );
78 is( Koha::Number::Price->new(3)->format( $format ), '3,00', 'FR: format 3' );
79 is(
80     Koha::Number::Price->new(1234567890)->format( $format ),
81     '1 234 567 890,00',
82     'FR: format 1234567890'
83 );
84 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
85     '€0,00', 'FR: format 0 with symbol' );
86 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
87     '€3,00', 'FR: format 3 with symbol' );
88 is(
89     Koha::Number::Price->new(1234567890)
90       ->format( { %$format, with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
91     '€1 234 567 890,00'
92 );
93
94 is( Koha::Number::Price->new->unformat,    '0', 'FR: unformat 0' );
95 is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
96 is( Koha::Number::Price->new(1234567890)->unformat,
97     '1234567890', 'FR: unformat 1234567890' );
98
99 # Price formatting for Switzerland: 1'234'567.89
100 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'CH' );
101 $currency = Koha::Acquisition::Currency->new({
102     currency => 'nnn',
103     symbol   => 'CHF',
104     rate     => 1,
105     active   => 1,
106 });
107
108 is( Koha::Number::Price->new->format( $format ),    '0.00', 'CH: format 0' );
109 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'CH: format 3' );
110 is(
111     Koha::Number::Price->new(1234567890)->format( $format ),
112     '1\'234\'567\'890.00',
113     'CHF: format 1234567890'
114 );
115 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
116     'CHF0.00', 'CH: format 0 with symbol' );
117 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
118     'CHF3.00', 'CH: format 3 with symbol' );
119 is(
120     Koha::Number::Price->new(1234567890)
121       ->format( { %$format, with_symbol => 1 }, 'CH: format 123567890 with symbol' ),
122     'CHF1\'234\'567\'890.00'
123 );
124
125 is( Koha::Number::Price->new->unformat,    '0', 'CHF: unformat 0' );
126 is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' );
127 is( Koha::Number::Price->new(1234567890)->unformat,
128     '1234567890', 'CHF: unformat 1234567890' );