Bug 33146: Unit tests
[koha-ffzg.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 qw( CalcDateDue checkHighHolds CanBookBeIssued );
22 use Koha::Database;
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::Patrons;
25 use Koha::Biblio;
26 use Koha::Item;
27 use Koha::Holds;
28 use Koha::Hold;
29 use Koha::CirculationRules;
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32
33 use Test::More tests => 26;
34
35 my $dbh    = C4::Context->dbh;
36 my $schema = Koha::Database->new()->schema();
37 my $builder = t::lib::TestBuilder->new;
38
39 $schema->storage->txn_begin();
40
41 my $now_value       = dt_from_string();
42 my $mocked_datetime = Test::MockModule->new('DateTime');
43 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
44
45 my $library  = $builder->build( { source => 'Branch' } );
46 my $category = $builder->build( { source => 'Category' } );
47 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
48
49 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
50 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
51
52 my $patron_category = $builder->build({
53     source => 'Category',
54     value => {
55         category_type => 'P',
56         enrolmentfee => 0
57     }
58 });
59
60 my @patrons;
61 for my $i ( 1 .. 20 ) {
62     my $patron = Koha::Patron->new({
63         firstname => 'Kyle',
64         surname => 'Hall',
65         categorycode => $category->{categorycode},
66         branchcode => $library->{branchcode},
67         categorycode => $patron_category->{categorycode},
68     })->store();
69     push( @patrons, $patron );
70 }
71
72 my $biblio = $builder->build_sample_biblio();
73
74 # The biblio gets 10 items
75 my @items;
76 for my $i ( 1 .. 10 ) {
77     my $item = $builder->build_sample_item(
78         {
79             biblionumber     => $biblio->id(),
80             itype            => $itemtype
81         }
82     );
83     push( @items, $item );
84 }
85
86 # Place 6 holds, patrons 0,1,2,3,4,5
87 for my $i ( 0 .. 5 ) {
88     my $patron = $patrons[$i];
89     my $hold   = Koha::Hold->new(
90         {
91             borrowernumber => $patron->id,
92             biblionumber   => $biblio->id,
93             branchcode     => $library->{branchcode},
94         }
95     )->store();
96 }
97
98 my $item   = shift(@items);
99 my $patron = shift(@patrons);
100 my $patron_hold = Koha::Holds->find({ borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
101
102 Koha::CirculationRules->set_rules(
103     {
104         branchcode   => undef,
105         categorycode => undef,
106         itemtype     => $item->itype,
107         rules        => {
108             issuelength     => '14',
109             lengthunit      => 'days',
110             reservesallowed => '99',
111             holds_per_record => '99',
112             decreaseloanholds => 0,
113         }
114     }
115 );
116
117 my $orig_due = C4::Circulation::CalcDateDue(
118     dt_from_string(),
119     $item->effective_itemtype,
120     $patron->branchcode,
121     $patron->unblessed
122 );
123
124 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
125 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
126 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
127 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
128 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
129
130 my $data = C4::Circulation::checkHighHolds( $item, $patron );
131 is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
132 is( $data->{outstanding},     5,          "Should have 5 outstanding holds" );
133 is( $data->{duration},        0,          "Should have duration of 0 because of specific circulation rules" );
134 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
135
136 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          5 );
137 $data = C4::Circulation::checkHighHolds( $item, $patron );
138 is( $data->{exceeded},        0,          "Static mode should not exceed threshold when it equals outstanding holds" );
139 is( $data->{outstanding},     5,          "Should have 5 outstanding holds" );
140 is( $data->{duration},        0,          "Should have duration of 0 because decrease not calculated" );
141 is( $data->{due_date},     undef,         "duedate undefined as not decreasing loan period" );
142 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
143
144 Koha::CirculationRules->set_rules(
145     {
146         branchcode   => undef,
147         categorycode => undef,
148         itemtype     => $item->itype,
149         rules        => {
150             issuelength     => '14',
151             lengthunit      => 'days',
152             reservesallowed => '99',
153             holds_per_record => '99',
154             decreaseloanholds => undef,
155         }
156     }
157 );
158
159 $data = C4::Circulation::checkHighHolds( $item, $patron );
160 is( $data->{duration}, 1, "Should have a duration of 1 because no specific circulation rules so defaults to system preference" );
161
162 my $duedate = $data->{due_date};
163 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
164 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
165 is($duedate->sec, 0, 'New due date second is zero.');
166
167 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
168 $data = C4::Circulation::checkHighHolds( $item, $patron );
169 is( $data->{exceeded}, 0, "Should not exceed threshold" );
170
171
172 # Place 7 more holds - patrons 5,6,7,8,9,10,11
173 for my $i ( 5 .. 11 ) {
174     my $patron = $patrons[$i];
175     my $hold   = Koha::Hold->new(
176         {
177             borrowernumber => $patron->id,
178             biblionumber   => $biblio->id,
179             branchcode     => $library->{branchcode},
180         }
181     )->store();
182 }
183
184 # Note in counts below, patron's own hold is not counted
185
186 # 12 holds, threshold is 1 over 10 holdable items = 11
187 $data = C4::Circulation::checkHighHolds( $item, $patron );
188 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
189 is( $data->{outstanding}, 12, "Should exceed threshold of 1" );
190
191 # 12 holds, threshold is 2 over 10 holdable items = 12 (equal is okay)
192 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
193 $data = C4::Circulation::checkHighHolds( $item, $patron );
194 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
195
196 my $unholdable = pop(@items);
197 $unholdable->damaged(-1);
198 $unholdable->store();
199
200 # 12 holds, threshold is 2 over 9 holdable items = 11
201 $data = C4::Circulation::checkHighHolds( $item, $patron );
202 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
203
204 $unholdable->damaged(0);
205 $unholdable->itemlost(-1);
206 $unholdable->store();
207
208 # 12 holds, threshold is 2 over 9 holdable items = 11
209 $data = C4::Circulation::checkHighHolds( $item, $patron );
210 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
211
212 $unholdable->itemlost(0);
213 $unholdable->notforloan(-1);
214 $unholdable->store();
215
216 # 12 holds, threshold is 2 over 9 holdable items = 11
217 $data = C4::Circulation::checkHighHolds( $item, $patron );
218 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
219
220 $unholdable->notforloan(0);
221 $unholdable->withdrawn(-1);
222 $unholdable->store();
223
224 # 12 holds, threshold is 2 over 9 holdable items = 11
225 $data = C4::Circulation::checkHighHolds( $item, $patron );
226 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
227
228 $patron_hold->found('F')->store;
229 # 11 holds, threshold is 2 over 9 holdable items = 11
230 $data = C4::Circulation::checkHighHolds( $item, $patron );
231 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
232 $patron_hold->found(undef)->store;
233
234 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
235
236 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode );
237 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
238
239 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
240 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
241
242 Koha::CirculationRules->set_rule(
243     {
244         branchcode   => undef,
245         categorycode => undef,
246         itemtype     => $item->itype,
247         rule_name    => 'decreaseloanholds',
248         rule_value   => 2,
249     }
250 );
251
252 $data = C4::Circulation::checkHighHolds( $item, $patron );
253 is( $data->{duration}, 2, "Circulation rules override system preferences" );
254
255
256 subtest "Test patron's own holds do not count towards HighHolds count" => sub {
257
258     plan tests => 2;
259
260     my $item = $builder->build_sample_item();
261     my $item2 = $builder->build_sample_item({ biblionumber => $item->biblionumber });
262     my $item3 = $builder->build_sample_item({ biblionumber => $item->biblionumber });
263
264     my $patron = $builder->build_object({
265         class => 'Koha::Patrons',
266         value => {
267             branchcode => $item->homebranch
268         }
269     });
270     my $hold = $builder->build_object({
271         class => 'Koha::Holds',
272         value => {
273             biblionumber => $item->biblionumber,
274             borrowernumber => $patron->id,
275             suspend => 0,
276             found => undef
277         }
278     });
279
280     Koha::CirculationRules->set_rules(
281         {
282             branchcode   => $item->homebranch,
283             categorycode => undef,
284             itemtype     => $item->itype,
285             rules        => {
286                 issuelength     => '14',
287                 lengthunit      => 'days',
288                 reservesallowed => '99',
289                 holds_per_record => '1',
290             }
291         }
292     );
293
294     t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
295     t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
296     t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
297     t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
298     t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
299
300     my $data = C4::Circulation::checkHighHolds( $item , $patron );
301     ok( !$data->{exceeded}, "Patron's hold on the record does not limit their own circulation if static decrease");
302     t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'dynamic' );
303     # 3 items on record, patron has 1 hold
304     $data = C4::Circulation::checkHighHolds( $item, $patron );
305     ok( !$data->{exceeded}, "Patron's hold on the record does not limit their own circulation if dynamic decrease");
306
307 };
308
309 $schema->storage->txn_rollback();