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>.
29 use t::lib::TestBuilder;
32 use Test::More tests => 19;
34 my $dbh = C4::Context->dbh;
35 my $schema = Koha::Database->new()->schema();
36 my $builder = t::lib::TestBuilder->new;
39 $dbh->{RaiseError} = 1;
40 $schema->storage->txn_begin();
42 my $now_value = dt_from_string();
43 my $mocked_datetime = Test::MockModule->new('DateTime');
44 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
46 my $library = $builder->build( { source => 'Branch' } );
47 my $category = $builder->build( { source => 'Category' } );
48 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
50 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
51 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
53 my $patron_category = $builder->build({
62 for my $i ( 1 .. 20 ) {
63 my $patron = Koha::Patron->new({
66 categorycode => $category->{categorycode},
67 branchcode => $library->{branchcode},
68 categorycode => $patron_category->{categorycode},
70 push( @patrons, $patron );
73 my $biblio = $builder->build_sample_biblio();
75 # The biblio gets 10 items
77 for my $i ( 1 .. 10 ) {
78 my $item = $builder->build_sample_item(
80 biblionumber => $biblio->id(),
84 push( @items, $item );
87 # Place 6 holds, patrons 0,1,2,3,4,5
88 for my $i ( 0 .. 5 ) {
89 my $patron = $patrons[$i];
90 my $hold = Koha::Hold->new(
92 borrowernumber => $patron->id,
93 biblionumber => $biblio->id,
94 branchcode => $library->{branchcode},
99 my $item = shift(@items);
100 my $patron = shift(@patrons);
101 my $patron_hold = Koha::Holds->find({ borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
105 source => 'Issuingrule',
109 itemtype => $item->itype,
111 lengthunit => 'days',
112 reservesallowed => '99',
118 my $orig_due = C4::Circulation::CalcDateDue(
120 $item->effective_itemtype,
125 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds', 1 );
126 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration', 1 );
127 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 1 );
128 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'static' );
129 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
131 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
132 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
134 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
135 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
136 is( $data->{outstanding}, 6, "Should have 6 outstanding holds" );
137 is( $data->{duration}, 1, "Should have duration of 1" );
138 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
140 my $duedate = $data->{due_date};
141 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
142 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
143 is($duedate->sec, 0, 'New due date second is zero.');
145 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
146 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
147 is( $data->{exceeded}, 0, "Should not exceed threshold" );
150 # Place 6 more holds - patrons 5,6,7,8,9,10
151 for my $i ( 5 .. 10 ) {
152 my $patron = $patrons[$i];
153 my $hold = Koha::Hold->new(
155 borrowernumber => $patron->id,
156 biblionumber => $biblio->id,
157 branchcode => $library->{branchcode},
162 # 12 holds, threshold is 1 over 10 holdable items = 11
163 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
164 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
165 is( $data->{outstanding}, 12, "Should exceed threshold of 1" );
167 # 12 holds, threshold is 2 over 10 holdable items = 12 (equal is okay)
168 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
169 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
170 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
172 my $unholdable = pop(@items);
173 $unholdable->damaged(-1);
174 $unholdable->store();
176 # 12 holds, threshold is 2 over 9 holdable items = 11
177 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
178 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
180 $unholdable->damaged(0);
181 $unholdable->itemlost(-1);
182 $unholdable->store();
184 # 12 holds, threshold is 2 over 9 holdable items = 11
185 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
186 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
188 $unholdable->itemlost(0);
189 $unholdable->notforloan(-1);
190 $unholdable->store();
192 # 12 holds, threshold is 2 over 9 holdable items = 11
193 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
194 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
196 $unholdable->notforloan(0);
197 $unholdable->withdrawn(-1);
198 $unholdable->store();
200 # 12 holds, threshold is 2 over 9 holdable items = 11
201 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
202 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
204 $patron_hold->found('F')->store;
205 # 11 holds, threshold is 2 over 9 holdable items = 11
206 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
207 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
208 $patron_hold->found(undef)->store;
210 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
212 my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
213 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
214 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
216 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
217 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
219 $schema->storage->txn_rollback();