Bug 25560: Unit tests
[koha.git] / t / db_dependent / Circulation / issue.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 53;
21 use DateTime::Duration;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use C4::Biblio qw( AddBiblio );
27 use C4::Circulation qw( AddIssue AddIssuingCharge AddRenewal AddReturn GetIssuingCharges GetRenewCount GetUpcomingDueIssues );
28 use C4::Context;
29 use C4::Items;
30 use C4::Reserves qw( AddReserve );
31 use Koha::Checkouts;
32 use Koha::Database;
33 use Koha::DateUtils qw( dt_from_string output_pref );
34 use Koha::Holds;
35 use Koha::Items;
36 use Koha::Library;
37 use Koha::Patrons;
38 use Koha::CirculationRules;
39 use Koha::Statistics;
40
41 BEGIN {
42     require_ok('C4::Circulation');
43 }
44
45 can_ok(
46     'C4::Circulation',
47     qw(AddIssue
48       AddIssuingCharge
49       AddRenewal
50       AddReturn
51       GetIssuingCharges
52       GetRenewCount
53       GetUpcomingDueIssues
54       )
55 );
56
57 #Start transaction
58 my $schema = Koha::Database->schema;
59 $schema->storage->txn_begin;
60 my $dbh = C4::Context->dbh;
61
62 my $builder = t::lib::TestBuilder->new();
63
64 $dbh->do(q|DELETE FROM issues|);
65 $dbh->do(q|DELETE FROM items|);
66 $dbh->do(q|DELETE FROM borrowers|);
67 $dbh->do(q|DELETE FROM categories|);
68 $dbh->do(q|DELETE FROM accountlines|);
69 $dbh->do(q|DELETE FROM circulation_rules|);
70 $dbh->do(q|DELETE FROM reserves|);
71 $dbh->do(q|DELETE FROM old_reserves|);
72 $dbh->do(q|DELETE FROM statistics|);
73
74 # Generate sample datas
75 my $itemtype = $builder->build(
76     {   source => 'Itemtype',
77         value  => { notforloan => undef, rentalcharge => 0 }
78     }
79 )->{itemtype};
80 my $itemtype2 = $builder->build(
81     {   source => 'Itemtype',
82         value  => { notforloan => undef }
83     }
84 )->{itemtype};
85 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
86 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
87 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
88 my $categorycode = $builder->build({
89         source => 'Category',
90         value => { enrolmentfee => undef }
91     })->{categorycode};
92
93 # A default issuingrule should always be present
94 Koha::CirculationRules->set_rules(
95     {
96         itemtype     => '*',
97         categorycode => '*',
98         branchcode   => '*',
99         rules        => {
100             lengthunit      => 'days',
101             issuelength     => 0,
102             renewalperiod   => 0,
103             renewalsallowed => 0
104         }
105     }
106 );
107
108 # Add Dates
109 my $dt_today = dt_from_string;
110 my $today    = output_pref(
111     {   dt         => $dt_today,
112         dateformat => 'iso',
113         timeformat => '24hr',
114         dateonly   => 1
115     }
116 );
117
118 my $dt_today2 = dt_from_string;
119 my $dur10 = DateTime::Duration->new( days => -10 );
120 $dt_today2->add_duration($dur10);
121 my $daysago10 = output_pref(
122     {   dt         => $dt_today2,
123         dateformat => 'iso',
124         timeformat => '24hr',
125         dateonly   => 1
126     }
127 );
128
129 # Add biblio and item
130 my $record = MARC::Record->new();
131 my $record2 = MARC::Record->new();
132 $record->append_fields(
133     MARC::Field->new( '942', '0', '0', c => $itemtype ),
134     MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
135
136 $record2->append_fields(
137     MARC::Field->new( '942', '0', '0', c => $itemtype2 ),
138     MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
139
140 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
141 my ( $biblionumber2, $biblioitemnumber2 ) = C4::Biblio::AddBiblio( $record2, '' );
142
143 my $barcode_1 = 'barcode_1';
144 my $barcode_2 = 'barcode_2';
145 my $item_id1 = Koha::Item->new(
146     {
147         biblionumber   => $biblionumber,
148         barcode        => $barcode_1,
149         itemcallnumber => 'callnumber1',
150         homebranch     => $branchcode_1,
151         holdingbranch  => $branchcode_1,
152         itype          => $itemtype
153     },
154 )->store->itemnumber;
155 my $item_id2 = Koha::Item->new(
156     {
157         biblionumber   => $biblionumber,
158         barcode        => $barcode_2,
159         itemcallnumber => 'callnumber2',
160         homebranch     => $branchcode_2,
161         holdingbranch  => $branchcode_2,
162         notforloan     => 1,
163         itype          => $itemtype
164     },
165 )->store->itemnumber;
166
167 #Add borrower
168 my $borrower_id1 = Koha::Patron->new({
169     firstname    => 'firstname1',
170     surname      => 'surname1 ',
171     categorycode => $categorycode,
172     branchcode   => $branchcode_1
173 })->store->borrowernumber;
174 my $patron_1 = Koha::Patrons->find( $borrower_id1 );
175 my $borrower_id2 = Koha::Patron->new({
176     firstname    => 'firstname2',
177     surname      => 'surname2 ',
178     categorycode => $categorycode,
179     branchcode   => $branchcode_2,
180 })->store->borrowernumber;
181 my $patron_2 = Koha::Patrons->find( $borrower_id2 );
182
183 t::lib::Mocks::mock_userenv({ patron => $patron_1 });
184
185 #Begin Tests
186
187 #Test AddIssue
188 my $query = " SELECT count(*) FROM issues";
189 my $sth = $dbh->prepare($query);
190 $sth->execute;
191 my $countissue = $sth->fetchrow_array;
192 is ($countissue ,0, "there is no issue");
193 my $issue1 = C4::Circulation::AddIssue( $patron_1, $barcode_1, $daysago10, 0, $today, '' );
194 is( ref $issue1, 'Koha::Checkout',
195        'AddIssue returns a Koha::Checkout object' );
196 my $datedue1 = dt_from_string( $issue1->date_due() );
197 like(
198     $datedue1,
199     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
200     "Koha::Checkout->date_due() returns a date"
201 );
202 my $issue_id1 = $issue1->issue_id;
203
204 my $issue2 = C4::Circulation::AddIssue( $patron_1, 'nonexistent_barcode' );
205 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
206
207 $sth->execute;
208 $countissue = $sth->fetchrow_array;
209 is ($countissue,1,"1 issues have been added");
210
211 #Test AddIssuingCharge
212 $query = " SELECT count(*) FROM accountlines";
213 $sth = $dbh->prepare($query);
214 $sth->execute;
215 my $countaccount = $sth->fetchrow_array;
216 is ($countaccount,0,"0 accountline exists");
217 my $checkout = Koha::Checkouts->find( $issue_id1 );
218 my $charge = C4::Circulation::AddIssuingCharge( $checkout, 10, 'RENT' );
219 is( ref( $charge ), 'Koha::Account::Line', "An issuing charge has been added" );
220 is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
221 my $offset = Koha::Account::Offsets->find( { debit_id => $charge->id } );
222 is( $offset->credit_id, undef, 'Offset was created');
223 $sth->execute;
224 $countaccount = $sth->fetchrow_array;
225 is ($countaccount,1,"1 accountline has been added");
226
227 # Test AddRenewal
228
229 my $se = Test::MockModule->new( 'C4::Context' );
230 $se->mock( 'interface', sub {return 'intranet'});
231
232 # Let's renew this one at a different library for statistical purposes to test Bug 17781
233 # Mocking userenv with a different branchcode
234 t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
235
236 my $datedue3 = AddRenewal(
237     {
238         borrowernumber  => $borrower_id1,
239         itemnumber      => $item_id1,
240         branch          => $branchcode_1,
241         datedue         => $datedue1,
242         lastreneweddate => $daysago10
243     }
244 );
245
246 # Restoring the userenv with the original branchcode
247 t::lib::Mocks::mock_userenv({ patron => $patron_1});
248
249 like(
250     $datedue3,
251     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
252     "AddRenewal returns a date"
253 );
254
255 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
256 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
257
258 subtest 'Show that AddRenewal respects OpacRenewalBranch and interface' => sub {
259     plan tests => 10;
260
261     my $item_library = $builder->build_object( { class => 'Koha::Libraries' } );
262     my $patron       = $builder->build_object( { class => 'Koha::Patrons' } );
263     my $logged_in_user = $builder->build_object( { class => 'Koha::Patrons' } );
264     t::lib::Mocks::mock_userenv( { patron => $logged_in_user } );
265
266     my $OpacRenewalBranch = {
267         opacrenew        => "OPACRenew",
268         checkoutbranch   => $logged_in_user->branchcode,
269         patronhomebranch => $patron->branchcode,
270         itemhomebranch   => $item_library->branchcode,
271         none             => "",
272     };
273
274     while ( my ( $syspref, $expected_branchcode ) = each %$OpacRenewalBranch ) {
275
276         t::lib::Mocks::mock_preference( 'OpacRenewalBranch', $syspref );
277
278         {
279             $se->mock( 'interface', sub { return 'opac' } );
280
281             my $item = $builder->build_sample_item(
282                 { library => $item_library->branchcode, itype => $itemtype } );
283             my $opac_renew_issue =
284               C4::Circulation::AddIssue( $patron, $item->barcode );
285
286             AddRenewal(
287                 {
288                     borrowernumber  => $patron->borrowernumber,
289                     itemnumber      => $item->itemnumber,
290                     branch          => "Stavromula",
291                     datedue         => $datedue1,
292                     lastreneweddate => $daysago10
293                 }
294             );
295
296             my $stat = Koha::Statistics->search(
297                 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
298             is( $stat->branch, $expected_branchcode,
299                 "->renewal_branchcode is respected for OpacRenewalBranch = $syspref"
300             );
301         }
302
303         {
304             $se->mock( 'interface', sub { return 'intranet' } );
305
306             my $item = $builder->build_sample_item(
307                 { library => $item_library->branchcode, itype => $itemtype } );
308             my $opac_renew_issue =
309               C4::Circulation::AddIssue( $patron, $item->barcode );
310
311             AddRenewal(
312                 {
313                     borrowernumber  => $patron->borrowernumber,
314                     itemnumber      => $item->itemnumber,
315                     branch          => "Stavromula",
316                     datedue         => $datedue1,
317                     lastreneweddate => $daysago10
318                 }
319             );
320
321             my $stat = Koha::Statistics->search(
322                 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
323             is( $stat->branch, $logged_in_user->branchcode,
324                 "->renewal_branchcode is always logged in branch for intranet"
325             );
326         }
327     }
328 };
329
330
331 my @renewcount;
332 #Test GetRenewCount
333 my $issue3 = C4::Circulation::AddIssue( $patron_1, $barcode_1 );
334 #Without anything in DB
335 @renewcount = C4::Circulation::GetRenewCount();
336 is_deeply(
337     \@renewcount,
338     [ 0, 0, 0, 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
339     "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
340 );
341 @renewcount = C4::Circulation::GetRenewCount(-1);
342 is_deeply(
343     \@renewcount,
344     [ 0, 0, 0, 0, 0, 0 ], # FIXME Need to be fixed
345     "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
346 );
347 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
348 is_deeply(
349     \@renewcount,
350     [ 2, 0, 0, 0, 0, 0 ],
351     "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
352 );
353
354 #With something in DB
355 @renewcount = C4::Circulation::GetRenewCount();
356 is_deeply(
357     \@renewcount,
358     [ 0, 0, 0, 0, 0, 0 ],
359     "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
360 );
361 @renewcount = C4::Circulation::GetRenewCount(-1);
362 is_deeply(
363     \@renewcount,
364     [ 0, 0, 0, 0, 0, 0 ],
365     "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
366 );
367 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
368 is_deeply(
369     \@renewcount,
370     [ 2, 0, 0, 0, 0, 0 ],
371     "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
372 );
373
374 # Add a default rule: renewal is allowed
375 Koha::CirculationRules->set_rules(
376     {
377         categorycode => undef,
378         itemtype     => undef,
379         branchcode   => undef,
380         rules        => {
381             renewalsallowed => 3,
382         }
383     }
384 );
385 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
386 is_deeply(
387     \@renewcount,
388     [ 2, 3, 1, 0, 0, 0 ],
389     "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
390 );
391
392 AddRenewal(
393     {
394         borrowernumber  => $borrower_id1,
395         itemnumber      => $item_id1,
396         branch          => $branchcode_1,
397         datedue         => $datedue3,
398         lastreneweddate => $daysago10
399     }
400 );
401 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
402 is_deeply(
403     \@renewcount,
404     [ 3, 3, 0, 0, 0, 0 ],
405     "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
406 );
407
408 $dbh->do("DELETE FROM old_issues");
409 AddReturn($barcode_1);
410 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
411 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
412
413 $dbh->do("DELETE FROM old_issues");
414 C4::Circulation::AddIssue( $patron_1, $barcode_1, $daysago10, 0, $today );
415 AddReturn($barcode_1, undef, undef, dt_from_string('2014-04-01 23:42'));
416 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
417 ok( $return->{returndate} eq '2014-04-01 23:42:00', "Item returned with a return date of '2014-04-01 23:42' has that return date" );
418
419 my $itemnumber = Koha::Item->new(
420     {
421         biblionumber   => $biblionumber,
422         barcode        => 'barcode_3',
423         itemcallnumber => 'callnumber3',
424         homebranch     => $branchcode_1,
425         holdingbranch  => $branchcode_1,
426         notforloan     => 1,
427         itype          => $itemtype,
428         biblioitemnumber => $biblioitemnumber
429     },
430 )->store->itemnumber;
431
432 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
433 t::lib::Mocks::mock_preference( 'CataloguingLog', 1 );
434 my $log_count_before = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
435
436 AddReturn( 'barcode_3', $branchcode_1 );
437 my $item = Koha::Items->find( $itemnumber );
438 ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
439
440 my $updatenotforloanstatusoncheckin = "
441 _ALL_:\n
442  1: 0\n
443  9: 1\n\n
444 $itemtype:\n
445   1: 9
446 ";
447 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', $updatenotforloanstatusoncheckin );
448 AddReturn( 'barcode_3', $branchcode_1 );
449 $item = Koha::Items->find( $itemnumber );
450 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin prioritises item type specific rule over _ALL_ rules} );
451 my $log_count_after = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
452 is($log_count_before, $log_count_after, "Change from UpdateNotForLoanStatusOnCheckin is not logged");
453
454 AddReturn( 'barcode_3', $branchcode_1 );
455 $item = Koha::Items->find( $itemnumber );
456 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin uses item type specific rules even if they do not target the returned items' notforloan value} );
457
458 # Change the returning item to an item type without a rule
459 Koha::Items->find( $itemnumber )->itype( $itemtype2 )->store;
460 Koha::Items->find( $itemnumber )->notforloan( 1 )->store;
461 AddReturn( 'barcode_3', $branchcode_1 );
462 $item = Koha::Items->find( $itemnumber );
463 ok( $item->notforloan eq 0, q{UpdateNotForLoanStatusOnCheckin _ALL_ rules are applied if there are no specific item type rule matching the returned item} );
464
465 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: ONLYMESSAGE' );
466 $item->notforloan(1)->store;
467 AddReturn( 'barcode_3', $branchcode_1 );
468 $item = Koha::Items->find( $itemnumber );
469 ok( $item->notforloan eq 1, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 1 with setting "1: ONLYMESSAGE"} );
470
471 my $itemnumber2 = Koha::Item->new(
472     {
473         biblionumber   => $biblionumber,
474         barcode        => 'barcode_4',
475         itemcallnumber => 'callnumber4',
476         homebranch     => $branchcode_1,
477         holdingbranch  => $branchcode_1,
478         location       => 'FIC',
479         itype          => $itemtype
480     }
481 )->store->itemnumber;
482
483 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', q{} );
484 AddReturn( 'barcode_4', $branchcode_1 );
485 my $item2 = Koha::Items->find( $itemnumber2 );
486 ok( $item2->location eq 'FIC', 'UpdateItemLocationOnCheckin does not modify value when not enabled' );
487
488 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', 'FIC: GEN' );
489 $log_count_before = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
490 AddReturn( 'barcode_4', $branchcode_1 );
491 $item2 = Koha::Items->find( $itemnumber2 );
492 is( $item2->location, 'GEN', q{UpdateItemLocationOnCheckin updates location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
493 is( $item2->permanent_location, 'GEN', q{UpdateItemLocationOnCheckin updates permanent_location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
494 $log_count_after = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
495 is($log_count_before, $log_count_after, "Change from UpdateNotForLoanStatusOnCheckin is not logged");
496 AddReturn( 'barcode_4', $branchcode_1 );
497 $item2 = Koha::Items->find( $itemnumber2 );
498 ok( $item2->location eq 'GEN', q{UpdateItemLocationOnCheckin does not update location value from 'GEN' with setting "FIC: GEN"} );
499
500 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', '_ALL_: CART' );
501 AddReturn( 'barcode_4', $branchcode_1 );
502 $item2 = Koha::Items->find( $itemnumber2 );
503 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART"} );
504 Koha::Item::Transfer->new({
505     itemnumber => $itemnumber2,
506     frombranch => $branchcode_2,
507     tobranch => $branchcode_1,
508     datesent => '2020-01-01'
509 })->store;
510 AddReturn( 'barcode_4', $branchcode_1 );
511 $item2 = Koha::Items->find( $itemnumber2 );
512 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART" when transfer filled} );
513
514 ok( $item2->permanent_location eq 'GEN', q{UpdateItemLocationOnCheckin does not update permanent_location value from 'GEN' with setting "_ALL_: CART"} );
515 AddIssue( $patron_1, 'barcode_4', $daysago10,0, $today, '' );
516 $item2 = Koha::Items->find( $itemnumber2 );
517 ok( $item2->location eq 'GEN', q{Location updates from 'CART' to permanent location on issue} );
518
519 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', "GEN: _BLANK_\n_BLANK_: PROC\nPROC: _PERM_" );
520 AddReturn( 'barcode_4', $branchcode_1 );
521 $item2 = Koha::Items->find( $itemnumber2 );
522 ok( $item2->location eq '', q{UpdateItemLocationOnCheckin updates location value from 'GEN' to '' with setting "GEN: _BLANK_"} );
523 AddReturn( 'barcode_4', $branchcode_1 );
524 $item2 = Koha::Items->find( $itemnumber2 );
525 ok( $item2->location eq 'PROC' , q{UpdateItemLocationOnCheckin updates location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
526 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
527 AddReturn( 'barcode_4', $branchcode_1 );
528 $item2 = Koha::Items->find( $itemnumber2 );
529 ok( $item2->location eq '' , q{UpdateItemLocationOnCheckin updates location value from 'PROC' to '' with setting "PROC: _PERM_" } );
530 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location from '' with setting "PROC: _PERM_" } );
531
532 # Bug 28472
533 my $itemnumber3 = Koha::Item->new(
534     {
535         biblionumber   => $biblionumber,
536         barcode        => 'barcode_5',
537         itemcallnumber => 'callnumber5',
538         homebranch     => $branchcode_1,
539         holdingbranch  => $branchcode_1,
540         location       => undef,
541         itype          => $itemtype
542     }
543 )->store->itemnumber;
544
545 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', '_ALL_: CART' );
546 AddReturn( 'barcode_5', $branchcode_1 );
547 my $item3 = Koha::Items->find( $itemnumber3 );
548 is( $item3->location, 'CART', q{UpdateItemLocationOnCheckin updates location value from NULL (i.e. the item has no shelving location set) to 'CART' with setting "_ALL_: CART"} );
549
550
551
552 # Bug 14640 - Cancel the hold on checking out if asked
553 Koha::Items->find({ barcode => $barcode_1 })->notforloan('0')->store;
554 my $reserve_id = AddReserve(
555     {
556         branchcode     => $branchcode_1,
557         borrowernumber => $borrower_id1,
558         biblionumber   => $biblionumber,
559         priority       => 1,
560         notes          => "a note",
561         title          => "a title",
562     }
563 );
564 ok( $reserve_id, 'The reserve should have been inserted' );
565 AddIssue( $patron_2, $barcode_1, dt_from_string, 'cancel' );
566 my $hold = Koha::Holds->find( $reserve_id );
567 is( $hold, undef, 'The reserve should have been correctly cancelled' );
568
569 # Unseen rewnewals
570 t::lib::Mocks::mock_preference('UnseenRenewals', 1);
571 # Add a default circ rule: 3 unseen renewals allowed
572 Koha::CirculationRules->set_rules(
573     {
574         categorycode => undef,
575         itemtype     => undef,
576         branchcode   => undef,
577         rules        => {
578             renewalsallowed => 10,
579             unseen_renewals_allowed => 3
580         }
581     }
582 );
583
584 my $unseen_library = $builder->build_object( { class => 'Koha::Libraries' } );
585 my $unseen_patron  = $builder->build_object( { class => 'Koha::Patrons' } );
586 my $unseen_item = $builder->build_sample_item(
587     { library => $unseen_library->branchcode, itype => $itemtype } );
588 my $unseen_issue = C4::Circulation::AddIssue( $unseen_patron, $unseen_item->barcode );
589
590 # Does an unseen renewal increment the issue's count
591 my ( $unseen_before ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
592 AddRenewal(
593     {
594         borrowernumber => $unseen_patron->borrowernumber,
595         itemnumber     => $unseen_item->itemnumber,
596         branch         => $branchcode_1,
597         seen           => 0
598     }
599 );
600 my ( $unseen_after ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
601 is( $unseen_after, $unseen_before + 1, 'unseen_renewals increments' );
602
603 # Does a seen renewal reset the unseen count
604 AddRenewal(
605     {
606         borrowernumber => $unseen_patron->borrowernumber,
607         itemnumber     => $unseen_item->itemnumber,
608         branch         => $branchcode_1,
609         seen           => 1
610     }
611 );
612 my ( $unseen_reset ) = ( C4::Circulation::GetRenewCount( $unseen_patron->borrowernumber, $unseen_item->itemnumber ) )[3];
613 is( $unseen_reset, 0, 'seen renewal resets the unseen count' );
614
615 my $itemnumber4 = Koha::Item->new(
616     {
617         biblionumber   => $biblionumber,
618         barcode        => 'barcode_6',
619         itemcallnumber => 'callnumber6',
620         homebranch     => $branchcode_1,
621         holdingbranch  => $branchcode_1,
622         notforloan     => -1,
623         itype          => $itemtype,
624         location       => 'loc1'
625     },
626 )->store->itemnumber;
627
628 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckout', q{} );
629 AddIssue( $patron_2, 'barcode_6', dt_from_string );
630 $item = Koha::Items->find( $itemnumber4 );
631 ok( $item->notforloan eq -1, 'UpdateNotForLoanStatusOnCheckout does not modify value when not enabled' );
632
633 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckout', '-1: 0' );
634 AddReturn( 'barcode_6', $branchcode_1 );
635 my $test = AddIssue( $patron_2, 'barcode_6', dt_from_string );
636 $item = Koha::Items->find( $itemnumber4 );
637 ok( $item->notforloan eq 0, q{UpdateNotForLoanStatusOnCheckout updates notforloan value from -1 to 0 with setting "-1: 0"} );
638
639 AddIssue( $patron_2, 'barcode_6', dt_from_string );
640 AddReturn( 'barcode_6', $branchcode_1 );
641 $item = Koha::Items->find( $itemnumber4 );
642 ok( $item->notforloan eq 0, q{UpdateNotForLoanStatusOnCheckout does not update notforloan value from 0 with setting "-1: 0"} );
643
644 #End transaction
645 $schema->storage->txn_rollback;