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