3 # Tests for SIP::Sip::MsgType
4 # Please help to extend it!
6 # This file is part of Koha.
8 # Copyright 2016 Rijksmuseum
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Test::More tests => 13;
31 use t::lib::TestBuilder;
33 use C4::Reserves qw( AddReserve );
34 use C4::Circulation qw( AddReturn );
36 use Koha::AuthUtils qw(hash_password);
37 use Koha::DateUtils qw( dt_from_string output_pref );
40 use Koha::Old::Checkouts;
45 use C4::SIP::ILS::Patron;
46 use C4::SIP::Sip qw(write_msg);
47 use C4::SIP::Sip::Constants qw(:all);
48 use C4::SIP::Sip::MsgType;
50 use constant PATRON_PW => 'do_not_ever_use_this_one';
53 subtest 'Testing Patron Status Request V2' => sub {
54 my $schema = Koha::Database->new->schema;
55 $schema->storage->txn_begin;
57 $C4::SIP::Sip::protocol_version = 2;
58 test_request_patron_status_v2();
59 $schema->storage->txn_rollback;
62 subtest 'Testing Patron Info Request V2' => sub {
63 my $schema = Koha::Database->new->schema;
64 $schema->storage->txn_begin;
66 $C4::SIP::Sip::protocol_version = 2;
67 test_request_patron_info_v2();
68 $schema->storage->txn_rollback;
71 subtest 'Checkout V2' => sub {
72 my $schema = Koha::Database->new->schema;
73 $schema->storage->txn_begin;
75 $C4::SIP::Sip::protocol_version = 2;
77 $schema->storage->txn_rollback;
80 subtest 'Test checkout desensitize' => sub {
81 my $schema = Koha::Database->new->schema;
82 $schema->storage->txn_begin;
84 $C4::SIP::Sip::protocol_version = 2;
85 test_checkout_desensitize();
86 $schema->storage->txn_rollback;
89 subtest 'Test renew desensitize' => sub {
90 my $schema = Koha::Database->new->schema;
91 $schema->storage->txn_begin;
93 $C4::SIP::Sip::protocol_version = 2;
94 test_renew_desensitize();
95 $schema->storage->txn_rollback;
98 subtest 'Checkin V2' => sub {
99 my $schema = Koha::Database->new->schema;
100 $schema->storage->txn_begin;
102 $C4::SIP::Sip::protocol_version = 2;
104 $schema->storage->txn_rollback;
107 subtest 'Test hold_patron_bcode' => sub {
108 my $schema = Koha::Database->new->schema;
109 $schema->storage->txn_begin;
111 $C4::SIP::Sip::protocol_version = 2;
112 test_hold_patron_bcode();
113 $schema->storage->txn_rollback;
116 subtest 'hold_patron_name() tests' => sub {
120 my $schema = Koha::Database->new->schema;
121 $schema->storage->txn_begin;
123 my $builder = t::lib::TestBuilder->new();
125 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
126 my ( $response, $findpatron );
127 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
129 my $item = $builder->build_sample_item(
135 homebranch => $branchcode,
136 holdingbranch => $branchcode
140 my $server = { ils => $mocks->{ils} };
141 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
143 is( $sip_item->hold_patron_name, q{}, "SIP item with no hold returns empty string for patron name" );
145 my $resp = C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_name, $server );
146 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
148 $schema->storage->txn_rollback;
151 subtest 'Lastseen response' => sub {
153 my $schema = Koha::Database->new->schema;
154 $schema->storage->txn_begin;
157 my $builder = t::lib::TestBuilder->new();
158 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
159 my ( $response, $findpatron );
160 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
161 my $seen_patron = $builder->build({
162 source => 'Borrower',
164 lastseen => '2001-01-01',
165 password => hash_password( PATRON_PW ),
166 branchcode => $branchcode,
169 my $cardnum = $seen_patron->{cardnumber};
170 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
171 $findpatron = $sip_patron;
173 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
174 FID_INST_ID. $branchcode. '|'.
175 FID_PATRON_ID. $cardnum. '|'.
176 FID_PATRON_PWD. PATRON_PW. '|';
177 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
179 my $server = { ils => $mocks->{ils} };
181 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
182 $msg->handle_patron_info( $server );
184 isnt( $response, undef, 'At least we got a response.' );
185 my $respcode = substr( $response, 0, 2 );
186 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
187 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
188 is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({str => '2001-01-01', dateonly => 1}),'Last seen not updated if not tracking patrons');
190 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
191 $msg->handle_patron_info( $server );
193 isnt( $response, undef, 'At least we got a response.' );
194 $respcode = substr( $response, 0, 2 );
195 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
196 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
197 is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
198 $schema->storage->txn_rollback;
202 subtest "Test build_additional_item_fields_string" => sub {
203 my $schema = Koha::Database->new->schema;
204 $schema->storage->txn_begin;
208 my $builder = t::lib::TestBuilder->new();
210 my $item = $builder->build_sample_item;
211 my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
214 $server->{account}->{item_field}->{code} = 'itemnumber';
215 $server->{account}->{item_field}->{field} = 'XY';
216 my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
217 is( $attribute_string, "XY".$item->itemnumber."|", 'Attribute field generated correctly with single param' );
220 $server->{account}->{item_field}->[0]->{code} = 'itemnumber';
221 $server->{account}->{item_field}->[0]->{field} = 'XY';
222 $server->{account}->{item_field}->[1]->{code} = 'biblionumber';
223 $server->{account}->{item_field}->[1]->{field} = 'YZ';
224 $attribute_string = $ils_item->build_additional_item_fields_string( $server );
225 is( $attribute_string, sprintf("XY%s|YZ%s|", $item->itemnumber, $item->biblionumber), 'Attribute field generated correctly with multiple params' );
227 $schema->storage->txn_rollback;
230 subtest "Test cr_item_field" => sub {
233 my $builder = t::lib::TestBuilder->new();
234 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
235 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
236 my ( $response, $findpatron );
237 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
240 my $patron1 = $builder->build({
241 source => 'Borrower',
243 password => hash_password( PATRON_PW ),
246 my $card1 = $patron1->{cardnumber};
247 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
248 $findpatron = $sip_patron1;
249 my $item_object = $builder->build_sample_item({
254 homebranch => $branchcode,
255 holdingbranch => $branchcode,
258 my $mockILS = $mocks->{ils};
259 my $server = { ils => $mockILS, account => {} };
260 $mockILS->mock( 'institution', sub { $branchcode; } );
261 $mockILS->mock( 'supports', sub { return; } );
262 $mockILS->mock( 'checkin', sub {
264 return C4::SIP::ILS->checkin(@_);
266 my $today = dt_from_string;
270 # Not checked out, toggle option checked_in_ok
271 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
272 siprequestdate( $today->clone->add( days => 1) ) .
273 FID_INST_ID . $branchcode . '|'.
274 FID_ITEM_ID . $item_object->barcode . '|' .
275 FID_TERMINAL_PWD . 'ignored' . '|';
277 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
279 $server->{account}->{cr_item_field} = 'itemnumber';
281 $msg->handle_checkin( $server );
283 my $id = $item_object->id;
284 ok( $response =~ m/CR$id/, "Found correct CR field in response");
286 $siprequest = ITEM_INFORMATION . 'YYYYMMDDZZZZHHMMSS' .
287 FID_INST_ID . $branchcode . '|'.
288 FID_ITEM_ID . $item_object->barcode . '|' .
289 FID_TERMINAL_PWD . 'ignored' . '|';
291 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
293 $mockILS->mock( 'find_item', sub {
294 return C4::SIP::ILS::Item->new( $item_object->barcode );
297 $server->{account}->{cr_item_field} = 'itype';
299 $msg->handle_item_information( $server );
301 my $itype = $item_object->itype;
302 ok( $response =~ m/CR$itype/, "Found correct CR field in response");
305 subtest 'Patron info summary > 5 should not crash server' => sub {
307 my $schema = Koha::Database->new->schema;
308 $schema->storage->txn_begin;
311 my $builder = t::lib::TestBuilder->new();
312 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
313 my ( $response, $findpatron );
314 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
315 my $seen_patron = $builder->build({
316 source => 'Borrower',
318 lastseen => '2001-01-01',
319 password => hash_password( PATRON_PW ),
320 branchcode => $branchcode,
323 my $cardnum = $seen_patron->{cardnumber};
324 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
325 $findpatron = $sip_patron;
340 for my $summary ( @summaries ) {
341 my $siprequest = PATRON_INFO . 'engYYYYMMDDZZZZHHMMSS' . $summary .
342 FID_INST_ID . $branchcode . '|' .
343 FID_PATRON_ID . $cardnum . '|' .
344 FID_PATRON_PWD . PATRON_PW . '|';
345 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
347 my $server = { ils => $mocks->{ils} };
349 $msg->handle_patron_info( $server );
351 isnt( $response, undef, 'At least we got a response.' );
352 my $respcode = substr( $response, 0, 2 );
353 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
356 $schema->storage->txn_rollback;
359 subtest 'SC status tests' => sub {
361 my $schema = Koha::Database->new->schema;
362 $schema->storage->txn_begin;
366 my $builder = t::lib::TestBuilder->new();
367 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
368 my $sip_user = $builder->build_object({ class => "Koha::Patrons" });
370 my ( $response, $findpatron );
371 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
372 my $mockILS = $mocks->{ils};
373 $mockILS->mock( 'checkout_ok', sub {1} );
374 $mockILS->mock( 'checkin_ok', sub {1} );
375 $mockILS->mock( 'status_update_ok', sub {1} );
376 $mockILS->mock( 'offline_ok', sub {1} );
377 $mockILS->mock( 'supports', sub {1} );
378 my $server = Test::MockObject->new();
379 $server->mock( 'get_timeout', sub {'100'});
380 $server->{ils} = $mockILS;
381 $server->{sip_username} = $sip_user->userid;
382 $server->{account} = {};
383 $server->{policy} = { renewal =>1,retries=>'000'};
385 my $siprequest = SC_STATUS . '0' . '030' . '2.00';
386 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
387 $msg->handle_sc_status( $server );
389 like( $response, qr/98YYYYYY100000[0-9 ]{19}.00AO|BXYYYYYYYYYYYYYYYY|/, 'At least we got a response.' );
393 dies_ok{ $msg->handle_sc_status( $server ) } ,"Dies if sip user cannot be found";
397 # Here is room for some more subtests
401 sub test_request_patron_status_v2 {
402 my $builder = t::lib::TestBuilder->new();
403 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
404 my ( $response, $findpatron );
405 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
407 my $patron1 = $builder->build({
408 source => 'Borrower',
410 password => hash_password( PATRON_PW ),
413 my $card1 = $patron1->{cardnumber};
414 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
415 $findpatron = $sip_patron1;
417 my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
418 FID_INST_ID. $branchcode. '|'.
419 FID_PATRON_ID. $card1. '|'.
420 FID_PATRON_PWD. PATRON_PW. '|';
421 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
423 my $server = { ils => $mocks->{ils} };
425 $msg->handle_patron_status( $server );
427 isnt( $response, undef, 'At least we got a response.' );
428 my $respcode = substr( $response, 0, 2 );
429 is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
431 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
432 check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
433 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
434 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
435 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
436 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
438 # Now, we pass a wrong password and verify CQ again
439 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
440 FID_INST_ID. $branchcode. '|'.
441 FID_PATRON_ID. $card1. '|'.
442 FID_PATRON_PWD. 'wrong_password'. '|';
443 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
445 $msg->handle_patron_status( $server );
446 $respcode = substr( $response, 0, 2 );
447 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
449 # Check empty password and verify CQ again
450 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
451 FID_INST_ID. $branchcode. '|'.
452 FID_PATRON_ID. $card1. '|'.
454 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
456 $msg->handle_patron_status( $server );
457 $respcode = substr( $response, 0, 2 );
458 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
460 # Finally, we send a wrong card number and check AE, BL
461 # This is done by removing the new patron first
462 Koha::Patrons->search({ cardnumber => $card1 })->delete;
464 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
465 FID_INST_ID. $branchcode. '|'.
466 FID_PATRON_ID. $card1. '|'.
467 FID_PATRON_PWD. PATRON_PW. '|';
468 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
470 $msg->handle_patron_status( $server );
471 $respcode = substr( $response, 0, 2 );
472 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
473 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
474 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
477 sub test_request_patron_info_v2 {
478 my $builder = t::lib::TestBuilder->new();
479 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
480 my ( $response, $findpatron );
481 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
483 my $patron2 = $builder->build({
484 source => 'Borrower',
486 password => hash_password( PATRON_PW ),
489 my $card = $patron2->{cardnumber};
490 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
491 $findpatron = $sip_patron2;
492 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
493 FID_INST_ID. $branchcode. '|'.
494 FID_PATRON_ID. $card. '|'.
495 FID_PATRON_PWD. PATRON_PW. '|';
496 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
498 my $server = { ils => $mocks->{ils} };
500 $msg->handle_patron_info( $server );
501 isnt( $response, undef, 'At least we got a response.' );
502 my $respcode = substr( $response, 0, 2 );
503 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
505 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
506 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
507 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
508 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
509 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
510 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
511 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
512 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
513 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
514 # No check for custom fields here (unofficial PB, PC and PI)
515 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
517 # Test customized patron name in AE with same sip request
518 # This implicitly tests C4::SIP::ILS::Patron->name
519 $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
520 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
522 $msg->handle_patron_info( $server );
523 $respcode = substr( $response, 0, 2 );
524 check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
527 $server->{account}->{hide_fields} = "BD,BE,BF,PB";
528 $msg->handle_patron_info( $server );
529 $respcode = substr( $response, 0, 2 );
530 check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
531 check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
532 check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
533 check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
534 $server->{account}->{hide_fields} = "";
536 # Check empty password and verify CQ again
537 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
538 FID_INST_ID. $branchcode. '|'.
539 FID_PATRON_ID. $card. '|'.
541 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
543 $msg->handle_patron_info( $server );
544 $respcode = substr( $response, 0, 2 );
545 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
546 # Test empty password is OK if account configured to allow
547 $server->{account}->{allow_empty_passwords} = 1;
548 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
550 $msg->handle_patron_info( $server );
551 $respcode = substr( $response, 0, 2 );
552 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
554 t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
555 my $patron = Koha::Patrons->find({ cardnumber => $card });
556 $patron->update({ login_attempts => 0 });
557 is( $patron->account_locked, 0, "Patron account not locked already" );
558 $msg->handle_patron_info( $server );
559 $patron = Koha::Patrons->find({ cardnumber => $card });
560 is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
562 # Finally, we send a wrong card number
563 Koha::Patrons->search({ cardnumber => $card })->delete;
565 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
567 $msg->handle_patron_info( $server );
568 $respcode = substr( $response, 0, 2 );
569 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
570 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
571 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
574 sub test_checkout_v2 {
575 my $builder = t::lib::TestBuilder->new();
576 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
577 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
578 my ( $response, $findpatron );
579 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
582 my $patron1 = $builder->build({
583 source => 'Borrower',
585 password => hash_password( PATRON_PW ),
588 my $card1 = $patron1->{cardnumber};
589 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
590 $findpatron = $sip_patron1;
591 my $item_object = $builder->build_sample_item({
596 homebranch => $branchcode,
597 holdingbranch => $branchcode,
600 my $mockILS = $mocks->{ils};
601 my $server = { ils => $mockILS, account => {} };
602 $mockILS->mock( 'institution', sub { $branchcode; } );
603 $mockILS->mock( 'supports', sub { return; } );
604 $mockILS->mock( 'checkout', sub {
606 return C4::SIP::ILS->checkout(@_);
608 my $today = dt_from_string;
609 t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
610 t::lib::Mocks::mock_preference( 'CheckPrevCheckout', 'hardyes' );
612 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
613 my $return = AddReturn($item_object->barcode, $branchcode);
615 my $siprequest = CHECKOUT . 'YN' . siprequestdate($today) .
616 siprequestdate( $today->clone->add( days => 1) ) .
617 FID_INST_ID . $branchcode . '|'.
618 FID_PATRON_ID . $sip_patron1->id . '|' .
619 FID_ITEM_ID . $item_object->barcode . '|' .
620 FID_TERMINAL_PWD . 'ignored' . '|';
623 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
624 $server->{account}->{prevcheckout_block_checkout} = 1;
625 $msg->handle_checkout( $server );
626 my $respcode = substr( $response, 0, 2 );
627 check_field( $respcode, $response, FID_SCREEN_MSG, 'This item was previously checked out by you', 'Check screen msg', 'equals' );
629 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 0, "Item was not checked out (prevcheckout_block_checkout enabled)");
631 $server->{account}->{prevcheckout_block_checkout} = 0;
632 $msg->handle_checkout( $server );
633 $respcode = substr( $response, 0, 2 );
634 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was checked out (prevcheckout_block_checkout disabled)");
636 $msg->handle_checkout( $server );
637 ok( $response =~ m/AH\d{8} \d{6}/, "Found AH field as timestamp in response");
638 $server->{account}->{format_due_date} = 1;
639 t::lib::Mocks::mock_preference( 'dateFormat', 'sql' );
641 $msg->handle_checkout( $server );
642 ok( $response =~ m/AH\d{4}-\d{2}-\d{2}/, "Found AH field as SQL date in response");
646 sub test_checkin_v2 {
647 my $builder = t::lib::TestBuilder->new();
648 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
649 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
650 my ( $response, $findpatron );
651 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
654 my $patron1 = $builder->build({
655 source => 'Borrower',
657 password => hash_password( PATRON_PW ),
660 my $card1 = $patron1->{cardnumber};
661 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
662 $findpatron = $sip_patron1;
663 my $item_object = $builder->build_sample_item({
668 homebranch => $branchcode,
669 holdingbranch => $branchcode,
672 my $mockILS = $mocks->{ils};
673 my $server = { ils => $mockILS, account => {} };
674 $mockILS->mock( 'institution', sub { $branchcode; } );
675 $mockILS->mock( 'supports', sub { return; } );
676 $mockILS->mock( 'checkin', sub {
678 return C4::SIP::ILS->checkin(@_);
680 my $today = dt_from_string;
682 # Checkin invalid barcode
683 Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
684 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
685 siprequestdate( $today->clone->add( days => 1) ) .
686 FID_INST_ID . $branchcode . '|'.
687 FID_ITEM_ID . 'not_to_be_found' . '|' .
688 FID_TERMINAL_PWD . 'ignored' . '|';
690 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
691 warnings_like { $msg->handle_checkin( $server ); }
692 [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
693 'Checkin of invalid item with two warnings';
694 my $respcode = substr( $response, 0, 2 );
695 is( $respcode, CHECKIN_RESP, 'Response code fine' );
696 is( substr($response,2,1), '0', 'OK flag is false' );
697 is( substr($response,5,1), 'Y', 'Alert flag is set' );
698 check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
700 # Not checked out, toggle option checked_in_ok
701 $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
702 siprequestdate( $today->clone->add( days => 1) ) .
703 FID_INST_ID . $branchcode . '|'.
704 FID_ITEM_ID . $item_object->barcode . '|' .
705 FID_TERMINAL_PWD . 'ignored' . '|';
707 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
708 $msg->handle_checkin( $server );
709 $respcode = substr( $response, 0, 2 );
710 is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
711 is( substr($response,5,1), 'Y', 'Alert flag is set' );
712 check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
714 $server->{account}->{checked_in_ok} = 1;
716 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
717 $msg->handle_checkin( $server );
718 is( substr($response,2,1), '1', 'OK flag is true now with checked_in_ok flag set when checking in an item that was not checked out' );
719 is( substr($response,5,1), 'N', 'Alert flag no longer set' );
720 check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
722 # Move item to another holding branch to trigger CV of 04 with alert flag
723 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
724 $item_object->holdingbranch( $branchcode2 )->store();
726 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
727 $msg->handle_checkin( $server );
728 is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
729 check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
730 $item_object->holdingbranch( $branchcode )->store();
731 t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
733 $server->{account}->{cv_send_00_on_success} = 0;
735 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
736 $msg->handle_checkin( $server );
737 $respcode = substr( $response, 0, 2 );
738 check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
739 $server->{account}->{cv_send_00_on_success} = 1;
741 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
742 $msg->handle_checkin( $server );
743 $respcode = substr( $response, 0, 2 );
744 check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
745 $server->{account}->{checked_in_ok} = 0;
746 $server->{account}->{cv_send_00_on_success} = 0;
748 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
749 $server->{account}->{checked_in_ok} = 0;
750 $server->{account}->{cv_triggers_alert} = 0;
752 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
753 $msg->handle_checkin( $server );
754 $respcode = substr( $response, 0, 2 );
755 is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
756 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
757 $server->{account}->{cv_triggers_alert} = 1;
759 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
760 $msg->handle_checkin( $server );
761 $respcode = substr( $response, 0, 2 );
762 is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
763 $server->{account}->{cv_triggers_alert} = 0;
764 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
766 $server->{account}->{checked_in_ok} = 1;
767 $server->{account}->{ct_always_send} = 0;
769 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
770 $msg->handle_checkin( $server );
771 $respcode = substr( $response, 0, 2 );
772 check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
773 $server->{account}->{ct_always_send} = 1;
775 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
776 $msg->handle_checkin( $server );
777 $respcode = substr( $response, 0, 2 );
778 check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
779 $server->{account}->{checked_in_ok} = 0;
780 $server->{account}->{ct_always_send} = 0;
782 # Checkin at wrong branch: issue item and switch branch, and checkin
783 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
784 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
785 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
787 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
788 $msg->handle_checkin( $server );
789 is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
790 is( substr($response,5,1), 'Y', 'Alert flag is set' );
791 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
792 $branchcode = $item_object->homebranch; # switch back
793 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
795 # Data corrupted: add same issue_id to old_issues
796 Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
798 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
799 warnings_like { $msg->handle_checkin( $server ); }
800 [ qr/Duplicate entry/, qr/data issues/ ],
801 'DBIx error on duplicate issue_id';
802 is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
803 is( substr($response,5,1), 'Y', 'Alert flag is set' );
804 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
806 # Finally checkin without problems (remove duplicate id)
807 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
809 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
810 $msg->handle_checkin( $server );
811 is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
812 is( substr($response,5,1), 'N', 'Alert flag is not set' );
813 is( Koha::Checkouts->find( $issue->issue_id ), undef,
814 'Issue record is gone now' );
816 # Test account option no_holds_check that prevents items on hold from being checked in via SIP
817 $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
818 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item is checked out");
819 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
820 $server->{account}->{holds_block_checkin} = 1;
821 my $reserve_id = AddReserve({
822 branchcode => $branchcode,
823 borrowernumber => $patron1->{borrowernumber},
824 biblionumber => $item_object->biblionumber,
827 my $hold = Koha::Holds->find( $reserve_id );
828 is( $hold->id, $reserve_id, "Hold was created successfully" );
830 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
831 $msg->handle_checkin( $server );
832 is( substr($response,2,1), '0', 'OK flag is false when we check in an item on hold and we do not allow it' );
833 is( substr($response,5,1), 'Y', 'Alert flag is set' );
834 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was not checked in");
835 $hold->discard_changes;
836 is( $hold->found, undef, "Hold was not marked as found by SIP when holds_block_checkin enabled");
837 $server->{account}->{holds_block_checkin} = 0;
839 # Test account option holds_get_captured that automatically sets the hold as found for a hold and possibly sets it to in transit
840 $server->{account}->{holds_get_captured} = 0;
842 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
843 $msg->handle_checkin( $server );
844 is( substr($response,2,1), '1', 'OK flag is true when we check in an item on hold and we allow it but do not capture it' );
845 is( substr($response,5,1), 'Y', 'Alert flag is set' );
846 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 0, "Item was checked in");
847 $hold->discard_changes;
848 is( $hold->found, undef, "Hold was not marked as found by SIP when holds_get_captured disabled");
850 $server->{account}->{holds_get_captured} = 1;
853 sub test_hold_patron_bcode {
854 my $builder = t::lib::TestBuilder->new();
855 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
856 my ( $response, $findpatron );
857 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
859 my $item = $builder->build_sample_item(
861 library => $branchcode
865 my $server = { ils => $mocks->{ils} };
866 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
868 is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
870 my $resp = C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
871 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
874 sub test_checkout_desensitize {
875 my $builder = t::lib::TestBuilder->new();
876 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
877 my ( $response, $findpatron );
878 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
881 my $patron1 = $builder->build({
882 source => 'Borrower',
884 password => hash_password( PATRON_PW ),
887 my $card1 = $patron1->{cardnumber};
888 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
889 my $patron_category = $sip_patron1->ptype();
890 $findpatron = $sip_patron1;
891 my $item_object = $builder->build_sample_item({
896 homebranch => $branchcode,
897 holdingbranch => $branchcode,
900 my $mockILS = $mocks->{ils};
901 my $server = { ils => $mockILS, account => {} };
902 $mockILS->mock( 'institution', sub { $branchcode; } );
903 $mockILS->mock( 'supports', sub { return; } );
904 $mockILS->mock( 'checkout', sub {
906 return C4::SIP::ILS->checkout(@_);
908 my $today = dt_from_string;
909 t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
910 t::lib::Mocks::mock_preference( 'CheckPrevCheckout', 'hardyes' );
912 my $siprequest = CHECKOUT . 'YN' . siprequestdate($today) .
913 siprequestdate( $today->clone->add( days => 1) ) .
914 FID_INST_ID . $branchcode . '|'.
915 FID_PATRON_ID . $sip_patron1->id . '|' .
916 FID_ITEM_ID . $item_object->barcode . '|' .
917 FID_TERMINAL_PWD . 'ignored' . '|';
920 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
921 $server->{account}->{inhouse_patron_categories} = "A,$patron_category,Z";
922 $msg->handle_checkout( $server );
923 my $respcode = substr( $response, 5, 1 );
924 is( $respcode, 'N', "Desensitize flag was not set for patron category in inhouse_patron_categories" );
927 $server->{account}->{inhouse_patron_categories} = "A,B,C";
928 $msg->handle_checkout( $server );
929 $respcode = substr( $response, 5, 1 );
930 is( $respcode, 'Y', "Desensitize flag was set for patron category not in inhouse_patron_categories" );
933 $server->{account}->{inhouse_patron_categories} = "";
934 $msg->handle_checkout( $server );
935 $respcode = substr( $response, 5, 1 );
936 is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_patron_categories" );
939 sub test_renew_desensitize {
940 my $builder = t::lib::TestBuilder->new();
941 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
942 my ( $response, $findpatron );
943 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
946 my $patron1 = $builder->build({
947 source => 'Borrower',
949 password => hash_password( PATRON_PW ),
952 my $card1 = $patron1->{cardnumber};
953 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
954 my $patron_category = $sip_patron1->ptype();
955 $findpatron = $sip_patron1;
956 my $item_object = $builder->build_sample_item({
961 homebranch => $branchcode,
962 holdingbranch => $branchcode,
965 my $mockILS = $mocks->{ils};
966 my $server = { ils => $mockILS, account => {} };
967 $mockILS->mock( 'institution', sub { $branchcode; } );
968 $mockILS->mock( 'supports', sub { return; } );
969 $mockILS->mock( 'checkout', sub {
971 return C4::SIP::ILS->checkout(@_);
973 my $today = dt_from_string;
974 t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
976 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
978 my $siprequest = RENEW . 'YN' . siprequestdate($today) .
979 siprequestdate( $today->clone->add( days => 1) ) .
980 FID_INST_ID . $branchcode . '|'.
981 FID_PATRON_ID . $sip_patron1->id . '|' .
982 FID_ITEM_ID . $item_object->barcode . '|' .
983 FID_TERMINAL_PWD . 'ignored' . '|';
986 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
987 $server->{account}->{inhouse_patron_categories} = "A,$patron_category,Z";
988 $msg->handle_checkout( $server );
989 my $respcode = substr( $response, 5, 1 );
990 is( $respcode, 'N', "Desensitize flag was not set for patron category in inhouse_patron_categories" );
993 $server->{account}->{inhouse_patron_categories} = "A,B,C";
994 $msg->handle_checkout( $server );
995 $respcode = substr( $response, 5, 1 );
996 is( $respcode, 'Y', "Desensitize flag was set for patron category not in inhouse_patron_categories" );
999 $server->{account}->{inhouse_patron_categories} = "";
1000 $msg->handle_checkout( $server );
1001 $respcode = substr( $response, 5, 1 );
1002 is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_patron_categories" );
1008 my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
1010 # mock write_msg (imported from Sip.pm into Message.pm)
1011 my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
1012 $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
1015 my $mockILS = Test::MockObject->new;
1016 $mockILS->mock( 'check_inst_id', sub {} );
1017 $mockILS->mock( 'institution_id', sub { $$branchcode; } );
1018 $mockILS->mock( 'find_patron', sub { $$findpatron; } );
1020 return { ils => $mockILS, message => $mockMsg };
1024 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
1025 # mode: contains || equals || regex (by default: equals)
1027 # strip fixed part; prefix to simplify next regex
1028 $resp = '|'. substr( $resp, fixed_length( $code ) );
1030 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
1032 } elsif( !defined($expr) ) { # field should not be found
1035 } else { # test fails
1036 is( 0, 1, "Code $fld not found in '$resp'?" );
1040 if( !$mode || $mode eq 'equals' ) { # default
1041 is( $fldval, $expr, $msg );
1042 } elsif( $mode eq 'regex' ) {
1043 is( $fldval =~ /$expr/, 1, $msg );
1045 is( index( $fldval, $expr ) > -1, 1, $msg );
1049 sub siprequestdate {
1051 return $dt->ymd('').(' 'x4).$dt->hms('');
1054 sub fixed_length { #length of fixed fields including response code
1056 ( PATRON_STATUS_RESP ) => 37,
1057 ( PATRON_INFO_RESP ) => 61,
1058 ( CHECKIN_RESP ) => 24,
1059 ( CHECKOUT_RESP ) => 24,