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();
54 my $branch = $builder->build({
58 my $category = $builder->build({
62 my $patron = $builder->build({
65 categorycode => $category->{categorycode},
66 branchcode => $branch->{branchcode},
70 my $biblio = $builder->build({
73 branchcode => $branch->{branchcode},
76 my $item = $builder->build({
79 biblionumber => $biblio->{biblionumber},
80 homebranch => $branch->{branchcode},
81 holdingbranch => $branch->{branchcode},
85 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
86 C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
88 # TooMany return ($current_loan_count, $max_loans_allowed) or undef
90 # OSCO: On-site checkout
92 subtest 'no rules exist' => sub {
95 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
97 'CO should be allowed, in any cases'
100 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
102 'OSCO should be allowed, in any cases'
106 subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
108 my $issuingrule = $builder->build({
109 source => 'Issuingrule',
111 branchcode => $branch->{branchcode},
112 categorycode => $category->{categorycode},
115 maxonsiteissueqty => 0,
118 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
120 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
122 reason => 'TOO_MANY_CHECKOUTS',
126 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
129 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
131 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
135 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
138 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
140 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
142 reason => 'TOO_MANY_CHECKOUTS',
146 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
149 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
151 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
155 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
161 subtest '1 Issuingrule exist 1 1: issue is allowed' => sub {
163 my $issuingrule = $builder->build({
164 source => 'Issuingrule',
166 branchcode => $branch->{branchcode},
167 categorycode => $category->{categorycode},
170 maxonsiteissueqty => 1,
173 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
175 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
177 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
180 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
182 'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
185 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
187 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
189 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
192 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
194 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
200 subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
202 my $issuingrule = $builder->build({
203 source => 'Issuingrule',
205 branchcode => $branch->{branchcode},
206 categorycode => $category->{categorycode},
209 maxonsiteissueqty => 1,
213 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string() );
214 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
216 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
218 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
220 reason => 'TOO_MANY_CHECKOUTS',
224 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
227 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
229 'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
232 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
234 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
236 reason => 'TOO_MANY_CHECKOUTS',
240 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
243 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
245 reason => 'TOO_MANY_CHECKOUTS',
249 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
255 subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed, Do a OSCO' => sub {
257 my $issuingrule = $builder->build({
258 source => 'Issuingrule',
260 branchcode => $branch->{branchcode},
261 categorycode => $category->{categorycode},
264 maxonsiteissueqty => 1,
268 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
269 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
271 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
273 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
275 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
278 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
280 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
284 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
287 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
289 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
291 reason => 'TOO_MANY_CHECKOUTS',
295 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
298 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
300 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
304 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
310 subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
311 # Note: the same test coul be done for
312 # DefaultBorrowerCircRule, DefaultBranchCircRule, DefaultBranchItemRule ans DefaultCircRule.pm
315 my $issuingrule = $builder->build({
316 source => 'BranchBorrowerCircRule',
318 branchcode => $branch->{branchcode},
319 categorycode => $category->{categorycode},
321 maxonsiteissueqty => 1,
325 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef );
326 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
328 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
330 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
332 reason => 'TOO_MANY_CHECKOUTS',
336 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
339 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
341 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
344 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
346 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
348 reason => 'TOO_MANY_CHECKOUTS',
352 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
355 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
357 reason => 'TOO_MANY_CHECKOUTS',
361 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
366 $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
367 like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
369 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
371 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
373 'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
376 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
378 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
382 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
385 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
387 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
389 reason => 'TOO_MANY_CHECKOUTS',
393 'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
396 C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
398 reason => 'TOO_MANY_ONSITE_CHECKOUTS',
402 'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
408 $schema->storage->txn_rollback;
411 $dbh->do(q|DELETE FROM issues|);
412 $dbh->do(q|DELETE FROM issuingrules|);