Bug 14167: Add a unit test for Koha::Logger
[koha.git] / t / db_dependent / Circulation_Branch.t
blob0f30f2edfe57232898c88cc7c66b47dbf09c74d3
1 #!/usr/bin/perl
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 use Data::Dumper;
11 use Test::More tests => 13;
13 BEGIN {
14 use_ok('C4::Circulation');
17 can_ok( 'C4::Circulation', qw(
18 AddIssue
19 AddReturn
20 GetBranchBorrowerCircRule
21 GetBranchItemRule
22 GetIssuingRule
26 #Start transaction
27 my $dbh = C4::Context->dbh;
28 $dbh->{RaiseError} = 1;
29 $dbh->{AutoCommit} = 0;
31 $dbh->do(q|DELETE FROM issues|);
32 $dbh->do(q|DELETE FROM items|);
33 $dbh->do(q|DELETE FROM borrowers|);
34 $dbh->do(q|DELETE FROM branches|);
35 $dbh->do(q|DELETE FROM categories|);
36 $dbh->do(q|DELETE FROM accountlines|);
37 $dbh->do(q|DELETE FROM itemtypes|);
38 $dbh->do(q|DELETE FROM branch_item_rules|);
39 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
40 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
41 $dbh->do(q|DELETE FROM default_circ_rules|);
42 $dbh->do(q|DELETE FROM default_branch_item_rules|);
44 #Add branch and category
45 my $samplebranch1 = {
46 add => 1,
47 branchcode => 'SAB1',
48 branchname => 'Sample Branch',
49 branchaddress1 => 'sample adr1',
50 branchaddress2 => 'sample adr2',
51 branchaddress3 => 'sample adr3',
52 branchzip => 'sample zip',
53 branchcity => 'sample city',
54 branchstate => 'sample state',
55 branchcountry => 'sample country',
56 branchphone => 'sample phone',
57 branchfax => 'sample fax',
58 branchemail => 'sample email',
59 branchurl => 'sample url',
60 branchip => 'sample ip',
61 branchprinter => undef,
62 opac_info => 'sample opac',
64 my $samplebranch2 = {
65 add => 1,
66 branchcode => 'SAB2',
67 branchname => 'Sample Branch2',
68 branchaddress1 => 'sample adr1_2',
69 branchaddress2 => 'sample adr2_2',
70 branchaddress3 => 'sample adr3_2',
71 branchzip => 'sample zip2',
72 branchcity => 'sample city2',
73 branchstate => 'sample state2',
74 branchcountry => 'sample country2',
75 branchphone => 'sample phone2',
76 branchfax => 'sample fax2',
77 branchemail => 'sample email2',
78 branchurl => 'sample url2',
79 branchip => 'sample ip2',
80 branchprinter => undef,
81 opac_info => 'sample opac2',
83 ModBranch($samplebranch1);
84 ModBranch($samplebranch2);
86 my $samplecat = {
87 categorycode => 'CAT1',
88 description => 'Description1',
89 enrolmentperiod => 'Null',
90 enrolmentperioddate => 'Null',
91 dateofbirthrequired => 'Null',
92 finetype => 'Null',
93 bulk => 'Null',
94 enrolmentfee => 'Null',
95 overduenoticerequired => 'Null',
96 issuelimit => 'Null',
97 reservefee => 'Null',
98 hidelostitems => 0,
99 category_type => 'Null'
101 my $query =
102 "INSERT INTO categories (categorycode,
103 description,
104 enrolmentperiod,
105 enrolmentperioddate,
106 dateofbirthrequired ,
107 finetype,
108 bulk,
109 enrolmentfee,
110 overduenoticerequired,
111 issuelimit,
112 reservefee,
113 hidelostitems,
114 category_type
116 VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
117 $dbh->do(
118 $query, {},
119 $samplecat->{categorycode}, $samplecat->{description},
120 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
121 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
122 $samplecat->{bulk}, $samplecat->{enrolmentfee},
123 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
124 $samplecat->{reservefee}, $samplecat->{hidelostitems},
125 $samplecat->{category_type}
128 #Add itemtypes
129 my $sampleitemtype1 = {
130 itemtype => 'BOOK',
131 description => 'BookDescription',
132 rentalcharge => '10.0',
133 notforloan => 1,
134 imageurl => 'Null',
135 summary => 'BookSummary'
137 my $sampleitemtype2 = {
138 itemtype => 'DVD',
139 description => 'DvdDescription',
140 rentalcharge => '5.0',
141 notforloan => 0,
142 imageurl => 'Null',
143 summary => 'DvdSummary'
145 $query =
146 "INSERT INTO itemtypes (itemtype,
147 description,
148 rentalcharge,
149 notforloan,
150 imageurl,
151 summary
153 VALUES( ?,?,?,?,?,?)";
154 my $sth = $dbh->prepare($query);
155 $sth->execute(
156 $sampleitemtype1->{itemtype}, $sampleitemtype1->{description},
157 $sampleitemtype1->{rentalcharge}, $sampleitemtype1->{notforloan},
158 $sampleitemtype1->{imageurl}, $sampleitemtype1->{summary}
160 $sth->execute(
161 $sampleitemtype2->{itemtype}, $sampleitemtype2->{description},
162 $sampleitemtype2->{rentalcharge}, $sampleitemtype2->{notforloan},
163 $sampleitemtype2->{imageurl}, $sampleitemtype2->{summary}
166 #Add biblio and item
167 my $record = MARC::Record->new();
168 $record->append_fields(
169 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
170 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
172 # item 2 has home branch and holding branch samplebranch1
173 my @sampleitem1 = C4::Items::AddItem(
175 barcode => 'barcode_1',
176 itemcallnumber => 'callnumber1',
177 homebranch => $samplebranch1->{branchcode},
178 holdingbranch => $samplebranch1->{branchcode}
180 $biblionumber
182 my $item_id1 = $sampleitem1[2];
184 # item 2 has holding branch samplebranch2
185 my @sampleitem2 = C4::Items::AddItem(
187 barcode => 'barcode_2',
188 itemcallnumber => 'callnumber2',
189 homebranch => $samplebranch2->{branchcode},
190 holdingbranch => $samplebranch1->{branchcode}
192 $biblionumber
194 my $item_id2 = $sampleitem2[2];
196 # item 3 has item type sampleitemtype2 with noreturn policy
197 my @sampleitem3 = C4::Items::AddItem(
199 barcode => 'barcode_3',
200 itemcallnumber => 'callnumber3',
201 homebranch => $samplebranch2->{branchcode},
202 holdingbranch => $samplebranch2->{branchcode},
203 itype => $sampleitemtype2->{itemtype}
205 $biblionumber
207 my $item_id3 = $sampleitem3[2];
209 #Add borrower
210 my $borrower_id1 = C4::Members::AddMember(
211 firstname => 'firstname1',
212 surname => 'surname1 ',
213 categorycode => $samplecat->{categorycode},
214 branchcode => $samplebranch1->{branchcode},
216 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
218 $query =
219 "INSERT INTO branch_borrower_circ_rules (branchcode,categorycode,maxissueqty) VALUES( ?,?,?)";
220 $dbh->do(
221 $query, {},
222 $samplebranch1->{branchcode},
223 $samplecat->{categorycode}, 5
226 $query =
227 "INSERT INTO default_circ_rules (singleton,maxissueqty,holdallowed,returnbranch) VALUES( ?,?,?,?)";
228 $dbh->do( $query, {}, 'singleton', 4, 3, 'homebranch' );
230 $query =
231 "INSERT INTO default_branch_circ_rules (branchcode,maxissueqty,holdallowed,returnbranch) VALUES( ?,?,?,?)";
232 $dbh->do( $query, {}, $samplebranch2->{branchcode}, 3, 1, 'holdingbranch' );
234 $query =
235 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
236 $sth = $dbh->prepare($query);
237 $sth->execute(
238 $samplebranch1->{branchcode},
239 $sampleitemtype1->{itemtype},
240 5, 'homebranch'
242 $sth->execute(
243 $samplebranch2->{branchcode},
244 $sampleitemtype1->{itemtype},
245 5, 'holdingbranch'
247 $sth->execute(
248 $samplebranch2->{branchcode},
249 $sampleitemtype2->{itemtype},
250 5, 'noreturn'
253 #Test GetBranchBorrowerCircRule
254 is_deeply(
255 GetBranchBorrowerCircRule(),
256 { maxissueqty => 4 },
257 "Without parameter, GetBranchBorrower returns the maxissueqty of default_circ_rules"
259 is_deeply(
260 GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
261 { maxissueqty => 3 },
262 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty corresponding"
264 is_deeply(
265 GetBranchBorrowerCircRule(
266 $samplebranch1->{branchcode},
267 $samplecat->{categorycode}
269 { maxissueqty => 5 },
270 "GetBranchBorrower returns the maxissueqty of the branch1 and the category1"
272 is_deeply(
273 GetBranchBorrowerCircRule( -1, -1 ),
274 { maxissueqty => 4 },
275 "GetBranchBorrower with wrong parameters returns tthe maxissueqty of default_circ_rules"
278 #Test GetBranchItemRule
279 is_deeply(
280 GetBranchItemRule(
281 $samplebranch1->{branchcode},
282 $sampleitemtype1->{itemtype}
284 { returnbranch => 'homebranch', holdallowed => 5 },
285 "GetBranchitem returns holdallowed and return branch"
287 is_deeply(
288 GetBranchItemRule(),
289 { returnbranch => 'homebranch', holdallowed => 3 },
290 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
292 is_deeply(
293 GetBranchItemRule( $samplebranch2->{branchcode} ),
294 { returnbranch => 'holdingbranch', holdallowed => 1 },
295 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
297 is_deeply(
298 GetBranchItemRule( -1, -1 ),
299 { returnbranch => 'homebranch', holdallowed => 3 },
300 "With only one parametern GetBranchItemRule returns default values"
303 # Test return policies
304 C4::Context->set_preference('AutomaticItemReturn','0');
306 # item1 returned at branch2 should trigger transfer to homebranch
307 $query =
308 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
309 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
311 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
312 $samplebranch2->{branchcode});
313 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
315 # item2 returned at branch2 should trigger transfer to holding branch
316 $query =
317 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
318 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
319 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
320 $samplebranch2->{branchcode});
321 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
323 # item3 should not trigger transfer - floating collection
324 $query =
325 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
326 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
327 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
328 $samplebranch1->{branchcode});
329 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");
332 $dbh->rollback;