Bug 34178: (QA follow-up) Tidy
[koha.git] / t / db_dependent / Holds / DisallowHoldIfItemsAvailable.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use C4::Circulation qw( AddIssue AddReturn );
7 use C4::Items;
8 use Koha::Items;
9 use Koha::Cache::Memory::Lite;
10 use Koha::CirculationRules;
11
12 use Test::More tests => 13;
13
14 use t::lib::TestBuilder;
15 use t::lib::Mocks;
16
17 BEGIN {
18     use_ok( 'C4::Reserves', qw( ItemsAnyAvailableAndNotRestricted IsAvailableForItemLevelRequest ) );
19 }
20
21 my $schema = Koha::Database->schema;
22 $schema->storage->txn_begin;
23 my $dbh   = C4::Context->dbh;
24 my $cache = Koha::Cache::Memory::Lite->get_instance();
25
26 my $builder = t::lib::TestBuilder->new;
27
28 my $library1 = $builder->build( { source => 'Branch', } );
29 my $library2 = $builder->build( { source => 'Branch', } );
30 my $itemtype = $builder->build(
31     {
32         source => 'Itemtype',
33         value  => { notforloan => 0 }
34     }
35 )->{itemtype};
36
37 t::lib::Mocks::mock_userenv( { branchcode => $library1->{branchcode} } );
38
39 my $patron1 = $builder->build_object(
40     {
41         class => 'Koha::Patrons',
42         value => {
43             branchcode => $library1->{branchcode},
44             dateexpiry => '3000-01-01',
45         }
46     }
47 );
48 my $borrower1 = $patron1->unblessed;
49
50 my $patron2 = $builder->build_object(
51     {
52         class => 'Koha::Patrons',
53         value => {
54             branchcode => $library1->{branchcode},
55             dateexpiry => '3000-01-01',
56         }
57     }
58 );
59
60 my $patron3 = $builder->build_object(
61     {
62         class => 'Koha::Patrons',
63         value => {
64             branchcode => $library2->{branchcode},
65             dateexpiry => '3000-01-01',
66         }
67     }
68 );
69
70 my $library_A = $library1->{branchcode};
71 my $library_B = $library2->{branchcode};
72
73 my $biblio       = $builder->build_sample_biblio( { itemtype => $itemtype } );
74 my $biblionumber = $biblio->biblionumber;
75 my $item1        = $builder->build_sample_item(
76     {
77         biblionumber  => $biblionumber,
78         itype         => $itemtype,
79         homebranch    => $library_A,
80         holdingbranch => $library_A
81     }
82 );
83 my $item2 = $builder->build_sample_item(
84     {
85         biblionumber  => $biblionumber,
86         itype         => $itemtype,
87         homebranch    => $library_A,
88         holdingbranch => $library_A
89     }
90 );
91
92 # Test hold_fulfillment_policy
93 $dbh->do("DELETE FROM circulation_rules");
94 Koha::CirculationRules->set_rules(
95     {
96         categorycode => undef,
97         itemtype     => $itemtype,
98         branchcode   => undef,
99         rules        => {
100             issuelength     => 7,
101             lengthunit      => 8,
102             reservesallowed => 99,
103             onshelfholds    => 2,
104         }
105     }
106 );
107
108 my $is;
109
110 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } );
111 is( $is, 1, "Items availability: both of 2 items are available" );
112
113 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
114 is( $is, 0, "Item cannot be held, 2 items available" );
115
116 my $issue1 = AddIssue( $patron2->unblessed, $item1->barcode );
117
118 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } );
119 is( $is, 1, "Items availability: one item is available" );
120
121 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
122 is( $is, 0, "Item cannot be held, 1 item available" );
123
124 AddIssue( $patron2->unblessed, $item2->barcode );
125 $cache->flush();
126
127 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } );
128 is( $is, 0, "Items availability: none of items are available" );
129
130 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
131 is( $is, 1, "Item can be held, no items available" );
132
133 AddReturn( $item1->barcode );
134
135 {    # Remove the issue for the first patron, and modify the branch for item1
136     subtest 'IsAvailableForItemLevelRequest behaviours depending on ReservesControlBranch + holdallowed' => sub {
137         plan tests => 2;
138
139         my $hold_allowed_from_home_library  = 'from_home_library';
140         my $hold_allowed_from_any_libraries = 'from_any_library';
141
142         subtest 'Item is available at a different library' => sub {
143             plan tests => 13;
144
145             $item1->set( { homebranch => $library_B, holdingbranch => $library_B } )->store;
146
147             #Scenario is:
148             #One shelf holds is 'If all unavailable'/2
149             #Item 1 homebranch library B is available
150             #Item 2 homebranch library A is checked out
151             #Borrower1 is from library A
152
153             {
154                 set_holdallowed_rule($hold_allowed_from_home_library);
155
156                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
157
158                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
159                     ;    # patron1 in library A, library A 0 items, library B 1 item
160                 is(
161                     $is, 0,
162                     "Items availability: hold allowed from home + ReservesControlBranch=ItemHomeLibrary + one item is available at different library"
163                 );
164
165                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
166                 is(
167                     $is, 1,
168                     "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
169                         . "One item is available at different library, not holdable = none available => the hold is allowed at item level"
170                 );
171                 $is = IsAvailableForItemLevelRequest( $item1, $patron2 );
172                 is(
173                     $is, 1,
174                     "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
175                         . "One item is available at home library, holdable = one available => the hold is not allowed at item level"
176                 );
177                 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_B );
178
179                 #Adding a rule for the item's home library affects the availability for a borrower from another library because ReservesControlBranch is set to ItemHomeLibrary
180                 $cache->flush();
181
182                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
183                     ;    # patron1 in library A, library A 0 items, library B 1 item
184                 is(
185                     $is, 1,
186                     "Items availability: hold allowed from any library for library B + ReservesControlBranch=ItemHomeLibrary + one item is available at different library"
187                 );
188
189                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
190                 is(
191                     $is, 0,
192                     "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
193                         . "One item is available at different library, holdable = one available => the hold is not allowed at item level"
194                 );
195
196                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
197                 $cache->flush();
198
199                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
200                     ;    # patron1 in library A, library A 0 items, library B 1 item
201                 is(
202                     $is, 0,
203                     "Items availability: hold allowed from any library for library B + ReservesControlBranch=PatronLibrary + one item is available at different library"
204                 );
205
206                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
207                 is(
208                     $is, 1,
209                     "Hold allowed from home library + ReservesControlBranch=PatronLibrary, "
210                         . "One item is available at different library, not holdable = none available => the hold is allowed at item level"
211                 );
212
213                 #Adding a rule for the patron's home library affects the availability for an item from another library because ReservesControlBranch is set to PatronLibrary
214                 set_holdallowed_rule( $hold_allowed_from_any_libraries, $library_A );
215                 $cache->flush();
216
217                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
218                     ;    # patron1 in library A, library A 0 items, library B 1 item
219                 is(
220                     $is, 1,
221                     "Items availability: hold allowed from any library for library A + ReservesControlBranch=PatronLibrary + one item is available at different library"
222                 );
223
224                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
225                 is(
226                     $is, 0,
227                     "Hold allowed from home library + ReservesControlBranch=PatronLibrary, "
228                         . "One item is available at different library, holdable = one available => the hold is not allowed at item level"
229                 );
230             }
231
232             {
233                 set_holdallowed_rule($hold_allowed_from_any_libraries);
234
235                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
236                 $cache->flush();
237
238                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
239                     ;    # patron1 in library A, library A 0 items, library B 1 item
240                 is(
241                     $is, 1,
242                     "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at different library"
243                 );
244
245                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
246                 is(
247                     $is, 0,
248                     "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, "
249                         . "One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level"
250                 );
251
252                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
253                 $cache->flush();
254
255                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
256                     ;    # patron1 in library A, library A 0 items, library B 1 item
257                 is(
258                     $is, 1,
259                     "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at different library"
260                 );
261
262                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
263                 is(
264                     $is, 0,
265                     "Hold allowed from any library + ReservesControlBranch=PatronLibrary, "
266                         . "One item is available at the diff library, holdable = 1 available => the hold is not allowed at item level"
267                 );
268             }
269         };
270
271         subtest 'Item is available at the same library' => sub {
272             plan tests => 8;
273
274             $item1->set( { homebranch => $library_A, holdingbranch => $library_A } )->store;
275
276             #Scenario is:
277             #One shelf holds is 'If all unavailable'/2
278             #Item 1 homebranch library A is available
279             #Item 2 homebranch library A is checked out
280             #Borrower1 is from library A
281             #CircControl has no effect - same rule for all branches as set at line 96
282             #ReservesControlBranch is not checked in these subs we are testing?
283
284             {
285                 set_holdallowed_rule($hold_allowed_from_home_library);
286
287                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
288
289                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
290                     ;    # patron1 in library A, library A 1 item
291                 is(
292                     $is, 1,
293                     "Items availability: hold allowed from home library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library"
294                 );
295
296                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
297                 is(
298                     $is, 0,
299                     "Hold allowed from home library + ReservesControlBranch=ItemHomeLibrary, "
300                         . "One item is available at the same library, holdable = 1 available  => the hold is not allowed at item level"
301                 );
302
303                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
304
305                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
306                     ;    # patron1 in library A, library A 1 item
307                 is(
308                     $is, 1,
309                     "Items availability: hold allowed from home library + ReservesControlBranch=PatronLibrary + one item is available at home library"
310                 );
311
312                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
313                 is(
314                     $is, 0,
315                     "Hold allowed from home library + ReservesControlBranch=PatronLibrary, "
316                         . "One item is available at the same library, holdable = 1 available  => the hold is not allowed at item level"
317                 );
318             }
319
320             {
321                 set_holdallowed_rule($hold_allowed_from_any_libraries);
322
323                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
324
325                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
326                     ;    # patron1 in library A, library A 1 item
327                 is(
328                     $is, 1,
329                     "Items availability: hold allowed from any library + ReservesControlBranch=ItemHomeLibrary + one item is available at home library"
330                 );
331
332                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
333                 is(
334                     $is, 0,
335                     "Hold allowed from any library + ReservesControlBranch=ItemHomeLibrary, "
336                         . "One item is available at the same library, holdable = 1 available => the hold is not allowed at item level"
337                 );
338
339                 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
340
341                 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
342                     ;    # patron1 in library A, library A 1 item
343                 is(
344                     $is, 1,
345                     "Items availability: hold allowed from any library + ReservesControlBranch=PatronLibrary + one item is available at home library"
346                 );
347
348                 $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
349                 is(
350                     $is, 0,
351                     "Hold allowed from any library + ReservesControlBranch=PatronLibrary, "
352                         . "One item is available at the same library, holdable = 1 available  => the hold is not allowed at item level"
353                 );
354             }
355         };
356     };
357 }
358
359 my $itemtype2 = $builder->build(
360     {
361         source => 'Itemtype',
362         value  => { notforloan => 0 }
363     }
364 )->{itemtype};
365 my $item3 = $builder->build_sample_item( { itype => $itemtype2 } );
366
367 my $hold = $builder->build(
368     {
369         source => 'Reserve',
370         value  => {
371             itemnumber => $item3->itemnumber,
372             found      => 'T'
373         }
374     }
375 );
376
377 Koha::CirculationRules->set_rules(
378     {
379         categorycode => undef,
380         itemtype     => $itemtype2,
381         branchcode   => undef,
382         rules        => {
383             maxissueqty  => 99,
384             onshelfholds => 0,
385         }
386     }
387 );
388
389 $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblionumber, patron => $patron1 } )
390     ;    # patron1 in library A, library A 1 item
391 is( $is, 1, "Items availability: 1 item is available, 1 item held in T" );
392
393 $is = IsAvailableForItemLevelRequest( $item3, $patron1 );
394 is( $is, 1, "Item can be held, items in transit are not available" );
395
396 subtest 'Check holds availability with different item types' => sub {
397     plan tests => 6;
398
399     # Check for holds availability when different item types have different
400     # smart rules assigned both with "if all unavailable" set,
401     # and $itemtype rule allows holds, $itemtype2 rule disallows holds.
402     # So, $item should be available for hold when checked out even if $item2
403     # is not checked out, because anyway $item2 unavailable for holds by rule
404     # (Bug 24683):
405
406     my $biblio2 = $builder->build_sample_biblio( { itemtype => $itemtype } );
407     my $item4   = $builder->build_sample_item(
408         {
409             biblionumber  => $biblio2->biblionumber,
410             itype         => $itemtype,
411             homebranch    => $library_A,
412             holdingbranch => $library_A
413         }
414     );
415     my $item5 = $builder->build_sample_item(
416         {
417             biblionumber  => $biblio2->biblionumber,
418             itype         => $itemtype2,
419             homebranch    => $library_A,
420             holdingbranch => $library_A
421         }
422     );
423
424     # Test hold_fulfillment_policy
425     $dbh->do("DELETE FROM circulation_rules");
426     Koha::CirculationRules->set_rules(
427         {
428             categorycode => undef,
429             itemtype     => $itemtype,
430             branchcode   => undef,
431             rules        => {
432                 issuelength      => 7,
433                 lengthunit       => 8,
434                 reservesallowed  => 99,
435                 holds_per_record => 99,
436                 onshelfholds     => 2,
437             }
438         }
439     );
440     Koha::CirculationRules->set_rules(
441         {
442             categorycode => undef,
443             itemtype     => $itemtype2,
444             branchcode   => undef,
445             rules        => {
446                 issuelength      => 7,
447                 lengthunit       => 8,
448                 reservesallowed  => 0,
449                 holds_per_record => 0,
450                 onshelfholds     => 2,
451             }
452         }
453     );
454
455     $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblio2->biblionumber, patron => $patron1 } );
456     is(
457         $is, 1,
458         "Items availability: 2 items, one allowed by smart rule but not checked out, another one not allowed to be held by smart rule"
459     );
460
461     $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
462     is( $is, 0, "Item4 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
463
464     $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
465     is( $is, 0, "Item5 cannot be requested to hold: 2 items, Item4 available, Item5 restricted" );
466
467     AddIssue( $patron2->unblessed, $item4->barcode );
468     $cache->flush();
469
470     $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblio2->biblionumber, patron => $patron1 } );
471     is(
472         $is, 0,
473         "Items availability: 2 items, one allowed by smart rule and checked out, another one not allowed to be held by smart rule"
474     );
475
476     $is = IsAvailableForItemLevelRequest( $item4, $patron1 );
477     is( $is, 1, "Item4 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
478
479     $is = IsAvailableForItemLevelRequest( $item5, $patron1 );
480
481     # Note: read IsAvailableForItemLevelRequest sub description about CanItemBeReserved/CanBookBeReserved:
482     is( $is, 1, "Item5 can be requested to hold, 2 items, Item4 checked out, Item5 restricted" );
483 };
484
485 subtest 'Check item checkout availability with ordered item' => sub {
486     plan tests => 1;
487
488     my $biblio2 = $builder->build_sample_biblio( { itemtype => $itemtype } );
489     my $item1   = $builder->build_sample_item(
490         {
491             biblionumber  => $biblio2->biblionumber,
492             itype         => $itemtype2,
493             homebranch    => $library_A,
494             holdingbranch => $library_A,
495             notforloan    => -1
496         }
497     );
498
499     $dbh->do("DELETE FROM circulation_rules");
500     Koha::CirculationRules->set_rules(
501         {
502             categorycode => undef,
503             itemtype     => $itemtype2,
504             branchcode   => undef,
505             rules        => {
506                 issuelength      => 7,
507                 lengthunit       => 8,
508                 reservesallowed  => 99,
509                 holds_per_record => 99,
510                 onshelfholds     => 2,
511             }
512         }
513     );
514     $cache->flush();
515
516     $is = ItemsAnyAvailableAndNotRestricted( { biblionumber => $biblio2->biblionumber, patron => $patron1 } );
517     is( $is, 0, "Ordered item cannot be checked out" );
518 };
519
520 subtest 'Check item availability for hold with ordered item' => sub {
521     plan tests => 1;
522
523     my $biblio2 = $builder->build_sample_biblio( { itemtype => $itemtype } );
524     my $item1   = $builder->build_sample_item(
525         {
526             biblionumber  => $biblio2->biblionumber,
527             itype         => $itemtype2,
528             homebranch    => $library_A,
529             holdingbranch => $library_A,
530             notforloan    => -1
531         }
532     );
533
534     $dbh->do("DELETE FROM circulation_rules");
535     Koha::CirculationRules->set_rules(
536         {
537             categorycode => undef,
538             itemtype     => $itemtype2,
539             branchcode   => undef,
540             rules        => {
541                 issuelength      => 7,
542                 lengthunit       => 8,
543                 reservesallowed  => 99,
544                 holds_per_record => 99,
545                 onshelfholds     => 2,
546             }
547         }
548     );
549
550     $cache->flush();
551     $is = IsAvailableForItemLevelRequest( $item1, $patron1 );
552     is( $is, 1, "Ordered items are available for hold" );
553 };
554
555 # Cleanup
556 $schema->storage->txn_rollback;
557
558 sub set_holdallowed_rule {
559     my ( $holdallowed, $branchcode ) = @_;
560     Koha::CirculationRules->set_rules(
561         {
562             branchcode => $branchcode || undef,
563             itemtype   => undef,
564             rules      => {
565                 holdallowed             => $holdallowed,
566                 hold_fulfillment_policy => 'any',
567                 returnbranch            => 'homebranch',
568             }
569         }
570     );
571 }