830f5c0e77b5bf6c11fa92fd8e91795ecbfee4d6
[koha.git] / t / db_dependent / MungeMarcPrice.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use C4::Biblio;
6 use C4::Budgets;
7 use Test::More;
8 use utf8;
9
10 # work around wide character warnings
11 binmode Test::More->builder->output, ":encoding(UTF-8)";
12 binmode Test::More->builder->failure_output, ":encoding(UTF-8)";
13
14 # start transaction
15 my $dbh = C4::Context->dbh;
16 $dbh->{AutoCommit} = 0;
17 $dbh->{RaiseError} = 1;
18
19 # set some test price strings and expected output
20 my @prices2test=( { string => '25,5 £, $34,55, $LD35',       expected => '34.55' },
21                   { string => '32 EUR, 42.50$ USD, 54 CAD',  expected=>'42.50' },
22                   { string => '38.80 Ksh, ¥300, 51,50 USD',  expected => '51.50' },
23                   { string => '44 $, 33 €, 64 Br, £30',      expected => '44' },
24                   { string => '9 EUR,$34,55 USD,$7.35 CAN',  expected => '34.55' },
25                   { string => '$55.32',                      expected => '55.32' },
26                   { string => '9.99 USD (paperback)',        expected => '9.99' },
27                   { string => '$9.99 USD (paperback)',       expected => '9.99' },
28                   { string => '18.95 (U.S.)',                expected => '18.95' },
29                   { string => '$5.99 ($7.75 CAN)',           expected => '5.99' },
30                   { string => '5.99 (7.75 CAN)',             expected => '5.99' },
31                 );
32
33 plan tests => scalar  @prices2test;
34
35 # set active currency test data
36 my $CURRENCY = 'TEST';
37 my $SYMBOL = '$';
38 my $ISOCODE = 'USD';
39 my $RATE= 1;
40
41 # disables existing active currency if necessary.
42 my $active_currency = C4::Budgets->GetCurrency();
43 my $curr;
44 if ($active_currency) {
45     $curr = $active_currency->{'currency'};
46     $dbh->do("UPDATE currency set active = 0 where currency = '$curr'");
47 }
48
49 $dbh->do("INSERT INTO currency ( currency,symbol,isocode,rate,active )
50           VALUES ('$CURRENCY','$SYMBOL','$ISOCODE','$RATE',1)");
51 foreach my $price (@prices2test) {
52     my $mungemarcprice=MungeMarcPrice($price->{'string'});
53     my $expected=$price->{'expected'};
54     ok ($mungemarcprice eq $expected, "must return $price->{'expected'} from initial string : $price->{'string'}");
55 }
56 # Cleanup
57 $dbh->rollback;