Bug 31080: Unit tests
[srvgit] / t / db_dependent / Koha / Item.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 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 use utf8;
22
23 use Test::More tests => 27;
24 use Test::Exception;
25 use Test::MockModule;
26
27 use C4::Biblio qw( GetMarcSubfieldStructure );
28 use C4::Circulation qw( AddIssue AddReturn );
29
30 use Koha::Caches;
31 use Koha::Items;
32 use Koha::Database;
33 use Koha::DateUtils qw( dt_from_string );
34 use Koha::Old::Items;
35 use Koha::Recalls;
36
37 use List::MoreUtils qw(all);
38
39 use t::lib::TestBuilder;
40 use t::lib::Mocks;
41 use t::lib::Dates;
42
43 my $schema  = Koha::Database->new->schema;
44 my $builder = t::lib::TestBuilder->new;
45
46 subtest 'return_claims relationship' => sub {
47     plan tests => 3;
48
49     $schema->storage->txn_begin;
50
51     my $biblio = $builder->build_sample_biblio();
52     my $item   = $builder->build_sample_item({
53         biblionumber => $biblio->biblionumber,
54     });
55     my $return_claims = $item->return_claims;
56     is( ref($return_claims), 'Koha::Checkouts::ReturnClaims', 'return_claims returns a Koha::Checkouts::ReturnClaims object set' );
57     is($item->return_claims->count, 0, "Empty Koha::Checkouts::ReturnClaims set returned if no return_claims");
58     my $claim1 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
59     my $claim2 = $builder->build({ source => 'ReturnClaim', value => { itemnumber => $item->itemnumber }});
60
61     is($item->return_claims()->count,2,"Two ReturnClaims found for item");
62
63     $schema->storage->txn_rollback;
64 };
65
66 subtest 'return_claim accessor' => sub {
67     plan tests => 5;
68
69     $schema->storage->txn_begin;
70
71     my $biblio = $builder->build_sample_biblio();
72     my $item   = $builder->build_sample_item({
73         biblionumber => $biblio->biblionumber,
74     });
75     my $return_claim = $item->return_claim;
76     is( $return_claim, undef, 'return_claim returned undefined if there are no claims for this item' );
77
78     my $claim1 = $builder->build_object(
79         {
80             class => 'Koha::Checkouts::ReturnClaims',
81             value => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 10 ) }
82         }
83     );
84     my $claim2 = $builder->build_object(
85         {
86             class => 'Koha::Checkouts::ReturnClaims',
87             value  => { itemnumber => $item->itemnumber, resolution => undef, created_on => dt_from_string()->subtract( minutes => 5 ) }
88         }
89     );
90
91     $return_claim = $item->return_claim;
92     is( ref($return_claim), 'Koha::Checkouts::ReturnClaim', 'return_claim returned a Koha::Checkouts::ReturnClaim object' );
93     is( $return_claim->id, $claim2->id, 'return_claim returns the most recent unresolved claim');
94
95     $claim2->resolution('test')->store();
96     $return_claim = $item->return_claim;
97     is( $return_claim->id, $claim1->id, 'return_claim returns the only unresolved claim');
98
99     $claim1->resolution('test')->store();
100     $return_claim = $item->return_claim;
101     is( $return_claim, undef, 'return_claim returned undefined if there are no active claims for this item' );
102
103     $schema->storage->txn_rollback;
104 };
105
106 subtest 'tracked_links relationship' => sub {
107     plan tests => 3;
108
109     my $biblio = $builder->build_sample_biblio();
110     my $item   = $builder->build_sample_item({
111         biblionumber => $biblio->biblionumber,
112     });
113     my $tracked_links = $item->tracked_links;
114     is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' );
115     is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links");
116     my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
117     my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }});
118
119     is($item->tracked_links()->count,2,"Two tracked links found");
120 };
121
122 subtest 'is_bundle tests' => sub {
123     plan tests => 2;
124
125     $schema->storage->txn_begin;
126
127     my $item   = $builder->build_sample_item();
128
129     my $is_bundle = $item->is_bundle;
130     is($is_bundle, 0, 'is_bundle returns 0 when there are no items attached');
131
132     my $item2 = $builder->build_sample_item();
133     $schema->resultset('ItemBundle')
134       ->create( { host => $item->itemnumber, item => $item2->itemnumber } );
135
136     $is_bundle = $item->is_bundle;
137     is($is_bundle, 1, 'is_bundle returns 1 when there is at least one item attached');
138
139     $schema->storage->txn_rollback;
140 };
141
142 subtest 'in_bundle tests' => sub {
143     plan tests => 2;
144
145     $schema->storage->txn_begin;
146
147     my $item   = $builder->build_sample_item();
148
149     my $in_bundle = $item->in_bundle;
150     is($in_bundle, 0, 'in_bundle returns 0 when the item is not in a bundle');
151
152     my $host_item = $builder->build_sample_item();
153     $schema->resultset('ItemBundle')
154       ->create( { host => $host_item->itemnumber, item => $item->itemnumber } );
155
156     $in_bundle = $item->in_bundle;
157     is($in_bundle, 1, 'in_bundle returns 1 when the item is in a bundle');
158
159     $schema->storage->txn_rollback;
160 };
161
162 subtest 'bundle_items tests' => sub {
163     plan tests => 3;
164
165     $schema->storage->txn_begin;
166
167     my $host_item = $builder->build_sample_item();
168     my $bundle_items = $host_item->bundle_items;
169     is( ref($bundle_items), 'Koha::Items',
170         'bundle_items returns a Koha::Items object set' );
171     is( $bundle_items->count, 0,
172         'bundle_items set is empty when no items are bundled' );
173
174     my $bundle_item1 = $builder->build_sample_item();
175     my $bundle_item2 = $builder->build_sample_item();
176     my $bundle_item3 = $builder->build_sample_item();
177     $schema->resultset('ItemBundle')
178       ->create(
179         { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
180     $schema->resultset('ItemBundle')
181       ->create(
182         { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
183     $schema->resultset('ItemBundle')
184       ->create(
185         { host => $host_item->itemnumber, item => $bundle_item3->itemnumber } );
186
187     $bundle_items = $host_item->bundle_items;
188     is( $bundle_items->count, 3,
189         'bundle_items returns all the bundled items in the set' );
190
191     $schema->storage->txn_rollback;
192 };
193
194 subtest 'bundle_host tests' => sub {
195     plan tests => 3;
196
197     $schema->storage->txn_begin;
198
199     my $host_item = $builder->build_sample_item();
200     my $bundle_item1 = $builder->build_sample_item();
201     my $bundle_item2 = $builder->build_sample_item();
202     $schema->resultset('ItemBundle')
203       ->create(
204         { host => $host_item->itemnumber, item => $bundle_item2->itemnumber } );
205
206     my $bundle_host = $bundle_item1->bundle_host;
207     is( $bundle_host, undef, 'bundle_host returns undefined when the item it not part of a bundle');
208     $bundle_host = $bundle_item2->bundle_host;
209     is( ref($bundle_host), 'Koha::Item', 'bundle_host returns a Koha::Item object when the item is in a bundle');
210     is( $bundle_host->id, $host_item->id, 'bundle_host returns the host item when called against an item in a bundle');
211
212     $schema->storage->txn_rollback;
213 };
214
215 subtest 'add_to_bundle tests' => sub {
216     plan tests => 6;
217
218     $schema->storage->txn_begin;
219
220     t::lib::Mocks::mock_preference( 'BundleNotLoanValue', 1 );
221
222     my $host_item = $builder->build_sample_item();
223     my $bundle_item1 = $builder->build_sample_item();
224     my $bundle_item2 = $builder->build_sample_item();
225
226     throws_ok { $host_item->add_to_bundle($host_item) }
227     'Koha::Exceptions::Item::Bundle::IsBundle',
228       'Exception thrown if you try to add the item to itself';
229
230     ok($host_item->add_to_bundle($bundle_item1), 'bundle_item1 added to bundle');
231     is($bundle_item1->notforloan, 1, 'add_to_bundle sets notforloan to BundleNotLoanValue');
232
233     throws_ok { $host_item->add_to_bundle($bundle_item1) }
234     'Koha::Exceptions::Object::DuplicateID',
235       'Exception thrown if you try to add the same item twice';
236
237     throws_ok { $bundle_item1->add_to_bundle($bundle_item2) }
238     'Koha::Exceptions::Item::Bundle::IsBundle',
239       'Exception thrown if you try to add an item to a bundled item';
240
241     throws_ok { $bundle_item2->add_to_bundle($host_item) }
242     'Koha::Exceptions::Item::Bundle::IsBundle',
243       'Exception thrown if you try to add a bundle host to a bundle item';
244
245     $schema->storage->txn_rollback;
246 };
247
248 subtest 'remove_from_bundle tests' => sub {
249     plan tests => 3;
250
251     $schema->storage->txn_begin;
252
253     my $host_item = $builder->build_sample_item();
254     my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
255     $schema->resultset('ItemBundle')
256       ->create(
257         { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
258
259     is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
260     is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
261     $bundle_item1 = $bundle_item1->get_from_storage;
262     is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
263
264     $schema->storage->txn_rollback;
265 };
266
267 subtest 'hidden_in_opac() tests' => sub {
268
269     plan tests => 4;
270
271     $schema->storage->txn_begin;
272
273     my $item  = $builder->build_sample_item({ itemlost => 2 });
274     my $rules = {};
275
276     # disable hidelostitems as it interteres with OpachiddenItems for the calculation
277     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
278
279     ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
280     ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
281
282     # enable hidelostitems to verify correct behaviour
283     t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
284     ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
285
286     # disable hidelostitems
287     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
288     my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
289
290     $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
291
292     ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
293
294
295
296     $schema->storage->txn_rollback;
297 };
298
299 subtest 'has_pending_hold() tests' => sub {
300
301     plan tests => 2;
302
303     $schema->storage->txn_begin;
304
305     my $dbh = C4::Context->dbh;
306     my $item  = $builder->build_sample_item({ itemlost => 0 });
307     my $itemnumber = $item->itemnumber;
308
309     $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
310     ok( $item->has_pending_hold, "Yes, we have a pending hold");
311     $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
312     ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
313
314     $schema->storage->txn_rollback;
315 };
316
317 subtest "as_marc_field() tests" => sub {
318
319     my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
320     my ( $itemtag, $itemtagsubfield) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
321
322     my @schema_columns = $schema->resultset('Item')->result_source->columns;
323     my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
324
325     plan tests => 2 * (scalar @mapped_columns + 1) + 4;
326
327     $schema->storage->txn_begin;
328
329     my $item = $builder->build_sample_item;
330     # Make sure it has at least one undefined attribute
331     $item->set({ replacementprice => undef })->store->discard_changes;
332
333     # Tests with the mss parameter
334     my $marc_field = $item->as_marc_field({ mss => $mss });
335
336     is(
337         $marc_field->tag,
338         $itemtag,
339         'Generated field set the right tag number'
340     );
341
342     foreach my $column ( @mapped_columns ) {
343         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
344         is( $marc_field->subfield($tagsubfield),
345             $item->$column, "Value is mapped correctly for column $column" );
346     }
347
348     # Tests without the mss parameter
349     $marc_field = $item->as_marc_field();
350
351     is(
352         $marc_field->tag,
353         $itemtag,
354         'Generated field set the right tag number'
355     );
356
357     foreach my $column (@mapped_columns) {
358         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
359         is( $marc_field->subfield($tagsubfield),
360             $item->$column, "Value is mapped correctly for column $column" );
361     }
362
363     my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
364         {
365             frameworkcode => '',
366             tagfield      => $itemtag,
367             tagsubfield   => 'X',
368         }
369     )->store;
370     Koha::MarcSubfieldStructure->new(
371         {
372             frameworkcode => '',
373             tagfield      => $itemtag,
374             tagsubfield   => 'Y',
375             kohafield     => '',
376         }
377     )->store;
378
379     my @unlinked_subfields;
380     push @unlinked_subfields, X => 'Something weird', Y => 'Something else';
381     $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
382
383     Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
384     Koha::MarcSubfieldStructures->search(
385         { frameworkcode => '', tagfield => $itemtag } )
386       ->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
387
388     $marc_field = $item->as_marc_field;
389
390     my $tagslib = C4::Biblio::GetMarcStructure(1, '');
391     my @subfields = $marc_field->subfields;
392     my $result = all { defined $_->[1] } @subfields;
393     ok( $result, 'There are no undef subfields' );
394     my @ordered_subfields = sort {
395             $tagslib->{$itemtag}->{ $a->[0] }->{display_order}
396         <=> $tagslib->{$itemtag}->{ $b->[0] }->{display_order}
397     } @subfields;
398     is_deeply(\@subfields, \@ordered_subfields);
399
400     is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered when kohafield is NULL' );
401     is( scalar $marc_field->subfield('Y'), 'Something else', 'more_subfield_xml is considered when kohafield = ""' );
402
403     $schema->storage->txn_rollback;
404     Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
405 };
406
407 subtest 'pickup_locations' => sub {
408     plan tests => 66;
409
410     $schema->storage->txn_begin;
411
412     my $dbh = C4::Context->dbh;
413
414     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
415     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
416     my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
417     my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
418     my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
419     my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
420     my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
421     my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
422
423     my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
424     my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
425
426     our @branchcodes = (
427         $library1->branchcode, $library2->branchcode,
428         $library3->branchcode, $library4->branchcode
429     );
430
431     my $item1 = $builder->build_sample_item(
432         {
433             homebranch    => $library1->branchcode,
434             holdingbranch => $library2->branchcode,
435             copynumber    => 1,
436             ccode         => 'Gollum'
437         }
438     )->store;
439
440     my $item3 = $builder->build_sample_item(
441         {
442             homebranch    => $library3->branchcode,
443             holdingbranch => $library4->branchcode,
444             copynumber    => 3,
445             itype         => $item1->itype,
446         }
447     )->store;
448
449     Koha::CirculationRules->set_rules(
450         {
451             categorycode => undef,
452             itemtype     => $item1->itype,
453             branchcode   => undef,
454             rules        => {
455                 reservesallowed => 25,
456             }
457         }
458     );
459
460
461     my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
462     my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
463
464     my $results = {
465         "1-1-from_home_library-any"               => 3,
466         "1-1-from_home_library-holdgroup"         => 2,
467         "1-1-from_home_library-patrongroup"       => 2,
468         "1-1-from_home_library-homebranch"        => 1,
469         "1-1-from_home_library-holdingbranch"     => 1,
470         "1-1-from_any_library-any"                => 3,
471         "1-1-from_any_library-holdgroup"          => 2,
472         "1-1-from_any_library-patrongroup"        => 2,
473         "1-1-from_any_library-homebranch"         => 1,
474         "1-1-from_any_library-holdingbranch"      => 1,
475         "1-1-from_local_hold_group-any"           => 3,
476         "1-1-from_local_hold_group-holdgroup"     => 2,
477         "1-1-from_local_hold_group-patrongroup"   => 2,
478         "1-1-from_local_hold_group-homebranch"    => 1,
479         "1-1-from_local_hold_group-holdingbranch" => 1,
480         "1-4-from_home_library-any"               => 0,
481         "1-4-from_home_library-holdgroup"         => 0,
482         "1-4-from_home_library-patrongroup"       => 0,
483         "1-4-from_home_library-homebranch"        => 0,
484         "1-4-from_home_library-holdingbranch"     => 0,
485         "1-4-from_any_library-any"                => 3,
486         "1-4-from_any_library-holdgroup"          => 2,
487         "1-4-from_any_library-patrongroup"        => 1,
488         "1-4-from_any_library-homebranch"         => 1,
489         "1-4-from_any_library-holdingbranch"      => 1,
490         "1-4-from_local_hold_group-any"           => 0,
491         "1-4-from_local_hold_group-holdgroup"     => 0,
492         "1-4-from_local_hold_group-patrongroup"   => 0,
493         "1-4-from_local_hold_group-homebranch"    => 0,
494         "1-4-from_local_hold_group-holdingbranch" => 0,
495         "3-1-from_home_library-any"               => 0,
496         "3-1-from_home_library-holdgroup"         => 0,
497         "3-1-from_home_library-patrongroup"       => 0,
498         "3-1-from_home_library-homebranch"        => 0,
499         "3-1-from_home_library-holdingbranch"     => 0,
500         "3-1-from_any_library-any"                => 3,
501         "3-1-from_any_library-holdgroup"          => 1,
502         "3-1-from_any_library-patrongroup"        => 2,
503         "3-1-from_any_library-homebranch"         => 0,
504         "3-1-from_any_library-holdingbranch"      => 1,
505         "3-1-from_local_hold_group-any"           => 0,
506         "3-1-from_local_hold_group-holdgroup"     => 0,
507         "3-1-from_local_hold_group-patrongroup"   => 0,
508         "3-1-from_local_hold_group-homebranch"    => 0,
509         "3-1-from_local_hold_group-holdingbranch" => 0,
510         "3-4-from_home_library-any"               => 0,
511         "3-4-from_home_library-holdgroup"         => 0,
512         "3-4-from_home_library-patrongroup"       => 0,
513         "3-4-from_home_library-homebranch"        => 0,
514         "3-4-from_home_library-holdingbranch"     => 0,
515         "3-4-from_any_library-any"                => 3,
516         "3-4-from_any_library-holdgroup"          => 1,
517         "3-4-from_any_library-patrongroup"        => 1,
518         "3-4-from_any_library-homebranch"         => 0,
519         "3-4-from_any_library-holdingbranch"      => 1,
520         "3-4-from_local_hold_group-any"           => 3,
521         "3-4-from_local_hold_group-holdgroup"     => 1,
522         "3-4-from_local_hold_group-patrongroup"   => 1,
523         "3-4-from_local_hold_group-homebranch"    => 0,
524         "3-4-from_local_hold_group-holdingbranch" => 1
525     };
526
527     sub _doTest {
528         my ( $item, $patron, $ha, $hfp, $results ) = @_;
529
530         Koha::CirculationRules->set_rules(
531             {
532                 branchcode => undef,
533                 itemtype   => undef,
534                 rules => {
535                     holdallowed => $ha,
536                     hold_fulfillment_policy => $hfp,
537                     returnbranch => 'any'
538                 }
539             }
540         );
541         my $ha_value =
542           $ha eq 'from_local_hold_group' ? 'holdgroup'
543           : (
544             $ha eq 'from_any_library' ? 'any'
545             : 'homebranch'
546           );
547
548         my @pl = map {
549             my $pickup_location = $_;
550             grep { $pickup_location->branchcode eq $_ } @branchcodes
551         } $item->pickup_locations( { patron => $patron } )->as_list;
552
553         ok(
554             scalar(@pl) eq $results->{
555                     $item->copynumber . '-'
556                   . $patron->firstname . '-'
557                   . $ha . '-'
558                   . $hfp
559             },
560             'item'
561               . $item->copynumber
562               . ', patron'
563               . $patron->firstname
564               . ', holdallowed: '
565               . $ha_value
566               . ', hold_fulfillment_policy: '
567               . $hfp
568               . ' should return '
569               . $results->{
570                     $item->copynumber . '-'
571                   . $patron->firstname . '-'
572                   . $ha . '-'
573                   . $hfp
574               }
575               . ' and returns '
576               . scalar(@pl)
577         );
578
579     }
580
581
582     foreach my $item ($item1, $item3) {
583         foreach my $patron ($patron1, $patron4) {
584             #holdallowed 1: homebranch, 2: any, 3: holdgroup
585             foreach my $ha ('from_home_library', 'from_any_library', 'from_local_hold_group') {
586                 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
587                     _doTest($item, $patron, $ha, $hfp, $results);
588                 }
589             }
590         }
591     }
592
593     # Now test that branchtransferlimits will further filter the pickup locations
594
595     my $item_no_ccode = $builder->build_sample_item(
596         {
597             homebranch    => $library1->branchcode,
598             holdingbranch => $library2->branchcode,
599             itype         => $item1->itype,
600         }
601     )->store;
602
603     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
604     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
605     Koha::CirculationRules->set_rules(
606         {
607             branchcode => undef,
608             itemtype   => $item1->itype,
609             rules      => {
610                 holdallowed             => 'from_home_library',
611                 hold_fulfillment_policy => 1,
612                 returnbranch            => 'any'
613             }
614         }
615     );
616     $builder->build_object(
617         {
618             class => 'Koha::Item::Transfer::Limits',
619             value => {
620                 toBranch   => $library1->branchcode,
621                 fromBranch => $library2->branchcode,
622                 itemtype   => $item1->itype,
623                 ccode      => undef,
624             }
625         }
626     );
627
628     my @pickup_locations = map {
629         my $pickup_location = $_;
630         grep { $pickup_location->branchcode eq $_ } @branchcodes
631     } $item1->pickup_locations( { patron => $patron1 } )->as_list;
632
633     is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
634
635     $builder->build_object(
636         {
637             class => 'Koha::Item::Transfer::Limits',
638             value => {
639                 toBranch   => $library4->branchcode,
640                 fromBranch => $library2->branchcode,
641                 itemtype   => $item1->itype,
642                 ccode      => undef,
643             }
644         }
645     );
646
647     @pickup_locations = map {
648         my $pickup_location = $_;
649         grep { $pickup_location->branchcode eq $_ } @branchcodes
650     } $item1->pickup_locations( { patron => $patron1 } )->as_list;
651
652     is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
653
654     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
655     @pickup_locations = map {
656         my $pickup_location = $_;
657         grep { $pickup_location->branchcode eq $_ } @branchcodes
658     } $item1->pickup_locations( { patron => $patron1 } )->as_list;
659     is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
660
661     @pickup_locations = map {
662         my $pickup_location = $_;
663         grep { $pickup_location->branchcode eq $_ } @branchcodes
664     } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
665     is( scalar @pickup_locations, 3, "With no transfer limits of type ccode and an item with no ccode we get back the libraries that are pickup locations");
666
667     $builder->build_object(
668         {
669             class => 'Koha::Item::Transfer::Limits',
670             value => {
671                 toBranch   => $library2->branchcode,
672                 fromBranch => $library2->branchcode,
673                 itemtype   => undef,
674                 ccode      => $item1->ccode,
675             }
676         }
677     );
678
679     @pickup_locations = map {
680         my $pickup_location = $_;
681         grep { $pickup_location->branchcode eq $_ } @branchcodes
682     } $item1->pickup_locations( { patron => $patron1 } )->as_list;
683     is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
684
685     $builder->build_object(
686         {
687             class => 'Koha::Item::Transfer::Limits',
688             value => {
689                 toBranch   => $library4->branchcode,
690                 fromBranch => $library2->branchcode,
691                 itemtype   => undef,
692                 ccode      => $item1->ccode,
693             }
694         }
695     );
696
697     @pickup_locations = map {
698         my $pickup_location = $_;
699         grep { $pickup_location->branchcode eq $_ } @branchcodes
700     } $item1->pickup_locations( { patron => $patron1 } )->as_list;
701     is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
702
703     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
704
705     $schema->storage->txn_rollback;
706 };
707
708 subtest 'request_transfer' => sub {
709     plan tests => 13;
710     $schema->storage->txn_begin;
711
712     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
713     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
714     my $item     = $builder->build_sample_item(
715         {
716             homebranch    => $library1->branchcode,
717             holdingbranch => $library2->branchcode,
718         }
719     );
720
721     # Mandatory fields tests
722     throws_ok { $item->request_transfer( { to => $library1 } ) }
723     'Koha::Exceptions::MissingParameter',
724       'Exception thrown if `reason` parameter is missing';
725
726     throws_ok { $item->request_transfer( { reason => 'Manual' } ) }
727     'Koha::Exceptions::MissingParameter',
728       'Exception thrown if `to` parameter is missing';
729
730     # Successful request
731     my $transfer = $item->request_transfer({ to => $library1, reason => 'Manual' });
732     is( ref($transfer), 'Koha::Item::Transfer',
733         'Koha::Item->request_transfer should return a Koha::Item::Transfer object'
734     );
735     my $original_transfer = $transfer->get_from_storage;
736
737     # Transfer already in progress
738     throws_ok { $item->request_transfer( { to => $library2, reason => 'Manual' } ) }
739     'Koha::Exceptions::Item::Transfer::InQueue',
740       'Exception thrown if transfer is already in progress';
741
742     my $exception = $@;
743     is( ref( $exception->transfer ),
744         'Koha::Item::Transfer',
745         'The exception contains the found Koha::Item::Transfer' );
746
747     # Queue transfer
748     my $queued_transfer = $item->request_transfer(
749         { to => $library2, reason => 'Manual', enqueue => 1 } );
750     is( ref($queued_transfer), 'Koha::Item::Transfer',
751         'Koha::Item->request_transfer allowed when enqueue is set' );
752     my $transfers = $item->get_transfers;
753     is($transfers->count, 2, "There are now 2 live transfers in the queue");
754     $transfer = $transfer->get_from_storage;
755     is_deeply($transfer->unblessed, $original_transfer->unblessed, "Original transfer unchanged");
756     $queued_transfer->datearrived(dt_from_string)->store();
757
758     # Replace transfer
759     my $replaced_transfer = $item->request_transfer(
760         { to => $library2, reason => 'Manual', replace => 1 } );
761     is( ref($replaced_transfer), 'Koha::Item::Transfer',
762         'Koha::Item->request_transfer allowed when replace is set' );
763     $original_transfer->discard_changes;
764     ok($original_transfer->datecancelled, "Original transfer cancelled");
765     $transfers = $item->get_transfers;
766     is($transfers->count, 1, "There is only 1 live transfer in the queue");
767     $replaced_transfer->datearrived(dt_from_string)->store();
768
769     # BranchTransferLimits
770     t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
771     t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
772     my $limit = Koha::Item::Transfer::Limit->new({
773         fromBranch => $library2->branchcode,
774         toBranch => $library1->branchcode,
775         itemtype => $item->effective_itemtype,
776     })->store;
777
778     throws_ok { $item->request_transfer( { to => $library1, reason => 'Manual' } ) }
779     'Koha::Exceptions::Item::Transfer::Limit',
780       'Exception thrown if transfer is prevented by limits';
781
782     my $forced_transfer = $item->request_transfer( { to => $library1, reason => 'Manual', ignore_limits => 1 } );
783     is( ref($forced_transfer), 'Koha::Item::Transfer',
784         'Koha::Item->request_transfer allowed when ignore_limits is set'
785     );
786
787     $schema->storage->txn_rollback;
788 };
789
790 subtest 'deletion' => sub {
791     plan tests => 15;
792
793     $schema->storage->txn_begin;
794
795     my $biblio = $builder->build_sample_biblio();
796
797     my $item = $builder->build_sample_item(
798         {
799             biblionumber => $biblio->biblionumber,
800         }
801     );
802     is( $item->deleted_on, undef, 'deleted_on not set for new item' );
803
804     my $deleted_item = $item->move_to_deleted;
805     is( ref( $deleted_item ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
806       ;    # FIXME This should be Koha::Deleted::Item
807     is( t::lib::Dates::compare( $deleted_item->deleted_on, dt_from_string() ), 0 );
808
809     is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
810     $item = $builder->build_sample_item(
811         {
812             biblionumber => $biblio->biblionumber,
813         }
814     );
815     $item->delete;
816     is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
817
818
819     my $library   = $builder->build_object({ class => 'Koha::Libraries' });
820     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
821     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
822
823     my $patron = $builder->build_object({class => 'Koha::Patrons'});
824     $item = $builder->build_sample_item({ library => $library->branchcode });
825
826     # book_on_loan
827     C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
828
829     is(
830         @{$item->safe_to_delete->messages}[0]->message,
831         'book_on_loan',
832         'Koha::Item->safe_to_delete reports item on loan',
833     );
834
835     is(
836         @{$item->safe_to_delete->messages}[0]->message,
837         'book_on_loan',
838         'item that is on loan cannot be deleted',
839     );
840
841     ok(
842         ! $item->safe_to_delete,
843         'Koha::Item->safe_to_delete shows item NOT safe to delete'
844     );
845
846     AddReturn( $item->barcode, $library->branchcode );
847
848     # book_reserved is tested in t/db_dependent/Reserves.t
849
850     # not_same_branch
851     t::lib::Mocks::mock_preference('IndependentBranches', 1);
852     my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
853
854     is(
855         @{$item_2->safe_to_delete->messages}[0]->message,
856         'not_same_branch',
857         'Koha::Item->safe_to_delete reports IndependentBranches restriction',
858     );
859
860     is(
861         @{$item_2->safe_to_delete->messages}[0]->message,
862         'not_same_branch',
863         'IndependentBranches prevents deletion at another branch',
864     );
865
866     # linked_analytics
867
868     { # codeblock to limit scope of $module->mock
869
870         my $module = Test::MockModule->new('C4::Items');
871         $module->mock( GetAnalyticsCount => sub { return 1 } );
872
873         $item->discard_changes;
874         is(
875             @{$item->safe_to_delete->messages}[0]->message,
876             'linked_analytics',
877             'Koha::Item->safe_to_delete reports linked analytics',
878         );
879
880         is(
881             @{$item->safe_to_delete->messages}[0]->message,
882             'linked_analytics',
883             'Linked analytics prevents deletion of item',
884         );
885
886     }
887
888     { # last_item_for_hold
889         C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
890         is(
891             @{$item->safe_to_delete->messages}[0]->message,
892             'last_item_for_hold',
893             'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio'
894         );
895         # With another item attached to the biblio, the item can be deleted
896         $builder->build_sample_item({ biblionumber => $item->biblionumber });
897     }
898
899     ok(
900         $item->safe_to_delete,
901         'Koha::Item->safe_to_delete shows item safe to delete'
902     );
903
904     $item->safe_delete,
905
906     my $test_item = Koha::Items->find( $item->itemnumber );
907
908     is( $test_item, undef,
909         "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
910     );
911
912     $schema->storage->txn_rollback;
913 };
914
915 subtest 'renewal_branchcode' => sub {
916     plan tests => 13;
917
918     $schema->storage->txn_begin;
919
920     my $item = $builder->build_sample_item();
921     my $branch = $builder->build_object({ class => 'Koha::Libraries' });
922     my $checkout = $builder->build_object({
923         class => 'Koha::Checkouts',
924         value => {
925             itemnumber => $item->itemnumber,
926         }
927     });
928
929
930     C4::Context->interface( 'intranet' );
931     t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
932
933     is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
934     is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
935     C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
936     is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
937
938     C4::Context->interface( 'opac' );
939
940     t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
941     is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
942     is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
943
944     t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
945     is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
946     is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
947
948     t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
949     is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
950     is( $item->renewal_branchcode({branch=>'MONKEY'}), $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout even if branch passed");
951
952     t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
953     is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
954     is( $item->renewal_branchcode({branch=>'TURKEY'}), $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron even if branch passed");
955
956     t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
957     is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
958     is( $item->renewal_branchcode({branch=>'MANATEE'}), $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed");
959
960     $schema->storage->txn_rollback;
961 };
962
963 subtest 'Tests for itemtype' => sub {
964     plan tests => 2;
965     $schema->storage->txn_begin;
966
967     my $biblio = $builder->build_sample_biblio;
968     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
969     my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
970
971     t::lib::Mocks::mock_preference('item-level_itypes', 1);
972     is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
973     t::lib::Mocks::mock_preference('item-level_itypes', 0);
974     is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
975
976     $schema->storage->txn_rollback;
977 };
978
979 subtest 'get_transfers' => sub {
980     plan tests => 16;
981     $schema->storage->txn_begin;
982
983     my $item = $builder->build_sample_item();
984
985     my $transfers = $item->get_transfers();
986     is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
987     is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty');
988
989     my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
990
991     my $transfer_1 = $builder->build_object(
992         {
993             class => 'Koha::Item::Transfers',
994             value => {
995                 itemnumber    => $item->itemnumber,
996                 frombranch    => $item->holdingbranch,
997                 tobranch      => $library_to->branchcode,
998                 reason        => 'Manual',
999                 datesent      => undef,
1000                 datearrived   => undef,
1001                 datecancelled => undef,
1002                 daterequested => \'NOW()'
1003             }
1004         }
1005     );
1006
1007     $transfers = $item->get_transfers();
1008     is($transfers->count, 1, 'When one transfer has been requested, the Koha::Item:Transfers object should contain one result');
1009
1010     my $transfer_2 = $builder->build_object(
1011         {
1012             class => 'Koha::Item::Transfers',
1013             value => {
1014                 itemnumber    => $item->itemnumber,
1015                 frombranch    => $item->holdingbranch,
1016                 tobranch      => $library_to->branchcode,
1017                 reason        => 'Manual',
1018                 datesent      => undef,
1019                 datearrived   => undef,
1020                 datecancelled => undef,
1021                 daterequested => \'NOW()'
1022             }
1023         }
1024     );
1025
1026     my $transfer_3 = $builder->build_object(
1027         {
1028             class => 'Koha::Item::Transfers',
1029             value => {
1030                 itemnumber    => $item->itemnumber,
1031                 frombranch    => $item->holdingbranch,
1032                 tobranch      => $library_to->branchcode,
1033                 reason        => 'Manual',
1034                 datesent      => undef,
1035                 datearrived   => undef,
1036                 datecancelled => undef,
1037                 daterequested => \'NOW()'
1038             }
1039         }
1040     );
1041
1042     $transfers = $item->get_transfers();
1043     is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all');
1044     my $result_1 = $transfers->next;
1045     my $result_2 = $transfers->next;
1046     my $result_3 = $transfers->next;
1047     is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first');
1048     is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second');
1049     is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last');
1050
1051     $transfer_2->datesent(\'NOW()')->store;
1052     $transfers = $item->get_transfers();
1053     is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all');
1054     $result_1 = $transfers->next;
1055     $result_2 = $transfers->next;
1056     $result_3 = $transfers->next;
1057     is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first');
1058     is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1059     is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1060
1061     $transfer_2->datearrived(\'NOW()')->store;
1062     $transfers = $item->get_transfers();
1063     is($transfers->count, 2, 'Once a transfer is received, it no longer appears in the list from ->get_transfers()');
1064     $result_1 = $transfers->next;
1065     $result_2 = $transfers->next;
1066     is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1067     is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest');
1068
1069     $transfer_1->datecancelled(\'NOW()')->store;
1070     $transfers = $item->get_transfers();
1071     is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()');
1072     $result_1 = $transfers->next;
1073     is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains');
1074
1075     $schema->storage->txn_rollback;
1076 };
1077
1078 subtest 'Tests for relationship between item and item_orders via aqorders_item' => sub {
1079     plan tests => 3;
1080
1081     $schema->storage->txn_begin;
1082
1083     my $biblio = $builder->build_sample_biblio();
1084     my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1085
1086     my $orders = $item->orders;
1087     is ($orders->count, 0, 'No order on this item yet');
1088
1089     my $order_note = 'Order for ' . $item->itemnumber;
1090
1091     my $aq_order1 = $builder->build_object({
1092         class => 'Koha::Acquisition::Orders',
1093         value  => {
1094             biblionumber => $biblio->biblionumber,
1095             order_internalnote => $order_note,
1096         },
1097     });
1098     my $aq_order2 = $builder->build_object({
1099         class => 'Koha::Acquisition::Orders',
1100         value  => {
1101             biblionumber => $biblio->biblionumber,
1102         },
1103     });
1104     my $aq_order_item1 = $builder->build({
1105         source => 'AqordersItem',
1106         value  => {
1107             ordernumber => $aq_order1->ordernumber,
1108             itemnumber => $item->itemnumber,
1109         },
1110     });
1111
1112     $orders = $item->orders;
1113     is ($orders->count, 1, 'One order found by item with the relationship');
1114     is ($orders->next->order_internalnote, $order_note, 'Correct order found by item with the relationship');
1115 };
1116
1117 subtest 'move_to_biblio() tests' => sub {
1118     plan tests => 16;
1119
1120     $schema->storage->txn_begin;
1121
1122     my $dbh = C4::Context->dbh;
1123
1124     my $source_biblio = $builder->build_sample_biblio();
1125     my $target_biblio = $builder->build_sample_biblio();
1126
1127     my $source_biblionumber = $source_biblio->biblionumber;
1128     my $target_biblionumber = $target_biblio->biblionumber;
1129
1130     my $item1 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1131     my $item2 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1132     my $item3 = $builder->build_sample_item({ biblionumber => $source_biblionumber });
1133
1134     my $itemnumber1 = $item1->itemnumber;
1135     my $itemnumber2 = $item2->itemnumber;
1136
1137     my $library = $builder->build_object({ class => 'Koha::Libraries' });
1138
1139     my $patron = $builder->build_object({
1140         class => 'Koha::Patrons',
1141         value => { branchcode => $library->branchcode }
1142     });
1143     my $borrowernumber = $patron->borrowernumber;
1144
1145     my $aq_budget = $builder->build({
1146         source => 'Aqbudget',
1147         value  => {
1148             budget_notes => 'test',
1149         },
1150     });
1151
1152     my $aq_order1 = $builder->build_object({
1153         class => 'Koha::Acquisition::Orders',
1154         value  => {
1155             biblionumber => $source_biblionumber,
1156             budget_id => $aq_budget->{budget_id},
1157         },
1158     });
1159     my $aq_order_item1 = $builder->build({
1160         source => 'AqordersItem',
1161         value  => {
1162             ordernumber => $aq_order1->ordernumber,
1163             itemnumber => $itemnumber1,
1164         },
1165     });
1166     my $aq_order2 = $builder->build_object({
1167         class => 'Koha::Acquisition::Orders',
1168         value  => {
1169             biblionumber => $source_biblionumber,
1170             budget_id => $aq_budget->{budget_id},
1171         },
1172     });
1173     my $aq_order_item2 = $builder->build({
1174         source => 'AqordersItem',
1175         value  => {
1176             ordernumber => $aq_order2->ordernumber,
1177             itemnumber => $itemnumber2,
1178         },
1179     });
1180
1181     my $bib_level_hold = $builder->build_object({
1182         class => 'Koha::Holds',
1183         value  => {
1184             biblionumber => $source_biblionumber,
1185             itemnumber => undef,
1186         },
1187     });
1188     my $item_level_hold1 = $builder->build_object({
1189         class => 'Koha::Holds',
1190         value  => {
1191             biblionumber => $source_biblionumber,
1192             itemnumber => $itemnumber1,
1193         },
1194     });
1195     my $item_level_hold2 = $builder->build_object({
1196         class => 'Koha::Holds',
1197         value  => {
1198             biblionumber => $source_biblionumber,
1199             itemnumber => $itemnumber2,
1200         }
1201     });
1202
1203     my $tmp_holdsqueue1 = $builder->build({
1204         source => 'TmpHoldsqueue',
1205         value  => {
1206             borrowernumber => $borrowernumber,
1207             biblionumber   => $source_biblionumber,
1208             itemnumber     => $itemnumber1,
1209         }
1210     });
1211     my $tmp_holdsqueue2 = $builder->build({
1212         source => 'TmpHoldsqueue',
1213         value  => {
1214             borrowernumber => $borrowernumber,
1215             biblionumber   => $source_biblionumber,
1216             itemnumber     => $itemnumber2,
1217         }
1218     });
1219     my $hold_fill_target1 = $builder->build({
1220         source => 'HoldFillTarget',
1221         value  => {
1222             borrowernumber     => $borrowernumber,
1223             biblionumber       => $source_biblionumber,
1224             itemnumber         => $itemnumber1,
1225         }
1226     });
1227     my $hold_fill_target2 = $builder->build({
1228         source => 'HoldFillTarget',
1229         value  => {
1230             borrowernumber     => $borrowernumber,
1231             biblionumber       => $source_biblionumber,
1232             itemnumber         => $itemnumber2,
1233         }
1234     });
1235     my $linktracker1 = $builder->build({
1236         source => 'Linktracker',
1237         value  => {
1238             borrowernumber     => $borrowernumber,
1239             biblionumber       => $source_biblionumber,
1240             itemnumber         => $itemnumber1,
1241         }
1242     });
1243     my $linktracker2 = $builder->build({
1244         source => 'Linktracker',
1245         value  => {
1246             borrowernumber     => $borrowernumber,
1247             biblionumber       => $source_biblionumber,
1248             itemnumber         => $itemnumber2,
1249         }
1250     });
1251
1252     my $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1253     is($to_biblionumber_after_move, $target_biblionumber, 'move_to_biblio returns the target biblionumber if success');
1254
1255     $to_biblionumber_after_move = $item1->move_to_biblio($target_biblio);
1256     is($to_biblionumber_after_move, undef, 'move_to_biblio returns undef if the move has failed. If called twice, the item is not attached to the first biblio anymore');
1257
1258     my $get_item1 = Koha::Items->find( $item1->itemnumber );
1259     is($get_item1->biblionumber, $target_biblionumber, 'item1 is moved');
1260     my $get_item2 = Koha::Items->find( $item2->itemnumber );
1261     is($get_item2->biblionumber, $source_biblionumber, 'item2 is not moved');
1262     my $get_item3 = Koha::Items->find( $item3->itemnumber );
1263     is($get_item3->biblionumber, $source_biblionumber, 'item3 is not moved');
1264
1265     $aq_order1->discard_changes;
1266     $aq_order2->discard_changes;
1267     is($aq_order1->biblionumber, $target_biblionumber, 'move_to_biblio moves aq_orders for item 1');
1268     is($aq_order2->biblionumber, $source_biblionumber, 'move_to_biblio does not move aq_orders for item 2');
1269
1270     $bib_level_hold->discard_changes;
1271     $item_level_hold1->discard_changes;
1272     $item_level_hold2->discard_changes;
1273     is($bib_level_hold->biblionumber,   $source_biblionumber, 'move_to_biblio does not move the biblio-level hold');
1274     is($item_level_hold1->biblionumber, $target_biblionumber, 'move_to_biblio moves the item-level hold placed on item 1');
1275     is($item_level_hold2->biblionumber, $source_biblionumber, 'move_to_biblio does not move the item-level hold placed on item 2');
1276
1277     my $get_tmp_holdsqueue1 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue1->{itemnumber} })->single;
1278     my $get_tmp_holdsqueue2 = $schema->resultset('TmpHoldsqueue')->search({ itemnumber => $tmp_holdsqueue2->{itemnumber} })->single;
1279     is($get_tmp_holdsqueue1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves tmp_holdsqueue for item 1');
1280     is($get_tmp_holdsqueue2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move tmp_holdsqueue for item 2');
1281
1282     my $get_hold_fill_target1 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target1->{itemnumber} })->single;
1283     my $get_hold_fill_target2 = $schema->resultset('HoldFillTarget')->search({ itemnumber => $hold_fill_target2->{itemnumber} })->single;
1284     # Why does ->biblionumber return a Biblio object???
1285     is($get_hold_fill_target1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves hold_fill_targets for item 1');
1286     is($get_hold_fill_target2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move hold_fill_targets for item 2');
1287
1288     my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single;
1289     my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single;
1290     is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1');
1291     is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2');
1292
1293     $schema->storage->txn_rollback;
1294 };
1295
1296 subtest 'columns_to_str' => sub {
1297     plan tests => 4;
1298
1299     $schema->storage->txn_begin;
1300
1301     my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
1302
1303     my $cache = Koha::Caches->get_instance();
1304     $cache->clear_from_cache("MarcStructure-0-");
1305     $cache->clear_from_cache("MarcStructure-1-");
1306     $cache->clear_from_cache("MarcSubfieldStructure-");
1307
1308     # Creating subfields 'é', 'è' that are not linked with a kohafield
1309     Koha::MarcSubfieldStructures->search(
1310         {
1311             frameworkcode => '',
1312             tagfield => $itemtag,
1313             tagsubfield => ['é', 'è'],
1314         }
1315     )->delete;    # In case it exist already
1316
1317     # Ã© is not linked with a AV
1318     # Ã¨ is linked with AV branches
1319     Koha::MarcSubfieldStructure->new(
1320         {
1321             frameworkcode => '',
1322             tagfield      => $itemtag,
1323             tagsubfield   => 'é',
1324             kohafield     => undef,
1325             repeatable    => 1,
1326             defaultvalue  => 'ééé',
1327             tab           => 10,
1328         }
1329     )->store;
1330     Koha::MarcSubfieldStructure->new(
1331         {
1332             frameworkcode    => '',
1333             tagfield         => $itemtag,
1334             tagsubfield      => 'è',
1335             kohafield        => undef,
1336             repeatable       => 1,
1337             defaultvalue     => 'èèè',
1338             tab              => 10,
1339             authorised_value => 'branches',
1340         }
1341     )->store;
1342
1343     my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
1344     my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1345     my $lost_av = $builder->build_object({ class => 'Koha::AuthorisedValues', value => { category => 'LOST', authorised_value => '42' }});
1346     my $dateaccessioned = '2020-12-15';
1347     my $library = Koha::Libraries->search->next;
1348     my $branchcode = $library->branchcode;
1349
1350     my $some_marc_xml = qq{<?xml version="1.0" encoding="UTF-8"?>
1351 <collection
1352   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1353   xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
1354   xmlns="http://www.loc.gov/MARC21/slim">
1355
1356 <record>
1357   <leader>         a              </leader>
1358   <datafield tag="999" ind1=" " ind2=" ">
1359     <subfield code="é">value Ã©</subfield>
1360     <subfield code="è">$branchcode</subfield>
1361   </datafield>
1362 </record>
1363
1364 </collection>};
1365
1366     $item->update(
1367         {
1368             itemlost           => $lost_av->authorised_value,
1369             dateaccessioned    => $dateaccessioned,
1370             more_subfields_xml => $some_marc_xml,
1371         }
1372     );
1373
1374     $item = $item->get_from_storage;
1375
1376     my $s = $item->columns_to_str;
1377     is( $s->{itemlost}, $lost_av->lib, 'Attributes linked with AV replaced with description' );
1378     is( $s->{dateaccessioned}, '2020-12-15', 'Date attributes iso formatted');
1379     is( $s->{'é'}, 'value Ã©', 'subfield ok with more than a-Z');
1380     is( $s->{'è'}, $library->branchname );
1381
1382     $cache->clear_from_cache("MarcStructure-0-");
1383     $cache->clear_from_cache("MarcStructure-1-");
1384     $cache->clear_from_cache("MarcSubfieldStructure-");
1385
1386     $schema->storage->txn_rollback;
1387
1388 };
1389
1390 subtest 'store() tests' => sub {
1391
1392     plan tests => 3;
1393
1394     subtest 'dateaccessioned handling' => sub {
1395
1396         plan tests => 3;
1397
1398         $schema->storage->txn_begin;
1399
1400         my $item = $builder->build_sample_item;
1401
1402         ok( defined $item->dateaccessioned, 'dateaccessioned is set' );
1403
1404         # reset dateaccessioned on the DB
1405         $schema->resultset('Item')->find({ itemnumber => $item->id })->update({ dateaccessioned => undef });
1406         $item->discard_changes;
1407
1408         ok( !defined $item->dateaccessioned );
1409
1410         # update something
1411         $item->replacementprice(100)->store->discard_changes;
1412
1413         ok( !defined $item->dateaccessioned, 'dateaccessioned not set on update if undefined' );
1414
1415         $schema->storage->txn_rollback;
1416     };
1417
1418     subtest '_set_found_trigger() tests' => sub {
1419
1420         plan tests => 8;
1421
1422         $schema->storage->txn_begin;
1423
1424         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1425         my $item   = $builder->build_sample_item({ itemlost => 1, itemlost_on => dt_from_string() });
1426
1427         # Add a lost item debit
1428         my $debit = $patron->account->add_debit(
1429             {
1430                 amount    => 10,
1431                 type      => 'LOST',
1432                 item_id   => $item->id,
1433                 interface => 'intranet',
1434             }
1435         );
1436
1437         # Add a lost item processing fee
1438         my $processing_debit = $patron->account->add_debit(
1439             {
1440                 amount    => 2,
1441                 type      => 'PROCESSING',
1442                 item_id   => $item->id,
1443                 interface => 'intranet',
1444             }
1445         );
1446
1447         my $lostreturn_policy = {
1448             lostreturn       => 'charge',
1449             processingreturn => 'refund'
1450         };
1451
1452         my $mocked_circ_rules = Test::MockModule->new('Koha::CirculationRules');
1453         $mocked_circ_rules->mock( 'get_lostreturn_policy', sub { return $lostreturn_policy; } );
1454
1455         # simulate it was found
1456         $item->set( { itemlost => 0 } )->store;
1457
1458         my $messages = $item->object_messages;
1459
1460         my $message_1 = $messages->[0];
1461
1462         is( $message_1->type,    'info',          'type is correct' );
1463         is( $message_1->message, 'lost_refunded', 'message is correct' );
1464
1465         # Find the refund credit
1466         my $credit = $debit->credits->next;
1467
1468         is_deeply(
1469             $message_1->payload,
1470             { credit_id => $credit->id },
1471             'type is correct'
1472         );
1473
1474         my $message_2 = $messages->[1];
1475
1476         is( $message_2->type,    'info',        'type is correct' );
1477         is( $message_2->message, 'lost_charge', 'message is correct' );
1478         is( $message_2->payload, undef,         'no payload' );
1479
1480         my $message_3 = $messages->[2];
1481         is( $message_3->message, 'processing_refunded', 'message is correct' );
1482
1483         my $processing_credit = $processing_debit->credits->next;
1484         is_deeply(
1485             $message_3->payload,
1486             { credit_id => $processing_credit->id },
1487             'type is correct'
1488         );
1489
1490         $schema->storage->txn_rollback;
1491     };
1492
1493     subtest 'holds_queue update tests' => sub {
1494
1495         plan tests => 2;
1496
1497         $schema->storage->txn_begin;
1498
1499         my $biblio = $builder->build_sample_biblio;
1500
1501         my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
1502         $mock->mock( 'enqueue', sub {
1503             my ( $self, $args ) = @_;
1504             is_deeply(
1505                 $args->{biblio_ids},
1506                 [ $biblio->id ],
1507                 '->store triggers a holds queue update for the related biblio'
1508             );
1509         } );
1510
1511         t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 );
1512
1513         # new item
1514         my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
1515
1516         # updated item
1517         $item->set({ reserves => 1 })->store;
1518
1519         t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
1520         # updated item
1521         $item->set({ reserves => 0 })->store;
1522
1523         $schema->storage->txn_rollback;
1524     };
1525 };
1526
1527 subtest 'Recalls tests' => sub {
1528
1529     plan tests => 22;
1530
1531     $schema->storage->txn_begin;
1532
1533     my $item1 = $builder->build_sample_item;
1534     my $biblio = $item1->biblio;
1535     my $branchcode = $item1->holdingbranch;
1536     my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1537     my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1538     my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $branchcode } });
1539     my $item2 = $builder->build_object(
1540         {   class => 'Koha::Items',
1541             value => { holdingbranch => $branchcode, homebranch => $branchcode, biblionumber => $biblio->biblionumber, itype => $item1->effective_itemtype }
1542         }
1543     );
1544
1545     t::lib::Mocks::mock_userenv( { patron => $patron1 } );
1546     t::lib::Mocks::mock_preference('UseRecalls', 1);
1547
1548     my $recall1 = Koha::Recall->new(
1549         {   patron_id         => $patron1->borrowernumber,
1550             created_date      => \'NOW()',
1551             biblio_id         => $biblio->biblionumber,
1552             pickup_library_id => $branchcode,
1553             item_id           => $item1->itemnumber,
1554             expiration_date   => undef,
1555             item_level        => 1
1556         }
1557     )->store;
1558     my $recall2 = Koha::Recall->new(
1559         {   patron_id         => $patron2->borrowernumber,
1560             created_date      => \'NOW()',
1561             biblio_id         => $biblio->biblionumber,
1562             pickup_library_id => $branchcode,
1563             item_id           => $item1->itemnumber,
1564             expiration_date   => undef,
1565             item_level        => 1
1566         }
1567     )->store;
1568
1569     is( $item1->recall->patron_id, $patron1->borrowernumber, 'Correctly returns most relevant recall' );
1570
1571     $recall2->set_cancelled;
1572
1573     t::lib::Mocks::mock_preference('UseRecalls', 0);
1574     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall with UseRecalls disabled" );
1575
1576     t::lib::Mocks::mock_preference("UseRecalls", 1);
1577
1578     $item1->update({ notforloan => 1 });
1579     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is not for loan" );
1580     $item1->update({ notforloan => 0, itemlost => 1 });
1581     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is marked lost" );
1582     $item1->update({ itemlost => 0, withdrawn => 1 });
1583     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall that is withdrawn" );
1584     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if not checked out" );
1585
1586     $item1->update({ withdrawn => 0 });
1587     C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1588
1589     Koha::CirculationRules->set_rules({
1590         branchcode => $branchcode,
1591         categorycode => $patron1->categorycode,
1592         itemtype => $item1->effective_itemtype,
1593         rules => {
1594             recalls_allowed => 0,
1595             recalls_per_record => 1,
1596             on_shelf_recalls => 'all',
1597         },
1598     });
1599     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if recalls_allowed = 0" );
1600
1601     Koha::CirculationRules->set_rules({
1602         branchcode => $branchcode,
1603         categorycode => $patron1->categorycode,
1604         itemtype => $item1->effective_itemtype,
1605         rules => {
1606             recalls_allowed => 1,
1607             recalls_per_record => 1,
1608             on_shelf_recalls => 'all',
1609         },
1610     });
1611     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_allowed" );
1612     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has more existing recall(s) than recalls_per_record" );
1613     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if patron has already recalled this item" );
1614
1615     my $reserve_id = C4::Reserves::AddReserve({ branchcode => $branchcode, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber, itemnumber => $item1->itemnumber });
1616     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall item if patron has already reserved it" );
1617     C4::Reserves::ModReserve({ rank => 'del', reserve_id => $reserve_id, branchcode => $branchcode, itemnumber => $item1->itemnumber, borrowernumber => $patron1->borrowernumber, biblionumber => $item1->biblionumber });
1618
1619     $recall1->set_cancelled;
1620     is( $item1->can_be_recalled({ patron => $patron2 }), 0, "Can't recall if patron has already checked out an item attached to this biblio" );
1621
1622     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if on_shelf_recalls = all and items are still available" );
1623
1624     Koha::CirculationRules->set_rules({
1625         branchcode => $branchcode,
1626         categorycode => $patron1->categorycode,
1627         itemtype => $item1->effective_itemtype,
1628         rules => {
1629             recalls_allowed => 1,
1630             recalls_per_record => 1,
1631             on_shelf_recalls => 'any',
1632         },
1633     });
1634     C4::Circulation::AddReturn( $item1->barcode, $branchcode );
1635     is( $item1->can_be_recalled({ patron => $patron1 }), 0, "Can't recall if no items are checked out" );
1636
1637     C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
1638     is( $item1->can_be_recalled({ patron => $patron1 }), 1, "Can recall item" );
1639
1640     $recall1 = Koha::Recall->new(
1641         {   patron_id         => $patron1->borrowernumber,
1642             created_date      => \'NOW()',
1643             biblio_id         => $biblio->biblionumber,
1644             pickup_library_id => $branchcode,
1645             item_id           => undef,
1646             expiration_date   => undef,
1647             item_level        => 0
1648         }
1649     )->store;
1650
1651     # Patron2 has Item1 checked out. Patron1 has placed a biblio-level recall on Biblio1, so check if Item1 can fulfill Patron1's recall.
1652
1653     Koha::CirculationRules->set_rules({
1654         branchcode => undef,
1655         categorycode => undef,
1656         itemtype => $item1->effective_itemtype,
1657         rules => {
1658             recalls_allowed => 0,
1659             recalls_per_record => 1,
1660             on_shelf_recalls => 'any',
1661         },
1662     });
1663     is( $item1->can_be_waiting_recall, 0, "Recalls not allowed for this itemtype" );
1664
1665     Koha::CirculationRules->set_rules({
1666         branchcode => undef,
1667         categorycode => undef,
1668         itemtype => $item1->effective_itemtype,
1669         rules => {
1670             recalls_allowed => 1,
1671             recalls_per_record => 1,
1672             on_shelf_recalls => 'any',
1673         },
1674     });
1675     is( $item1->can_be_waiting_recall, 1, "Recalls are allowed for this itemtype" );
1676
1677     # check_recalls tests
1678
1679     $recall1 = Koha::Recall->new(
1680         {   patron_id         => $patron2->borrowernumber,
1681             created_date      => \'NOW()',
1682             biblio_id         => $biblio->biblionumber,
1683             pickup_library_id => $branchcode,
1684             item_id           => $item1->itemnumber,
1685             expiration_date   => undef,
1686             item_level        => 1
1687         }
1688     )->store;
1689     $recall2 = Koha::Recall->new(
1690         {   patron_id         => $patron1->borrowernumber,
1691             created_date      => \'NOW()',
1692             biblio_id         => $biblio->biblionumber,
1693             pickup_library_id => $branchcode,
1694             item_id           => undef,
1695             expiration_date   => undef,
1696             item_level        => 0
1697         }
1698     )->store;
1699     $recall2->set_waiting( { item => $item1 } );
1700     is( $item1->has_pending_recall, 1, 'Item has pending recall' );
1701
1702     # return a waiting recall
1703     my $check_recall = $item1->check_recalls;
1704     is( $check_recall->patron_id, $patron1->borrowernumber, "Waiting recall is highest priority and returned" );
1705
1706     $recall2->revert_waiting;
1707
1708     is( $item1->has_pending_recall, 0, 'Item does not have pending recall' );
1709
1710     # return recall based on recalldate
1711     $check_recall = $item1->check_recalls;
1712     is( $check_recall->patron_id, $patron1->borrowernumber, "No waiting recall, so oldest recall is returned" );
1713
1714     $recall1->set_cancelled;
1715
1716     # return a biblio-level recall
1717     $check_recall = $item1->check_recalls;
1718     is( $check_recall->patron_id, $patron1->borrowernumber, "Only remaining recall is returned" );
1719
1720     $recall2->set_cancelled;
1721
1722     $schema->storage->txn_rollback;
1723 };
1724
1725 subtest 'Notforloan tests' => sub {
1726
1727     plan tests => 3;
1728
1729     $schema->storage->txn_begin;
1730
1731     my $item1 = $builder->build_sample_item;
1732     $item1->update({ notforloan => 0 });
1733     $item1->itemtype->notforloan(0);
1734     is ( $item1->is_notforloan, 0, 'Notforloan is correctly false by item status and item type');
1735     $item1->update({ notforloan => 1 });
1736     is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item status');
1737     $item1->update({ notforloan => 0 });
1738     $item1->itemtype->update({ notforloan => 1 });
1739     is ( $item1->is_notforloan, 1, 'Notforloan is correctly true by item type');
1740
1741     $schema->storage->txn_rollback;
1742 };
1743
1744 subtest 'item_group() tests' => sub {
1745
1746     plan tests => 4;
1747
1748     $schema->storage->txn_begin;
1749
1750     my $biblio = $builder->build_sample_biblio();
1751     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1752     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1753
1754     is( $item_1->item_group, undef, 'Item 1 has no item group');
1755     is( $item_2->item_group, undef, 'Item 2 has no item group');
1756
1757     my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1758     my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store();
1759
1760     $item_group_1->add_item({ item_id => $item_1->id });
1761     $item_group_2->add_item({ item_id => $item_2->id });
1762
1763     is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' );
1764     is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' );
1765
1766     $schema->storage->txn_rollback;
1767 };
1768
1769 subtest 'has_pending_recall() tests' => sub {
1770
1771     plan tests => 2;
1772
1773     $schema->storage->txn_begin;
1774
1775     my $library = $builder->build_object({ class => 'Koha::Libraries' });
1776     my $item    = $builder->build_sample_item;
1777     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
1778
1779     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
1780     t::lib::Mocks::mock_preference( 'UseRecalls', 1 );
1781
1782     C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
1783
1784     my ($recall) = Koha::Recalls->add_recall({ biblio => $item->biblio, item => $item, patron => $patron });
1785
1786     ok( !$item->has_pending_recall, 'The item has no pending recalls' );
1787
1788     $recall->status('waiting')->store;
1789
1790     ok( $item->has_pending_recall, 'The item has a pending recall' );
1791
1792     $schema->storage->txn_rollback;
1793 };
1794
1795 subtest 'is_denied_renewal' => sub {
1796     plan tests => 11;
1797
1798     $schema->storage->txn_begin;
1799
1800     my $library = $builder->build_object({ class => 'Koha::Libraries'});
1801
1802     my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
1803         homebranch => $library->branchcode,
1804         withdrawn => 1,
1805         itype => 'HIDE',
1806         location => 'PROC',
1807         itemcallnumber => undef,
1808         itemnotes => "",
1809         }
1810     });
1811
1812     my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
1813         homebranch => $library->branchcode,
1814         withdrawn => 0,
1815         itype => 'NOHIDE',
1816         location => 'NOPROC'
1817         }
1818     });
1819
1820     my $idr_rules = "";
1821     C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1822     is( $deny_book->is_denied_renewal, 0, 'Renewal allowed when no rules' );
1823
1824     $idr_rules="withdrawn: [1]";
1825     C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1826     is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 1 rules (withdrawn)' );
1827     is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 1 rules not matched (withdrawn)' );
1828
1829     $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
1830     is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
1831     is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
1832
1833     $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
1834     C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1835     is( $deny_book->is_denied_renewal, 1, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
1836     is( $allow_book->is_denied_renewal, 0, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
1837
1838     $idr_rules="itemcallnumber: [NULL]";
1839     C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1840     is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for undef when NULL in pref' );
1841
1842     $idr_rules="itemcallnumber: ['']";
1843     C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1844     is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for undef when "" in pref' );
1845
1846     $idr_rules="itemnotes: [NULL]";
1847     C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1848     is( $deny_book->is_denied_renewal, 0, 'Renewal not blocked for "" when NULL in pref' );
1849
1850     $idr_rules="itemnotes: ['']";
1851     C4::Context->set_preference('ItemsDeniedRenewal', $idr_rules);
1852     is( $deny_book->is_denied_renewal, 1, 'Renewal blocked for empty string when "" in pref' );
1853
1854     $schema->storage->txn_rollback;
1855 };