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 => 12;
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'});
48 C4::Context->_new_userenv('xxx');
49 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
50 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
53 for my $i ( 1 .. 20 ) {
54 my $patron = Koha::Patron->new(
55 { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode} } )
57 push( @patrons, $patron );
60 my $biblio = Koha::Biblio->new()->store();
62 $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
65 for my $i ( 1 .. 10 ) {
66 my $item = Koha::Item->new( { biblionumber => $biblio->id(), biblioitemnumber => $biblioitem->id(), } )->store();
67 push( @items, $item );
70 for my $i ( 0 .. 5 ) {
71 my $patron = $patrons[$i];
72 my $hold = Koha::Hold->new(
74 borrowernumber => $patron->id,
75 biblionumber => $biblio->id,
76 branchcode => $library->{branchcode},
83 source => 'Issuingrule',
90 reservesallowed => '99',
95 my $item = pop(@items);
96 my $patron = pop(@patrons);
98 C4::Context->set_preference( 'decreaseLoanHighHolds', 1 );
99 C4::Context->set_preference( 'decreaseLoanHighHoldsDuration', 1 );
100 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 1 );
101 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'static' );
102 C4::Context->set_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
104 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode} };
105 my $patron_hr = { borrower => $patron->id, branchcode => $library->{branchcode} };
107 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
108 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
109 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
110 is( $data->{duration}, 1, "Should have duration of 1" );
111 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
113 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
114 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
115 is( $data->{exceeded}, 0, "Should not exceed threshold" );
117 for my $i ( 5 .. 10 ) {
118 my $patron = $patrons[$i];
119 my $hold = Koha::Hold->new(
121 borrowernumber => $patron->id,
122 biblionumber => $biblio->id,
123 branchcode => $library->{branchcode},
128 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
129 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
131 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 2 );
132 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
133 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
135 my $unholdable = pop(@items);
136 $unholdable->damaged(-1);
137 $unholdable->store();
139 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
140 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
142 $unholdable->damaged(0);
143 $unholdable->itemlost(-1);
144 $unholdable->store();
146 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
147 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
149 $unholdable->itemlost(0);
150 $unholdable->notforloan(-1);
151 $unholdable->store();
153 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
154 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
156 $unholdable->notforloan(0);
157 $unholdable->withdrawn(-1);
158 $unholdable->store();
160 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
161 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
163 $schema->storage->txn_rollback();