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 t::lib::TestBuilder;
29 use Test::More tests => 14;
31 my $dbh = C4::Context->dbh;
32 my $schema = Koha::Database->new()->schema();
33 my $builder = t::lib::TestBuilder->new;
36 $dbh->{RaiseError} = 1;
37 $schema->storage->txn_begin();
39 $dbh->do('DELETE FROM issues');
40 $dbh->do('DELETE FROM issuingrules');
41 $dbh->do('DELETE FROM borrowers');
42 $dbh->do('DELETE FROM items');
44 my $library = $builder->build( { source => 'Branch' } );
45 my $category = $builder->build( { source => 'Category' } );
46 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
49 C4::Context->_new_userenv('xxx');
50 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
51 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
54 for my $i ( 1 .. 20 ) {
55 my $patron = Koha::Patron->new(
56 { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode} } )
58 push( @patrons, $patron );
61 my $biblio = Koha::Biblio->new()->store();
63 $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
66 for my $i ( 1 .. 10 ) {
67 my $item = Koha::Item->new(
69 biblionumber => $biblio->id(),
70 biblioitemnumber => $biblioitem->id(),
75 push( @items, $item );
78 for my $i ( 0 .. 5 ) {
79 my $patron = $patrons[$i];
80 my $hold = Koha::Hold->new(
82 borrowernumber => $patron->id,
83 biblionumber => $biblio->id,
84 branchcode => $library->{branchcode},
91 source => 'Issuingrule',
98 reservesallowed => '99',
103 my $item = pop(@items);
104 my $patron = pop(@patrons);
106 C4::Context->set_preference( 'decreaseLoanHighHolds', 1 );
107 C4::Context->set_preference( 'decreaseLoanHighHoldsDuration', 1 );
108 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 1 );
109 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'static' );
110 C4::Context->set_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
112 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
113 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
115 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
116 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
117 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
118 is( $data->{duration}, 1, "Should have duration of 1" );
119 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
121 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
122 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
123 is( $data->{exceeded}, 0, "Should not exceed threshold" );
125 for my $i ( 5 .. 10 ) {
126 my $patron = $patrons[$i];
127 my $hold = Koha::Hold->new(
129 borrowernumber => $patron->id,
130 biblionumber => $biblio->id,
131 branchcode => $library->{branchcode},
136 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
137 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
139 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 2 );
140 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
141 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
143 my $unholdable = pop(@items);
144 $unholdable->damaged(-1);
145 $unholdable->store();
147 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
148 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
150 $unholdable->damaged(0);
151 $unholdable->itemlost(-1);
152 $unholdable->store();
154 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
155 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
157 $unholdable->itemlost(0);
158 $unholdable->notforloan(-1);
159 $unholdable->store();
161 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
162 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
164 $unholdable->notforloan(0);
165 $unholdable->withdrawn(-1);
166 $unholdable->store();
168 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
169 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
171 C4::Context->set_preference('CircControl', 'PatronLibrary');
173 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
174 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
176 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
177 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
179 $schema->storage->txn_rollback();