Bug 17781 - Improper branchcode set during renewal
[koha.git] / t / db_dependent / Stats.t
blob5ad960878ce100882142781c5d5160f9d8506157
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use C4::Stats;
6 use Test::More tests => 17;
8 BEGIN {
9 use_ok('C4::Stats');
11 can_ok(
12 'C4::Stats',
13 qw(UpdateStats
14 TotalPaid
18 #Start transaction
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
21 $dbh->{AutoCommit} = 0;
24 # Test UpdateStats
27 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
29 my $params = {
30 branch => "BRA",
31 itemnumber => 31,
32 borrowernumber => 5,
33 amount =>5.1,
34 other => "bla",
35 itemtype => "BK",
36 accountno => 51,
37 ccode => "CODE",
39 my $return_error;
41 # returns undef and croaks if type is not allowed
42 $params -> {type} = "bla";
43 eval {UpdateStats($params)};
44 $return_error = $@;
45 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
47 delete $params->{type};
48 # returns undef and croaks if type is missing
49 eval {UpdateStats($params)};
50 $return_error = $@;
51 isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
53 $params -> {type} = undef;
54 # returns undef and croaks if type is undef
55 eval {UpdateStats($params)};
56 $return_error = $@;
57 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
59 # returns undef and croaks if mandatory params are missing
60 my @allowed_circulation_types = qw (renew issue localuse return);
61 my @allowed_accounts_types = qw (writeoff payment);
62 my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
63 my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
65 my @missing_errors = ();
66 foreach my $key (@circulation_mandatory_keys) {
67 my $value = $params->{$key};
68 delete $params->{$key};
69 foreach my $type (@allowed_circulation_types) {
70 $params->{type} = $type;
71 eval {UpdateStats($params)};
72 $return_error = $@;
73 push @missing_errors, "key:$key for type:$type" unless $return_error;
75 $params->{$key} = $value;
77 foreach my $key (@accounts_mandatory_keys) {
78 my $value = $params->{$key};
79 delete $params->{$key};
80 foreach my $type (@allowed_accounts_types) {
81 $params->{type} = $type;
82 eval {UpdateStats($params)};
83 $return_error = $@;
84 push @missing_errors, "key:$key for type:$type" unless $return_error;
86 $params->{$key} = $value;
89 is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
91 # returns undef and croaks if forbidden params are given
92 $params -> {type} = "return";
93 $params -> {newparam} = "true";
94 eval {UpdateStats($params)};
95 $return_error = $@;
96 isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
97 delete $params->{newparam};
99 # save the params in the right database fields
100 $dbh->do(q|DELETE FROM statistics|);
101 $params = {
102 branch => "BRA",
103 itemnumber => 31,
104 borrowernumber => 5,
105 amount =>5.1,
106 other => "bla",
107 itemtype => "BK",
108 accountno => 51,
109 ccode => "CODE",
110 type => "return"
112 UpdateStats ($params);
113 my $sth = $dbh->prepare("SELECT * FROM statistics");
114 $sth->execute();
115 my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
116 is ($params-> {branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table");
117 is ($params-> {type}, $line->{type}, "UpdateStats save type param in type field of statistics table");
118 is ($params-> {borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
119 cmp_ok($params-> {amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table");
120 is ($params-> {other}, $line->{other}, "UpdateStats save other param in other field of statistics table");
121 is ($params-> {itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table");
122 is ($params-> {accountno}, $line->{proccode}, "UpdateStats save accountno param in proccode field of statistics table");
123 is ($params-> {ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table");
126 # Test TotalPaid
129 is (TotalPaid (),undef,"TotalPaid returns undef if no params are given");
130 # More tests to write!
132 #End transaction
133 $dbh->rollback;