Bug 20322: (follow up) design refresh for circulation page
[koha.git] / t / Number / Price.t
blobd7d143ad01fe35fe566ad3cc174f49e6535ce457
1 use Modern::Perl;
3 use Test::More tests => 32;
5 use Test::MockModule;
6 use t::lib::Mocks;
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');
14 my $currency;
15 $budget_module->mock( 'get_active', sub { return $currency; } );
16 use_ok('Koha::Number::Price');
18 my $orig_locale = setlocale(LC_NUMERIC);
19 my $format = {
20     p_cs_precedes => 1, # Force to place the symbol at the beginning
22 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
23 $currency = Koha::Acquisition::Currency->new({
24     currency => 'USD',
25     symbol   => '$',
26     rate     => 1,
27     active   => 1,
28     p_sep_by_space => 0, # Force to not add a space between the symbol and the number. This is the default behaviour
29 });
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 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'
48 $currency->p_sep_by_space(1);
49 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
50     '$ 3.00', 'US: format 3 with symbol and a space' );
52 is( Koha::Number::Price->new->unformat,    '0', 'US: unformat 0' );
53 is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
54 is( Koha::Number::Price->new(1234567890)->unformat,
55     '1234567890', 'US: unformat 1234567890' );
57 SKIP: {
58     # Bug 18900 - Check params are not from system environement
59     setlocale(LC_NUMERIC, "fr_FR.UTF-8");
60     my $current_locale = setlocale(LC_NUMERIC);
62     skip "fr_FR.UTF-8 locale required for tests and missing", 2
63         unless $current_locale eq 'fr_FR.UTF-8';
65     is( Koha::Number::Price->new(12345678.9)->format( { %$format, with_symbol => 1 } ),
66         '$ 12,345,678.90', 'US: format 12,345,678.90 with symbol' );
67     is( Koha::Number::Price->new('12,345,678.90')->unformat,
68         '12345678.9', 'US: unformat 12345678.9' );
69     setlocale(LC_NUMERIC, $orig_locale);
72 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
73 $currency = Koha::Acquisition::Currency->new({
74     currency => 'EUR',
75     symbol   => '€',
76     rate     => 1,
77     active   => 1,
78 });
80 # Actually,the price formating for France is 3,00€
81 # How put the symbol at the end with Number::Format?
82 is( Koha::Number::Price->new->format( $format ),    '0,00', 'FR: format 0' );
83 is( Koha::Number::Price->new(3)->format( $format ), '3,00', 'FR: format 3' );
84 is(
85     Koha::Number::Price->new(1234567890)->format( $format ),
86     '1 234 567 890,00',
87     'FR: format 1234567890'
89 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
90     '€0,00', 'FR: format 0 with symbol' );
91 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
92     '€3,00', 'FR: format 3 with symbol' );
93 is(
94     Koha::Number::Price->new(1234567890)
95       ->format( { %$format, with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
96     '€1 234 567 890,00'
99 is( Koha::Number::Price->new->unformat,    '0', 'FR: unformat 0' );
100 is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
101 is( Koha::Number::Price->new(1234567890)->unformat,
102     '1234567890', 'FR: unformat 1234567890' );
104 # Price formatting for Switzerland: 1'234'567.89
105 t::lib::Mocks::mock_preference( 'CurrencyFormat', 'CH' );
106 $currency = Koha::Acquisition::Currency->new({
107     currency => 'nnn',
108     symbol   => 'CHF',
109     rate     => 1,
110     active   => 1,
113 is( Koha::Number::Price->new->format( $format ),    '0.00', 'CH: format 0' );
114 is( Koha::Number::Price->new(3)->format( $format ), '3.00', 'CH: format 3' );
116     Koha::Number::Price->new(1234567890)->format( $format ),
117     '1\'234\'567\'890.00',
118     'CHF: format 1234567890'
120 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
121     'CHF0.00', 'CH: format 0 with symbol' );
122 is( Koha::Number::Price->new(3)->format( { %$format, with_symbol => 1 } ),
123     'CHF3.00', 'CH: format 3 with symbol' );
125     Koha::Number::Price->new(1234567890)
126       ->format( { %$format, with_symbol => 1 }, 'CH: format 123567890 with symbol' ),
127     'CHF1\'234\'567\'890.00'
130 is( Koha::Number::Price->new->unformat,    '0', 'CHF: unformat 0' );
131 is( Koha::Number::Price->new(3)->unformat, '3', 'CHF: unformat 3' );
132 is( Koha::Number::Price->new(1234567890)->unformat,
133     '1234567890', 'CHF: unformat 1234567890' );