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