Bug 22284: (follow-up) Squash multiple follow-ups
[koha.git] / t / db_dependent / Koha / Items.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 10;
23 use Test::Exception;
24
25 use C4::Circulation;
26 use C4::Context;
27 use Koha::Item;
28 use Koha::Item::Transfer::Limits;
29 use Koha::Items;
30 use Koha::Database;
31
32 use t::lib::TestBuilder;
33 use t::lib::Mocks;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $dbh     = C4::Context->dbh;
39
40 my $builder     = t::lib::TestBuilder->new;
41 my $library     = $builder->build( { source => 'Branch' } );
42 my $nb_of_items = Koha::Items->search->count;
43 my $biblio      = $builder->build_sample_biblio();
44 my $new_item_1   = $builder->build_sample_item({
45     biblionumber => $biblio->biblionumber,
46     homebranch       => $library->{branchcode},
47     holdingbranch    => $library->{branchcode},
48 });
49 my $new_item_2   = $builder->build_sample_item({
50     biblionumber => $biblio->biblionumber,
51     homebranch       => $library->{branchcode},
52     holdingbranch    => $library->{branchcode},
53 });
54
55
56 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
57
58 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
59 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
60
61 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
62 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
63
64 subtest 'get_transfer' => sub {
65     plan tests => 3;
66
67     my $transfer = $new_item_1->get_transfer();
68     is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
69
70     my $library_to = $builder->build( { source => 'Branch' } );
71
72     C4::Circulation::transferbook( $library_to->{branchcode}, $new_item_1->barcode );
73
74     $transfer = $new_item_1->get_transfer();
75     is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
76
77     is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer should return a valid Koha::Item::Transfers object' );
78 };
79
80 subtest 'holds' => sub {
81     plan tests => 5;
82
83     my $biblio = $builder->build_sample_biblio();
84     my $item   = $builder->build_sample_item({
85         biblionumber => $biblio->biblionumber,
86     });
87     $nb_of_items++;
88     is($item->holds->count, 0, "Nothing returned if no holds");
89     my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
90     my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
91     my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
92
93     is($item->holds()->count,3,"Three holds found");
94     is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
95     is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
96     is($item->holds({found => undef})->count, 0,"Nothing returned if no matching holds");
97 };
98
99 subtest 'biblio' => sub {
100     plan tests => 2;
101
102     my $biblio = $retrieved_item_1->biblio;
103     is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
104     is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
105 };
106
107 subtest 'biblioitem' => sub {
108     plan tests => 2;
109
110     my $biblioitem = $retrieved_item_1->biblioitem;
111     is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
112     is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
113 };
114
115 subtest 'checkout' => sub {
116     plan tests => 5;
117     my $item = Koha::Items->find( $new_item_1->itemnumber );
118     # No checkout yet
119     my $checkout = $item->checkout;
120     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
121
122     # Add a checkout
123     my $patron = $builder->build({ source => 'Borrower' });
124     C4::Circulation::AddIssue( $patron, $item->barcode );
125     $checkout = $retrieved_item_1->checkout;
126     is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
127     is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
128     is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
129
130     # Do the return
131     C4::Circulation::AddReturn( $item->barcode );
132
133     # There is no more checkout on this item, making sure it will not return old checkouts
134     $checkout = $item->checkout;
135     is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
136 };
137
138 subtest 'can_be_transferred' => sub {
139     plan tests => 5;
140
141     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
142     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
143
144     my $biblio   = $builder->build_sample_biblio();
145     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
146     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
147     my $item  = $builder->build_sample_item({
148         biblionumber     => $biblio->biblionumber,
149         homebranch       => $library1->branchcode,
150         holdingbranch    => $library1->branchcode,
151     });
152     $nb_of_items++;
153
154     is(Koha::Item::Transfer::Limits->search({
155         fromBranch => $library1->branchcode,
156         toBranch => $library2->branchcode,
157     })->count, 0, 'There are no transfer limits between libraries.');
158     ok($item->can_be_transferred({ to => $library2 }),
159        'Item can be transferred between libraries.');
160
161     my $limit = Koha::Item::Transfer::Limit->new({
162         fromBranch => $library1->branchcode,
163         toBranch => $library2->branchcode,
164         itemtype => $item->effective_itemtype,
165     })->store;
166     is(Koha::Item::Transfer::Limits->search({
167         fromBranch => $library1->branchcode,
168         toBranch => $library2->branchcode,
169     })->count, 1, 'Given we have added a transfer limit,');
170     is($item->can_be_transferred({ to => $library2 }), 0,
171        'Item can no longer be transferred between libraries.');
172     is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
173        'We get the same result also if we pass the from-library parameter.');
174 };
175
176 $retrieved_item_1->delete;
177 is( Koha::Items->search->count, $nb_of_items + 1, 'Delete should have deleted the item' );
178
179 $schema->storage->txn_rollback;
180
181 subtest 'pickup_locations' => sub {
182     plan tests => 33;
183
184     $schema->storage->txn_begin;
185
186     # Cleanup database
187     Koha::Holds->search->delete;
188     Koha::Patrons->search->delete;
189     Koha::Items->search->delete;
190     Koha::Libraries->search->delete;
191     $dbh->do('DELETE FROM issues');
192     $dbh->do('DELETE FROM issuingrules');
193     $dbh->do(
194         q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
195         VALUES (?, ?, ?, ?)},
196         {},
197         '*', '*', '*', 25
198     );
199     $dbh->do('DELETE FROM circulation_rules');
200
201     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
202     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
203
204     my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
205     my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
206     my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
207     my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
208
209     my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
210     my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
211
212     my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
213     my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
214
215     my $biblioitem  = $builder->build( { source => 'Biblioitem' } );
216
217     my $item1  = Koha::Item->new({
218         biblionumber     => $biblioitem->{biblionumber},
219         biblioitemnumber => $biblioitem->{biblioitemnumber},
220         homebranch       => $library1->branchcode,
221         holdingbranch    => $library2->branchcode,
222         itype            => 'test',
223         barcode          => "item1barcode",
224     })->store;
225
226     my $item3  = Koha::Item->new({
227         biblionumber     => $biblioitem->{biblionumber},
228         biblioitemnumber => $biblioitem->{biblioitemnumber},
229         homebranch       => $library3->branchcode,
230         holdingbranch    => $library4->branchcode,
231         itype            => 'test',
232         barcode          => "item3barcode",
233     })->store;
234
235     my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode } } );
236     my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode } } );
237
238     t::lib::Mocks::mock_preference('HomeOrHoldingBranch', 'homebranch');
239
240     #Case 1: holdallowed any, hold_fulfillment_policy any
241     Koha::CirculationRules->set_rules(
242         {
243             branchcode => undef,
244             itemtype   => undef,
245             categorycode => undef,
246             rules => {
247                 holdallowed => 2,
248                 hold_fulfillment_policy => 'any',
249                 returnbranch => 'any'
250             }
251         }
252     );
253
254     my @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
255     my @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
256     my @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
257     my @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
258
259     ok(scalar(@pl_1_1) == scalar(@pl_1_4) && scalar(@pl_1_1) == scalar(@pl_3_1) && scalar(@pl_1_1) == scalar(@pl_3_4), 'All combinations of patron/item renders the same number of locations');
260
261     #Case 2: holdallowed homebranch, hold_fulfillment_policy any, HomeOrHoldingBranch 'homebranch'
262     Koha::CirculationRules->set_rules(
263         {
264             branchcode => undef,
265             itemtype   => undef,
266             categorycode => undef,
267             rules => {
268                 holdallowed => 1,
269                 hold_fulfillment_policy => 'any',
270                 returnbranch => 'any'
271             }
272         }
273     );
274
275     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
276     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
277     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
278     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
279
280     ok(scalar(@pl_1_1) == 3, 'Pickup location for patron 1 and item 1 renders all libraries that are pickup_locations');
281     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_3_4) == 0, 'Any other combination renders no locations');
282
283     #Case 3: holdallowed holdgroup, hold_fulfillment_policy any
284     Koha::CirculationRules->set_rules(
285         {
286             branchcode => undef,
287             itemtype   => undef,
288             categorycode => undef,
289             rules => {
290                 holdallowed => 3,
291                 hold_fulfillment_policy => 'any',
292                 returnbranch => 'any'
293             }
294         }
295     );
296
297     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
298     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
299     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
300     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
301
302     ok(scalar(@pl_1_1) == 3, 'Pickup location for patron 1 and item 1 renders all libraries that are pickup_locations');
303     ok(scalar(@pl_3_4) == 3, 'Pickup location for patron 4 and item 3 renders all libraries that are pickup_locations');
304     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0, 'Any other combination renders no locations');
305
306     #Case 4: holdallowed any, hold_fulfillment_policy holdgroup
307     Koha::CirculationRules->set_rules(
308         {
309             branchcode => undef,
310             itemtype   => undef,
311             categorycode => undef,
312             rules => {
313                 holdallowed => 2,
314                 hold_fulfillment_policy => 'holdgroup',
315                 returnbranch => 'any'
316             }
317         }
318     );
319
320     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
321     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
322     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
323     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
324
325     ok(scalar(@pl_1_1) == 2 && scalar(@pl_1_4) == 2, 'Pickup locations for item 1 renders all libraries in items\'s holdgroup that are pickup_locations');
326     ok(scalar(@pl_3_1) == 1 && scalar(@pl_3_4) == 1, 'Pickup locations for item 3 renders all libraries in items\'s holdgroup that are pickup_locations');
327
328     #Case 5: holdallowed homebranch, hold_fulfillment_policy holdgroup, HomeOrHoldingBranch 'homebranch'
329     Koha::CirculationRules->set_rules(
330         {
331             branchcode => undef,
332             itemtype   => undef,
333             categorycode => undef,
334             rules => {
335                 holdallowed => 1,
336                 hold_fulfillment_policy => 'holdgroup',
337                 returnbranch => 'any'
338             }
339         }
340     );
341
342     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
343     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
344     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
345     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
346
347     ok(scalar(@pl_1_1) == 2, 'Pickup location for patron 1 and item 1 renders all libraries in holdgroup that are pickup_locations');
348     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_3_4) == 0, 'Any other combination renders no locations');
349
350     #Case 6: holdallowed holdgroup, hold_fulfillment_policy holdgroup
351     Koha::CirculationRules->set_rules(
352         {
353             branchcode => undef,
354             itemtype   => undef,
355             categorycode => undef,
356             rules => {
357                 holdallowed => 3,
358                 hold_fulfillment_policy => 'holdgroup',
359                 returnbranch => 'any'
360             }
361         }
362     );
363
364     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
365     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
366     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
367     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
368
369     ok(scalar(@pl_1_1) == 2, 'Pickup location for patron 1 and item 1 renders all libraries that are pickup_locations');
370     ok(scalar(@pl_3_4) == 1, 'Pickup location for patron 4 and item 3 renders all libraries that are pickup_locations');
371     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0, 'Any other combination renders no locations');
372
373     #Case 7: holdallowed any, hold_fulfillment_policy homebranch
374     Koha::CirculationRules->set_rules(
375         {
376             branchcode => undef,
377             itemtype   => undef,
378             categorycode => undef,
379             rules => {
380                 holdallowed => 2,
381                 hold_fulfillment_policy => 'homebranch',
382                 returnbranch => 'any'
383             }
384         }
385     );
386
387     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
388     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
389     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
390     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
391
392     ok(scalar(@pl_1_1) == 1 && scalar(@pl_1_4) == 1 && $pl_1_1[0]->{branchcode} eq $library1->branchcode && $pl_1_4[0]->{branchcode} eq $library1->id, 'Pickup locations for item 1 renders item\'s homelibrary');
393     ok(scalar(@pl_3_1) == 0 && scalar(@pl_3_4) == 0, 'Any other combination renders no locations, because library3 is not pickup_location');
394
395     #Case 8: holdallowed homebranch, hold_fulfillment_policy homebranch, HomeOrHoldingBranch 'homebranch'
396     Koha::CirculationRules->set_rules(
397         {
398             branchcode => undef,
399             itemtype   => undef,
400             categorycode => undef,
401             rules => {
402                 holdallowed => 1,
403                 hold_fulfillment_policy => 'homebranch',
404                 returnbranch => 'any'
405             }
406         }
407     );
408
409     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
410     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
411     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
412     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
413
414     ok(scalar(@pl_1_1) == 1 && $pl_1_1[0]->{branchcode} eq $library1->branchcode, 'Pickup location for patron 1 and item 1 renders item\'s homebranch');
415     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_3_4) == 0, 'Any other combination renders no locations');
416
417     #Case 9: holdallowed holdgroup, hold_fulfillment_policy homebranch
418     Koha::CirculationRules->set_rules(
419         {
420             branchcode => undef,
421             itemtype   => undef,
422             categorycode => undef,
423             rules => {
424                 holdallowed => 3,
425                 hold_fulfillment_policy => 'homebranch',
426                 returnbranch => 'any'
427             }
428         }
429     );
430
431     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
432     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
433     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
434     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
435
436     ok(scalar(@pl_1_1) == 1, 'Pickup location for patron 1 and item 1 renders item\'s homebranch');
437     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_3_4) == 0, 'Any other combination renders no locations');
438
439     #Case 10: holdallowed any, hold_fulfillment_policy holdingbranch
440     Koha::CirculationRules->set_rules(
441         {
442             branchcode => undef,
443             itemtype   => undef,
444             categorycode => undef,
445             rules => {
446                 holdallowed => 2,
447                 hold_fulfillment_policy => 'holdingbranch',
448                 returnbranch => 'any'
449             }
450         }
451     );
452
453     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
454     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
455     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
456     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
457
458     ok(scalar(@pl_1_1) == 1 && scalar(@pl_1_4) == 1 && $pl_1_1[0]->{branchcode} eq $library2->branchcode && $pl_1_4[0]->{branchcode} eq $library2->branchcode, 'Pickup locations for item 1 renders item\'s holding branch');
459     ok(scalar(@pl_3_1) == 1 && scalar(@pl_3_4) == 1 && $pl_3_1[0]->{branchcode} eq $library4->branchcode && $pl_3_4[0]->{branchcode} eq $library4->branchcode, 'Pickup locations for item 3 renders item\'s holding branch');
460
461
462     #Case 11: holdallowed homebranch, hold_fulfillment_policy holdingbranch, HomeOrHoldingBranch 'homebranch'
463     Koha::CirculationRules->set_rules(
464         {
465             branchcode => undef,
466             itemtype   => undef,
467             categorycode => undef,
468             rules => {
469                 holdallowed => 1,
470                 hold_fulfillment_policy => 'holdingbranch',
471                 returnbranch => 'any'
472             }
473         }
474     );
475
476     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
477     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
478     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
479     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
480
481     ok(scalar(@pl_1_1) == 1 && $pl_1_1[0]->{branchcode} eq $library2->branchcode, 'Pickup location for patron 1 and item 1 renders item\'s holding branch');
482     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_3_4) == 0, 'Any other combination renders no locations');
483
484     #Case 12: holdallowed holdgroup, hold_fulfillment_policy holdingbranch
485     Koha::CirculationRules->set_rules(
486         {
487             branchcode => undef,
488             itemtype   => undef,
489             categorycode => undef,
490             rules => {
491                 holdallowed => 3,
492                 hold_fulfillment_policy => 'holdingbranch',
493                 returnbranch => 'any'
494             }
495         }
496     );
497
498     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
499     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
500     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
501     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
502
503     ok(scalar(@pl_1_1) == 1 && $pl_1_1[0]->{branchcode} eq $library2->branchcode, 'Pickup location for patron 1 and item 1 renders item\'s holding branch');
504     ok(scalar(@pl_3_4) == 1 && $pl_3_4[0]->{branchcode} eq $library4->branchcode, 'Pickup location for patron 4 and item 3 renders item\'s holding branch');
505     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0, 'Any other combination renders no locations');
506
507     t::lib::Mocks::mock_preference('HomeOrHoldingBranch', 'holdingbranch');
508
509     #Case 13: holdallowed homebranch, hold_fulfillment_policy any, HomeOrHoldingBranch 'holdingbranch'
510     Koha::CirculationRules->set_rules(
511         {
512             branchcode => undef,
513             itemtype   => undef,
514             categorycode => undef,
515             rules => {
516                 holdallowed => 1,
517                 hold_fulfillment_policy => 'any',
518                 returnbranch => 'any'
519             }
520         }
521     );
522
523     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
524     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
525     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
526     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
527
528     ok(scalar(@pl_3_4) == 3, 'Pickup location for patron 4 and item 3 renders all libraries that are pickup_locations');
529     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_1_1) == 0, 'Any other combination renders no locations');
530
531     #Case 14: holdallowed homebranch, hold_fulfillment_policy holdgroup, HomeOrHoldingBranch 'holdingbranch'
532     Koha::CirculationRules->set_rules(
533         {
534             branchcode => undef,
535             itemtype   => undef,
536             categorycode => undef,
537             rules => {
538                 holdallowed => 1,
539                 hold_fulfillment_policy => 'holdgroup',
540                 returnbranch => 'any'
541             }
542         }
543     );
544
545     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
546     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
547     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
548     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
549
550     ok(scalar(@pl_3_4) == 1, 'Pickup location for patron 4 and item 3 renders all libraries in holdgroup that are pickup_locations');
551     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_1_1) == 0, 'Any other combination renders no locations');
552
553     #Case 15: holdallowed homebranch, hold_fulfillment_policy homebranch, HomeOrHoldingBranch 'holdingbranch'
554     Koha::CirculationRules->set_rules(
555         {
556             branchcode => undef,
557             itemtype   => undef,
558             categorycode => undef,
559             rules => {
560                 holdallowed => 1,
561                 hold_fulfillment_policy => 'homebranch',
562                 returnbranch => 'any'
563             }
564         }
565     );
566
567     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
568     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
569     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
570     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
571
572     #ok(scalar(@pl_3_4) == 1 && $pl_3_4[0]->{branchcode} eq $library4->branchcode, 'Pickup location for patron 4 and item 3 renders item\'s holding branch');
573     ok(scalar(@pl_3_4) == 0 && scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_1_1) == 0, 'Any combination of patron/item renders no locations');
574
575     #Case 16: holdallowed homebranch, hold_fulfillment_policy holdingbranch, HomeOrHoldingBranch 'holdingbranch'
576     Koha::CirculationRules->set_rules(
577         {
578             branchcode => undef,
579             itemtype   => undef,
580             categorycode => undef,
581             rules => {
582                 holdallowed => 1,
583                 hold_fulfillment_policy => 'holdingbranch',
584                 returnbranch => 'any'
585             }
586         }
587     );
588
589     @pl_1_1 = $item1->pickup_locations( { patron => $patron1 } );
590     @pl_1_4 = $item1->pickup_locations( { patron => $patron4 } );
591     @pl_3_1 = $item3->pickup_locations( { patron => $patron1 } );
592     @pl_3_4 = $item3->pickup_locations( { patron => $patron4 } );
593
594     ok(scalar(@pl_3_4) == 1 && $pl_3_4[0]->{branchcode} eq $library4->branchcode, 'Pickup location for patron 1 and item 1 renders item\'s holding branch');
595     ok(scalar(@pl_1_4) == 0 && scalar(@pl_3_1) == 0 && scalar(@pl_1_1) == 0, 'Any other combination renders no locations');
596
597     $schema->storage->txn_rollback;
598 };