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;
39 $dbh->{RaiseError} = 1;
40 $schema->storage->txn_begin();
42 my $now_value = DateTime->now();
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();
76 for my $i ( 1 .. 10 ) {
77 my $item = $builder->build_sample_item(
79 biblionumber => $biblio->id(),
83 push( @items, $item );
86 for my $i ( 0 .. 5 ) {
87 my $patron = $patrons[$i];
88 my $hold = Koha::Hold->new(
90 borrowernumber => $patron->id,
91 biblionumber => $biblio->id,
92 branchcode => $library->{branchcode},
97 my $item = pop(@items);
98 my $patron = pop(@patrons);
100 Koha::CirculationRules->set_rules(
103 categorycode => undef,
104 itemtype => $item->itype,
107 lengthunit => 'days',
108 reservesallowed => '99',
109 holds_per_record => '99',
115 my $orig_due = C4::Circulation::CalcDateDue(
116 DateTime->now(time_zone => C4::Context->tz()),
117 $item->effective_itemtype,
122 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds', 1 );
123 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration', 1 );
124 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 1 );
125 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'static' );
126 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
128 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
129 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
131 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
132 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
133 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
134 is( $data->{duration}, 1, "Should have duration of 1" );
135 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
137 my $duedate = $data->{due_date};
138 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
139 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
140 is($duedate->sec, 0, 'New due date second is zero.');
142 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
143 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
144 is( $data->{exceeded}, 0, "Should not exceed threshold" );
146 for my $i ( 5 .. 10 ) {
147 my $patron = $patrons[$i];
148 my $hold = Koha::Hold->new(
150 borrowernumber => $patron->id,
151 biblionumber => $biblio->id,
152 branchcode => $library->{branchcode},
157 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
158 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
160 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
161 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
162 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
164 my $unholdable = pop(@items);
165 $unholdable->damaged(-1);
166 $unholdable->store();
168 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
169 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
171 $unholdable->damaged(0);
172 $unholdable->itemlost(-1);
173 $unholdable->store();
175 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
176 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
178 $unholdable->itemlost(0);
179 $unholdable->notforloan(-1);
180 $unholdable->store();
182 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
183 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
185 $unholdable->notforloan(0);
186 $unholdable->withdrawn(-1);
187 $unholdable->store();
189 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
190 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
192 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
194 my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
195 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
196 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
198 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
199 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
201 $schema->storage->txn_rollback();