Bug 28254: Add missing tests for PUT and POST
[koha.git] / t / Number / Price.t
1 use Modern::Perl;
2
3 use Test::More tests => 35;
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 is( Koha::Number::Price->new(-100000000000000)->format, '-100000000000000', 'Negative numbers too big are not formatted');
41
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' );
46 is(
47     Koha::Number::Price->new(1234567890)
48       ->format( { %$format, with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
49     '$1,234,567,890.00'
50 );
51
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' );
55
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' );
60
61 SKIP: {
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);
65
66     skip "fr_FR.UTF-8 locale required for tests and missing", 2
67         unless $current_locale eq 'fr_FR.UTF-8';
68
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);
74 }
75
76 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
77 $currency = Koha::Acquisition::Currency->new({
78     currency => 'EUR',
79     symbol   => '€',
80     rate     => 1,
81     active   => 1,
82 });
83
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' );
88 is(
89     Koha::Number::Price->new(1234567890)->format( $format ),
90     '1 234 567 890,00',
91     'FR: format 1234567890'
92 );
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' );
97 is(
98     Koha::Number::Price->new(1234567890)
99       ->format( { %$format, with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
100     '€1 234 567 890,00'
101 );
102
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' );
107
108 # Price formatting for Switzerland: 1'234'567.89
109 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'CH' );
110 $currency = Koha::Acquisition::Currency->new({
111     currency => 'nnn',
112     symbol   => 'CHF',
113     rate     => 1,
114     active   => 1,
115 });
116
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' );
119 is(
120     Koha::Number::Price->new(1234567890)->format( $format ),
121     '1\'234\'567\'890.00',
122     'CHF: format 1234567890'
123 );
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' );
128 is(
129     Koha::Number::Price->new(1234567890)
130       ->format( { %$format, with_symbol => 1 }, 'CH: format 123567890 with symbol' ),
131     'CHF1\'234\'567\'890.00'
132 );
133
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' );
138
139 subtest 'Changes for format' => sub { # See also bug 18736
140     plan tests => 3;
141
142     t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
143
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
151 };