Bug 24546: Fix intentions of build_sample_item in Club/Hold.t
[koha.git] / t / db_dependent / ILSDI_Services.t
blob430d900c773055e06efa5c5c3e1390bc64a956b7
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use CGI qw ( -utf8 );
22 use Test::More tests => 9;
23 use Test::MockModule;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
27 use C4::Items qw( ModItemTransfer );
28 use C4::Circulation;
30 use Koha::AuthUtils;
32 BEGIN {
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 {
42 plan tests => 14;
44 $schema->storage->txn_begin;
46 my $plain_password = 'tomasito';
48 $builder->build({
49 source => 'Borrower',
50 value => {
51 cardnumber => undef,
53 });
55 my $borrower = $builder->build({
56 source => 'Borrower',
57 value => {
58 cardnumber => undef,
59 password => Koha::AuthUtils::hash_password( $plain_password )
61 });
63 my $query = new CGI;
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 {
109 plan tests => 5;
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( {
128 source => 'Branch',
129 value => {
130 branchcode => 'T_ILSDI',
132 } );
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,
141 } );
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 => '',
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 => '',
161 } );
163 # Create a new user:
164 my $brwr = $builder->build( {
165 source => 'Borrower',
166 value => {
167 categorycode => $cat->{'categorycode'},
168 branchcode => $lib->{'branchcode'},
170 } );
172 # Authorised value:
173 my $auth = $builder->build( {
174 source => 'AuthorisedValue',
175 value => {
176 category => $cat->{'categorycode'}
178 } );
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',
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',
196 } );
198 my $fine = $builder->build(
200 source => 'Accountline',
201 value => {
202 borrowernumber => $brwr->{borrowernumber},
203 debit_type_code => 'OVERDUE',
204 amountoutstanding => 10
209 # Prepare and send web request for IL-SDI server:
210 my $query = new CGI;
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:
219 my $cmp = {
220 category_code => $attr_type_visible->{'category_code'},
221 class => $attr_type_visible->{'class'},
222 code => $attr_shown->{'code'},
223 description => $attr_type_visible->{'description'},
224 display_checkout => $attr_type_visible->{'display_checkout'},
225 value => $attr_shown->{'attribute'},
226 value_description => undef,
229 is( $reply->{'charges'}, '10.00',
230 'The \'charges\' attribute should be correctly filled (bug 17836)' );
232 is( scalar( @{$reply->{fines}->{fine}}), 1, 'There should be only 1 account line');
234 $reply->{fines}->{fine}->[0]->{accountlines_id},
235 $fine->{accountlines_id},
236 "The accountline should be the correct one"
239 # Check results:
240 is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
242 ok( exists $reply->{is_expired}, 'There should be the is_expired information');
244 # Cleanup
245 $schema->storage->txn_rollback;
248 subtest 'LookupPatron test' => sub {
250 plan tests => 9;
252 $schema->storage->txn_begin;
254 $schema->resultset( 'Issue' )->delete_all;
255 $schema->resultset( 'Borrower' )->delete_all;
256 $schema->resultset( 'BorrowerAttribute' )->delete_all;
257 $schema->resultset( 'BorrowerAttributeType' )->delete_all;
258 $schema->resultset( 'Category' )->delete_all;
259 $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
260 $schema->resultset( 'Branch' )->delete_all;
262 my $borrower = $builder->build({
263 source => 'Borrower',
266 my $query = CGI->new();
267 my $bad_result = C4::ILSDI::Services::LookupPatron($query);
268 is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
270 $query->delete_all();
271 $query->param( 'id', $borrower->{firstname} );
272 my $optional_result = C4::ILSDI::Services::LookupPatron($query);
274 $optional_result->{id},
275 $borrower->{borrowernumber},
276 'Valid Firstname only'
279 $query->delete_all();
280 $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
281 my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
282 is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
284 foreach my $id_type (
285 'cardnumber',
286 'userid',
287 'email',
288 'borrowernumber',
289 'surname',
290 'firstname'
292 $query->delete_all();
293 $query->param( 'id_type', $id_type );
294 $query->param( 'id', $borrower->{$id_type} );
295 my $result = C4::ILSDI::Services::LookupPatron($query);
296 is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
299 # Cleanup
300 $schema->storage->txn_rollback;
303 subtest 'Holds test' => sub {
305 plan tests => 5;
307 $schema->storage->txn_begin;
309 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
311 my $patron = $builder->build({
312 source => 'Borrower',
315 my $biblio = $builder->build({
316 source => 'Biblio',
319 my $biblioitems = $builder->build({
320 source => 'Biblioitem',
321 value => {
322 biblionumber => $biblio->{biblionumber},
326 my $item = $builder->build({
327 source => 'Item',
328 value => {
329 biblionumber => $biblio->{biblionumber},
330 damaged => 1
334 my $query = new CGI;
335 $query->param( 'patron_id', $patron->{borrowernumber});
336 $query->param( 'bib_id', $biblio->{biblionumber});
338 my $reply = C4::ILSDI::Services::HoldTitle( $query );
339 is( $reply->{code}, 'damaged', "Item damaged" );
341 my $item_o = Koha::Items->find($item->{itemnumber});
342 $item_o->damaged(0)->store;
344 my $hold = $builder->build({
345 source => 'Reserve',
346 value => {
347 borrowernumber => $patron->{borrowernumber},
348 biblionumber => $biblio->{biblionumber},
349 itemnumber => $item->{itemnumber}
353 $reply = C4::ILSDI::Services::HoldTitle( $query );
354 is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
356 my $biblio_with_no_item = $builder->build({
357 source => 'Biblio',
360 $query = new CGI;
361 $query->param( 'patron_id', $patron->{borrowernumber});
362 $query->param( 'bib_id', $biblio_with_no_item->{biblionumber});
364 $reply = C4::ILSDI::Services::HoldTitle( $query );
365 is( $reply->{code}, 'NoItems', 'Biblio has no item' );
367 my $biblio2 = $builder->build({
368 source => 'Biblio',
371 my $biblioitems2 = $builder->build({
372 source => 'Biblioitem',
373 value => {
374 biblionumber => $biblio2->{biblionumber},
378 my $item2 = $builder->build({
379 source => 'Item',
380 value => {
381 biblionumber => $biblio2->{biblionumber},
382 damaged => 0
386 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
387 my $issuingrule = $builder->build({
388 source => 'Issuingrule',
389 value => {
390 categorycode => $patron->{categorycode},
391 itemtype => $item2->{itype},
392 branchcode => $patron->{branchcode},
393 reservesallowed => 0,
397 $query = new CGI;
398 $query->param( 'patron_id', $patron->{borrowernumber});
399 $query->param( 'bib_id', $biblio2->{biblionumber});
400 $query->param( 'item_id', $item2->{itemnumber});
402 $reply = C4::ILSDI::Services::HoldItem( $query );
403 is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
405 my $biblio3 = $builder->build({
406 source => 'Biblio',
409 my $biblioitems3 = $builder->build({
410 source => 'Biblioitem',
411 value => {
412 biblionumber => $biblio3->{biblionumber},
416 # Adding a holdable item to biblio 3.
417 my $item3 = $builder->build({
418 source => 'Item',
419 value => {
420 biblionumber => $biblio3->{biblionumber},
421 damaged => 0,
425 my $item4 = $builder->build({
426 source => 'Item',
427 value => {
428 biblionumber => $biblio3->{biblionumber},
429 damaged => 1,
433 my $issuingrule2 = $builder->build({
434 source => 'Issuingrule',
435 value => {
436 categorycode => $patron->{categorycode},
437 itemtype => $item3->{itype},
438 branchcode => $patron->{branchcode},
439 reservesallowed => 10,
443 $query = new CGI;
444 $query->param( 'patron_id', $patron->{borrowernumber});
445 $query->param( 'bib_id', $biblio3->{biblionumber});
446 $query->param( 'item_id', $item4->{itemnumber});
448 $reply = C4::ILSDI::Services::HoldItem( $query );
449 is( $reply->{code}, 'damaged', "Item is damaged" );
451 $schema->storage->txn_rollback;
454 subtest 'Holds test for branch transfer limits' => sub {
456 plan tests => 4;
458 $schema->storage->txn_begin;
460 # Test enforement of branch transfer limits
461 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
462 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
464 my $patron = $builder->build({
465 source => 'Borrower',
468 my $origin_branch = $builder->build(
470 source => 'Branch',
471 value => {
472 pickup_location => 1,
476 my $pickup_branch = $builder->build(
478 source => 'Branch',
479 value => {
480 pickup_location => 1,
485 my $biblio = $builder->build({
486 source => 'Biblio',
488 my $biblioitem = $builder->build({
489 source => 'Biblioitem',
490 value => {
491 biblionumber => $biblio->{biblionumber},
494 my $item = $builder->build({
495 source => 'Item',
496 value => {
497 homebranch => $origin_branch->{branchcode},
498 holdingbranch => $origin_branch->{branchcode},
499 biblionumber => $biblio->{biblionumber},
500 damaged => 0,
501 itemlost => 0,
505 Koha::IssuingRules->search()->delete();
506 my $issuingrule = $builder->build({
507 source => 'Issuingrule',
508 value => {
509 categorycode => '*',
510 itemtype => '*',
511 branchcode => '*',
512 reservesallowed => 99,
516 my $limit = Koha::Item::Transfer::Limit->new({
517 toBranch => $pickup_branch->{branchcode},
518 fromBranch => $item->{holdingbranch},
519 itemtype => $item->{itype},
520 })->store();
522 my $query = new CGI;
523 $query->param( 'pickup_location', $pickup_branch->{branchcode} );
524 $query->param( 'patron_id', $patron->{borrowernumber});
525 $query->param( 'bib_id', $biblio->{biblionumber});
526 $query->param( 'item_id', $item->{itemnumber});
528 my $reply = C4::ILSDI::Services::HoldItem( $query );
529 is( $reply->{code}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
531 $reply = C4::ILSDI::Services::HoldTitle( $query );
532 is( $reply->{code}, 'cannotBeTransferred', "Record hold, Item cannot be transferred" );
534 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
536 $reply = C4::ILSDI::Services::HoldItem( $query );
537 is( $reply->{code}, undef, "Item hold, Item can be transferred" );
539 Koha::Holds->search()->delete();
541 $reply = C4::ILSDI::Services::HoldTitle( $query );
542 is( $reply->{code}, undef, "Record hold, Item con be transferred" );
544 $schema->storage->txn_rollback;
547 subtest 'GetRecords' => sub {
549 plan tests => 1;
551 $schema->storage->txn_begin;
553 t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
555 my $branch1 = $builder->build({
556 source => 'Branch',
558 my $branch2 = $builder->build({
559 source => 'Branch',
562 my $biblio = $builder->build({
563 source => 'Biblio',
565 my $biblioitem = $builder->build({
566 source => 'Biblioitem',
567 value => {
568 biblionumber => $biblio->{biblionumber},
571 my $item = $builder->build_object({
572 class => 'Koha::Items',
573 value => {
574 biblionumber => $biblio->{biblionumber},
575 biblioitemnumber => $biblioitem->{biblioitemnumber},
576 homebranch => $branch1->{branchcode},
577 holdingbranch => $branch1->{branchcode},
581 ModItemTransfer($item->itemnumber, $branch1->{branchcode}, $branch2->{branchcode});
583 my $cgi = new CGI;
584 $cgi->param(service => 'GetRecords');
585 $cgi->param(id => $biblio->{biblionumber});
587 my $reply = C4::ILSDI::Services::GetRecords($cgi);
589 my $transfer = $item->get_transfer;
590 my $expected = {
591 datesent => $transfer->datesent,
592 frombranch => $transfer->frombranch,
593 tobranch => $transfer->tobranch,
595 is_deeply($reply->{record}->[0]->{items}->{item}->[0]->{transfer}, $expected,
596 'GetRecords returns transfer informations');
598 $schema->storage->txn_rollback;
601 subtest 'RenewHold' => sub {
602 plan tests => 4;
604 $schema->storage->txn_begin;
606 my $cgi = new CGI;
607 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
608 my $item = $builder->build_object( { class => 'Koha::Items' } );
609 $cgi->param( patron_id => $patron->borrowernumber );
610 $cgi->param( item_id => $item->itemnumber );
612 t::lib::Mocks::mock_userenv( { patron => $patron } ); # For AddIssue
613 my $checkout = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
615 # Everything is ok
616 my $reply = C4::ILSDI::Services::RenewLoan($cgi);
617 is( exists $reply->{date_due}, 1, 'If the item is checked out, the date_due key should exist' );
619 # The item is not checked out
620 $checkout->delete;
621 $reply = C4::ILSDI::Services::RenewLoan($cgi);
622 is( $reply, undef, 'If the item is not checked out, we should not explode.'); # FIXME We should return an error code instead
624 # The item does not exist
625 $item->delete;
626 $reply = C4::ILSDI::Services::RenewLoan($cgi);
627 is( $reply->{code}, 'RecordNotFound', 'If the item does not exist, RecordNotFound should be returned');
629 $patron->delete;
630 $reply = C4::ILSDI::Services::RenewLoan($cgi);
631 is( $reply->{code}, 'PatronNotFound', 'If the patron does not exist, PatronNotFound should be returned');
633 $schema->storage->txn_rollback;
636 subtest 'GetPatronInfo paginated loans' => sub {
637 plan tests => 7;
639 $schema->storage->txn_begin;
641 my $library = $builder->build_object({
642 class => 'Koha::Libraries',
645 my $item1 = $builder->build_sample_item({ library => $library->branchcode });
646 my $item2 = $builder->build_sample_item({ library => $library->branchcode });
647 my $item3 = $builder->build_sample_item({ library => $library->branchcode });
648 my $patron = $builder->build_object({
649 class => 'Koha::Patrons',
650 value => {
651 branchcode => $library->branchcode,
654 my $module = new Test::MockModule('C4::Context');
655 $module->mock('userenv', sub { { branch => $library->branchcode } });
656 my $date_due = DateTime->now->add(weeks => 2);
657 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
658 my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
659 my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
660 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
661 my $issue3 = C4::Circulation::AddIssue($patron->unblessed, $item3->barcode, $date_due);
662 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
664 my $cgi = new CGI;
666 $cgi->param( 'service', 'GetPatronInfo' );
667 $cgi->param( 'patron_id', $patron->borrowernumber );
668 $cgi->param( 'show_loans', '1' );
669 $cgi->param( 'loans_per_page', '2' );
670 $cgi->param( 'loans_page', '1' );
671 my $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
673 is($reply->{total_loans}, 3, 'total_loans == 3');
674 is(scalar @{ $reply->{loans}->{loan} }, 2, 'GetPatronInfo returned only 2 loans');
675 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item3->itemnumber);
676 is($reply->{loans}->{loan}->[1]->{itemnumber}, $item2->itemnumber);
678 $cgi->param( 'loans_page', '2' );
679 $reply = C4::ILSDI::Services::GetPatronInfo($cgi);
681 is($reply->{total_loans}, 3, 'total_loans == 3');
682 is(scalar @{ $reply->{loans}->{loan} }, 1, 'GetPatronInfo returned only 1 loan');
683 is($reply->{loans}->{loan}->[0]->{itemnumber}, $item1->itemnumber);
685 $schema->storage->txn_rollback;