Bug 28630: Add unit tests
[koha.git] / t / db_dependent / ILSDI_Services.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 CGI qw ( -utf8 );
21
22 use Test::More tests => 10;
23 use Test::MockModule;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Items qw( ModItemTransfer );
28 use C4::Circulation;
29
30 use Koha::AuthUtils;
31 use Koha::DateUtils;
32
33 BEGIN {
34     use_ok('C4::ILSDI::Services');
35 }
36
37 my $schema  = Koha::Database->schema;
38 my $dbh     = C4::Context->dbh;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'AuthenticatePatron test' => sub {
42
43     plan tests => 16;
44
45     $schema->storage->txn_begin;
46
47     my $plain_password = 'tomasito';
48
49     $builder->build({
50         source => 'Borrower',
51         value => {
52             cardnumber => undef,
53         }
54     });
55
56     my $borrower = $builder->build({
57         source => 'Borrower',
58         value  => {
59             cardnumber => undef,
60             password => Koha::AuthUtils::hash_password( $plain_password ),
61             lastseen => "2001-01-01"
62         }
63     });
64
65     my $query = CGI->new;
66     $query->param( 'username', $borrower->{userid});
67     $query->param( 'password', $plain_password);
68
69     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
70     my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
71     is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
72     is( $reply->{code}, undef, "Error code undef");
73     my $seen_patron = Koha::Patrons->find({ borrowernumber => $reply->{id} });
74     is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({str => '2001-01-01', dateonly => 1}),'Last seen not updated if not tracking patrons');
75
76     $query->param('password','ilsdi-passworD');
77     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
78     is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
79     is( $reply->{id}, undef, "id undef");
80
81     $query->param( 'password', $plain_password );
82     $query->param( 'username', 'wrong-ilsdi-useriD' );
83     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
84     is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
85     is( $reply->{id}, undef, "id undef");
86
87     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
88     $query->param( 'username', uc( $borrower->{userid} ));
89     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
90     is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
91     is( $reply->{code}, undef, "Error code undef");
92     $seen_patron = Koha::Patrons->find({ borrowernumber => $reply->{id} });
93     is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated to today if tracking patrons');
94
95     $query->param( 'username', $borrower->{cardnumber} );
96     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
97     is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
98     is( $reply->{code}, undef, "Error code undef" );
99
100     $query->param( 'password', 'ilsdi-passworD' );
101     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
102     is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
103     is( $reply->{id}, undef, "id undef" );
104
105     $query->param( 'username', 'randomcardnumber1234' );
106     $query->param( 'password', $plain_password );
107     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
108     is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
109     is( $reply->{id}, undef, "id undef");
110
111     $schema->storage->txn_rollback;
112 };
113
114
115 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
116
117     plan tests => 5;
118
119     $schema->storage->txn_begin;
120
121     $schema->resultset( 'Issue' )->delete_all;
122     $schema->resultset( 'Borrower' )->delete_all;
123     $schema->resultset( 'BorrowerAttribute' )->delete_all;
124     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
125     $schema->resultset( 'Category' )->delete_all;
126     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
127     $schema->resultset( 'Club' )->delete_all;
128     $schema->resultset( 'Branch' )->delete_all;
129
130     # Configure Koha to enable ILS-DI server and extended attributes:
131     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
132     t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
133
134     # Set up a library/branch for our user to belong to:
135     my $lib = $builder->build( {
136         source => 'Branch',
137         value => {
138             branchcode => 'T_ILSDI',
139         }
140     } );
141
142     # Create a new category for user to belong to:
143     my $cat = $builder->build( {
144         source => 'Category',
145         value  => {
146             category_type                 => 'A',
147             BlockExpiredPatronOpacActions => -1,
148         }
149     } );
150
151     # Create a new attribute type:
152     my $attr_type = $builder->build( {
153         source => 'BorrowerAttributeType',
154         value  => {
155             code                      => 'HIDEME',
156             opac_display              => 0,
157             authorised_value_category => '',
158             class                     => '',
159         }
160     } );
161     my $attr_type_visible = $builder->build( {
162         source => 'BorrowerAttributeType',
163         value  => {
164             code                      => 'SHOWME',
165             opac_display              => 1,
166             authorised_value_category => '',
167             class                     => '',
168         }
169     } );
170
171     # Create a new user:
172     my $brwr = $builder->build( {
173         source => 'Borrower',
174         value  => {
175             categorycode => $cat->{'categorycode'},
176             branchcode   => $lib->{'branchcode'},
177         }
178     } );
179
180     # Authorised value:
181     my $auth = $builder->build( {
182         source => 'AuthorisedValue',
183         value  => {
184             category => $cat->{'categorycode'}
185         }
186     } );
187
188     # Set the new attribute for our user:
189     my $attr_hidden = $builder->build( {
190         source => 'BorrowerAttribute',
191         value  => {
192             borrowernumber => $brwr->{'borrowernumber'},
193             code           => $attr_type->{'code'},
194             attribute      => '1337 hidden',
195         }
196     } );
197     my $attr_shown = $builder->build( {
198         source => 'BorrowerAttribute',
199         value  => {
200             borrowernumber => $brwr->{'borrowernumber'},
201             code           => $attr_type_visible->{'code'},
202             attribute      => '1337 shown',
203         }
204     } );
205
206     my $fine = $builder->build(
207         {
208             source => 'Accountline',
209             value  => {
210                 borrowernumber    => $brwr->{borrowernumber},
211                 debit_type_code   => 'OVERDUE',
212                 amountoutstanding => 10
213             }
214         }
215     );
216
217     # Prepare and send web request for IL-SDI server:
218     my $query = CGI->new;
219     $query->param( 'service', 'GetPatronInfo' );
220     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
221     $query->param( 'show_attributes', '1' );
222     $query->param( 'show_fines', '1' );
223
224     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
225
226     # Build a structure for comparison:
227     my $cmp = {
228         borrowernumber    => $brwr->{borrowernumber},
229         value             => $attr_shown->{'attribute'},
230         value_description => $attr_shown->{'attribute'},
231         %$attr_type_visible,
232         %$attr_shown,
233     };
234
235     is( $reply->{'charges'}, '10.00',
236         'The \'charges\' attribute should be correctly filled (bug 17836)' );
237
238     is( scalar( @{$reply->{fines}->{fine}}), 1, 'There should be only 1 account line');
239     is(
240         $reply->{fines}->{fine}->[0]->{accountlines_id},
241         $fine->{accountlines_id},
242         "The accountline should be the correct one"
243     );
244
245     # Check results:
246     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
247
248     ok( exists $reply->{is_expired}, 'There should be the is_expired information');
249
250     # Cleanup
251     $schema->storage->txn_rollback;
252 };
253
254 subtest 'LookupPatron test' => sub {
255
256     plan tests => 9;
257
258     $schema->storage->txn_begin;
259
260     $schema->resultset( 'Issue' )->delete_all;
261     $schema->resultset( 'Borrower' )->delete_all;
262     $schema->resultset( 'BorrowerAttribute' )->delete_all;
263     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
264     $schema->resultset( 'Category' )->delete_all;
265     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
266     $schema->resultset( 'Branch' )->delete_all;
267
268     my $borrower = $builder->build({
269         source => 'Borrower',
270     });
271
272     my $query = CGI->new();
273     my $bad_result = C4::ILSDI::Services::LookupPatron($query);
274     is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
275
276     $query->delete_all();
277     $query->param( 'id', $borrower->{firstname} );
278     my $optional_result = C4::ILSDI::Services::LookupPatron($query);
279     is(
280         $optional_result->{id},
281         $borrower->{borrowernumber},
282         'Valid Firstname only'
283     );
284
285     $query->delete_all();
286     $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
287     my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
288     is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
289
290     foreach my $id_type (
291         'cardnumber',
292         'userid',
293         'email',
294         'borrowernumber',
295         'surname',
296         'firstname'
297     ) {
298         $query->delete_all();
299         $query->param( 'id_type', $id_type );
300         $query->param( 'id', $borrower->{$id_type} );
301         my $result = C4::ILSDI::Services::LookupPatron($query);
302         is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
303     }
304
305     # Cleanup
306     $schema->storage->txn_rollback;
307 };
308
309 subtest 'Holds test' => sub {
310
311     plan tests => 9;
312
313     $schema->storage->txn_begin;
314
315     t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
316
317     my $patron = $builder->build({
318         source => 'Borrower',
319     });
320
321     my $item = $builder->build_sample_item(
322         {
323             damaged => 1
324         }
325     );
326
327     my $query = CGI->new;
328     $query->param( 'patron_id', $patron->{borrowernumber});
329     $query->param( 'bib_id', $item->biblionumber);
330
331     my $reply = C4::ILSDI::Services::HoldTitle( $query );
332     is( $reply->{code}, 'damaged', "Item damaged" );
333
334     $item->damaged(0)->store;
335
336     my $hold = $builder->build({
337         source => 'Reserve',
338         value => {
339             borrowernumber => $patron->{borrowernumber},
340             biblionumber => $item->biblionumber,
341             itemnumber => $item->itemnumber
342         }
343     });
344
345     $reply = C4::ILSDI::Services::HoldTitle( $query );
346     is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
347
348     my $biblio_with_no_item = $builder->build_sample_biblio;
349
350     $query = CGI->new;
351     $query->param( 'patron_id', $patron->{borrowernumber});
352     $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
353
354     $reply = C4::ILSDI::Services::HoldTitle( $query );
355     is( $reply->{code}, 'NoItems', 'Biblio has no item' );
356
357     my $item2 = $builder->build_sample_item(
358         {
359             damaged => 0,
360         }
361     );
362
363     t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
364     Koha::CirculationRules->set_rule(
365         {
366             categorycode => $patron->{categorycode},
367             itemtype     => $item2->{itype},
368             branchcode   => $patron->{branchcode},
369             rule_name    => 'reservesallowed',
370             rule_value   => 1,
371         }
372     );
373
374     $query = CGI->new;
375     $query->param( 'patron_id', $patron->{borrowernumber});
376     $query->param( 'bib_id', $item2->biblionumber);
377     $query->param( 'item_id', $item2->itemnumber);
378
379     $reply = C4::ILSDI::Services::HoldItem( $query );
380     is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
381
382     Koha::CirculationRules->set_rule(
383         {
384             categorycode => $patron->{categorycode},
385             itemtype     => $item2->{itype},
386             branchcode   => $patron->{branchcode},
387             rule_name    => 'reservesallowed',
388             rule_value   => 0,
389         }
390     );
391
392     $query = CGI->new;
393     $query->param( 'patron_id', $patron->{borrowernumber});
394     $query->param( 'bib_id', $item2->biblionumber);
395     $query->param( 'item_id', $item2->itemnumber);
396
397     $reply = C4::ILSDI::Services::HoldItem( $query );
398     is( $reply->{code}, 'noReservesAllowed', "No reserves allowed" );
399
400     my $origin_branch = $builder->build(
401         {
402             source => 'Branch',
403             value  => {
404                 pickup_location => 1,
405             }
406         }
407     );
408
409     # Adding a holdable item.
410     my $item3 = $builder->build_sample_item(
411        {
412            barcode => '123456789',
413            library => $origin_branch->{branchcode}
414        });
415
416     my $item4 = $builder->build_sample_item(
417         {
418            biblionumber => $item3->biblionumber,
419            damaged => 1,
420            library => $origin_branch->{branchcode}
421        });
422
423     Koha::CirculationRules->set_rule(
424         {
425             categorycode => $patron->{categorycode},
426             itemtype     => $item3->{itype},
427             branchcode   => $patron->{branchcode},
428             rule_name    => 'reservesallowed',
429             rule_value   => 10,
430         }
431     );
432
433     $query = CGI->new;
434     $query->param( 'patron_id', $patron->{borrowernumber});
435     $query->param( 'bib_id', $item4->biblionumber);
436     $query->param( 'item_id', $item4->itemnumber);
437
438     $reply = C4::ILSDI::Services::HoldItem( $query );
439     is( $reply->{code}, 'damaged', "Item is damaged" );
440
441     my $module = Test::MockModule->new('C4::Context');
442     $module->mock('userenv', sub { { patron => $patron } });
443     my $issue = C4::Circulation::AddIssue($patron, $item3->barcode);
444     t::lib::Mocks::mock_preference( 'AllowHoldsOnPatronsPossessions', '0' );
445
446     $query = CGI->new;
447     $query->param( 'patron_id', $patron->{borrowernumber});
448     $query->param( 'bib_id', $item3->biblionumber);
449     $query->param( 'item_id', $item3->itemnumber);
450     $query->param( 'pickup_location', $origin_branch->{branchcode});
451     $reply = C4::ILSDI::Services::HoldItem( $query );
452
453     is( $reply->{code}, 'alreadypossession', "Patron has issued same book" );
454     is( $reply->{pickup_location}, undef, "No reserve placed");
455
456     # Test Patron cannot reserve if expired and BlockExpiredPatronOpacActions
457     my $category = $builder->build({
458         source => 'Category',
459         value => { BlockExpiredPatronOpacActions => -1 }
460         });
461
462     my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
463
464     my $expired_borrowernumber = Koha::Patron->new({
465         firstname =>  'Expired',
466         surname => 'Patron',
467         categorycode => $category->{categorycode},
468         branchcode => $branch_1,
469         dateexpiry => '2000-01-01',
470     })->store->borrowernumber;
471
472     t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
473
474     my $item5 = $builder->build({
475         source => 'Item',
476         value => {
477             biblionumber => $biblio_with_no_item->biblionumber,
478             damaged => 0,
479         }
480     });
481
482     $query = CGI->new;
483     $query->param( 'patron_id', $expired_borrowernumber);
484     $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
485     $query->param( 'item_id', $item5->{itemnumber});
486
487     $reply = C4::ILSDI::Services::HoldItem( $query );
488     is( $reply->{code}, 'PatronExpired', "Patron is expired" );
489
490     $schema->storage->txn_rollback;
491 };
492
493 subtest 'Holds test for branch transfer limits' => sub {
494
495     plan tests => 6;
496
497     $schema->storage->txn_begin;
498
499     # Test enforement of branch transfer limits
500     t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
501     t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
502
503     my $patron = $builder->build({
504         source => 'Borrower',
505     });
506
507     my $origin_branch = $builder->build(
508         {
509             source => 'Branch',
510             value  => {
511                 pickup_location => 1,
512             }
513         }
514     );
515     my $pickup_branch = $builder->build(
516         {
517             source => 'Branch',
518             value  => {
519                 pickup_location => 1,
520             }
521         }
522     );
523
524     my $item = $builder->build_sample_item(
525         {
526             library => $origin_branch->{branchcode},
527         }
528     );
529
530     Koha::CirculationRules->set_rule(
531         {
532             categorycode => undef,
533             itemtype     => undef,
534             branchcode   => undef,
535             rule_name    => 'reservesallowed',
536             rule_value   => 99,
537         }
538     );
539
540     my $limit = Koha::Item::Transfer::Limit->new({
541         toBranch => $pickup_branch->{branchcode},
542         fromBranch => $item->holdingbranch,
543         itemtype => $item->effective_itemtype,
544     })->store();
545
546     my $query = CGI->new;
547     $query->param( 'pickup_location', $pickup_branch->{branchcode} );
548     $query->param( 'patron_id', $patron->{borrowernumber});
549     $query->param( 'bib_id', $item->biblionumber);
550     $query->param( 'item_id', $item->itemnumber);
551
552     my $reply = C4::ILSDI::Services::HoldItem( $query );
553     is( $reply->{code}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
554
555     $reply = C4::ILSDI::Services::HoldTitle( $query );
556     is( $reply->{code}, 'cannotBeTransferred', "Record hold, Item cannot be transferred" );
557
558     t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
559
560     $reply = C4::ILSDI::Services::HoldItem( $query );
561     is( $reply->{code}, undef, "Item hold, Item can be transferred" );
562     my $hold = Koha::Holds->search({ itemnumber => $item->itemnumber, borrowernumber => $patron->{borrowernumber} })->next;
563     is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
564
565     Koha::Holds->search()->delete();
566
567     $reply = C4::ILSDI::Services::HoldTitle( $query );
568     is( $reply->{code}, undef, "Record hold, Item con be transferred" );
569     $hold = Koha::Holds->search({ biblionumber => $item->biblionumber, borrowernumber => $patron->{borrowernumber} })->next;
570     is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
571
572     $schema->storage->txn_rollback;
573 };
574
575 subtest 'Holds test with start_date and end_date' => sub {
576
577     plan tests => 8;
578
579     $schema->storage->txn_begin;
580
581     my $pickup_library = $builder->build_object(
582         {
583             class  => 'Koha::Libraries',
584             value  => {
585                 pickup_location => 1,
586             }
587         }
588     );
589
590     my $patron = $builder->build_object({
591         class => 'Koha::Patrons',
592     });
593
594     my $item = $builder->build_sample_item({ library => $pickup_library->branchcode });
595
596     Koha::CirculationRules->set_rule(
597         {
598             categorycode => undef,
599             itemtype     => undef,
600             branchcode   => undef,
601             rule_name    => 'reservesallowed',
602             rule_value   => 99,
603         }
604     );
605
606     my $query = CGI->new;
607     $query->param( 'pickup_location', $pickup_library->branchcode );
608     $query->param( 'patron_id', $patron->borrowernumber);
609     $query->param( 'bib_id', $item->biblionumber);
610     $query->param( 'item_id', $item->itemnumber);
611     $query->param( 'start_date', '2020-03-20');
612     $query->param( 'expiry_date', '2020-04-22');
613
614     my $reply = C4::ILSDI::Services::HoldItem( $query );
615     is ($reply->{pickup_location}, $pickup_library->branchname, "Item hold with date parameters was placed");
616     my $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
617     is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
618     is( $hold->reservedate, '2020-03-20', "Item hold has correct start date" );
619     is( $hold->expirationdate, '2020-04-22', "Item hold has correct end date" );
620
621     $hold->delete();
622
623     $reply = C4::ILSDI::Services::HoldTitle( $query );
624     is ($reply->{pickup_location}, $pickup_library->branchname, "Record hold with date parameters was placed");
625     $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
626     is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
627     is( $hold->reservedate, '2020-03-20', "Record hold has correct start date" );
628     is( $hold->expirationdate, '2020-04-22', "Record hold has correct end date" );
629
630     $schema->storage->txn_rollback;
631 };
632
633 subtest 'GetRecords' => sub {
634
635     plan tests => 8;
636
637     $schema->storage->txn_begin;
638
639     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
640
641     my $branch1 = $builder->build({
642         source => 'Branch',
643     });
644     my $branch2 = $builder->build({
645         source => 'Branch',
646     });
647
648     my $item = $builder->build_sample_item(
649         {
650             library => $branch1->{branchcode},
651         }
652     );
653
654     my $patron = $builder->build({
655         source => 'Borrower',
656     });
657
658     my $issue = $builder->build({
659         source => 'Issue',
660         value => {
661             itemnumber => $item->itemnumber,
662         }
663     });
664
665     my $hold = $builder->build({
666         source => 'Reserve',
667         value => {
668             biblionumber => $item->biblionumber,
669         }
670     });
671
672     ModItemTransfer($item->itemnumber, $branch1->{branchcode}, $branch2->{branchcode}, 'Manual');
673
674     my $cgi = CGI->new;
675     $cgi->param(service => 'GetRecords');
676     $cgi->param(id => $item->biblionumber);
677
678     my $reply = C4::ILSDI::Services::GetRecords($cgi);
679
680     my $transfer = $item->get_transfer;
681     my $expected = {
682         datesent => $transfer->datesent,
683         frombranch => $transfer->frombranch,
684         tobranch => $transfer->tobranch,
685     };
686     is_deeply($reply->{record}->[0]->{items}->{item}->[0]->{transfer}, $expected,
687         'GetRecords returns transfer informations');
688
689     # Check informations exposed
690     my $reply_issue = $reply->{record}->[0]->{issues}->{issue}->[0];
691     is($reply_issue->{itemnumber}, $item->itemnumber, 'GetRecords has an issue tag');
692     is($reply_issue->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in issue tag');
693     is($reply_issue->{surname}, undef, 'GetRecords does not expose surname in issue tag');
694     is($reply_issue->{firstname}, undef, 'GetRecords does not expose firstname in issue tag');
695     is($reply_issue->{cardnumber}, undef, 'GetRecords does not expose cardnumber in issue tag');
696     my $reply_reserve = $reply->{record}->[0]->{reserves}->{reserve}->[0];
697     is($reply_reserve->{biblionumber}, $item->biblionumber, 'GetRecords has a reserve tag');
698     is($reply_reserve->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in reserve tag');
699
700     $schema->storage->txn_rollback;
701 };
702
703 subtest 'RenewHold' => sub {
704     plan tests => 4;
705
706     $schema->storage->txn_begin;
707
708     my $cgi    = CGI->new;
709     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
710     my $item   = $builder->build_sample_item;
711     $cgi->param( patron_id => $patron->borrowernumber );
712     $cgi->param( item_id   => $item->itemnumber );
713
714     t::lib::Mocks::mock_userenv( { patron => $patron } );    # For AddIssue
715     my $checkout = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
716
717     # Everything is ok
718     my $reply = C4::ILSDI::Services::RenewLoan($cgi);
719     is( exists $reply->{date_due}, 1, 'If the item is checked out, the date_due key should exist' );
720
721     # The item is not checked out
722     $checkout->delete;
723     $reply = C4::ILSDI::Services::RenewLoan($cgi);
724     is( $reply, undef, 'If the item is not checked out, we should not explode.');    # FIXME We should return an error code instead
725
726     # The item does not exist
727     $item->delete;
728     $reply = C4::ILSDI::Services::RenewLoan($cgi);
729     is( $reply->{code}, 'RecordNotFound', 'If the item does not exist, RecordNotFound should be returned');
730
731     $patron->delete;
732     $reply = C4::ILSDI::Services::RenewLoan($cgi);
733     is( $reply->{code}, 'PatronNotFound', 'If the patron does not exist, PatronNotFound should be returned');
734
735     $schema->storage->txn_rollback;
736 };
737
738 subtest 'GetPatronInfo paginated loans' => sub {
739     plan tests => 7;
740
741     $schema->storage->txn_begin;
742
743     my $library = $builder->build_object({
744         class => 'Koha::Libraries',
745     });
746
747     my $item1 = $builder->build_sample_item({ library => $library->branchcode });
748     my $item2 = $builder->build_sample_item({ library => $library->branchcode });
749     my $item3 = $builder->build_sample_item({ library => $library->branchcode });
750     my $patron = $builder->build_object({
751         class => 'Koha::Patrons',
752         value => {
753             branchcode => $library->branchcode,
754         },
755     });
756     my $module = Test::MockModule->new('C4::Context');
757     $module->mock('userenv', sub { { branch => $library->branchcode } });
758     my $date_due = Koha::DateUtils::dt_from_string()->add(weeks => 2);
759     my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
760     my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
761     my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
762     my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
763     my $issue3 = C4::Circulation::AddIssue($patron->unblessed, $item3->barcode, $date_due);
764     my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
765
766     my $cgi = CGI->new;
767
768     $cgi->param( 'service', 'GetPatronInfo' );
769     $cgi->param( 'patron_id', $patron->borrowernumber );
770     $cgi->param( 'show_loans', '1' );
771     $cgi->param( 'loans_per_page', '2' );
772     $cgi->param( 'loans_page', '1' );
773     my $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
774
775     is($reply->{total_loans}, 3, 'total_loans == 3');
776     is(scalar @{ $reply->{loans}->{loan} }, 2, 'GetPatronInfo returned only 2 loans');
777     is($reply->{loans}->{loan}->[0]->{itemnumber}, $item3->itemnumber);
778     is($reply->{loans}->{loan}->[1]->{itemnumber}, $item2->itemnumber);
779
780     $cgi->param( 'loans_page', '2' );
781     $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
782
783     is($reply->{total_loans}, 3, 'total_loans == 3');
784     is(scalar @{ $reply->{loans}->{loan} }, 1, 'GetPatronInfo returned only 1 loan');
785     is($reply->{loans}->{loan}->[0]->{itemnumber}, $item1->itemnumber);
786
787     $schema->storage->txn_rollback;
788 };