Bug 23177: (RM follow-up) Further test clarifications
[koha.git] / t / Number / Price.t
1 use Modern::Perl;
2
3 use Test::More tests => 34;
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 };
22
23 is( Koha::Number::Price->new->format( $format ),    '0.00', 'There is no currency defined yet, do not explode!' );
24
25 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
26 $currency = Koha::Acquisition::Currency->new({
27     currency => 'USD',
28     symbol   => '$',
29     rate     => 1,
30     active   => 1,
31     p_sep_by_space => 0, # Force to not add a space between the symbol and the number. This is the default behaviour
32 });
33
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' );
38
39 is( Koha::Number::Price->new(100000000000000)->format, '100000000000000', 'Numbers too big are not formatted');
40
41 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
42     '$0.00', 'US: format 0 with symbol' );
43 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
44     '$3.00', 'US: format 3 with symbol' );
45 is(
46     Koha::Number::Price->new(1234567890)
47       ->format( { %$format, with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
48     '$1,234,567,890.00'
49 );
50
51 $currency->p_sep_by_space(1);
52 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
53     '$ 3.00', 'US: format 3 with symbol and a space' );
54
55 is( Koha::Number::Price->new->unformat,    '0', 'US: unformat 0' );
56 is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
57 is( Koha::Number::Price->new(1234567890)->unformat,
58     '1234567890', 'US: unformat 1234567890' );
59
60 SKIP: {
61     # Bug 18900 - Check params are not from system environement
62     setlocale(LC_NUMERIC, "fr_FR.UTF-8");
63     my $current_locale = setlocale(LC_NUMERIC);
64
65     skip "fr_FR.UTF-8 locale required for tests and missing", 2
66         unless $current_locale eq 'fr_FR.UTF-8';
67
68     is( Koha::Number::Price->new(12345678.9)->format( { %$format, with_symbol => 1 } ),
69         '$ 12,345,678.90', 'US: format 12,345,678.90 with symbol' );
70     is( Koha::Number::Price->new('12,345,678.90')->unformat,
71         '12345678.9', 'US: unformat 12345678.9' );
72     setlocale(LC_NUMERIC, $orig_locale);
73 }
74
75 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
76 $currency = Koha::Acquisition::Currency->new({
77     currency => 'EUR',
78     symbol   => '€',
79     rate     => 1,
80     active   => 1,
81 });
82
83 # Actually,the price formating for France is 3,00€
84 # How put the symbol at the end with Number::Format?
85 is( Koha::Number::Price->new->format( $format ),    '0,00', 'FR: format 0' );
86 is( Koha::Number::Price->new(3)->format( $format ), '3,00', 'FR: format 3' );
87 is(
88     Koha::Number::Price->new(1234567890)->format( $format ),
89     '1 234 567 890,00',
90     'FR: format 1234567890'
91 );
92 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
93     '€0,00', 'FR: format 0 with symbol' );
94 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
95     '€3,00', 'FR: format 3 with symbol' );
96 is(
97     Koha::Number::Price->new(1234567890)
98       ->format( { %$format, with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
99     '€1 234 567 890,00'
100 );
101
102 is( Koha::Number::Price->new->unformat,    '0', 'FR: unformat 0' );
103 is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
104 is( Koha::Number::Price->new(1234567890)->unformat,
105     '1234567890', 'FR: unformat 1234567890' );
106
107 # Price formatting for Switzerland: 1'234'567.89
108 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'CH' );
109 $currency = Koha::Acquisition::Currency->new({
110     currency => 'nnn',
111     symbol   => 'CHF',
112     rate     => 1,
113     active   => 1,
114 });
115
116 is( Koha::Number::Price->new->format( $format ),    '0.00', 'CH: format 0' );
117 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'CH: format 3' );
118 is(
119     Koha::Number::Price->new(1234567890)->format( $format ),
120     '1\'234\'567\'890.00',
121     'CHF: format 1234567890'
122 );
123 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
124     'CHF0.00', 'CH: format 0 with symbol' );
125 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
126     'CHF3.00', 'CH: format 3 with symbol' );
127 is(
128     Koha::Number::Price->new(1234567890)
129       ->format( { %$format, with_symbol => 1 }, 'CH: format 123567890 with symbol' ),
130     'CHF1\'234\'567\'890.00'
131 );
132
133 is( Koha::Number::Price->new->unformat,    '0', 'CHF: unformat 0' );
134 is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' );
135 is( Koha::Number::Price->new(1234567890)->unformat,
136     '1234567890', 'CHF: unformat 1234567890' );
137
138 subtest 'Changes for format' => sub { # See also bug 18736
139     plan tests => 3;
140
141     t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
142
143     is( Koha::Number::Price->new(-2.125)->format, "-2.13", "Check negative value" );
144     my $large_number = 2**53; # MAX_INT
145     my $price = Koha::Number::Price->new($large_number);
146     is( $price->format, $price->value, 'Format '.$price->value.' returns value' );
147     like( Koha::Number::Price->new( 2**53/100 )->format,
148         qr/\d\.\d{2}$/, 'This price still seems to be formatted' );
149         # Note that the comparison with MAX_INT is already subject to rounding
150 };