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 => 9;
25 use t::lib::TestBuilder;
27 use C4::Items qw( ModItemTransfer );
33 use_ok('C4::ILSDI::Services');
36 my $schema = Koha::Database->schema;
37 my $dbh = C4::Context->dbh;
38 my $builder = t::lib::TestBuilder->new;
40 subtest 'AuthenticatePatron test' => sub {
44 $schema->storage->txn_begin;
46 my $plain_password = 'tomasito';
55 my $borrower = $builder->build({
59 password => Koha::AuthUtils::hash_password( $plain_password )
64 $query->param( 'username', $borrower->{userid});
65 $query->param( 'password', $plain_password);
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");
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");
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");
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");
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" );
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" );
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");
103 $schema->storage->txn_rollback;
107 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
111 $schema->storage->txn_begin;
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;
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 );
126 # Set up a library/branch for our user to belong to:
127 my $lib = $builder->build( {
130 branchcode => 'T_ILSDI',
134 # Create a new category for user to belong to:
135 my $cat = $builder->build( {
136 source => 'Category',
138 category_type => 'A',
139 BlockExpiredPatronOpacActions => -1,
143 # Create a new attribute type:
144 my $attr_type = $builder->build( {
145 source => 'BorrowerAttributeType',
149 authorised_value_category => '',
153 my $attr_type_visible = $builder->build( {
154 source => 'BorrowerAttributeType',
158 authorised_value_category => '',
164 my $brwr = $builder->build( {
165 source => 'Borrower',
167 categorycode => $cat->{'categorycode'},
168 branchcode => $lib->{'branchcode'},
173 my $auth = $builder->build( {
174 source => 'AuthorisedValue',
176 category => $cat->{'categorycode'}
180 # Set the new attribute for our user:
181 my $attr_hidden = $builder->build( {
182 source => 'BorrowerAttribute',
184 borrowernumber => $brwr->{'borrowernumber'},
185 code => $attr_type->{'code'},
186 attribute => '1337 hidden',
189 my $attr_shown = $builder->build( {
190 source => 'BorrowerAttribute',
192 borrowernumber => $brwr->{'borrowernumber'},
193 code => $attr_type_visible->{'code'},
194 attribute => '1337 shown',
198 my $fine = $builder->build(
200 source => 'Accountline',
202 borrowernumber => $brwr->{borrowernumber},
203 accounttype => undef,
204 debit_type_code => 'OVERDUE',
205 amountoutstanding => 10
210 # Prepare and send web request for IL-SDI server:
212 $query->param( 'service', 'GetPatronInfo' );
213 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
214 $query->param( 'show_attributes', '1' );
215 $query->param( 'show_fines', '1' );
217 my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
219 # Build a structure for comparison:
221 category_code => $attr_type_visible->{'category_code'},
222 class => $attr_type_visible->{'class'},
223 code => $attr_shown->{'code'},
224 description => $attr_type_visible->{'description'},
225 display_checkout => $attr_type_visible->{'display_checkout'},
226 value => $attr_shown->{'attribute'},
227 value_description => undef,
230 is( $reply->{'charges'}, '10.00',
231 'The \'charges\' attribute should be correctly filled (bug 17836)' );
233 is( scalar( @{$reply->{fines}->{fine}}), 1, 'There should be only 1 account line');
235 $reply->{fines}->{fine}->[0]->{accountlines_id},
236 $fine->{accountlines_id},
237 "The accountline should be the correct one"
241 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
243 ok( exists $reply->{is_expired}, 'There should be the is_expired information');
246 $schema->storage->txn_rollback;
249 subtest 'LookupPatron test' => sub {
253 $schema->storage->txn_begin;
255 $schema->resultset( 'Issue' )->delete_all;
256 $schema->resultset( 'Borrower' )->delete_all;
257 $schema->resultset( 'BorrowerAttribute' )->delete_all;
258 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
259 $schema->resultset( 'Category' )->delete_all;
260 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
261 $schema->resultset( 'Branch' )->delete_all;
263 my $borrower = $builder->build({
264 source => 'Borrower',
267 my $query = CGI->new();
268 my $bad_result = C4::ILSDI::Services::LookupPatron($query);
269 is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
271 $query->delete_all();
272 $query->param( 'id', $borrower->{firstname} );
273 my $optional_result = C4::ILSDI::Services::LookupPatron($query);
275 $optional_result->{id},
276 $borrower->{borrowernumber},
277 'Valid Firstname only'
280 $query->delete_all();
281 $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
282 my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
283 is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
285 foreach my $id_type (
293 $query->delete_all();
294 $query->param( 'id_type', $id_type );
295 $query->param( 'id', $borrower->{$id_type} );
296 my $result = C4::ILSDI::Services::LookupPatron($query);
297 is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
301 $schema->storage->txn_rollback;
304 subtest 'Holds test' => sub {
308 $schema->storage->txn_begin;
310 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
312 my $patron = $builder->build({
313 source => 'Borrower',
316 my $biblio = $builder->build({
320 my $biblioitems = $builder->build({
321 source => 'Biblioitem',
323 biblionumber => $biblio->{biblionumber},
327 my $item = $builder->build({
330 biblionumber => $biblio->{biblionumber},
336 $query->param( 'patron_id', $patron->{borrowernumber});
337 $query->param( 'bib_id', $biblio->{biblionumber});
339 my $reply = C4::ILSDI::Services::HoldTitle( $query );
340 is( $reply->{code}, 'damaged', "Item damaged" );
342 my $item_o = Koha::Items->find($item->{itemnumber});
343 $item_o->damaged(0)->store;
345 my $hold = $builder->build({
348 borrowernumber => $patron->{borrowernumber},
349 biblionumber => $biblio->{biblionumber},
350 itemnumber => $item->{itemnumber}
354 $reply = C4::ILSDI::Services::HoldTitle( $query );
355 is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
357 my $biblio_with_no_item = $builder->build({
362 $query->param( 'patron_id', $patron->{borrowernumber});
363 $query->param( 'bib_id', $biblio_with_no_item->{biblionumber});
365 $reply = C4::ILSDI::Services::HoldTitle( $query );
366 is( $reply->{code}, 'NoItems', 'Biblio has no item' );
368 my $biblio2 = $builder->build({
372 my $biblioitems2 = $builder->build({
373 source => 'Biblioitem',
375 biblionumber => $biblio2->{biblionumber},
379 my $item2 = $builder->build({
382 biblionumber => $biblio2->{biblionumber},
387 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
388 my $issuingrule = $builder->build({
389 source => 'Issuingrule',
391 categorycode => $patron->{categorycode},
392 itemtype => $item2->{itype},
393 branchcode => $patron->{branchcode},
394 reservesallowed => 0,
399 $query->param( 'patron_id', $patron->{borrowernumber});
400 $query->param( 'bib_id', $biblio2->{biblionumber});
401 $query->param( 'item_id', $item2->{itemnumber});
403 $reply = C4::ILSDI::Services::HoldItem( $query );
404 is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
406 my $biblio3 = $builder->build({
410 my $biblioitems3 = $builder->build({
411 source => 'Biblioitem',
413 biblionumber => $biblio3->{biblionumber},
417 # Adding a holdable item to biblio 3.
418 my $item3 = $builder->build({
421 biblionumber => $biblio3->{biblionumber},
426 my $item4 = $builder->build({
429 biblionumber => $biblio3->{biblionumber},
434 my $issuingrule2 = $builder->build({
435 source => 'Issuingrule',
437 categorycode => $patron->{categorycode},
438 itemtype => $item3->{itype},
439 branchcode => $patron->{branchcode},
440 reservesallowed => 10,
445 $query->param( 'patron_id', $patron->{borrowernumber});
446 $query->param( 'bib_id', $biblio3->{biblionumber});
447 $query->param( 'item_id', $item4->{itemnumber});
449 $reply = C4::ILSDI::Services::HoldItem( $query );
450 is( $reply->{code}, 'damaged', "Item is damaged" );
452 $schema->storage->txn_rollback;
455 subtest 'Holds test for branch transfer limits' => sub {
459 $schema->storage->txn_begin;
461 # Test enforement of branch transfer limits
462 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
463 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
465 my $patron = $builder->build({
466 source => 'Borrower',
469 my $origin_branch = $builder->build(
473 pickup_location => 1,
477 my $pickup_branch = $builder->build(
481 pickup_location => 1,
486 my $biblio = $builder->build({
489 my $biblioitem = $builder->build({
490 source => 'Biblioitem',
492 biblionumber => $biblio->{biblionumber},
495 my $item = $builder->build({
498 homebranch => $origin_branch->{branchcode},
499 holdingbranch => $origin_branch->{branchcode},
500 biblionumber => $biblio->{biblionumber},
506 Koha::IssuingRules->search()->delete();
507 my $issuingrule = $builder->build({
508 source => 'Issuingrule',
513 reservesallowed => 99,
517 my $limit = Koha::Item::Transfer::Limit->new({
518 toBranch => $pickup_branch->{branchcode},
519 fromBranch => $item->{holdingbranch},
520 itemtype => $item->{itype},
524 $query->param( 'pickup_location', $pickup_branch->{branchcode} );
525 $query->param( 'patron_id', $patron->{borrowernumber});
526 $query->param( 'bib_id', $biblio->{biblionumber});
527 $query->param( 'item_id', $item->{itemnumber});
529 my $reply = C4::ILSDI::Services::HoldItem( $query );
530 is( $reply->{code}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
532 $reply = C4::ILSDI::Services::HoldTitle( $query );
533 is( $reply->{code}, 'cannotBeTransferred', "Record hold, Item cannot be transferred" );
535 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
537 $reply = C4::ILSDI::Services::HoldItem( $query );
538 is( $reply->{code}, undef, "Item hold, Item can be transferred" );
540 Koha::Holds->search()->delete();
542 $reply = C4::ILSDI::Services::HoldTitle( $query );
543 is( $reply->{code}, undef, "Record hold, Item con be transferred" );
545 $schema->storage->txn_rollback;
548 subtest 'GetRecords' => sub {
552 $schema->storage->txn_begin;
554 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
556 my $branch1 = $builder->build({
559 my $branch2 = $builder->build({
563 my $biblio = $builder->build({
566 my $biblioitem = $builder->build({
567 source => 'Biblioitem',
569 biblionumber => $biblio->{biblionumber},
572 my $item = $builder->build_object({
573 class => 'Koha::Items',
575 biblionumber => $biblio->{biblionumber},
576 biblioitemnumber => $biblioitem->{biblioitemnumber},
577 homebranch => $branch1->{branchcode},
578 holdingbranch => $branch1->{branchcode},
582 ModItemTransfer($item->itemnumber, $branch1->{branchcode}, $branch2->{branchcode});
585 $cgi->param(service => 'GetRecords');
586 $cgi->param(id => $biblio->{biblionumber});
588 my $reply = C4::ILSDI::Services::GetRecords($cgi);
590 my $transfer = $item->get_transfer;
592 datesent => $transfer->datesent,
593 frombranch => $transfer->frombranch,
594 tobranch => $transfer->tobranch,
596 is_deeply($reply->{record}->[0]->{items}->{item}->[0]->{transfer}, $expected,
597 'GetRecords returns transfer informations');
599 $schema->storage->txn_rollback;
602 subtest 'RenewHold' => sub {
605 $schema->storage->txn_begin;
608 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
609 my $item = $builder->build_object( { class => 'Koha::Items' } );
610 $cgi->param( patron_id => $patron->borrowernumber );
611 $cgi->param( item_id => $item->itemnumber );
613 t::lib::Mocks::mock_userenv( { patron => $patron } ); # For AddIssue
614 my $checkout = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
617 my $reply = C4::ILSDI::Services::RenewLoan($cgi);
618 is( exists $reply->{date_due}, 1, 'If the item is checked out, the date_due key should exist' );
620 # The item is not checked out
622 $reply = C4::ILSDI::Services::RenewLoan($cgi);
623 is( $reply, undef, 'If the item is not checked out, we should not explode.'); # FIXME We should return an error code instead
625 # The item does not exist
627 $reply = C4::ILSDI::Services::RenewLoan($cgi);
628 is( $reply->{code}, 'RecordNotFound', 'If the item does not exist, RecordNotFound should be returned');
631 $reply = C4::ILSDI::Services::RenewLoan($cgi);
632 is( $reply->{code}, 'PatronNotFound', 'If the patron does not exist, PatronNotFound should be returned');
634 $schema->storage->txn_rollback;
637 subtest 'GetPatronInfo paginated loans' => sub {
640 $schema->storage->txn_begin;
642 my $library = $builder->build_object({
643 class => 'Koha::Libraries',
646 my $item1 = $builder->build_sample_item({ library => $library->branchcode });
647 my $item2 = $builder->build_sample_item({ library => $library->branchcode });
648 my $item3 = $builder->build_sample_item({ library => $library->branchcode });
649 my $patron = $builder->build_object({
650 class => 'Koha::Patrons',
652 branchcode => $library->branchcode,
655 my $module = new Test::MockModule('C4::Context');
656 $module->mock('userenv', sub { { branch => $library->branchcode } });
657 my $date_due = DateTime->now->add(weeks => 2);
658 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
659 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
660 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
661 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
662 my $issue3 = C4::Circulation::AddIssue($patron->unblessed, $item3->barcode, $date_due);
663 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
667 $cgi->param( 'service', 'GetPatronInfo' );
668 $cgi->param( 'patron_id', $patron->borrowernumber );
669 $cgi->param( 'show_loans', '1' );
670 $cgi->param( 'loans_per_page', '2' );
671 $cgi->param( 'loans_page', '1' );
672 my $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
674 is($reply->{total_loans}, 3, 'total_loans == 3');
675 is(scalar @{ $reply->{loans}->{loan} }, 2, 'GetPatronInfo returned only 2 loans');
676 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item3->itemnumber);
677 is($reply->{loans}->{loan}->[1]->{itemnumber}, $item2->itemnumber);
679 $cgi->param( 'loans_page', '2' );
680 $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
682 is($reply->{total_loans}, 3, 'total_loans == 3');
683 is(scalar @{ $reply->{loans}->{loan} }, 1, 'GetPatronInfo returned only 1 loan');
684 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item1->itemnumber);
686 $schema->storage->txn_rollback;