3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Test::More tests => 6;
27 use Koha::DateUtils qw( dt_from_string );
30 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 our $dbh = C4::Context->dbh;
38 $dbh->do(q|DELETE FROM issues|);
39 $dbh->do(q|DELETE FROM items|);
40 $dbh->do(q|DELETE FROM borrowers|);
41 $dbh->do(q|DELETE FROM branches|);
42 $dbh->do(q|DELETE FROM categories|);
43 $dbh->do(q|DELETE FROM accountlines|);
44 $dbh->do(q|DELETE FROM itemtypes|);
45 $dbh->do(q|DELETE FROM branch_item_rules|);
46 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
47 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
48 $dbh->do(q|DELETE FROM default_circ_rules|);
49 $dbh->do(q|DELETE FROM default_branch_item_rules|);
50 $dbh->do(q|DELETE FROM issuingrules|);
52 my $builder = t::lib::TestBuilder->new();
53 t::lib::Mocks::mock_preference('item-level_itypes', 1); # Assuming the item type is defined at item level
55 my $branch = $builder->build({
59 my $category = $builder->build({
63 my $patron = $builder->build({
66 categorycode => $category->{categorycode},
67 branchcode => $branch->{branchcode},
71 my $biblio = $builder->build({
74 branchcode => $branch->{branchcode},
77 my $item = $builder->build({
80 biblionumber => $biblio->{biblionumber},
81 homebranch => $branch->{branchcode},
82 holdingbranch => $branch->{branchcode},
86 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
87 C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
89 # TooMany return ($current_loan_count, $max_loans_allowed) or undef
91 # OSCO: On-site checkout
93 subtest 'no rules exist' => sub {
96 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
97 { reason => 'NO_RULE_DEFINED', max_allowed => 0 },
98 'CO should not be allowed, in any cases'
101 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
102 { reason => 'NO_RULE_DEFINED', max_allowed => 0 },
103 'OSCO should not be allowed, in any cases'
107 subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
109 my $issuingrule = $builder->build({
110 source => 'Issuingrule',
112 branchcode => $branch->{branchcode},
113 categorycode => $category->{categorycode},
116 maxonsiteissueqty => 0,
119 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
121 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
123 reason => 'TOO_MANY_CHECKOUTS',
127 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
130 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
132 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
136 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
139 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
141 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
143 reason => 'TOO_MANY_CHECKOUTS',
147 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
150 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
152 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
156 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
162 subtest '1 Issuingrule exist 1 1: issue is allowed' => sub {
164 my $issuingrule = $builder->build({
165 source => 'Issuingrule',
167 branchcode => $branch->{branchcode},
168 categorycode => $category->{categorycode},
171 maxonsiteissueqty => 1,
174 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
176 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
178 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
181 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
183 'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
186 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
188 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
190 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
193 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
195 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
201 subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
203 my $issuingrule = $builder->build({
204 source => 'Issuingrule',
206 branchcode => $branch->{branchcode},
207 categorycode => $category->{categorycode},
210 maxonsiteissueqty => 1,
214 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string() );
215 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
217 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
219 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
221 reason => 'TOO_MANY_CHECKOUTS',
225 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
228 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
230 'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
233 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
235 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
237 reason => 'TOO_MANY_CHECKOUTS',
241 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
244 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
246 reason => 'TOO_MANY_CHECKOUTS',
250 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
256 subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed, Do a OSCO' => sub {
258 my $issuingrule = $builder->build({
259 source => 'Issuingrule',
261 branchcode => $branch->{branchcode},
262 categorycode => $category->{categorycode},
265 maxonsiteissueqty => 1,
269 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
270 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
272 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
274 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
276 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
279 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
281 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
285 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
288 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
290 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
292 reason => 'TOO_MANY_CHECKOUTS',
296 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
299 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
301 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
305 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
311 subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
312 # Note: the same test coul be done for
313 # DefaultBorrowerCircRule, DefaultBranchCircRule, DefaultBranchItemRule ans DefaultCircRule.pm
316 my $issuingrule = $builder->build({
317 source => 'BranchBorrowerCircRule',
319 branchcode => $branch->{branchcode},
320 categorycode => $category->{categorycode},
322 maxonsiteissueqty => 1,
326 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef );
327 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
329 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
331 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
333 reason => 'TOO_MANY_CHECKOUTS',
337 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
340 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
342 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
345 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
347 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
349 reason => 'TOO_MANY_CHECKOUTS',
353 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
356 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
358 reason => 'TOO_MANY_CHECKOUTS',
362 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
367 $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
368 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
370 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
372 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
374 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
377 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
379 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
383 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
386 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
388 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
390 reason => 'TOO_MANY_CHECKOUTS',
394 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
397 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
399 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
403 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
409 $schema->storage->txn_rollback;
412 $dbh->do(q|DELETE FROM issues|);
413 $dbh->do(q|DELETE FROM issuingrules|);