3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
27 use Test
::More tests
=> 14;
29 use t
::lib
::TestBuilder
;
32 use_ok
('C4::Circulation');
35 can_ok
( 'C4::Circulation', qw(
38 GetBranchBorrowerCircRule
44 my $schema = Koha
::Database
->schema;
45 $schema->storage->txn_begin;
46 my $dbh = C4
::Context
->dbh;
48 $dbh->do(q
|DELETE FROM issues
|);
49 $dbh->do(q
|DELETE FROM items
|);
50 $dbh->do(q
|DELETE FROM borrowers
|);
51 $dbh->do(q
|DELETE FROM branches
|);
52 $dbh->do(q
|DELETE FROM categories
|);
53 $dbh->do(q
|DELETE FROM accountlines
|);
54 $dbh->do(q
|DELETE FROM itemtypes
|);
55 $dbh->do(q
|DELETE FROM branch_item_rules
|);
56 $dbh->do(q
|DELETE FROM branch_borrower_circ_rules
|);
57 $dbh->do(q
|DELETE FROM default_branch_circ_rules
|);
58 $dbh->do(q
|DELETE FROM default_circ_rules
|);
59 $dbh->do(q
|DELETE FROM default_branch_item_rules
|);
61 my $builder = t
::lib
::TestBuilder
->new();
64 my $samplebranch1 = $builder->build({ source
=> 'Branch' });
65 my $samplebranch2 = $builder->build({ source
=> 'Branch' });
67 my $no_circ_itemtype = $builder->build({
74 my $sampleitemtype1 = $builder->build({
77 rentalcharge
=> '10.0',
81 my $sampleitemtype2 = $builder->build({
84 rentalcharge
=> '5.0',
89 my $samplecat = $builder->build({
97 my $record = MARC
::Record
->new();
98 $record->append_fields(
99 MARC
::Field
->new( '952', '0', '0', a
=> $samplebranch1->{branchcode
} ) );
100 my ( $biblionumber, $biblioitemnumber ) = C4
::Biblio
::AddBiblio
( $record, '' );
102 # item 1 has home branch and holding branch samplebranch1
103 my @sampleitem1 = C4
::Items
::AddItem
(
105 barcode
=> 'barcode_1',
106 itemcallnumber
=> 'callnumber1',
107 homebranch
=> $samplebranch1->{branchcode
},
108 holdingbranch
=> $samplebranch1->{branchcode
},
109 itype
=> $no_circ_itemtype->{ itemtype
}
113 my $item_id1 = $sampleitem1[2];
115 # item 2 has holding branch samplebranch2
116 my @sampleitem2 = C4
::Items
::AddItem
(
118 barcode
=> 'barcode_2',
119 itemcallnumber
=> 'callnumber2',
120 homebranch
=> $samplebranch2->{branchcode
},
121 holdingbranch
=> $samplebranch1->{branchcode
},
122 itype
=> $no_circ_itemtype->{ itemtype
}
126 my $item_id2 = $sampleitem2[2];
128 # item 3 has item type sampleitemtype2 with noreturn policy
129 my @sampleitem3 = C4
::Items
::AddItem
(
131 barcode
=> 'barcode_3',
132 itemcallnumber
=> 'callnumber3',
133 homebranch
=> $samplebranch2->{branchcode
},
134 holdingbranch
=> $samplebranch2->{branchcode
},
135 itype
=> $sampleitemtype2->{itemtype
}
139 my $item_id3 = $sampleitem3[2];
142 my $borrower_id1 = C4
::Members
::AddMember
(
143 firstname
=> 'firstname1',
144 surname
=> 'surname1 ',
145 categorycode
=> $samplecat->{categorycode
},
146 branchcode
=> $samplebranch1->{branchcode
},
148 my $borrower_1 = C4
::Members
::GetMember
(borrowernumber
=> $borrower_id1);
151 GetBranchBorrowerCircRule
(),
152 { maxissueqty
=> undef, maxonsiteissueqty
=> undef },
153 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
157 INSERT INTO branch_borrower_circ_rules
158 (branchcode
, categorycode
, maxissueqty
, maxonsiteissueqty
)
164 $samplebranch1->{branchcode
},
165 $samplecat->{categorycode
}, 5, 6
169 INSERT INTO default_branch_circ_rules
170 (branchcode
, maxissueqty
, maxonsiteissueqty
, holdallowed
, returnbranch
)
171 VALUES
( ?
, ?
, ?
, ?
, ?
)
173 $dbh->do( $query, {}, $samplebranch2->{branchcode
},
174 3, 2, 1, 'holdingbranch' );
176 INSERT INTO default_circ_rules
177 (singleton
, maxissueqty
, maxonsiteissueqty
, holdallowed
, returnbranch
)
178 VALUES
( ?
, ?
, ?
, ?
, ?
)
180 $dbh->do( $query, {}, 'singleton', 4, 5, 3, 'homebranch' );
183 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
184 my $sth = $dbh->prepare($query);
186 $samplebranch1->{branchcode
},
187 $sampleitemtype1->{itemtype
},
191 $samplebranch2->{branchcode
},
192 $sampleitemtype1->{itemtype
},
196 $samplebranch2->{branchcode
},
197 $sampleitemtype2->{itemtype
},
201 #Test GetBranchBorrowerCircRule
203 GetBranchBorrowerCircRule
(),
204 { maxissueqty
=> 4, maxonsiteissueqty
=> 5 },
205 "Without parameter, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
208 GetBranchBorrowerCircRule
( $samplebranch2->{branchcode
} ),
209 { maxissueqty
=> 3, maxonsiteissueqty
=> 2 },
210 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty corresponding"
213 GetBranchBorrowerCircRule
(
214 $samplebranch1->{branchcode
},
215 $samplecat->{categorycode
}
217 { maxissueqty
=> 5, maxonsiteissueqty
=> 6 },
218 "GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of the branch1 and the category1"
221 GetBranchBorrowerCircRule
( -1, -1 ),
222 { maxissueqty
=> 4, maxonsiteissueqty
=> 5 },
223 "GetBranchBorrower with wrong parameters returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
226 #Test GetBranchItemRule
229 $samplebranch1->{branchcode
},
230 $sampleitemtype1->{itemtype
}
232 { returnbranch
=> 'homebranch', holdallowed
=> 5 },
233 "GetBranchitem returns holdallowed and return branch"
237 { returnbranch
=> 'homebranch', holdallowed
=> 3 },
238 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
241 GetBranchItemRule
( $samplebranch2->{branchcode
} ),
242 { returnbranch
=> 'holdingbranch', holdallowed
=> 1 },
243 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
246 GetBranchItemRule
( -1, -1 ),
247 { returnbranch
=> 'homebranch', holdallowed
=> 3 },
248 "With only one parametern GetBranchItemRule returns default values"
251 # Test return policies
252 t
::lib
::Mocks
::mock_preference
('AutomaticItemReturn','0');
254 # item1 returned at branch2 should trigger transfer to homebranch
256 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
257 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode
} );
259 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn
('barcode_1',
260 $samplebranch2->{branchcode
});
261 is
( $messages->{NeedsTransfer
}, $samplebranch1->{branchcode
}, "AddReturn respects default return policy - return to homebranch" );
263 # item2 returned at branch2 should trigger transfer to holding branch
265 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
266 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode
} );
267 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn
('barcode_2',
268 $samplebranch2->{branchcode
});
269 is
( $messages->{NeedsTransfer
}, $samplebranch1->{branchcode
}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
271 # item3 should not trigger transfer - floating collection
273 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
274 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode
} );
275 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn
('barcode_3',
276 $samplebranch1->{branchcode
});
277 is
($messages->{NeedsTransfer
},undef,"AddReturn respects branch item return policy - noreturn");
279 $schema->storage->txn_rollback;