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