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