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
=> 14;
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' );
55 for my $i ( 1 .. 20 ) {
56 my $patron = Koha
::Patron
->new(
57 { cardnumber
=> $i, firstname
=> 'Kyle', surname
=> 'Hall', categorycode
=> $category->{categorycode
}, branchcode
=> $library->{branchcode
} } )
59 push( @patrons, $patron );
62 my $biblio = Koha
::Biblio
->new()->store();
64 $schema->resultset('Biblioitem')->new( { biblionumber
=> $biblio->biblionumber } )->insert();
67 for my $i ( 1 .. 10 ) {
68 my $item = Koha
::Item
->new(
70 biblionumber
=> $biblio->id(),
71 biblioitemnumber
=> $biblioitem->id(),
76 push( @items, $item );
79 for my $i ( 0 .. 5 ) {
80 my $patron = $patrons[$i];
81 my $hold = Koha
::Hold
->new(
83 borrowernumber
=> $patron->id,
84 biblionumber
=> $biblio->id,
85 branchcode
=> $library->{branchcode
},
92 source
=> 'Issuingrule',
99 reservesallowed
=> '99',
104 my $item = pop(@items);
105 my $patron = pop(@patrons);
107 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHolds', 1 );
108 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsDuration', 1 );
109 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 1 );
110 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'static' );
111 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
113 my $item_hr = { itemnumber
=> $item->id, biblionumber
=> $biblio->id, homebranch
=> $library->{branchcode
}, holdingbranch
=> $library->{branchcode
}, barcode
=> $item->barcode };
114 my $patron_hr = { borrowernumber
=> $patron->id, branchcode
=> $library->{branchcode
} };
116 my $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
117 is
( $data->{exceeded
}, 1, "Static mode should exceed threshold" );
118 is
( $data->{outstanding
}, 6, "Should have 5 outstanding holds" );
119 is
( $data->{duration
}, 1, "Should have duration of 1" );
120 is
( ref( $data->{due_date
} ), 'DateTime', "due_date should be a DateTime object" );
122 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsControl', 'dynamic' );
123 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
124 is
( $data->{exceeded
}, 0, "Should not exceed threshold" );
126 for my $i ( 5 .. 10 ) {
127 my $patron = $patrons[$i];
128 my $hold = Koha
::Hold
->new(
130 borrowernumber
=> $patron->id,
131 biblionumber
=> $biblio->id,
132 branchcode
=> $library->{branchcode
},
137 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
138 is
( $data->{exceeded
}, 1, "Should exceed threshold of 1" );
140 t
::lib
::Mocks
::mock_preference
( 'decreaseLoanHighHoldsValue', 2 );
141 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
142 is
( $data->{exceeded
}, 0, "Should not exceed threshold of 2" );
144 my $unholdable = pop(@items);
145 $unholdable->damaged(-1);
146 $unholdable->store();
148 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
149 is
( $data->{exceeded
}, 1, "Should exceed threshold with one damaged item" );
151 $unholdable->damaged(0);
152 $unholdable->itemlost(-1);
153 $unholdable->store();
155 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
156 is
( $data->{exceeded
}, 1, "Should exceed threshold with one lost item" );
158 $unholdable->itemlost(0);
159 $unholdable->notforloan(-1);
160 $unholdable->store();
162 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
163 is
( $data->{exceeded
}, 1, "Should exceed threshold with one notforloan item" );
165 $unholdable->notforloan(0);
166 $unholdable->withdrawn(-1);
167 $unholdable->store();
169 $data = C4
::Circulation
::checkHighHolds
( $item_hr, $patron_hr );
170 is
( $data->{exceeded
}, 1, "Should exceed threshold with one withdrawn item" );
172 t
::lib
::Mocks
::mock_preference
('CircControl', 'PatronLibrary');
174 my ( undef, $needsconfirmation ) = CanBookBeIssued
( $patron_hr, $item->barcode );
175 ok
( $needsconfirmation->{HIGHHOLDS
}, "High holds checkout needs confirmation" );
177 ( undef, $needsconfirmation ) = CanBookBeIssued
( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds
=> 1 } );
178 ok
( !$needsconfirmation->{HIGHHOLDS
}, "High holds checkout does not need confirmation" );
180 $schema->storage->txn_rollback();