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