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