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' );
54 my $patron_category = $builder->build({ source
=> 'Category', value
=> { categorycode
=> 'NOT_X', category_type
=> 'P', enrolmentfee
=> 0 } });
56 for my $i ( 1 .. 20 ) {
57 my $patron = Koha
::Patron
->new(
58 { cardnumber
=> $i, firstname
=> 'Kyle', surname
=> 'Hall', categorycode
=> $category->{categorycode
}, branchcode
=> $library->{branchcode
}, categorycode
=> $patron_category->{categorycode
}, } )
60 push( @patrons, $patron );
63 my $biblio = Koha
::Biblio
->new()->store();
65 $schema->resultset('Biblioitem')->new( { biblionumber
=> $biblio->biblionumber } )->insert();
68 for my $i ( 1 .. 10 ) {
69 my $item = Koha
::Item
->new(
71 biblionumber
=> $biblio->id(),
72 biblioitemnumber
=> $biblioitem->id(),
77 push( @items, $item );
80 for my $i ( 0 .. 5 ) {
81 my $patron = $patrons[$i];
82 my $hold = Koha
::Hold
->new(
84 borrowernumber
=> $patron->id,
85 biblionumber
=> $biblio->id,
86 branchcode
=> $library->{branchcode
},
93 source
=> 'Issuingrule',
100 reservesallowed
=> '99',
105 my $item = pop(@items);
106 my $patron = pop(@patrons);
108 my $orig_due = C4
::Circulation
::CalcDateDue
(
109 DateTime
->now(time_zone
=> C4
::Context
->tz()),
110 $item->effective_itemtype,
115 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHolds', 1 );
116 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsDuration', 1 );
117 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 1 );
118 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'static' );
119 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
121 my $item_hr = { itemnumber
=> $item->id, biblionumber
=> $biblio->id, homebranch
=> $library->{branchcode
}, holdingbranch
=> $library->{branchcode
}, barcode
=> $item->barcode };
122 my $patron_hr = { borrowernumber
=> $patron->id, branchcode
=> $library->{branchcode
} };
124 my $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
125 is
( $data->{exceeded
}, 1, "Static mode should exceed threshold" );
126 is
( $data->{outstanding
}, 6, "Should have 5 outstanding holds" );
127 is
( $data->{duration
}, 1, "Should have duration of 1" );
128 is
( ref( $data->{due_date
} ), 'DateTime', "due_date should be a DateTime object" );
130 my $duedate = $data->{due_date
};
131 is
($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
132 is
($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
133 is
($duedate->sec, 0, 'New due date second is zero.');
135 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'dynamic' );
136 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
137 is
( $data->{exceeded
}, 0, "Should not exceed threshold" );
139 for my $i ( 5 .. 10 ) {
140 my $patron = $patrons[$i];
141 my $hold = Koha
::Hold
->new(
143 borrowernumber
=> $patron->id,
144 biblionumber
=> $biblio->id,
145 branchcode
=> $library->{branchcode
},
150 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
151 is
( $data->{exceeded
}, 1, "Should exceed threshold of 1" );
153 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 2 );
154 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
155 is
( $data->{exceeded
}, 0, "Should not exceed threshold of 2" );
157 my $unholdable = pop(@items);
158 $unholdable->damaged(-1);
159 $unholdable->store();
161 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
162 is
( $data->{exceeded
}, 1, "Should exceed threshold with one damaged item" );
164 $unholdable->damaged(0);
165 $unholdable->itemlost(-1);
166 $unholdable->store();
168 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
169 is
( $data->{exceeded
}, 1, "Should exceed threshold with one lost item" );
171 $unholdable->itemlost(0);
172 $unholdable->notforloan(-1);
173 $unholdable->store();
175 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
176 is
( $data->{exceeded
}, 1, "Should exceed threshold with one notforloan item" );
178 $unholdable->notforloan(0);
179 $unholdable->withdrawn(-1);
180 $unholdable->store();
182 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
183 is
( $data->{exceeded
}, 1, "Should exceed threshold with one withdrawn item" );
185 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
187 my ( $un, $needsconfirmation ) = CanBookBeIssued
( $patron_hr, $item->barcode );
188 ok
( $needsconfirmation->{HIGHHOLDS
}, "High holds checkout needs confirmation" );
190 ( undef, $needsconfirmation ) = CanBookBeIssued
( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds
=> 1 } );
191 ok
( !$needsconfirmation->{HIGHHOLDS
}, "High holds checkout does not need confirmation" );
193 $schema->storage->txn_rollback();