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