3 # This file is part of Koha.
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.
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.
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>.
22 use Test::More tests => 12;
25 use t::lib::TestBuilder;
29 use C4::Items qw( ModItemTransfer );
30 use C4::Circulation qw( AddIssue );
31 use C4::Reserves qw (AddReserve ModReserve ModReserveAffect ModReserveStatus);
34 use Koha::DateUtils qw( dt_from_string );
37 use_ok('C4::ILSDI::Services', qw( AuthenticatePatron GetPatronInfo LookupPatron HoldTitle HoldItem GetRecords RenewLoan GetAvailability ));
40 my $schema = Koha::Database->schema;
41 my $dbh = C4::Context->dbh;
42 my $builder = t::lib::TestBuilder->new;
44 subtest 'AuthenticatePatron test' => sub {
48 $schema->storage->txn_begin;
50 my $plain_password = 'tomasito';
59 my $borrower = $builder->build({
63 password => Koha::AuthUtils::hash_password( $plain_password ),
64 lastseen => "2001-01-01 12:34:56"
69 $query->param( 'username', $borrower->{userid});
70 $query->param( 'password', $plain_password);
72 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
73 my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
74 is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
75 is( $reply->{code}, undef, "Error code undef");
76 my $seen_patron = Koha::Patrons->find({ borrowernumber => $reply->{id} });
77 is( $seen_patron->lastseen(), '2001-01-01 12:34:56','Last seen not updated if not tracking patrons');
79 $query->param('password','ilsdi-passworD');
80 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
81 is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
82 is( $reply->{id}, undef, "id undef");
84 $query->param( 'password', $plain_password );
85 $query->param( 'username', 'wrong-ilsdi-useriD' );
86 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
87 is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
88 is( $reply->{id}, undef, "id undef");
90 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
91 $query->param( 'username', uc( $borrower->{userid} ));
92 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
93 my $now = dt_from_string;
94 is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
95 is( $reply->{code}, undef, "Error code undef");
96 $seen_patron = Koha::Patrons->find({ borrowernumber => $reply->{id} });
97 is( t::lib::Dates::compare( $seen_patron->lastseen, $now), 0, 'Last seen updated to today if tracking patrons' );
99 $query->param( 'username', $borrower->{cardnumber} );
100 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
101 is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
102 is( $reply->{code}, undef, "Error code undef" );
104 $query->param( 'password', 'ilsdi-passworD' );
105 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
106 is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
107 is( $reply->{id}, undef, "id undef" );
109 $query->param( 'username', 'randomcardnumber1234' );
110 $query->param( 'password', $plain_password );
111 $reply = C4::ILSDI::Services::AuthenticatePatron($query);
112 is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
113 is( $reply->{id}, undef, "id undef");
115 $schema->storage->txn_rollback;
119 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
123 $schema->storage->txn_begin;
125 $schema->resultset( 'Issue' )->delete_all;
126 $schema->resultset( 'Borrower' )->delete_all;
127 $schema->resultset( 'BorrowerAttribute' )->delete_all;
128 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
129 $schema->resultset( 'Category' )->delete_all;
130 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
131 $schema->resultset( 'Club' )->delete_all;
132 $schema->resultset( 'Branch' )->delete_all;
134 # Configure Koha to enable ILS-DI server and extended attributes:
135 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
136 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
138 # Set up a library/branch for our user to belong to:
139 my $lib = $builder->build( {
142 branchcode => 'T_ILSDI',
146 # Create a new category for user to belong to:
147 my $cat = $builder->build( {
148 source => 'Category',
150 category_type => 'A',
151 BlockExpiredPatronOpacActions => -1,
155 # Create a new attribute type:
156 my $attr_type = $builder->build( {
157 source => 'BorrowerAttributeType',
161 authorised_value_category => '',
165 my $attr_type_visible = $builder->build( {
166 source => 'BorrowerAttributeType',
170 authorised_value_category => '',
176 my $brwr = $builder->build( {
177 source => 'Borrower',
179 categorycode => $cat->{'categorycode'},
180 branchcode => $lib->{'branchcode'},
185 my $auth = $builder->build( {
186 source => 'AuthorisedValue',
188 category => $cat->{'categorycode'}
192 # Set the new attribute for our user:
193 my $attr_hidden = $builder->build( {
194 source => 'BorrowerAttribute',
196 borrowernumber => $brwr->{'borrowernumber'},
197 code => $attr_type->{'code'},
198 attribute => '1337 hidden',
201 my $attr_shown = $builder->build( {
202 source => 'BorrowerAttribute',
204 borrowernumber => $brwr->{'borrowernumber'},
205 code => $attr_type_visible->{'code'},
206 attribute => '1337 shown',
210 my $fine = $builder->build(
212 source => 'Accountline',
214 borrowernumber => $brwr->{borrowernumber},
215 debit_type_code => 'OVERDUE',
216 amountoutstanding => 10
221 # Prepare and send web request for IL-SDI server:
222 my $query = CGI->new;
223 $query->param( 'service', 'GetPatronInfo' );
224 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
225 $query->param( 'show_attributes', '1' );
226 $query->param( 'show_fines', '1' );
228 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
230 # Build a structure for comparison:
232 borrowernumber => $brwr->{borrowernumber},
233 value => $attr_shown->{'attribute'},
234 value_description => $attr_shown->{'attribute'},
239 is( $reply->{'charges'}, '10.00',
240 'The \'charges\' attribute should be correctly filled (bug 17836)' );
242 is( scalar( @{$reply->{fines}->{fine}}), 1, 'There should be only 1 account line');
244 $reply->{fines}->{fine}->[0]->{accountlines_id},
245 $fine->{accountlines_id},
246 "The accountline should be the correct one"
250 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
252 ok( exists $reply->{is_expired}, 'There should be the is_expired information');
255 $schema->storage->txn_rollback;
258 subtest 'LookupPatron test' => sub {
262 $schema->storage->txn_begin;
264 $schema->resultset( 'Issue' )->delete_all;
265 $schema->resultset( 'Borrower' )->delete_all;
266 $schema->resultset( 'BorrowerAttribute' )->delete_all;
267 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
268 $schema->resultset( 'Category' )->delete_all;
269 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
270 $schema->resultset( 'Branch' )->delete_all;
272 my $borrower = $builder->build({
273 source => 'Borrower',
276 my $query = CGI->new();
277 my $bad_result = C4::ILSDI::Services::LookupPatron($query);
278 is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
280 $query->delete_all();
281 $query->param( 'id', $borrower->{firstname} );
282 my $optional_result = C4::ILSDI::Services::LookupPatron($query);
284 $optional_result->{id},
285 $borrower->{borrowernumber},
286 'Valid Firstname only'
289 $query->delete_all();
290 $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
291 my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
292 is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
294 foreach my $id_type (
302 $query->delete_all();
303 $query->param( 'id_type', $id_type );
304 $query->param( 'id', $borrower->{$id_type} );
305 my $result = C4::ILSDI::Services::LookupPatron($query);
306 is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
310 $schema->storage->txn_rollback;
313 subtest 'Holds test' => sub {
317 $schema->storage->txn_begin;
319 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
321 my $patron = $builder->build({
322 source => 'Borrower',
325 my $item = $builder->build_sample_item(
331 my $query = CGI->new;
332 $query->param( 'patron_id', $patron->{borrowernumber});
333 $query->param( 'bib_id', $item->biblionumber);
335 my $reply = C4::ILSDI::Services::HoldTitle( $query );
336 is( $reply->{code}, 'damaged', "Item damaged" );
338 $item->damaged(0)->store;
340 my $hold = $builder->build({
343 borrowernumber => $patron->{borrowernumber},
344 biblionumber => $item->biblionumber,
345 itemnumber => $item->itemnumber
349 $reply = C4::ILSDI::Services::HoldTitle( $query );
350 is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
352 my $biblio_with_no_item = $builder->build_sample_biblio;
355 $query->param( 'patron_id', $patron->{borrowernumber});
356 $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
358 $reply = C4::ILSDI::Services::HoldTitle( $query );
359 is( $reply->{code}, 'NoItems', 'Biblio has no item' );
361 my $item2 = $builder->build_sample_item(
367 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
368 Koha::CirculationRules->set_rule(
370 categorycode => $patron->{categorycode},
371 itemtype => $item2->{itype},
372 branchcode => $patron->{branchcode},
373 rule_name => 'reservesallowed',
379 $query->param( 'patron_id', $patron->{borrowernumber});
380 $query->param( 'bib_id', $item2->biblionumber);
381 $query->param( 'item_id', $item2->itemnumber);
383 $reply = C4::ILSDI::Services::HoldItem( $query );
384 is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
386 Koha::CirculationRules->set_rule(
388 categorycode => $patron->{categorycode},
389 itemtype => $item2->{itype},
390 branchcode => $patron->{branchcode},
391 rule_name => 'reservesallowed',
397 $query->param( 'patron_id', $patron->{borrowernumber});
398 $query->param( 'bib_id', $item2->biblionumber);
399 $query->param( 'item_id', $item2->itemnumber);
401 $reply = C4::ILSDI::Services::HoldItem( $query );
402 is( $reply->{code}, 'noReservesAllowed', "No reserves allowed" );
404 my $origin_branch = $builder->build(
408 pickup_location => 1,
413 # Adding a holdable item.
414 my $item3 = $builder->build_sample_item(
416 barcode => '123456789',
417 library => $origin_branch->{branchcode}
420 my $item4 = $builder->build_sample_item(
422 biblionumber => $item3->biblionumber,
424 library => $origin_branch->{branchcode}
427 Koha::CirculationRules->set_rule(
429 categorycode => $patron->{categorycode},
430 itemtype => $item3->{itype},
431 branchcode => $patron->{branchcode},
432 rule_name => 'reservesallowed',
438 $query->param( 'patron_id', $patron->{borrowernumber});
439 $query->param( 'bib_id', $item4->biblionumber);
440 $query->param( 'item_id', $item4->itemnumber);
442 $reply = C4::ILSDI::Services::HoldItem( $query );
443 is( $reply->{code}, 'damaged', "Item is damaged" );
445 my $module = Test::MockModule->new('C4::Context');
446 $module->mock('userenv', sub { { patron => $patron } });
447 my $issue = C4::Circulation::AddIssue($patron, $item3->barcode);
448 t::lib::Mocks::mock_preference( 'AllowHoldsOnPatronsPossessions', '0' );
451 $query->param( 'patron_id', $patron->{borrowernumber});
452 $query->param( 'bib_id', $item3->biblionumber);
453 $query->param( 'item_id', $item3->itemnumber);
454 $query->param( 'pickup_location', $origin_branch->{branchcode});
455 $reply = C4::ILSDI::Services::HoldItem( $query );
457 is( $reply->{code}, 'alreadypossession', "Patron has issued same book" );
458 is( $reply->{pickup_location}, undef, "No reserve placed");
460 # Test Patron cannot reserve if expired and BlockExpiredPatronOpacActions
461 my $category = $builder->build({
462 source => 'Category',
463 value => { BlockExpiredPatronOpacActions => -1 }
466 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
468 my $expired_borrowernumber = Koha::Patron->new({
469 firstname => 'Expired',
471 categorycode => $category->{categorycode},
472 branchcode => $branch_1,
473 dateexpiry => '2000-01-01',
474 })->store->borrowernumber;
476 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
478 my $item5 = $builder->build({
481 biblionumber => $biblio_with_no_item->biblionumber,
487 $query->param( 'patron_id', $expired_borrowernumber);
488 $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
489 $query->param( 'item_id', $item5->{itemnumber});
491 $reply = C4::ILSDI::Services::HoldItem( $query );
492 is( $reply->{code}, 'PatronExpired', "Patron is expired" );
494 $schema->storage->txn_rollback;
497 subtest 'Holds test for branch transfer limits' => sub {
501 $schema->storage->txn_begin;
503 # Test enforement of branch transfer limits
504 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
505 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
507 my $patron = $builder->build({
508 source => 'Borrower',
511 my $origin_branch = $builder->build(
515 pickup_location => 1,
519 my $pickup_branch = $builder->build(
523 pickup_location => 1,
528 my $item = $builder->build_sample_item(
530 library => $origin_branch->{branchcode},
534 Koha::CirculationRules->set_rule(
536 categorycode => undef,
539 rule_name => 'reservesallowed',
544 my $limit = Koha::Item::Transfer::Limit->new({
545 toBranch => $pickup_branch->{branchcode},
546 fromBranch => $item->holdingbranch,
547 itemtype => $item->effective_itemtype,
550 my $query = CGI->new;
551 $query->param( 'pickup_location', $pickup_branch->{branchcode} );
552 $query->param( 'patron_id', $patron->{borrowernumber});
553 $query->param( 'bib_id', $item->biblionumber);
554 $query->param( 'item_id', $item->itemnumber);
556 my $reply = C4::ILSDI::Services::HoldItem( $query );
557 is( $reply->{code}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
559 $reply = C4::ILSDI::Services::HoldTitle( $query );
560 is( $reply->{code}, 'cannotBeTransferred', "Record hold, Item cannot be transferred" );
562 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
564 $reply = C4::ILSDI::Services::HoldItem( $query );
565 is( $reply->{code}, undef, "Item hold, Item can be transferred" );
566 my $hold = Koha::Holds->search({ itemnumber => $item->itemnumber, borrowernumber => $patron->{borrowernumber} })->next;
567 is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
569 Koha::Holds->search()->delete();
571 $reply = C4::ILSDI::Services::HoldTitle( $query );
572 is( $reply->{code}, undef, "Record hold, Item con be transferred" );
573 $hold = Koha::Holds->search({ biblionumber => $item->biblionumber, borrowernumber => $patron->{borrowernumber} })->next;
574 is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
576 $schema->storage->txn_rollback;
579 subtest 'Holds test with start_date and end_date' => sub {
583 $schema->storage->txn_begin;
585 my $pickup_library = $builder->build_object(
587 class => 'Koha::Libraries',
589 pickup_location => 1,
594 my $patron = $builder->build_object({
595 class => 'Koha::Patrons',
598 my $item = $builder->build_sample_item({ library => $pickup_library->branchcode });
600 Koha::CirculationRules->set_rule(
602 categorycode => undef,
605 rule_name => 'reservesallowed',
610 my $query = CGI->new;
611 $query->param( 'pickup_location', $pickup_library->branchcode );
612 $query->param( 'patron_id', $patron->borrowernumber);
613 $query->param( 'bib_id', $item->biblionumber);
614 $query->param( 'item_id', $item->itemnumber);
615 $query->param( 'start_date', '2020-03-20');
616 $query->param( 'expiry_date', '2020-04-22');
618 my $reply = C4::ILSDI::Services::HoldItem( $query );
619 is ($reply->{pickup_location}, $pickup_library->branchname, "Item hold with date parameters was placed");
620 my $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
621 is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
622 is( $hold->reservedate, '2020-03-20', "Item hold has correct start date" );
623 is( $hold->expirationdate, '2020-04-22', "Item hold has correct end date" );
627 $reply = C4::ILSDI::Services::HoldTitle( $query );
628 is ($reply->{pickup_location}, $pickup_library->branchname, "Record hold with date parameters was placed");
629 $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
630 is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
631 is( $hold->reservedate, '2020-03-20', "Record hold has correct start date" );
632 is( $hold->expirationdate, '2020-04-22', "Record hold has correct end date" );
634 $schema->storage->txn_rollback;
637 subtest 'GetRecords' => sub {
641 $schema->storage->txn_begin;
643 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
645 my $branch1 = $builder->build({
648 my $branch2 = $builder->build({
652 my $item = $builder->build_sample_item(
654 library => $branch1->{branchcode},
658 my $patron = $builder->build({
659 source => 'Borrower',
662 my $issue = $builder->build({
665 itemnumber => $item->itemnumber,
669 my $hold = $builder->build({
672 biblionumber => $item->biblionumber,
676 ModItemTransfer($item->itemnumber, $branch1->{branchcode}, $branch2->{branchcode}, 'Manual');
679 $cgi->param(service => 'GetRecords');
680 $cgi->param(id => $item->biblionumber);
682 my $reply = C4::ILSDI::Services::GetRecords($cgi);
684 my $transfer = $item->get_transfer;
686 datesent => $transfer->datesent,
687 frombranch => $transfer->frombranch,
688 tobranch => $transfer->tobranch,
690 is_deeply($reply->{record}->[0]->{items}->{item}->[0]->{transfer}, $expected,
691 'GetRecords returns transfer informations');
693 # Check informations exposed
694 my $reply_issue = $reply->{record}->[0]->{issues}->{issue}->[0];
695 is($reply_issue->{itemnumber}, $item->itemnumber, 'GetRecords has an issue tag');
696 is($reply_issue->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in issue tag');
697 is($reply_issue->{surname}, undef, 'GetRecords does not expose surname in issue tag');
698 is($reply_issue->{firstname}, undef, 'GetRecords does not expose firstname in issue tag');
699 is($reply_issue->{cardnumber}, undef, 'GetRecords does not expose cardnumber in issue tag');
700 my $reply_reserve = $reply->{record}->[0]->{reserves}->{reserve}->[0];
701 is($reply_reserve->{biblionumber}, $item->biblionumber, 'GetRecords has a reserve tag');
702 is($reply_reserve->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in reserve tag');
704 $schema->storage->txn_rollback;
707 subtest 'RenewHold' => sub {
710 $schema->storage->txn_begin;
713 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
714 my $item = $builder->build_sample_item;
715 $cgi->param( patron_id => $patron->borrowernumber );
716 $cgi->param( item_id => $item->itemnumber );
718 t::lib::Mocks::mock_userenv( { patron => $patron } ); # For AddIssue
719 my $checkout = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
722 my $reply = C4::ILSDI::Services::RenewLoan($cgi);
723 is( exists $reply->{date_due}, 1, 'If the item is checked out, the date_due key should exist' );
725 # The item is not checked out
727 $reply = C4::ILSDI::Services::RenewLoan($cgi);
728 is( $reply, undef, 'If the item is not checked out, we should not explode.'); # FIXME We should return an error code instead
730 # The item does not exist
732 $reply = C4::ILSDI::Services::RenewLoan($cgi);
733 is( $reply->{code}, 'RecordNotFound', 'If the item does not exist, RecordNotFound should be returned');
736 $reply = C4::ILSDI::Services::RenewLoan($cgi);
737 is( $reply->{code}, 'PatronNotFound', 'If the patron does not exist, PatronNotFound should be returned');
739 $schema->storage->txn_rollback;
742 subtest 'CancelHold' => sub {
745 $schema->storage->txn_begin;
749 my $library = $builder->build_object({
750 class => 'Koha::Libraries',
753 my $patron = $builder->build_object( { class => 'Koha::Patrons',
755 branchcode => $library->branchcode,
759 my $patron2 = $builder->build_object( { class => 'Koha::Patrons',
761 branchcode => $library->branchcode,
765 my $item = $builder->build_sample_item({ library => $library->branchcode });
767 my $reserve = C4::Reserves::AddReserve({branchcode => $library->branchcode,
768 borrowernumber => $patron->borrowernumber,
769 biblionumber => $item->biblionumber });
771 # Affecting the reserve sets it to a waiting state
772 C4::Reserves::ModReserveAffect( $item->itemnumber,
773 $patron->borrowernumber,
778 $cgi->param( patron_id => $patron2->borrowernumber );
779 $cgi->param( item_id => $reserve );
782 my $reply = C4::ILSDI::Services::CancelHold($cgi);
783 is( $reply->{code}, 'BorrowerCannotCancelHold', 'If the patron is wrong, BorrowerCannotCancelHold should be returned');
785 $cgi->param( patron_id => $patron->borrowernumber );
786 $reply = C4::ILSDI::Services::CancelHold($cgi);
787 is( $reply->{code}, 'BorrowerCannotCancelHold', 'If reserve in a Waiting state, patron cannot cancel');
789 C4::Reserves::ModReserveStatus( $item->itemnumber, 'T' );
790 $reply = C4::ILSDI::Services::CancelHold($cgi);
791 is( $reply->{code}, 'BorrowerCannotCancelHold', 'If reserve in a Transfer state, patron cannot cancel');
793 C4::Reserves::ModReserve( {rank => 1, reserve_id => $reserve, branchcode => $library->branchcode} );
794 $cgi->param( item_id => $reserve );
795 $reply = C4::ILSDI::Services::CancelHold($cgi);
796 is( $reply->{code}, 'Canceled', 'If the patron is fine and reserve not waiting, Canceled should be returned and reserve canceled');
798 $schema->storage->txn_rollback;
801 subtest 'GetPatronInfo paginated loans' => sub {
804 $schema->storage->txn_begin;
806 my $library = $builder->build_object({
807 class => 'Koha::Libraries',
810 my $item1 = $builder->build_sample_item({ library => $library->branchcode });
811 my $item2 = $builder->build_sample_item({ library => $library->branchcode });
812 my $item3 = $builder->build_sample_item({ library => $library->branchcode });
813 my $patron = $builder->build_object({
814 class => 'Koha::Patrons',
816 branchcode => $library->branchcode,
819 my $module = Test::MockModule->new('C4::Context');
820 $module->mock('userenv', sub { { branch => $library->branchcode } });
821 my $date_due = Koha::DateUtils::dt_from_string()->add(weeks => 2);
822 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
823 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
824 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
825 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
826 my $issue3 = C4::Circulation::AddIssue($patron->unblessed, $item3->barcode, $date_due);
827 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
831 $cgi->param( 'service', 'GetPatronInfo' );
832 $cgi->param( 'patron_id', $patron->borrowernumber );
833 $cgi->param( 'show_loans', '1' );
834 $cgi->param( 'loans_per_page', '2' );
835 $cgi->param( 'loans_page', '1' );
836 my $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
838 is($reply->{total_loans}, 3, 'total_loans == 3');
839 is(scalar @{ $reply->{loans}->{loan} }, 2, 'GetPatronInfo returned only 2 loans');
840 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item3->itemnumber);
841 is($reply->{loans}->{loan}->[1]->{itemnumber}, $item2->itemnumber);
843 $cgi->param( 'loans_page', '2' );
844 $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
846 is($reply->{total_loans}, 3, 'total_loans == 3');
847 is(scalar @{ $reply->{loans}->{loan} }, 1, 'GetPatronInfo returned only 1 loan');
848 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item1->itemnumber);
850 $schema->storage->txn_rollback;
853 subtest 'GetAvailability itemcallnumber' => sub {
857 $schema->storage->txn_begin;
859 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
861 my $item1 = $builder->build_sample_item(
863 itemcallnumber => "callnumber",
867 my $item2 = $builder->build_sample_item( {} );
871 $cgi->param( service => 'GetAvailability' );
872 $cgi->param( id => $item1->itemnumber );
873 $cgi->param( id_type => 'item' );
875 # Output of GetAvailability is a string containing XML
876 my $reply = C4::ILSDI::Services::GetAvailability($cgi);
878 # Parse the output and get info
879 my $result_XML = XML::LibXML->load_xml( string => $reply );
880 my $reply_callnumber =
881 $result_XML->findnodes('//dlf:itemcallnumber')->to_literal();
884 is( $reply_callnumber, $item1->itemcallnumber,
885 "GetAvailability item has an itemcallnumber tag" );
888 $cgi->param( service => 'GetAvailability' );
889 $cgi->param( id => $item2->itemnumber );
890 $cgi->param( id_type => 'item' );
891 $reply = C4::ILSDI::Services::GetAvailability($cgi);
892 $result_XML = XML::LibXML->load_xml( string => $reply );
894 $result_XML->findnodes('//dlf:itemcallnumber')->to_literal();
895 is( $reply_callnumber, '',
896 "As expected, GetAvailability item has no itemcallnumber tag" );
899 $cgi->param( service => 'GetAvailability' );
900 $cgi->param( id => $item1->biblionumber );
901 $cgi->param( id_type => 'biblio' );
902 $reply = C4::ILSDI::Services::GetAvailability($cgi);
903 $result_XML = XML::LibXML->load_xml( string => $reply );
905 $result_XML->findnodes('//dlf:itemcallnumber')->to_literal();
906 is( $reply_callnumber, $item1->itemcallnumber,
907 "GetAvailability biblio has an itemcallnumber tag" );
910 $cgi->param( service => 'GetAvailability' );
911 $cgi->param( id => $item2->biblionumber );
912 $cgi->param( id_type => 'biblio' );
913 $reply = C4::ILSDI::Services::GetAvailability($cgi);
914 $result_XML = XML::LibXML->load_xml( string => $reply );
916 $result_XML->findnodes('//dlf:itemcallnumber')->to_literal();
917 is( $reply_callnumber, '',
918 "As expected, GetAvailability biblio has no itemcallnumber tag" );
920 $schema->storage->txn_rollback;