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
;
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 debit_type_code
=> 'OVERDUE',
204 amountoutstanding
=> 10
209 # Prepare and send web request for IL-SDI server:
211 $query->param( 'service', 'GetPatronInfo' );
212 $query->param( 'patron_id', $brwr->{'borrowernumber'} );
213 $query->param( 'show_attributes', '1' );
214 $query->param( 'show_fines', '1' );
216 my $reply = C4
::ILSDI
::Services
::GetPatronInfo
( $query );
218 # Build a structure for comparison:
220 borrowernumber
=> $brwr->{borrowernumber
},
221 value
=> $attr_shown->{'attribute'},
222 value_description
=> $attr_shown->{'attribute'},
227 is
( $reply->{'charges'}, '10.00',
228 'The \'charges\' attribute should be correctly filled (bug 17836)' );
230 is
( scalar( @
{$reply->{fines
}->{fine
}}), 1, 'There should be only 1 account line');
232 $reply->{fines
}->{fine
}->[0]->{accountlines_id
},
233 $fine->{accountlines_id
},
234 "The accountline should be the correct one"
238 is_deeply
( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
240 ok
( exists $reply->{is_expired
}, 'There should be the is_expired information');
243 $schema->storage->txn_rollback;
246 subtest
'LookupPatron test' => sub {
250 $schema->storage->txn_begin;
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;
260 my $borrower = $builder->build({
261 source
=> 'Borrower',
264 my $query = CGI
->new();
265 my $bad_result = C4
::ILSDI
::Services
::LookupPatron
($query);
266 is
( $bad_result->{message
}, 'PatronNotFound', 'No parameters' );
268 $query->delete_all();
269 $query->param( 'id', $borrower->{firstname
} );
270 my $optional_result = C4
::ILSDI
::Services
::LookupPatron
($query);
272 $optional_result->{id
},
273 $borrower->{borrowernumber
},
274 'Valid Firstname only'
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' );
282 foreach my $id_type (
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" );
298 $schema->storage->txn_rollback;
301 subtest
'Holds test' => sub {
305 $schema->storage->txn_begin;
307 t
::lib
::Mocks
::mock_preference
( 'AllowHoldsOnDamagedItems', 0 );
309 my $patron = $builder->build({
310 source
=> 'Borrower',
313 my $item = $builder->build_sample_item(
320 $query->param( 'patron_id', $patron->{borrowernumber
});
321 $query->param( 'bib_id', $item->biblionumber);
323 my $reply = C4
::ILSDI
::Services
::HoldTitle
( $query );
324 is
( $reply->{code
}, 'damaged', "Item damaged" );
326 $item->damaged(0)->store;
328 my $hold = $builder->build({
331 borrowernumber
=> $patron->{borrowernumber
},
332 biblionumber
=> $item->biblionumber,
333 itemnumber
=> $item->itemnumber
337 $reply = C4
::ILSDI
::Services
::HoldTitle
( $query );
338 is
( $reply->{code
}, 'itemAlreadyOnHold', "Item already on hold" );
340 my $biblio_with_no_item = $builder->build_sample_biblio;
343 $query->param( 'patron_id', $patron->{borrowernumber
});
344 $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
346 $reply = C4
::ILSDI
::Services
::HoldTitle
( $query );
347 is
( $reply->{code
}, 'NoItems', 'Biblio has no item' );
349 my $item2 = $builder->build_sample_item(
355 t
::lib
::Mocks
::mock_preference
( 'ReservesControlBranch', 'PatronLibrary' );
356 Koha
::CirculationRules
->set_rule(
358 categorycode
=> $patron->{categorycode
},
359 itemtype
=> $item2->{itype
},
360 branchcode
=> $patron->{branchcode
},
361 rule_name
=> 'reservesallowed',
367 $query->param( 'patron_id', $patron->{borrowernumber
});
368 $query->param( 'bib_id', $item2->biblionumber);
369 $query->param( 'item_id', $item2->itemnumber);
371 $reply = C4
::ILSDI
::Services
::HoldItem
( $query );
372 is
( $reply->{code
}, 'tooManyReserves', "Too many reserves" );
374 # Adding a holdable item to biblio 3.
375 my $item3 = $builder->build_sample_item(
381 my $item4 = $builder->build_sample_item(
383 biblionumber
=> $item3->biblionumber,
388 Koha
::CirculationRules
->set_rule(
390 categorycode
=> $patron->{categorycode
},
391 itemtype
=> $item3->{itype
},
392 branchcode
=> $patron->{branchcode
},
393 rule_name
=> 'reservesallowed',
399 $query->param( 'patron_id', $patron->{borrowernumber
});
400 $query->param( 'bib_id', $item3->biblionumber);
401 $query->param( 'item_id', $item4->itemnumber);
403 $reply = C4
::ILSDI
::Services
::HoldItem
( $query );
404 is
( $reply->{code
}, 'damaged', "Item is damaged" );
406 $schema->storage->txn_rollback;
409 subtest
'Holds test for branch transfer limits' => sub {
413 $schema->storage->txn_begin;
415 # Test enforement of branch transfer limits
416 t
::lib
::Mocks
::mock_preference
( 'UseBranchTransferLimits', '1' );
417 t
::lib
::Mocks
::mock_preference
( 'BranchTransferLimitsType', 'itemtype' );
419 my $patron = $builder->build({
420 source
=> 'Borrower',
423 my $origin_branch = $builder->build(
427 pickup_location
=> 1,
431 my $pickup_branch = $builder->build(
435 pickup_location
=> 1,
440 my $item = $builder->build_sample_item(
442 library
=> $origin_branch->{branchcode
},
446 Koha
::CirculationRules
->set_rule(
448 categorycode
=> undef,
451 rule_name
=> 'reservesallowed',
456 my $limit = Koha
::Item
::Transfer
::Limit
->new({
457 toBranch
=> $pickup_branch->{branchcode
},
458 fromBranch
=> $item->holdingbranch,
459 itemtype
=> $item->effective_itemtype,
463 $query->param( 'pickup_location', $pickup_branch->{branchcode
} );
464 $query->param( 'patron_id', $patron->{borrowernumber
});
465 $query->param( 'bib_id', $item->biblionumber);
466 $query->param( 'item_id', $item->itemnumber);
468 my $reply = C4
::ILSDI
::Services
::HoldItem
( $query );
469 is
( $reply->{code
}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
471 $reply = C4
::ILSDI
::Services
::HoldTitle
( $query );
472 is
( $reply->{code
}, 'cannotBeTransferred', "Record hold, Item cannot be transferred" );
474 t
::lib
::Mocks
::mock_preference
( 'UseBranchTransferLimits', '0' );
476 $reply = C4
::ILSDI
::Services
::HoldItem
( $query );
477 is
( $reply->{code
}, undef, "Item hold, Item can be transferred" );
478 my $hold = Koha
::Holds
->search({ itemnumber
=> $item->itemnumber, borrowernumber
=> $patron->{borrowernumber
} })->next;
479 is
( $hold->branchcode, $pickup_branch->{branchcode
}, 'The library id is correctly set' );
481 Koha
::Holds
->search()->delete();
483 $reply = C4
::ILSDI
::Services
::HoldTitle
( $query );
484 is
( $reply->{code
}, undef, "Record hold, Item con be transferred" );
485 $hold = Koha
::Holds
->search({ biblionumber
=> $item->biblionumber, borrowernumber
=> $patron->{borrowernumber
} })->next;
486 is
( $hold->branchcode, $pickup_branch->{branchcode
}, 'The library id is correctly set' );
488 $schema->storage->txn_rollback;
491 subtest
'Holds test with start_date and end_date' => sub {
495 $schema->storage->txn_begin;
497 my $pickup_library = $builder->build_object(
499 class => 'Koha::Libraries',
501 pickup_location
=> 1,
506 my $patron = $builder->build_object({
507 class => 'Koha::Patrons',
510 my $item = $builder->build_sample_item({ library
=> $pickup_library->branchcode });
512 Koha
::CirculationRules
->set_rule(
514 categorycode
=> undef,
517 rule_name
=> 'reservesallowed',
523 $query->param( 'pickup_location', $pickup_library->branchcode );
524 $query->param( 'patron_id', $patron->borrowernumber);
525 $query->param( 'bib_id', $item->biblionumber);
526 $query->param( 'item_id', $item->itemnumber);
527 $query->param( 'start_date', '2020-03-20');
528 $query->param( 'expiry_date', '2020-04-22');
530 my $reply = C4
::ILSDI
::Services
::HoldItem
( $query );
531 is
($reply->{pickup_location
}, $pickup_library->branchname, "Item hold with date parameters was placed");
532 my $hold = Koha
::Holds
->search({ biblionumber
=> $item->biblionumber})->next();
533 is
( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
534 is
( $hold->reservedate, '2020-03-20', "Item hold has correct start date" );
535 is
( $hold->expirationdate, '2020-04-22', "Item hold has correct end date" );
539 $reply = C4
::ILSDI
::Services
::HoldTitle
( $query );
540 is
($reply->{pickup_location
}, $pickup_library->branchname, "Record hold with date parameters was placed");
541 $hold = Koha
::Holds
->search({ biblionumber
=> $item->biblionumber})->next();
542 is
( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
543 is
( $hold->reservedate, '2020-03-20', "Record hold has correct start date" );
544 is
( $hold->expirationdate, '2020-04-22', "Record hold has correct end date" );
546 $schema->storage->txn_rollback;
549 subtest
'GetRecords' => sub {
553 $schema->storage->txn_begin;
555 t
::lib
::Mocks
::mock_preference
( 'ILS-DI', 1 );
557 my $branch1 = $builder->build({
560 my $branch2 = $builder->build({
564 my $item = $builder->build_sample_item(
566 library
=> $branch1->{branchcode
},
570 ModItemTransfer
($item->itemnumber, $branch1->{branchcode
}, $branch2->{branchcode
});
573 $cgi->param(service
=> 'GetRecords');
574 $cgi->param(id
=> $item->biblionumber);
576 my $reply = C4
::ILSDI
::Services
::GetRecords
($cgi);
578 my $transfer = $item->get_transfer;
580 datesent
=> $transfer->datesent,
581 frombranch
=> $transfer->frombranch,
582 tobranch
=> $transfer->tobranch,
584 is_deeply
($reply->{record
}->[0]->{items
}->{item
}->[0]->{transfer
}, $expected,
585 'GetRecords returns transfer informations');
587 $schema->storage->txn_rollback;
590 subtest
'RenewHold' => sub {
593 $schema->storage->txn_begin;
596 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
597 my $item = $builder->build_sample_item;
598 $cgi->param( patron_id
=> $patron->borrowernumber );
599 $cgi->param( item_id
=> $item->itemnumber );
601 t
::lib
::Mocks
::mock_userenv
( { patron
=> $patron } ); # For AddIssue
602 my $checkout = C4
::Circulation
::AddIssue
( $patron->unblessed, $item->barcode );
605 my $reply = C4
::ILSDI
::Services
::RenewLoan
($cgi);
606 is
( exists $reply->{date_due
}, 1, 'If the item is checked out, the date_due key should exist' );
608 # The item is not checked out
610 $reply = C4
::ILSDI
::Services
::RenewLoan
($cgi);
611 is
( $reply, undef, 'If the item is not checked out, we should not explode.'); # FIXME We should return an error code instead
613 # The item does not exist
615 $reply = C4
::ILSDI
::Services
::RenewLoan
($cgi);
616 is
( $reply->{code
}, 'RecordNotFound', 'If the item does not exist, RecordNotFound should be returned');
619 $reply = C4
::ILSDI
::Services
::RenewLoan
($cgi);
620 is
( $reply->{code
}, 'PatronNotFound', 'If the patron does not exist, PatronNotFound should be returned');
622 $schema->storage->txn_rollback;
625 subtest
'GetPatronInfo paginated loans' => sub {
628 $schema->storage->txn_begin;
630 my $library = $builder->build_object({
631 class => 'Koha::Libraries',
634 my $item1 = $builder->build_sample_item({ library
=> $library->branchcode });
635 my $item2 = $builder->build_sample_item({ library
=> $library->branchcode });
636 my $item3 = $builder->build_sample_item({ library
=> $library->branchcode });
637 my $patron = $builder->build_object({
638 class => 'Koha::Patrons',
640 branchcode
=> $library->branchcode,
643 my $module = new Test
::MockModule
('C4::Context');
644 $module->mock('userenv', sub { { branch
=> $library->branchcode } });
645 my $date_due = DateTime
->now->add(weeks
=> 2);
646 my $issue1 = C4
::Circulation
::AddIssue
($patron->unblessed, $item1->barcode, $date_due);
647 my $date_due1 = Koha
::DateUtils
::dt_from_string
( $issue1->date_due );
648 my $issue2 = C4
::Circulation
::AddIssue
($patron->unblessed, $item2->barcode, $date_due);
649 my $date_due2 = Koha
::DateUtils
::dt_from_string
( $issue2->date_due );
650 my $issue3 = C4
::Circulation
::AddIssue
($patron->unblessed, $item3->barcode, $date_due);
651 my $date_due3 = Koha
::DateUtils
::dt_from_string
( $issue3->date_due );
655 $cgi->param( 'service', 'GetPatronInfo' );
656 $cgi->param( 'patron_id', $patron->borrowernumber );
657 $cgi->param( 'show_loans', '1' );
658 $cgi->param( 'loans_per_page', '2' );
659 $cgi->param( 'loans_page', '1' );
660 my $reply = C4
::ILSDI
::Services
::GetPatronInfo
($cgi);
662 is
($reply->{total_loans
}, 3, 'total_loans == 3');
663 is
(scalar @
{ $reply->{loans
}->{loan
} }, 2, 'GetPatronInfo returned only 2 loans');
664 is
($reply->{loans
}->{loan
}->[0]->{itemnumber
}, $item3->itemnumber);
665 is
($reply->{loans
}->{loan
}->[1]->{itemnumber
}, $item2->itemnumber);
667 $cgi->param( 'loans_page', '2' );
668 $reply = C4
::ILSDI
::Services
::GetPatronInfo
($cgi);
670 is
($reply->{total_loans
}, 3, 'total_loans == 3');
671 is
(scalar @
{ $reply->{loans
}->{loan
} }, 1, 'GetPatronInfo returned only 1 loan');
672 is
($reply->{loans
}->{loan
}->[0]->{itemnumber
}, $item1->itemnumber);
674 $schema->storage->txn_rollback;