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 t::lib::TestBuilder;
31 use Test::More tests => 17;
33 my $dbh = C4::Context->dbh;
34 my $schema = Koha::Database->new()->schema();
35 my $builder = t::lib::TestBuilder->new;
38 $dbh->{RaiseError} = 1;
39 $schema->storage->txn_begin();
41 $dbh->do('DELETE FROM issues');
42 $dbh->do('DELETE FROM issuingrules');
43 $dbh->do('DELETE FROM borrowers');
44 $dbh->do('DELETE FROM items');
46 my $now_value = DateTime->now();
47 my $mocked_datetime = Test::MockModule->new('DateTime');
48 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
50 my $library = $builder->build( { source => 'Branch' } );
51 my $category = $builder->build( { source => 'Category' } );
52 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
54 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
55 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
57 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
59 for my $i ( 1 .. 20 ) {
60 my $patron = Koha::Patron->new(
61 { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode}, } )
63 push( @patrons, $patron );
66 my $biblio = Koha::Biblio->new()->store();
68 $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
71 for my $i ( 1 .. 10 ) {
72 my $item = Koha::Item->new(
74 biblionumber => $biblio->id(),
75 biblioitemnumber => $biblioitem->id(),
80 push( @items, $item );
83 for my $i ( 0 .. 5 ) {
84 my $patron = $patrons[$i];
85 my $hold = Koha::Hold->new(
87 borrowernumber => $patron->id,
88 biblionumber => $biblio->id,
89 branchcode => $library->{branchcode},
96 source => 'Issuingrule',
102 lengthunit => 'days',
103 reservesallowed => '99',
108 my $item = pop(@items);
109 my $patron = pop(@patrons);
111 my $orig_due = C4::Circulation::CalcDateDue(
112 DateTime->now(time_zone => C4::Context->tz()),
113 $item->effective_itemtype,
118 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds', 1 );
119 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration', 1 );
120 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 1 );
121 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'static' );
122 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
124 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
125 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
127 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
128 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
129 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
130 is( $data->{duration}, 1, "Should have duration of 1" );
131 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
133 my $duedate = $data->{due_date};
134 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
135 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
136 is($duedate->sec, 0, 'New due date second is zero.');
138 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
139 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
140 is( $data->{exceeded}, 0, "Should not exceed threshold" );
142 for my $i ( 5 .. 10 ) {
143 my $patron = $patrons[$i];
144 my $hold = Koha::Hold->new(
146 borrowernumber => $patron->id,
147 biblionumber => $biblio->id,
148 branchcode => $library->{branchcode},
153 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
154 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
156 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
157 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
158 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
160 my $unholdable = pop(@items);
161 $unholdable->damaged(-1);
162 $unholdable->store();
164 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
165 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
167 $unholdable->damaged(0);
168 $unholdable->itemlost(-1);
169 $unholdable->store();
171 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
172 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
174 $unholdable->itemlost(0);
175 $unholdable->notforloan(-1);
176 $unholdable->store();
178 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
179 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
181 $unholdable->notforloan(0);
182 $unholdable->withdrawn(-1);
183 $unholdable->store();
185 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
186 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
188 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
190 my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
191 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
192 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
194 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
195 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
197 $schema->storage->txn_rollback();