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