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>.
28 use Koha::CirculationRules;
29 use t::lib::TestBuilder;
32 use Test::More tests => 17;
34 my $dbh = C4::Context->dbh;
35 my $schema = Koha::Database->new()->schema();
36 my $builder = t::lib::TestBuilder->new;
38 $schema->storage->txn_begin();
40 my $now_value = DateTime->now();
41 my $mocked_datetime = Test::MockModule->new('DateTime');
42 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
44 my $library = $builder->build( { source => 'Branch' } );
45 my $category = $builder->build( { source => 'Category' } );
46 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
48 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
49 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
51 my $patron_category = $builder->build({
60 for my $i ( 1 .. 20 ) {
61 my $patron = Koha::Patron->new({
64 categorycode => $category->{categorycode},
65 branchcode => $library->{branchcode},
66 categorycode => $patron_category->{categorycode},
68 push( @patrons, $patron );
71 my $biblio = $builder->build_sample_biblio();
74 for my $i ( 1 .. 10 ) {
75 my $item = $builder->build_sample_item(
77 biblionumber => $biblio->id(),
81 push( @items, $item );
84 for my $i ( 0 .. 5 ) {
85 my $patron = $patrons[$i];
86 my $hold = Koha::Hold->new(
88 borrowernumber => $patron->id,
89 biblionumber => $biblio->id,
90 branchcode => $library->{branchcode},
95 my $item = pop(@items);
96 my $patron = pop(@patrons);
98 Koha::CirculationRules->set_rules(
101 categorycode => undef,
102 itemtype => $item->itype,
105 lengthunit => 'days',
106 reservesallowed => '99',
107 holds_per_record => '99',
113 my $orig_due = C4::Circulation::CalcDateDue(
114 DateTime->now(time_zone => C4::Context->tz()),
115 $item->effective_itemtype,
120 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds', 1 );
121 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration', 1 );
122 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 1 );
123 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'static' );
124 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
126 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
127 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
129 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
130 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
131 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
132 is( $data->{duration}, 1, "Should have duration of 1" );
133 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
135 my $duedate = $data->{due_date};
136 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
137 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
138 is($duedate->sec, 0, 'New due date second is zero.');
140 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
141 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
142 is( $data->{exceeded}, 0, "Should not exceed threshold" );
144 for my $i ( 5 .. 10 ) {
145 my $patron = $patrons[$i];
146 my $hold = Koha::Hold->new(
148 borrowernumber => $patron->id,
149 biblionumber => $biblio->id,
150 branchcode => $library->{branchcode},
155 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
156 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
158 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
159 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
160 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
162 my $unholdable = pop(@items);
163 $unholdable->damaged(-1);
164 $unholdable->store();
166 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
167 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
169 $unholdable->damaged(0);
170 $unholdable->itemlost(-1);
171 $unholdable->store();
173 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
174 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
176 $unholdable->itemlost(0);
177 $unholdable->notforloan(-1);
178 $unholdable->store();
180 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
181 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
183 $unholdable->notforloan(0);
184 $unholdable->withdrawn(-1);
185 $unholdable->store();
187 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
188 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
190 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
192 my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
193 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
194 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
196 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
197 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
199 $schema->storage->txn_rollback();