Koha/t/Number/Price.t
Jonathan Druart e6d1d8727b Bug 12844: New module to manage prices into Koha
This patch introduces a new module to manage prices into Koha and
especially the acquisition module.

How to use is:
1/ You can use it in a perl script/module:
  my $price = Koha::Number::Price->new(3);
  $price->format; # Will display 3.00 (or 3,00 depending on the CurrencyFormat syspref).
  $price->format({with_symbol => 1}); # Will display €3.00 (or [$]3,00 depending on the CurrencyFormat syspref).

2/ But this module is usefull to display the price from a template file.
  [% my_price | Price %]

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2014-10-27 12:56:18 -03:00

73 lines
2.4 KiB
Perl

use Modern::Perl;
use Test::More tests => 19;
use Test::MockModule;
use t::lib::Mocks;
use C4::Budgets;
my $budget_module = Test::MockModule->new('C4::Budgets');
my $currency;
$budget_module->mock( 'GetCurrency', sub { return $currency; } );
use_ok('Koha::Number::Price');
t::lib::Mocks::mock_preference( 'CurrencyFormat', 'US' );
$currency = {
currency => 'USD',
symbol => '$',
rate => 1,
active => 1,
};
is( Koha::Number::Price->new->format, '0.00', 'US: format 0' );
is( Koha::Number::Price->new(3)->format, '3.00', 'US: format 3' );
is( Koha::Number::Price->new(1234567890)->format,
'1,234,567,890.00', 'US: format 1234567890' );
# FIXME This should be display symbol, but it was the case before the creation of this module
is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
'0.00', 'US: format 0 with symbol' );
is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
'3.00', 'US: format 3 with symbol' );
is(
Koha::Number::Price->new(1234567890)
->format( { with_symbol => 1 }, 'US: format 1234567890 with symbol' ),
'1,234,567,890.00'
);
is( Koha::Number::Price->new->unformat, '0', 'US: unformat 0' );
is( Koha::Number::Price->new(3)->unformat, '3', 'US: unformat 3' );
is( Koha::Number::Price->new(1234567890)->unformat,
'1234567890', 'US: unformat 1234567890' );
t::lib::Mocks::mock_preference( 'CurrencyFormat', 'FR' );
$currency = {
currency => 'EUR',
symbol => '€',
rate => 1,
active => 1,
};
# Actually,the price formating for France is 3,00€
# How put the symbol at the end with Number::Format?
is( Koha::Number::Price->new->format, '0,00', 'FR: format 0' );
is( Koha::Number::Price->new(3)->format, '3,00', 'FR: format 3' );
is(
Koha::Number::Price->new(1234567890)->format,
'1 234 567 890,00',
'FR: format 1234567890'
);
is( Koha::Number::Price->new->format( { with_symbol => 1 } ),
'€0,00', 'FR: format 0 with symbol' );
is( Koha::Number::Price->new(3)->format( { with_symbol => 1 } ),
'€3,00', 'FR: format 3 with symbol' );
is(
Koha::Number::Price->new(1234567890)
->format( { with_symbol => 1 }, 'FR: format 123567890 with symbol' ),
'€1 234 567 890,00'
);
is( Koha::Number::Price->new->unformat, '0', 'FR: unformat 0' );
is( Koha::Number::Price->new(3)->unformat, '3', 'FR: unformat 3' );
is( Koha::Number::Price->new(1234567890)->unformat,
'1234567890', 'FR: unformat 1234567890' );