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