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 => 10;
25 use t::lib::TestBuilder;
28 use C4::Items qw( ModItemTransfer );
35 use_ok('C4::ILSDI::Services');
38 my $schema = Koha::Database->schema;
39 my $dbh = C4::Context->dbh;
40 my $builder = t::lib::TestBuilder->new;
42 subtest 'AuthenticatePatron test' => sub {
46 $schema->storage->txn_begin;
48 my $plain_password = 'tomasito';
57 my $borrower = $builder->build({
61 password => Koha::AuthUtils::hash_password( $plain_password ),
62 lastseen => "2001-01-01 12:34:56"
67 $query->param( 'username', $borrower->{userid});
68 $query->param( 'password', $plain_password);
70 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
71 my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
72 is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
73 is( $reply->{code}, undef, "Error code undef");
74 my $seen_patron = Koha::Patrons->find({ borrowernumber => $reply->{id} });
75 is( $seen_patron->lastseen(), '2001-01-01 12:34:56','Last seen not updated if not tracking patrons');
77 $query->param('password','ilsdi-passworD');
78 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
79 is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
80 is( $reply->{id}, undef, "id undef");
82 $query->param( 'password', $plain_password );
83 $query->param( 'username', 'wrong-ilsdi-useriD' );
84 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
85 is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
86 is( $reply->{id}, undef, "id undef");
88 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
89 $query->param( 'username', uc( $borrower->{userid} ));
90 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
91 my $now = dt_from_string;
92 is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
93 is( $reply->{code}, undef, "Error code undef");
94 $seen_patron = Koha::Patrons->find({ borrowernumber => $reply->{id} });
95 is( t::lib::Dates::compare( $seen_patron->lastseen, $now), 0, 'Last seen updated to today if tracking patrons' );
97 $query->param( 'username', $borrower->{cardnumber} );
98 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
99 is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
100 is( $reply->{code}, undef, "Error code undef" );
102 $query->param( 'password', 'ilsdi-passworD' );
103 $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
104 is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
105 is( $reply->{id}, undef, "id undef" );
107 $query->param( 'username', 'randomcardnumber1234' );
108 $query->param( 'password', $plain_password );
109 $reply = C4::ILSDI::Services::AuthenticatePatron($query);
110 is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
111 is( $reply->{id}, undef, "id undef");
113 $schema->storage->txn_rollback;
117 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
121 $schema->storage->txn_begin;
123 $schema->resultset( 'Issue' )->delete_all;
124 $schema->resultset( 'Borrower' )->delete_all;
125 $schema->resultset( 'BorrowerAttribute' )->delete_all;
126 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
127 $schema->resultset( 'Category' )->delete_all;
128 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
129 $schema->resultset( 'Club' )->delete_all;
130 $schema->resultset( 'Branch' )->delete_all;
132 # Configure Koha to enable ILS-DI server and extended attributes:
133 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
134 t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
136 # Set up a library/branch for our user to belong to:
137 my $lib = $builder->build( {
140 branchcode => 'T_ILSDI',
144 # Create a new category for user to belong to:
145 my $cat = $builder->build( {
146 source => 'Category',
148 category_type => 'A',
149 BlockExpiredPatronOpacActions => -1,
153 # Create a new attribute type:
154 my $attr_type = $builder->build( {
155 source => 'BorrowerAttributeType',
159 authorised_value_category => '',
163 my $attr_type_visible = $builder->build( {
164 source => 'BorrowerAttributeType',
168 authorised_value_category => '',
174 my $brwr = $builder->build( {
175 source => 'Borrower',
177 categorycode => $cat->{'categorycode'},
178 branchcode => $lib->{'branchcode'},
183 my $auth = $builder->build( {
184 source => 'AuthorisedValue',
186 category => $cat->{'categorycode'}
190 # Set the new attribute for our user:
191 my $attr_hidden = $builder->build( {
192 source => 'BorrowerAttribute',
194 borrowernumber => $brwr->{'borrowernumber'},
195 code => $attr_type->{'code'},
196 attribute => '1337 hidden',
199 my $attr_shown = $builder->build( {
200 source => 'BorrowerAttribute',
202 borrowernumber => $brwr->{'borrowernumber'},
203 code => $attr_type_visible->{'code'},
204 attribute => '1337 shown',
208 my $fine = $builder->build(
210 source => 'Accountline',
212 borrowernumber => $brwr->{borrowernumber},
213 debit_type_code => 'OVERDUE',
214 amountoutstanding => 10
219 # Prepare and send web request for IL-SDI server:
220 my $query = CGI->new;
221 $query->param( 'service', 'GetPatronInfo' );
222 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
223 $query->param( 'show_attributes', '1' );
224 $query->param( 'show_fines', '1' );
226 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
228 # Build a structure for comparison:
230 borrowernumber => $brwr->{borrowernumber},
231 value => $attr_shown->{'attribute'},
232 value_description => $attr_shown->{'attribute'},
237 is( $reply->{'charges'}, '10.00',
238 'The \'charges\' attribute should be correctly filled (bug 17836)' );
240 is( scalar( @{$reply->{fines}->{fine}}), 1, 'There should be only 1 account line');
242 $reply->{fines}->{fine}->[0]->{accountlines_id},
243 $fine->{accountlines_id},
244 "The accountline should be the correct one"
248 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
250 ok( exists $reply->{is_expired}, 'There should be the is_expired information');
253 $schema->storage->txn_rollback;
256 subtest 'LookupPatron test' => sub {
260 $schema->storage->txn_begin;
262 $schema->resultset( 'Issue' )->delete_all;
263 $schema->resultset( 'Borrower' )->delete_all;
264 $schema->resultset( 'BorrowerAttribute' )->delete_all;
265 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
266 $schema->resultset( 'Category' )->delete_all;
267 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
268 $schema->resultset( 'Branch' )->delete_all;
270 my $borrower = $builder->build({
271 source => 'Borrower',
274 my $query = CGI->new();
275 my $bad_result = C4::ILSDI::Services::LookupPatron($query);
276 is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
278 $query->delete_all();
279 $query->param( 'id', $borrower->{firstname} );
280 my $optional_result = C4::ILSDI::Services::LookupPatron($query);
282 $optional_result->{id},
283 $borrower->{borrowernumber},
284 'Valid Firstname only'
287 $query->delete_all();
288 $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
289 my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
290 is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
292 foreach my $id_type (
300 $query->delete_all();
301 $query->param( 'id_type', $id_type );
302 $query->param( 'id', $borrower->{$id_type} );
303 my $result = C4::ILSDI::Services::LookupPatron($query);
304 is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
308 $schema->storage->txn_rollback;
311 subtest 'Holds test' => sub {
315 $schema->storage->txn_begin;
317 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
319 my $patron = $builder->build({
320 source => 'Borrower',
323 my $item = $builder->build_sample_item(
329 my $query = CGI->new;
330 $query->param( 'patron_id', $patron->{borrowernumber});
331 $query->param( 'bib_id', $item->biblionumber);
333 my $reply = C4::ILSDI::Services::HoldTitle( $query );
334 is( $reply->{code}, 'damaged', "Item damaged" );
336 $item->damaged(0)->store;
338 my $hold = $builder->build({
341 borrowernumber => $patron->{borrowernumber},
342 biblionumber => $item->biblionumber,
343 itemnumber => $item->itemnumber
347 $reply = C4::ILSDI::Services::HoldTitle( $query );
348 is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
350 my $biblio_with_no_item = $builder->build_sample_biblio;
353 $query->param( 'patron_id', $patron->{borrowernumber});
354 $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
356 $reply = C4::ILSDI::Services::HoldTitle( $query );
357 is( $reply->{code}, 'NoItems', 'Biblio has no item' );
359 my $item2 = $builder->build_sample_item(
365 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
366 Koha::CirculationRules->set_rule(
368 categorycode => $patron->{categorycode},
369 itemtype => $item2->{itype},
370 branchcode => $patron->{branchcode},
371 rule_name => 'reservesallowed',
377 $query->param( 'patron_id', $patron->{borrowernumber});
378 $query->param( 'bib_id', $item2->biblionumber);
379 $query->param( 'item_id', $item2->itemnumber);
381 $reply = C4::ILSDI::Services::HoldItem( $query );
382 is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
384 my $origin_branch = $builder->build(
388 pickup_location => 1,
393 # Adding a holdable item.
394 my $item3 = $builder->build_sample_item(
396 barcode => '123456789',
397 library => $origin_branch->{branchcode}
400 my $item4 = $builder->build_sample_item(
402 biblionumber => $item3->biblionumber,
404 library => $origin_branch->{branchcode}
407 Koha::CirculationRules->set_rule(
409 categorycode => $patron->{categorycode},
410 itemtype => $item3->{itype},
411 branchcode => $patron->{branchcode},
412 rule_name => 'reservesallowed',
418 $query->param( 'patron_id', $patron->{borrowernumber});
419 $query->param( 'bib_id', $item4->biblionumber);
420 $query->param( 'item_id', $item4->itemnumber);
422 $reply = C4::ILSDI::Services::HoldItem( $query );
423 is( $reply->{code}, 'damaged', "Item is damaged" );
425 my $module = Test::MockModule->new('C4::Context');
426 $module->mock('userenv', sub { { patron => $patron } });
427 my $issue = C4::Circulation::AddIssue($patron, $item3->barcode);
428 t::lib::Mocks::mock_preference( 'AllowHoldsOnPatronsPossessions', '0' );
431 $query->param( 'patron_id', $patron->{borrowernumber});
432 $query->param( 'bib_id', $item3->biblionumber);
433 $query->param( 'item_id', $item3->itemnumber);
434 $query->param( 'pickup_location', $origin_branch->{branchcode});
435 $reply = C4::ILSDI::Services::HoldItem( $query );
437 is( $reply->{code}, 'alreadypossession', "Patron has issued same book" );
438 is( $reply->{pickup_location}, undef, "No reserve placed");
440 # Test Patron cannot reserve if expired and BlockExpiredPatronOpacActions
441 my $category = $builder->build({
442 source => 'Category',
443 value => { BlockExpiredPatronOpacActions => -1 }
446 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
448 my $expired_borrowernumber = Koha::Patron->new({
449 firstname => 'Expired',
451 categorycode => $category->{categorycode},
452 branchcode => $branch_1,
453 dateexpiry => '2000-01-01',
454 })->store->borrowernumber;
456 t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
458 my $item5 = $builder->build({
461 biblionumber => $biblio_with_no_item->biblionumber,
467 $query->param( 'patron_id', $expired_borrowernumber);
468 $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
469 $query->param( 'item_id', $item5->{itemnumber});
471 $reply = C4::ILSDI::Services::HoldItem( $query );
472 is( $reply->{code}, 'PatronExpired', "Patron is expired" );
474 $schema->storage->txn_rollback;
477 subtest 'Holds test for branch transfer limits' => sub {
481 $schema->storage->txn_begin;
483 # Test enforement of branch transfer limits
484 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
485 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
487 my $patron = $builder->build({
488 source => 'Borrower',
491 my $origin_branch = $builder->build(
495 pickup_location => 1,
499 my $pickup_branch = $builder->build(
503 pickup_location => 1,
508 my $item = $builder->build_sample_item(
510 library => $origin_branch->{branchcode},
514 Koha::CirculationRules->set_rule(
516 categorycode => undef,
519 rule_name => 'reservesallowed',
524 my $limit = Koha::Item::Transfer::Limit->new({
525 toBranch => $pickup_branch->{branchcode},
526 fromBranch => $item->holdingbranch,
527 itemtype => $item->effective_itemtype,
530 my $query = CGI->new;
531 $query->param( 'pickup_location', $pickup_branch->{branchcode} );
532 $query->param( 'patron_id', $patron->{borrowernumber});
533 $query->param( 'bib_id', $item->biblionumber);
534 $query->param( 'item_id', $item->itemnumber);
536 my $reply = C4::ILSDI::Services::HoldItem( $query );
537 is( $reply->{code}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
539 $reply = C4::ILSDI::Services::HoldTitle( $query );
540 is( $reply->{code}, 'cannotBeTransferred', "Record hold, Item cannot be transferred" );
542 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
544 $reply = C4::ILSDI::Services::HoldItem( $query );
545 is( $reply->{code}, undef, "Item hold, Item can be transferred" );
546 my $hold = Koha::Holds->search({ itemnumber => $item->itemnumber, borrowernumber => $patron->{borrowernumber} })->next;
547 is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
549 Koha::Holds->search()->delete();
551 $reply = C4::ILSDI::Services::HoldTitle( $query );
552 is( $reply->{code}, undef, "Record hold, Item con be transferred" );
553 $hold = Koha::Holds->search({ biblionumber => $item->biblionumber, borrowernumber => $patron->{borrowernumber} })->next;
554 is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
556 $schema->storage->txn_rollback;
559 subtest 'Holds test with start_date and end_date' => sub {
563 $schema->storage->txn_begin;
565 my $pickup_library = $builder->build_object(
567 class => 'Koha::Libraries',
569 pickup_location => 1,
574 my $patron = $builder->build_object({
575 class => 'Koha::Patrons',
578 my $item = $builder->build_sample_item({ library => $pickup_library->branchcode });
580 Koha::CirculationRules->set_rule(
582 categorycode => undef,
585 rule_name => 'reservesallowed',
590 my $query = CGI->new;
591 $query->param( 'pickup_location', $pickup_library->branchcode );
592 $query->param( 'patron_id', $patron->borrowernumber);
593 $query->param( 'bib_id', $item->biblionumber);
594 $query->param( 'item_id', $item->itemnumber);
595 $query->param( 'start_date', '2020-03-20');
596 $query->param( 'expiry_date', '2020-04-22');
598 my $reply = C4::ILSDI::Services::HoldItem( $query );
599 is ($reply->{pickup_location}, $pickup_library->branchname, "Item hold with date parameters was placed");
600 my $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
601 is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
602 is( $hold->reservedate, '2020-03-20', "Item hold has correct start date" );
603 is( $hold->expirationdate, '2020-04-22', "Item hold has correct end date" );
607 $reply = C4::ILSDI::Services::HoldTitle( $query );
608 is ($reply->{pickup_location}, $pickup_library->branchname, "Record hold with date parameters was placed");
609 $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
610 is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
611 is( $hold->reservedate, '2020-03-20', "Record hold has correct start date" );
612 is( $hold->expirationdate, '2020-04-22', "Record hold has correct end date" );
614 $schema->storage->txn_rollback;
617 subtest 'GetRecords' => sub {
621 $schema->storage->txn_begin;
623 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
625 my $branch1 = $builder->build({
628 my $branch2 = $builder->build({
632 my $item = $builder->build_sample_item(
634 library => $branch1->{branchcode},
638 my $patron = $builder->build({
639 source => 'Borrower',
642 my $issue = $builder->build({
645 itemnumber => $item->itemnumber,
649 my $hold = $builder->build({
652 biblionumber => $item->biblionumber,
656 ModItemTransfer($item->itemnumber, $branch1->{branchcode}, $branch2->{branchcode});
659 $cgi->param(service => 'GetRecords');
660 $cgi->param(id => $item->biblionumber);
662 my $reply = C4::ILSDI::Services::GetRecords($cgi);
664 my $transfer = $item->get_transfer;
666 datesent => $transfer->datesent,
667 frombranch => $transfer->frombranch,
668 tobranch => $transfer->tobranch,
670 is_deeply($reply->{record}->[0]->{items}->{item}->[0]->{transfer}, $expected,
671 'GetRecords returns transfer informations');
673 # Check informations exposed
674 my $reply_issue = $reply->{record}->[0]->{issues}->{issue}->[0];
675 is($reply_issue->{itemnumber}, $item->itemnumber, 'GetRecords has an issue tag');
676 is($reply_issue->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in issue tag');
677 is($reply_issue->{surname}, undef, 'GetRecords does not expose surname in issue tag');
678 is($reply_issue->{firstname}, undef, 'GetRecords does not expose firstname in issue tag');
679 is($reply_issue->{cardnumber}, undef, 'GetRecords does not expose cardnumber in issue tag');
680 my $reply_reserve = $reply->{record}->[0]->{reserves}->{reserve}->[0];
681 is($reply_reserve->{biblionumber}, $item->biblionumber, 'GetRecords has a reserve tag');
682 is($reply_reserve->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in reserve tag');
684 $schema->storage->txn_rollback;
687 subtest 'RenewHold' => sub {
690 $schema->storage->txn_begin;
693 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
694 my $item = $builder->build_sample_item;
695 $cgi->param( patron_id => $patron->borrowernumber );
696 $cgi->param( item_id => $item->itemnumber );
698 t::lib::Mocks::mock_userenv( { patron => $patron } ); # For AddIssue
699 my $checkout = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
702 my $reply = C4::ILSDI::Services::RenewLoan($cgi);
703 is( exists $reply->{date_due}, 1, 'If the item is checked out, the date_due key should exist' );
705 # The item is not checked out
707 $reply = C4::ILSDI::Services::RenewLoan($cgi);
708 is( $reply, undef, 'If the item is not checked out, we should not explode.'); # FIXME We should return an error code instead
710 # The item does not exist
712 $reply = C4::ILSDI::Services::RenewLoan($cgi);
713 is( $reply->{code}, 'RecordNotFound', 'If the item does not exist, RecordNotFound should be returned');
716 $reply = C4::ILSDI::Services::RenewLoan($cgi);
717 is( $reply->{code}, 'PatronNotFound', 'If the patron does not exist, PatronNotFound should be returned');
719 $schema->storage->txn_rollback;
722 subtest 'GetPatronInfo paginated loans' => sub {
725 $schema->storage->txn_begin;
727 my $library = $builder->build_object({
728 class => 'Koha::Libraries',
731 my $item1 = $builder->build_sample_item({ library => $library->branchcode });
732 my $item2 = $builder->build_sample_item({ library => $library->branchcode });
733 my $item3 = $builder->build_sample_item({ library => $library->branchcode });
734 my $patron = $builder->build_object({
735 class => 'Koha::Patrons',
737 branchcode => $library->branchcode,
740 my $module = Test::MockModule->new('C4::Context');
741 $module->mock('userenv', sub { { branch => $library->branchcode } });
742 my $date_due = Koha::DateUtils::dt_from_string()->add(weeks => 2);
743 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
744 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
745 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
746 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
747 my $issue3 = C4::Circulation::AddIssue($patron->unblessed, $item3->barcode, $date_due);
748 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
752 $cgi->param( 'service', 'GetPatronInfo' );
753 $cgi->param( 'patron_id', $patron->borrowernumber );
754 $cgi->param( 'show_loans', '1' );
755 $cgi->param( 'loans_per_page', '2' );
756 $cgi->param( 'loans_page', '1' );
757 my $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
759 is($reply->{total_loans}, 3, 'total_loans == 3');
760 is(scalar @{ $reply->{loans}->{loan} }, 2, 'GetPatronInfo returned only 2 loans');
761 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item3->itemnumber);
762 is($reply->{loans}->{loan}->[1]->{itemnumber}, $item2->itemnumber);
764 $cgi->param( 'loans_page', '2' );
765 $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
767 is($reply->{total_loans}, 3, 'total_loans == 3');
768 is(scalar @{ $reply->{loans}->{loan} }, 1, 'GetPatronInfo returned only 1 loan');
769 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item1->itemnumber);
771 $schema->storage->txn_rollback;