Bug 15517: Change wording for tests
[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 => 14;
12
13 BEGIN {
14     use_ok('C4::Circulation');
15 }
16
17 can_ok( 'C4::Circulation', qw(
18     AddIssue
19     AddReturn
20     GetBranchBorrowerCircRule
21     GetBranchItemRule
22     GetIssuingRule
23     )
24 );
25
26 #Start transaction
27 my $dbh = C4::Context->dbh;
28 $dbh->{RaiseError} = 1;
29 $dbh->{AutoCommit} = 0;
30
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|);
43
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',
63 };
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',
82 };
83 ModBranch($samplebranch1);
84 ModBranch($samplebranch2);
85
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'
100 };
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
115                         )
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}
126 );
127
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'
136 };
137 my $sampleitemtype2 = {
138     itemtype     => 'DVD',
139     description  => 'DvdDescription',
140     rentalcharge => '5.0',
141     notforloan   => 0,
142     imageurl     => 'Null',
143     summary      => 'DvdSummary'
144 };
145 $query =
146 "INSERT INTO itemtypes (itemtype,
147                     description,
148                     rentalcharge,
149                     notforloan,
150                     imageurl,
151                     summary
152                     )
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}
159 );
160 $sth->execute(
161     $sampleitemtype2->{itemtype},     $sampleitemtype2->{description},
162     $sampleitemtype2->{rentalcharge}, $sampleitemtype2->{notforloan},
163     $sampleitemtype2->{imageurl},     $sampleitemtype2->{summary}
164 );
165
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, '' );
171
172 # item 2 has home branch and holding branch samplebranch1
173 my @sampleitem1 = C4::Items::AddItem(
174     {
175         barcode        => 'barcode_1',
176         itemcallnumber => 'callnumber1',
177         homebranch     => $samplebranch1->{branchcode},
178         holdingbranch  => $samplebranch1->{branchcode}
179     },
180     $biblionumber
181 );
182 my $item_id1    = $sampleitem1[2];
183
184 # item 2 has holding branch samplebranch2
185 my @sampleitem2 = C4::Items::AddItem(
186     {
187         barcode        => 'barcode_2',
188         itemcallnumber => 'callnumber2',
189         homebranch     => $samplebranch2->{branchcode},
190         holdingbranch  => $samplebranch1->{branchcode}
191     },
192     $biblionumber
193 );
194 my $item_id2 = $sampleitem2[2];
195
196 # item 3 has item type sampleitemtype2 with noreturn policy
197 my @sampleitem3 = C4::Items::AddItem(
198     {
199         barcode        => 'barcode_3',
200         itemcallnumber => 'callnumber3',
201         homebranch     => $samplebranch2->{branchcode},
202         holdingbranch  => $samplebranch2->{branchcode},
203         itype          => $sampleitemtype2->{itemtype}
204     },
205     $biblionumber
206 );
207 my $item_id3 = $sampleitem3[2];
208
209 #Add borrower
210 my $borrower_id1 = C4::Members::AddMember(
211     firstname    => 'firstname1',
212     surname      => 'surname1 ',
213     categorycode => $samplecat->{categorycode},
214     branchcode   => $samplebranch1->{branchcode},
215 );
216 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
217
218 is_deeply(
219     GetBranchBorrowerCircRule(),
220     { maxissueqty => undef, maxonsiteissueqty => undef },
221 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
222 );
223
224 $query = q|
225     INSERT INTO branch_borrower_circ_rules
226     (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
227     VALUES( ?, ?, ?, ? )
228 |;
229
230 $dbh->do(
231     $query, {},
232     $samplebranch1->{branchcode},
233     $samplecat->{categorycode}, 5, 6
234 );
235
236 $query = q|
237     INSERT INTO default_branch_circ_rules
238     (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
239     VALUES( ?, ?, ?, ?, ? )
240 |;
241 $dbh->do( $query, {}, $samplebranch2->{branchcode},
242     3, 2, 1, 'holdingbranch' );
243 $query = q|
244     INSERT INTO default_circ_rules
245     (singleton, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
246     VALUES( ?, ?, ?, ?, ? )
247 |;
248 $dbh->do( $query, {}, 'singleton', 4, 5, 3, 'homebranch' );
249
250 $query =
251 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
252 $sth = $dbh->prepare($query);
253 $sth->execute(
254     $samplebranch1->{branchcode},
255     $sampleitemtype1->{itemtype},
256     5, 'homebranch'
257 );
258 $sth->execute(
259     $samplebranch2->{branchcode},
260     $sampleitemtype1->{itemtype},
261     5, 'holdingbranch'
262 );
263 $sth->execute(
264     $samplebranch2->{branchcode},
265     $sampleitemtype2->{itemtype},
266     5, 'noreturn'
267 );
268
269 #Test GetBranchBorrowerCircRule
270 is_deeply(
271     GetBranchBorrowerCircRule(),
272     { maxissueqty => 4, maxonsiteissueqty => 5 },
273 "Without parameter, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
274 );
275 is_deeply(
276     GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
277     { maxissueqty => 3, maxonsiteissueqty => 2 },
278 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty corresponding"
279 );
280 is_deeply(
281     GetBranchBorrowerCircRule(
282         $samplebranch1->{branchcode},
283         $samplecat->{categorycode}
284     ),
285     { maxissueqty => 5, maxonsiteissueqty => 6 },
286     "GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of the branch1 and the category1"
287 );
288 is_deeply(
289     GetBranchBorrowerCircRule( -1, -1 ),
290     { maxissueqty => 4, maxonsiteissueqty => 5 },
291 "GetBranchBorrower with wrong parameters returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
292 );
293
294 #Test GetBranchItemRule
295 is_deeply(
296     GetBranchItemRule(
297         $samplebranch1->{branchcode},
298         $sampleitemtype1->{itemtype}
299     ),
300     { returnbranch => 'homebranch', holdallowed => 5 },
301     "GetBranchitem returns holdallowed and return branch"
302 );
303 is_deeply(
304     GetBranchItemRule(),
305     { returnbranch => 'homebranch', holdallowed => 3 },
306 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
307 );
308 is_deeply(
309     GetBranchItemRule( $samplebranch2->{branchcode} ),
310     { returnbranch => 'holdingbranch', holdallowed => 1 },
311 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
312 );
313 is_deeply(
314     GetBranchItemRule( -1, -1 ),
315     { returnbranch => 'homebranch', holdallowed => 3 },
316     "With only one parametern GetBranchItemRule returns default values"
317 );
318
319 # Test return policies
320 C4::Context->set_preference('AutomaticItemReturn','0');
321
322 # item1 returned at branch2 should trigger transfer to homebranch
323 $query =
324 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
325 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
326
327 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
328     $samplebranch2->{branchcode});
329 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
330
331 # item2 returned at branch2 should trigger transfer to holding branch
332 $query =
333 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
334 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
335 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
336     $samplebranch2->{branchcode});
337 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
338
339 # item3 should not trigger transfer - floating collection
340 $query =
341 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
342 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
343 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
344     $samplebranch1->{branchcode});
345 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");
346
347
348 $dbh->rollback;