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