Bug 24254: Implement separate filters and add patron param
[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 use DateTime;
20
21 use C4::Circulation;
22 use Koha::Database;
23 use Koha::DateUtils;
24 use Koha::Patrons;
25 use Koha::Biblio;
26 use Koha::Item;
27 use Koha::Holds;
28 use Koha::Hold;
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 use Test::More tests => 19;
33
34 my $dbh    = C4::Context->dbh;
35 my $schema = Koha::Database->new()->schema();
36 my $builder = t::lib::TestBuilder->new;
37
38 # Start transaction
39 $dbh->{RaiseError} = 1;
40 $schema->storage->txn_begin();
41
42 my $now_value       = dt_from_string();
43 my $mocked_datetime = Test::MockModule->new('DateTime');
44 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
45
46 my $library  = $builder->build( { source => 'Branch' } );
47 my $category = $builder->build( { source => 'Category' } );
48 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
49
50 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
51 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
52
53 my $patron_category = $builder->build({
54     source => 'Category',
55     value => {
56         category_type => 'P',
57         enrolmentfee => 0
58     }
59 });
60
61 my @patrons;
62 for my $i ( 1 .. 20 ) {
63     my $patron = Koha::Patron->new({
64         firstname => 'Kyle',
65         surname => 'Hall',
66         categorycode => $category->{categorycode},
67         branchcode => $library->{branchcode},
68         categorycode => $patron_category->{categorycode},
69     })->store();
70     push( @patrons, $patron );
71 }
72
73 my $biblio = $builder->build_sample_biblio();
74
75 # The biblio gets 10 items
76 my @items;
77 for my $i ( 1 .. 10 ) {
78     my $item = $builder->build_sample_item(
79         {
80             biblionumber     => $biblio->id(),
81             itype            => $itemtype
82         }
83     );
84     push( @items, $item );
85 }
86
87 # Place 6 holds, patrons 0,1,2,3,4,5
88 for my $i ( 0 .. 5 ) {
89     my $patron = $patrons[$i];
90     my $hold   = Koha::Hold->new(
91         {
92             borrowernumber => $patron->id,
93             biblionumber   => $biblio->id,
94             branchcode     => $library->{branchcode},
95         }
96     )->store();
97 }
98
99 my $item   = shift(@items);
100 my $patron = shift(@patrons);
101 my $patron_hold = Koha::Holds->find({ borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
102
103 $builder->build(
104     {
105         source => 'Issuingrule',
106         value => {
107             branchcode => '*',
108             categorycode => '*',
109             itemtype => $item->itype,
110             issuelength => '14',
111             lengthunit => 'days',
112             reservesallowed => '99',
113         }
114     }
115 );
116
117
118 my $orig_due = C4::Circulation::CalcDateDue(
119     dt_from_string(),
120     $item->effective_itemtype,
121     $patron->branchcode,
122     $patron->unblessed
123 );
124
125 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
126 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
127 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
128 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
129 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
130
131 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
132 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
133
134 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
135 is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
136 is( $data->{outstanding},     6,          "Should have 6 outstanding holds" );
137 is( $data->{duration},        1,          "Should have duration of 1" );
138 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
139
140 my $duedate = $data->{due_date};
141 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
142 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
143 is($duedate->sec, 0, 'New due date second is zero.');
144
145 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
146 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
147 is( $data->{exceeded}, 0, "Should not exceed threshold" );
148
149
150 # Place 6 more holds - patrons 5,6,7,8,9,10
151 for my $i ( 5 .. 10 ) {
152     my $patron = $patrons[$i];
153     my $hold   = Koha::Hold->new(
154         {
155             borrowernumber => $patron->id,
156             biblionumber   => $biblio->id,
157             branchcode     => $library->{branchcode},
158         }
159     )->store();
160 }
161
162 # 12 holds, threshold is 1 over 10 holdable items = 11
163 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
164 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
165 is( $data->{outstanding}, 12, "Should exceed threshold of 1" );
166
167 # 12 holds, threshold is 2 over 10 holdable items = 12 (equal is okay)
168 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
169 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
170 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
171
172 my $unholdable = pop(@items);
173 $unholdable->damaged(-1);
174 $unholdable->store();
175
176 # 12 holds, threshold is 2 over 9 holdable items = 11
177 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
178 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
179
180 $unholdable->damaged(0);
181 $unholdable->itemlost(-1);
182 $unholdable->store();
183
184 # 12 holds, threshold is 2 over 9 holdable items = 11
185 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
186 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
187
188 $unholdable->itemlost(0);
189 $unholdable->notforloan(-1);
190 $unholdable->store();
191
192 # 12 holds, threshold is 2 over 9 holdable items = 11
193 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
194 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
195
196 $unholdable->notforloan(0);
197 $unholdable->withdrawn(-1);
198 $unholdable->store();
199
200 # 12 holds, threshold is 2 over 9 holdable items = 11
201 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
202 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
203
204 $patron_hold->found('F')->store;
205 # 11 holds, threshold is 2 over 9 holdable items = 11
206 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
207 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
208 $patron_hold->found(undef)->store;
209
210 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
211
212 my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
213 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
214 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
215
216 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
217 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
218
219 $schema->storage->txn_rollback();