Bug 10565: Add a "Patron List" feature for storing and manipulating collections of...
[koha.git] / t / db_dependent / Circulation_Branch.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Biblio;
5 use C4::Members;
6 use C4::Branch;
7 use C4::Circulation;
8 use C4::Items;
9 use C4::Context;
10
11 use Test::More tests => 10;
12
13 BEGIN {
14     use_ok('C4::Circulation');
15 }
16
17 can_ok( 'C4::Circulation', qw(
18                             GetBranchBorrowerCircRule
19                             GetBranchItemRule
20                             )
21 );
22
23 #Start transaction
24 my $dbh = C4::Context->dbh;
25 $dbh->{RaiseError} = 1;
26 $dbh->{AutoCommit} = 0;
27
28 $dbh->do(q|DELETE FROM issues|);
29 $dbh->do(q|DELETE FROM items|);
30 $dbh->do(q|DELETE FROM borrowers|);
31 $dbh->do(q|DELETE FROM branches|);
32 $dbh->do(q|DELETE FROM categories|);
33 $dbh->do(q|DELETE FROM accountlines|);
34 $dbh->do(q|DELETE FROM itemtypes|);
35 $dbh->do(q|DELETE FROM branch_item_rules|);
36 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
37 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
38 $dbh->do(q|DELETE FROM default_circ_rules|);
39 $dbh->do(q|DELETE FROM default_branch_item_rules|);
40
41 #Add branch and category
42 my $samplebranch1 = {
43     add            => 1,
44     branchcode     => 'SAB1',
45     branchname     => 'Sample Branch',
46     branchaddress1 => 'sample adr1',
47     branchaddress2 => 'sample adr2',
48     branchaddress3 => 'sample adr3',
49     branchzip      => 'sample zip',
50     branchcity     => 'sample city',
51     branchstate    => 'sample state',
52     branchcountry  => 'sample country',
53     branchphone    => 'sample phone',
54     branchfax      => 'sample fax',
55     branchemail    => 'sample email',
56     branchurl      => 'sample url',
57     branchip       => 'sample ip',
58     branchprinter  => undef,
59     opac_info      => 'sample opac',
60 };
61 my $samplebranch2 = {
62     add            => 1,
63     branchcode     => 'SAB2',
64     branchname     => 'Sample Branch2',
65     branchaddress1 => 'sample adr1_2',
66     branchaddress2 => 'sample adr2_2',
67     branchaddress3 => 'sample adr3_2',
68     branchzip      => 'sample zip2',
69     branchcity     => 'sample city2',
70     branchstate    => 'sample state2',
71     branchcountry  => 'sample country2',
72     branchphone    => 'sample phone2',
73     branchfax      => 'sample fax2',
74     branchemail    => 'sample email2',
75     branchurl      => 'sample url2',
76     branchip       => 'sample ip2',
77     branchprinter  => undef,
78     opac_info      => 'sample opac2',
79 };
80 ModBranch($samplebranch1);
81 ModBranch($samplebranch2);
82
83 my $samplecat = {
84     categorycode          => 'CAT1',
85     description           => 'Description1',
86     enrolmentperiod       => 'Null',
87     enrolmentperioddate   => 'Null',
88     dateofbirthrequired   => 'Null',
89     finetype              => 'Null',
90     bulk                  => 'Null',
91     enrolmentfee          => 'Null',
92     overduenoticerequired => 'Null',
93     issuelimit            => 'Null',
94     reservefee            => 'Null',
95     hidelostitems         => 0,
96     category_type         => 'Null'
97 };
98 my $query =
99 "INSERT INTO categories (categorycode,
100                         description,
101                         enrolmentperiod,
102                         enrolmentperioddate,
103                         dateofbirthrequired ,
104                         finetype,
105                         bulk,
106                         enrolmentfee,
107                         overduenoticerequired,
108                         issuelimit,
109                         reservefee,
110                         hidelostitems,
111                         category_type
112                         )
113 VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
114 $dbh->do(
115     $query, {},
116     $samplecat->{categorycode},          $samplecat->{description},
117     $samplecat->{enrolmentperiod},       $samplecat->{enrolmentperioddate},
118     $samplecat->{dateofbirthrequired},   $samplecat->{finetype},
119     $samplecat->{bulk},                  $samplecat->{enrolmentfee},
120     $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
121     $samplecat->{reservefee},            $samplecat->{hidelostitems},
122     $samplecat->{category_type}
123 );
124
125 #Add itemtypes
126 my $sampleitemtype1 = {
127     itemtype     => 'BOOK',
128     description  => 'BookDescription',
129     rentalcharge => '10.0',
130     notforloan   => 1,
131     imageurl     => 'Null',
132     summary      => 'BookSummary'
133 };
134 my $sampleitemtype2 = {
135     itemtype     => 'DVD',
136     description  => 'DvdDescription',
137     rentalcharge => '5.0',
138     notforloan   => 0,
139     imageurl     => 'Null',
140     summary      => 'DvdSummary'
141 };
142 $query =
143 "INSERT INTO itemtypes (itemtype,
144                     description,
145                     rentalcharge,
146                     notforloan,
147                     imageurl,
148                     summary
149                     )
150  VALUES( ?,?,?,?,?,?)";
151 my $sth = $dbh->prepare($query);
152 $sth->execute(
153     $sampleitemtype1->{itemtype},     $sampleitemtype1->{description},
154     $sampleitemtype1->{rentalcharge}, $sampleitemtype1->{notforloan},
155     $sampleitemtype1->{imageurl},     $sampleitemtype1->{summary}
156 );
157 $sth->execute(
158     $sampleitemtype2->{itemtype},     $sampleitemtype2->{description},
159     $sampleitemtype2->{rentalcharge}, $sampleitemtype2->{notforloan},
160     $sampleitemtype2->{imageurl},     $sampleitemtype2->{summary}
161 );
162
163 $query =
164 "INSERT INTO branch_borrower_circ_rules (branchcode,categorycode,maxissueqty) VALUES( ?,?,?)";
165 $dbh->do(
166     $query, {},
167     $samplebranch1->{branchcode},
168     $samplecat->{categorycode}, 5
169 );
170 $query =
171 "INSERT INTO default_branch_circ_rules (branchcode,maxissueqty,holdallowed,returnbranch) VALUES( ?,?,?,?)";
172 $dbh->do( $query, {}, $samplebranch2->{branchcode},
173     3, 1, $samplebranch2->{branchcode} );
174 $query =
175 "INSERT INTO default_circ_rules (singleton,maxissueqty,holdallowed,returnbranch) VALUES( ?,?,?,?)";
176 $dbh->do( $query, {}, 'singleton', 4, 3, $samplebranch1->{branchcode} );
177
178 $query =
179 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
180 $sth = $dbh->prepare($query);
181 $sth->execute(
182     $samplebranch1->{branchcode},
183     $sampleitemtype1->{itemtype},
184     5, $samplebranch1->{branchcode}
185 );
186 $sth->execute(
187     $samplebranch2->{branchcode},
188     $sampleitemtype2->{itemtype},
189     5, $samplebranch1->{branchcode}
190 );
191
192 #Test GetBranchBorrowerCircRule
193 is_deeply(
194     GetBranchBorrowerCircRule(),
195     { maxissueqty => 4 },
196 "Without parameter, GetBranchBorrower returns the maxissueqty of default_circ_rules"
197 );
198 is_deeply(
199     GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
200     { maxissueqty => 3 },
201 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty corresponding"
202 );
203 is_deeply(
204     GetBranchBorrowerCircRule(
205         $samplebranch1->{branchcode},
206         $samplecat->{categorycode}
207     ),
208     { maxissueqty => 5 },
209     "GetBranchBorrower returns the maxissueqty of the branch1 and the category1"
210 );
211 is_deeply(
212     GetBranchBorrowerCircRule( -1, -1 ),
213     { maxissueqty => 4 },
214 "GetBranchBorrower  with wrong parameters returns tthe maxissueqty of default_circ_rules"
215 );
216
217 #Test GetBranchItemRule
218 is_deeply(
219     GetBranchItemRule(
220         $samplebranch1->{branchcode},
221         $sampleitemtype1->{itemtype}
222     ),
223     { returnbranch => $samplebranch1->{branchcode}, holdallowed => 5 },
224     "GetBranchitem returns holdallowed and return branch"
225 );
226 is_deeply(
227     GetBranchItemRule(),
228     { returnbranch => $samplebranch1->{branchcode}, holdallowed => 3 },
229 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
230 );
231 is_deeply(
232     GetBranchItemRule( $samplebranch1->{branchcode} ),
233     { returnbranch => $samplebranch1->{branchcode}, holdallowed => 3 },
234 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
235 );
236 is_deeply(
237     GetBranchItemRule( -1, -1 ),
238     { returnbranch => $samplebranch1->{branchcode}, holdallowed => 3 },
239     "With only one parametern GetBranchItemRule returns default values"
240 );
241
242 $dbh->rollback;