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
;
30 use Test
::More tests
=> 17;
32 my $dbh = C4
::Context
->dbh;
33 my $schema = Koha
::Database
->new()->schema();
34 my $builder = t
::lib
::TestBuilder
->new;
37 $dbh->{RaiseError
} = 1;
38 $schema->storage->txn_begin();
40 $dbh->do('DELETE FROM issues');
41 $dbh->do('DELETE FROM issuingrules');
42 $dbh->do('DELETE FROM borrowers');
43 $dbh->do('DELETE FROM items');
45 my $library = $builder->build( { source
=> 'Branch' } );
46 my $category = $builder->build( { source
=> 'Category' } );
47 my $itemtype = $builder->build( { source
=> 'Itemtype' } )->{itemtype
};
50 C4
::Context
->_new_userenv('xxx');
51 C4
::Context
->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode
}, 'Midway Public Library', '', '', '' );
52 is
( C4
::Context
->userenv->{branch
}, $library->{branchcode
}, 'userenv set' );
55 for my $i ( 1 .. 20 ) {
56 my $patron = Koha
::Patron
->new(
57 { cardnumber
=> $i, firstname
=> 'Kyle', surname
=> 'Hall', categorycode
=> $category->{categorycode
}, branchcode
=> $library->{branchcode
} } )
59 push( @patrons, $patron );
62 my $biblio = Koha
::Biblio
->new()->store();
64 $schema->resultset('Biblioitem')->new( { biblionumber
=> $biblio->biblionumber } )->insert();
67 for my $i ( 1 .. 10 ) {
68 my $item = Koha
::Item
->new(
70 biblionumber
=> $biblio->id(),
71 biblioitemnumber
=> $biblioitem->id(),
76 push( @items, $item );
79 for my $i ( 0 .. 5 ) {
80 my $patron = $patrons[$i];
81 my $hold = Koha
::Hold
->new(
83 borrowernumber
=> $patron->id,
84 biblionumber
=> $biblio->id,
85 branchcode
=> $library->{branchcode
},
92 source
=> 'Issuingrule',
99 reservesallowed
=> '99',
104 my $item = pop(@items);
105 my $patron = pop(@patrons);
107 my $orig_due = C4
::Circulation
::CalcDateDue
(
108 DateTime
->now(time_zone
=> C4
::Context
->tz()),
109 $item->effective_itemtype,
114 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHolds', 1 );
115 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsDuration', 1 );
116 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 1 );
117 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'static' );
118 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
120 my $item_hr = { itemnumber
=> $item->id, biblionumber
=> $biblio->id, homebranch
=> $library->{branchcode
}, holdingbranch
=> $library->{branchcode
}, barcode
=> $item->barcode };
121 my $patron_hr = { borrowernumber
=> $patron->id, branchcode
=> $library->{branchcode
} };
123 my $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
124 is
( $data->{exceeded
}, 1, "Static mode should exceed threshold" );
125 is
( $data->{outstanding
}, 6, "Should have 5 outstanding holds" );
126 is
( $data->{duration
}, 1, "Should have duration of 1" );
127 is
( ref( $data->{due_date
} ), 'DateTime', "due_date should be a DateTime object" );
129 my $duedate = $data->{due_date
};
130 is
($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
131 is
($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
132 is
($duedate->sec, 0, 'New due date second is zero.');
134 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'dynamic' );
135 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
136 is
( $data->{exceeded
}, 0, "Should not exceed threshold" );
138 for my $i ( 5 .. 10 ) {
139 my $patron = $patrons[$i];
140 my $hold = Koha
::Hold
->new(
142 borrowernumber
=> $patron->id,
143 biblionumber
=> $biblio->id,
144 branchcode
=> $library->{branchcode
},
149 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
150 is
( $data->{exceeded
}, 1, "Should exceed threshold of 1" );
152 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 2 );
153 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
154 is
( $data->{exceeded
}, 0, "Should not exceed threshold of 2" );
156 my $unholdable = pop(@items);
157 $unholdable->damaged(-1);
158 $unholdable->store();
160 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
161 is
( $data->{exceeded
}, 1, "Should exceed threshold with one damaged item" );
163 $unholdable->damaged(0);
164 $unholdable->itemlost(-1);
165 $unholdable->store();
167 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
168 is
( $data->{exceeded
}, 1, "Should exceed threshold with one lost item" );
170 $unholdable->itemlost(0);
171 $unholdable->notforloan(-1);
172 $unholdable->store();
174 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
175 is
( $data->{exceeded
}, 1, "Should exceed threshold with one notforloan item" );
177 $unholdable->notforloan(0);
178 $unholdable->withdrawn(-1);
179 $unholdable->store();
181 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
182 is
( $data->{exceeded
}, 1, "Should exceed threshold with one withdrawn item" );
184 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
186 my ( undef, $needsconfirmation ) = CanBookBeIssued
( $patron_hr, $item->barcode );
187 ok
( $needsconfirmation->{HIGHHOLDS
}, "High holds checkout needs confirmation" );
189 ( undef, $needsconfirmation ) = CanBookBeIssued
( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds
=> 1 } );
190 ok
( !$needsconfirmation->{HIGHHOLDS
}, "High holds checkout does not need confirmation" );
192 $schema->storage->txn_rollback();