Bug 15839: Koha::Reviews - Add Koha::Review[s] classes
[koha.git] / t / db_dependent / DecreaseLoanHighHolds.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
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;
28
29 use Test::More tests => 14;
30
31 my $dbh    = C4::Context->dbh;
32 my $schema = Koha::Database->new()->schema();
33 my $builder = t::lib::TestBuilder->new;
34
35 # Start transaction
36 $dbh->{RaiseError} = 1;
37 $schema->storage->txn_begin();
38
39 $dbh->do('DELETE FROM issues');
40 $dbh->do('DELETE FROM issuingrules');
41 $dbh->do('DELETE FROM borrowers');
42 $dbh->do('DELETE FROM items');
43
44 my $library = $builder->build({source => 'Branch'});
45 my $category = $builder->build({source => 'Category'});
46
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' );
51
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 );
58 }
59
60 my $biblio = Koha::Biblio->new()->store();
61 my $biblioitem =
62   $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
63
64 my @items;
65 for my $i ( 1 .. 10 ) {
66     my $item = Koha::Item->new(
67         {
68             biblionumber     => $biblio->id(),
69             biblioitemnumber => $biblioitem->id(),
70             barcode          => $i
71         }
72     )->store();
73     push( @items, $item );
74 }
75
76 for my $i ( 0 .. 5 ) {
77     my $patron = $patrons[$i];
78     my $hold   = Koha::Hold->new(
79         {
80             borrowernumber => $patron->id,
81             biblionumber   => $biblio->id,
82             branchcode     => $library->{branchcode},
83         }
84     )->store();
85 }
86
87 $builder->build(
88     {
89         source => 'Issuingrule',
90         value => {
91             branchcode => '*',
92             categorycode => '*',
93             itemtype => '*',
94             issuelength => '14',
95             lengthunit => 'days',
96             reservesallowed => '99',
97         }
98     }
99 );
100
101 my $item   = pop(@items);
102 my $patron = pop(@patrons);
103
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' );
109
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} };
112
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" );
118
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" );
122
123 for my $i ( 5 .. 10 ) {
124     my $patron = $patrons[$i];
125     my $hold   = Koha::Hold->new(
126         {
127             borrowernumber => $patron->id,
128             biblionumber   => $biblio->id,
129             branchcode     => $library->{branchcode},
130         }
131     )->store();
132 }
133
134 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
135 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
136
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" );
140
141 my $unholdable = pop(@items);
142 $unholdable->damaged(-1);
143 $unholdable->store();
144
145 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
146 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
147
148 $unholdable->damaged(0);
149 $unholdable->itemlost(-1);
150 $unholdable->store();
151
152 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
153 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
154
155 $unholdable->itemlost(0);
156 $unholdable->notforloan(-1);
157 $unholdable->store();
158
159 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
160 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
161
162 $unholdable->notforloan(0);
163 $unholdable->withdrawn(-1);
164 $unholdable->store();
165
166 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
167 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
168
169 C4::Context->set_preference('CircControl', 'PatronLibrary');
170
171 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
172 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
173
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" );
176
177 $schema->storage->txn_rollback();
178 1;