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