Bug 15895 - Add Koha::Account module, use Koha::Account::pay internally for recordpayment
[koha.git] / t / db_dependent / DecreaseLoanHighHolds.t
blob3eb8e6bddb187aebf98434ad5ac20103809fe2f2
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use C4::Circulation;
21 use Koha::Database;
22 use Koha::Patron;
23 use Koha::Biblio;
24 use Koha::Item;
25 use Koha::Holds;
26 use Koha::Hold;
27 use t::lib::TestBuilder;
29 use Test::More tests => 14;
31 my $dbh = C4::Context->dbh;
32 my $schema = Koha::Database->new()->schema();
33 my $builder = t::lib::TestBuilder->new;
35 # Start transaction
36 $dbh->{RaiseError} = 1;
37 $schema->storage->txn_begin();
39 $dbh->do('DELETE FROM issues');
40 $dbh->do('DELETE FROM issuingrules');
41 $dbh->do('DELETE FROM borrowers');
42 $dbh->do('DELETE FROM items');
44 my $library = $builder->build({source => 'Branch'});
45 my $category = $builder->build({source => 'Category'});
47 # Set userenv
48 C4::Context->_new_userenv('xxx');
49 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
50 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
52 my @patrons;
53 for my $i ( 1 .. 20 ) {
54 my $patron = Koha::Patron->new(
55 { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode} } )
56 ->store();
57 push( @patrons, $patron );
60 my $biblio = Koha::Biblio->new()->store();
61 my $biblioitem =
62 $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
64 my @items;
65 for my $i ( 1 .. 10 ) {
66 my $item = Koha::Item->new(
68 biblionumber => $biblio->id(),
69 biblioitemnumber => $biblioitem->id(),
70 barcode => $i
72 )->store();
73 push( @items, $item );
76 for my $i ( 0 .. 5 ) {
77 my $patron = $patrons[$i];
78 my $hold = Koha::Hold->new(
80 borrowernumber => $patron->id,
81 biblionumber => $biblio->id,
82 branchcode => $library->{branchcode},
84 )->store();
87 $builder->build(
89 source => 'Issuingrule',
90 value => {
91 branchcode => '*',
92 categorycode => '*',
93 itemtype => '*',
94 issuelength => '14',
95 lengthunit => 'days',
96 reservesallowed => '99',
101 my $item = pop(@items);
102 my $patron = pop(@patrons);
104 C4::Context->set_preference( 'decreaseLoanHighHolds', 1 );
105 C4::Context->set_preference( 'decreaseLoanHighHoldsDuration', 1 );
106 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 1 );
107 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'static' );
108 C4::Context->set_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
110 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
111 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
113 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
114 is( $data->{exceeded}, 1, "Static mode should exceed threshold" );
115 is( $data->{outstanding}, 6, "Should have 5 outstanding holds" );
116 is( $data->{duration}, 1, "Should have duration of 1" );
117 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
119 C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
120 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
121 is( $data->{exceeded}, 0, "Should not exceed threshold" );
123 for my $i ( 5 .. 10 ) {
124 my $patron = $patrons[$i];
125 my $hold = Koha::Hold->new(
127 borrowernumber => $patron->id,
128 biblionumber => $biblio->id,
129 branchcode => $library->{branchcode},
131 )->store();
134 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
135 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
137 C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 2 );
138 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
139 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
141 my $unholdable = pop(@items);
142 $unholdable->damaged(-1);
143 $unholdable->store();
145 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
146 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
148 $unholdable->damaged(0);
149 $unholdable->itemlost(-1);
150 $unholdable->store();
152 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
153 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
155 $unholdable->itemlost(0);
156 $unholdable->notforloan(-1);
157 $unholdable->store();
159 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
160 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
162 $unholdable->notforloan(0);
163 $unholdable->withdrawn(-1);
164 $unholdable->store();
166 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
167 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
169 C4::Context->set_preference('CircControl', 'PatronLibrary');
171 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
172 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
174 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
175 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
177 $schema->storage->txn_rollback();