Bug 21716: Escape publisher in item search
[koha.git] / t / db_dependent / Stats.t
blob7e2e6f7b6c34c7419a05bf94389c27fea72f658f
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use C4::Stats;
5 use Koha::Database;
7 use Test::More tests => 18;
9 BEGIN {
10 use_ok('C4::Stats');
12 can_ok(
13 'C4::Stats',
14 qw(UpdateStats)
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19 my $dbh = C4::Context->dbh;
22 # Test UpdateStats
25 is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
27 my $params = {
28 branch => "BRA",
29 itemnumber => 31,
30 borrowernumber => 5,
31 amount =>5.1,
32 other => "bla",
33 itemtype => "BK",
34 location => "LOC",
35 ccode => "CODE",
37 my $return_error;
39 # returns undef and croaks if type is not allowed
40 $params -> {type} = "bla";
41 eval {UpdateStats($params)};
42 $return_error = $@;
43 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is not allowed");
45 delete $params->{type};
46 # returns undef and croaks if type is missing
47 eval {UpdateStats($params)};
48 $return_error = $@;
49 isnt ($return_error,'',"UpdateStats returns undef and croaks if no type given");
51 $params -> {type} = undef;
52 # returns undef and croaks if type is undef
53 eval {UpdateStats($params)};
54 $return_error = $@;
55 isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
57 # returns undef and croaks if mandatory params are missing
58 my @allowed_circulation_types = qw (renew issue localuse return);
59 my @allowed_accounts_types = qw (writeoff payment);
60 my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
61 my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
63 my @missing_errors = ();
64 foreach my $key (@circulation_mandatory_keys) {
65 my $value = $params->{$key};
66 delete $params->{$key};
67 foreach my $type (@allowed_circulation_types) {
68 $params->{type} = $type;
69 eval {UpdateStats($params)};
70 $return_error = $@;
71 push @missing_errors, "key:$key for type:$type" unless $return_error;
73 $params->{$key} = $value;
75 foreach my $key (@accounts_mandatory_keys) {
76 my $value = $params->{$key};
77 delete $params->{$key};
78 foreach my $type (@allowed_accounts_types) {
79 $params->{type} = $type;
80 eval {UpdateStats($params)};
81 $return_error = $@;
82 push @missing_errors, "key:$key for type:$type" unless $return_error;
84 $params->{$key} = $value;
87 is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
89 # returns undef and croaks if forbidden params are given
90 $params -> {type} = "return";
91 $params -> {newparam} = "true";
92 eval {UpdateStats($params)};
93 $return_error = $@;
94 isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
95 delete $params->{newparam};
97 # save the params in the right database fields
98 $dbh->do(q|DELETE FROM statistics|);
99 $params = {
100 branch => "BRA",
101 itemnumber => 31,
102 borrowernumber => 5,
103 amount =>5.1,
104 other => "bla",
105 itemtype => "BK",
106 location => "LOC",
107 ccode => "CODE",
108 type => "return"
110 UpdateStats ($params);
111 my $sth = $dbh->prepare("SELECT * FROM statistics");
112 $sth->execute();
113 my $line = ${ $sth->fetchall_arrayref( {} ) }[0];
114 is ($params->{branch}, $line->{branch}, "UpdateStats save branch param in branch field of statistics table");
115 is ($params->{type}, $line->{type}, "UpdateStats save type param in type field of statistics table");
116 is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
117 cmp_ok($params->{amount},'==', $line->{value}, "UpdateStats save amount param in value field of statistics table");
118 is ($params->{other}, $line->{other}, "UpdateStats save other param in other field of statistics table");
119 is ($params->{itemtype}, $line->{itemtype}, "UpdateStats save itemtype param in itemtype field of statistics table");
120 is ($params->{location}, $line->{location}, "UpdateStats save location param in location field of statistics table");
121 is ($params->{ccode}, $line->{ccode}, "UpdateStats save ccode param in ccode field of statistics table");
123 $dbh->do(q|DELETE FROM statistics|);
124 $params = {
125 branch => "BRA",
126 itemnumber => 31,
127 borrowernumber => 5,
128 amount => 5.1,
129 other => "bla",
130 itemtype => "BK",
131 ccode => "CODE",
132 type => "return"
134 UpdateStats($params);
135 $sth = $dbh->prepare("SELECT * FROM statistics");
136 $sth->execute();
137 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
138 is( $line->{location}, undef,
139 "UpdateStats sets location to NULL if no location is passed in." );
141 $dbh->do(q|DELETE FROM statistics|);
142 $params = {
143 branch => "BRA",
144 itemnumber => 31,
145 borrowernumber => 5,
146 amount => 5.1,
147 other => "bla",
148 itemtype => "BK",
149 location => undef,
150 ccode => "CODE",
151 type => "return"
153 UpdateStats($params);
154 $sth = $dbh->prepare("SELECT * FROM statistics");
155 $sth->execute();
156 $line = ${ $sth->fetchall_arrayref( {} ) }[0];
157 is( $line->{location}, undef,
158 "UpdateStats sets location to NULL if undef is passed in." );
160 # More tests to write!