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 => 15;
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 'UseLocationAsAQInSIP syspref tests' => sub {
119 my $schema = Koha::Database->new->schema;
120 $schema->storage->txn_begin;
122 my $builder = t::lib::TestBuilder->new();
124 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
126 t::lib::Mocks::mock_preference('UseLocationAsAQInSIP', 0);
128 my $item = $builder->build_sample_item(
134 homebranch => $branchcode,
135 holdingbranch => $branchcode,
136 permanent_location => "PERMANENT_LOCATION"
140 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
141 is( $sip_item->permanent_location, $branchcode, "When UseLocationAsAQInSIP is not set SIP item has permanent_location set to value of homebranch" );
143 t::lib::Mocks::mock_preference('UseLocationAsAQInSIP', 1);
145 $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
146 is( $sip_item->permanent_location, "PERMANENT_LOCATION", "When UseLocationAsAQInSIP is set SIP item has permanent_location set to value of item permanent_location" );
148 $schema->storage->txn_rollback;
151 subtest 'hold_patron_name() tests' => sub {
155 my $schema = Koha::Database->new->schema;
156 $schema->storage->txn_begin;
158 my $builder = t::lib::TestBuilder->new();
160 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
161 my ( $response, $findpatron );
162 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
164 my $item = $builder->build_sample_item(
170 homebranch => $branchcode,
171 holdingbranch => $branchcode
175 my $server = { ils => $mocks->{ils} };
176 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
178 is( $sip_item->hold_patron_name, q{}, "SIP item with no hold returns empty string for patron name" );
180 my $resp = C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_name, $server );
181 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
183 $resp = C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, "0", $server );
184 is( $resp, q{CS0|}, "maybe_add will create the field of the string '0'" );
186 $schema->storage->txn_rollback;
189 subtest 'Lastseen response' => sub {
191 my $schema = Koha::Database->new->schema;
192 $schema->storage->txn_begin;
195 my $builder = t::lib::TestBuilder->new();
196 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
197 my ( $response, $findpatron );
198 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
199 my $seen_patron = $builder->build({
200 source => 'Borrower',
202 lastseen => '2001-01-01',
203 password => hash_password( PATRON_PW ),
204 branchcode => $branchcode,
207 my $cardnum = $seen_patron->{cardnumber};
208 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
209 $findpatron = $sip_patron;
211 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
212 FID_INST_ID. $branchcode. '|'.
213 FID_PATRON_ID. $cardnum. '|'.
214 FID_PATRON_PWD. PATRON_PW. '|';
215 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
217 my $server = { ils => $mocks->{ils} };
219 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
220 $msg->handle_patron_info( $server );
222 isnt( $response, undef, 'At least we got a response.' );
223 my $respcode = substr( $response, 0, 2 );
224 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
225 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
226 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');
228 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
229 $msg->handle_patron_info( $server );
231 isnt( $response, undef, 'At least we got a response.' );
232 $respcode = substr( $response, 0, 2 );
233 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
234 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
235 is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
236 $schema->storage->txn_rollback;
240 subtest "Test build_additional_item_fields_string" => sub {
241 my $schema = Koha::Database->new->schema;
242 $schema->storage->txn_begin;
246 my $builder = t::lib::TestBuilder->new();
248 my $item = $builder->build_sample_item;
249 my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
252 $server->{account}->{item_field}->{code} = 'itemnumber';
253 $server->{account}->{item_field}->{field} = 'XY';
254 my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
255 is( $attribute_string, "XY".$item->itemnumber."|", 'Attribute field generated correctly with single param' );
258 $server->{account}->{item_field}->[0]->{code} = 'itemnumber';
259 $server->{account}->{item_field}->[0]->{field} = 'XY';
260 $server->{account}->{item_field}->[1]->{code} = 'biblionumber';
261 $server->{account}->{item_field}->[1]->{field} = 'YZ';
262 $attribute_string = $ils_item->build_additional_item_fields_string( $server );
263 is( $attribute_string, sprintf("XY%s|YZ%s|", $item->itemnumber, $item->biblionumber), 'Attribute field generated correctly with multiple params' );
265 $schema->storage->txn_rollback;
268 subtest "Test build_custom_field_string" => sub {
269 my $schema = Koha::Database->new->schema;
270 $schema->storage->txn_begin;
274 my $builder = t::lib::TestBuilder->new();
276 my $item = $builder->build_sample_item;
277 my $item_id = $item->id;
278 my $item_barcode = $item->barcode;
279 my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
282 $server->{account}->{custom_item_field}->{field} = "XY";
283 $server->{account}->{custom_item_field}->{template} = "[% item.id %] [% item.barcode %], woo!";
284 my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
285 is( $attribute_string, "XY$item_id $item_barcode, woo!|", 'Attribute field generated correctly with single param' );
288 $server->{account}->{custom_item_field}->[0]->{field} = "ZY";
289 $server->{account}->{custom_item_field}->[0]->{template} = "[% item.id %]!";
290 $server->{account}->{custom_item_field}->[1]->{field} = "ZX";
291 $server->{account}->{custom_item_field}->[1]->{template} = "[% item.barcode %]*";
292 $attribute_string = $ils_item->build_additional_item_fields_string( $server );
293 is( $attribute_string, sprintf("ZY%s!|ZX%s*|", $item_id, $item_barcode), 'Attribute field generated correctly with multiple params' );
295 $schema->storage->txn_rollback;
298 subtest "Test cr_item_field" => sub {
301 my $builder = t::lib::TestBuilder->new();
302 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
303 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
304 my ( $response, $findpatron );
305 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
308 my $patron1 = $builder->build({
309 source => 'Borrower',
311 password => hash_password( PATRON_PW ),
314 my $card1 = $patron1->{cardnumber};
315 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
316 $findpatron = $sip_patron1;
317 my $item_object = $builder->build_sample_item({
322 homebranch => $branchcode,
323 holdingbranch => $branchcode,
326 my $mockILS = $mocks->{ils};
327 my $server = { ils => $mockILS, account => {} };
328 $mockILS->mock( 'institution', sub { $branchcode; } );
329 $mockILS->mock( 'supports', sub { return; } );
330 $mockILS->mock( 'checkin', sub {
332 return C4::SIP::ILS->checkin(@_);
334 my $today = dt_from_string;
338 # Not checked out, toggle option checked_in_ok
339 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
340 siprequestdate( $today->clone->add( days => 1) ) .
341 FID_INST_ID . $branchcode . '|'.
342 FID_ITEM_ID . $item_object->barcode . '|' .
343 FID_TERMINAL_PWD . 'ignored' . '|';
345 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
347 $server->{account}->{cr_item_field} = 'itemnumber';
349 $msg->handle_checkin( $server );
351 my $id = $item_object->id;
352 ok( $response =~ m/CR$id/, "Found correct CR field in response");
354 $siprequest = ITEM_INFORMATION . 'YYYYMMDDZZZZHHMMSS' .
355 FID_INST_ID . $branchcode . '|'.
356 FID_ITEM_ID . $item_object->barcode . '|' .
357 FID_TERMINAL_PWD . 'ignored' . '|';
359 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
361 $mockILS->mock( 'find_item', sub {
362 return C4::SIP::ILS::Item->new( $item_object->barcode );
365 $server->{account}->{cr_item_field} = 'itype';
367 $msg->handle_item_information( $server );
369 my $itype = $item_object->itype;
370 ok( $response =~ m/CR$itype/, "Found correct CR field in response");
373 subtest 'Patron info summary > 5 should not crash server' => sub {
375 my $schema = Koha::Database->new->schema;
376 $schema->storage->txn_begin;
379 my $builder = t::lib::TestBuilder->new();
380 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
381 my ( $response, $findpatron );
382 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
383 my $seen_patron = $builder->build({
384 source => 'Borrower',
386 lastseen => '2001-01-01',
387 password => hash_password( PATRON_PW ),
388 branchcode => $branchcode,
391 my $cardnum = $seen_patron->{cardnumber};
392 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
393 $findpatron = $sip_patron;
408 for my $summary ( @summaries ) {
409 my $siprequest = PATRON_INFO . 'engYYYYMMDDZZZZHHMMSS' . $summary .
410 FID_INST_ID . $branchcode . '|' .
411 FID_PATRON_ID . $cardnum . '|' .
412 FID_PATRON_PWD . PATRON_PW . '|';
413 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
415 my $server = { ils => $mocks->{ils} };
417 $msg->handle_patron_info( $server );
419 isnt( $response, undef, 'At least we got a response.' );
420 my $respcode = substr( $response, 0, 2 );
421 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
424 $schema->storage->txn_rollback;
427 subtest 'SC status tests' => sub {
429 my $schema = Koha::Database->new->schema;
430 $schema->storage->txn_begin;
434 my $builder = t::lib::TestBuilder->new();
435 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
436 my $sip_user = $builder->build_object({ class => "Koha::Patrons" });
438 my ( $response, $findpatron );
439 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
440 my $mockILS = $mocks->{ils};
441 $mockILS->mock( 'checkout_ok', sub {1} );
442 $mockILS->mock( 'checkin_ok', sub {1} );
443 $mockILS->mock( 'status_update_ok', sub {1} );
444 $mockILS->mock( 'offline_ok', sub {1} );
445 $mockILS->mock( 'supports', sub {1} );
446 my $server = Test::MockObject->new();
447 $server->mock( 'get_timeout', sub {'100'});
448 $server->{ils} = $mockILS;
449 $server->{sip_username} = $sip_user->userid;
450 $server->{account} = {};
451 $server->{policy} = { renewal =>1,retries=>'000'};
453 my $siprequest = SC_STATUS . '0' . '030' . '2.00';
454 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
455 $msg->handle_sc_status( $server );
457 like( $response, qr/98YYYYYY100000[0-9 ]{19}.00AO|BXYYYYYYYYYYYYYYYY|/, 'At least we got a response.' );
461 dies_ok{ $msg->handle_sc_status( $server ) } ,"Dies if sip user cannot be found";
465 # Here is room for some more subtests
469 sub test_request_patron_status_v2 {
470 my $builder = t::lib::TestBuilder->new();
471 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
472 my ( $response, $findpatron );
473 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
475 my $patron1 = $builder->build({
476 source => 'Borrower',
478 password => hash_password( PATRON_PW ),
481 my $card1 = $patron1->{cardnumber};
482 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
483 $findpatron = $sip_patron1;
485 my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
486 FID_INST_ID. $branchcode. '|'.
487 FID_PATRON_ID. $card1. '|'.
488 FID_PATRON_PWD. PATRON_PW. '|';
489 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
491 my $server = { ils => $mocks->{ils} };
493 $msg->handle_patron_status( $server );
495 isnt( $response, undef, 'At least we got a response.' );
496 my $respcode = substr( $response, 0, 2 );
497 is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
499 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
500 check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
501 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
502 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
503 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
504 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
506 # Now, we pass a wrong password and verify CQ again
507 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
508 FID_INST_ID. $branchcode. '|'.
509 FID_PATRON_ID. $card1. '|'.
510 FID_PATRON_PWD. 'wrong_password'. '|';
511 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
513 $msg->handle_patron_status( $server );
514 $respcode = substr( $response, 0, 2 );
515 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
517 # Check empty password and verify CQ again
518 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
519 FID_INST_ID. $branchcode. '|'.
520 FID_PATRON_ID. $card1. '|'.
522 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
524 $msg->handle_patron_status( $server );
525 $respcode = substr( $response, 0, 2 );
526 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
528 # Finally, we send a wrong card number and check AE, BL
529 # This is done by removing the new patron first
530 Koha::Patrons->search({ cardnumber => $card1 })->delete;
532 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
533 FID_INST_ID. $branchcode. '|'.
534 FID_PATRON_ID. $card1. '|'.
535 FID_PATRON_PWD. PATRON_PW. '|';
536 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
538 $msg->handle_patron_status( $server );
539 $respcode = substr( $response, 0, 2 );
540 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
541 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
542 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
545 sub test_request_patron_info_v2 {
546 my $builder = t::lib::TestBuilder->new();
547 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
548 my ( $response, $findpatron );
549 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
551 my $patron2 = $builder->build({
552 source => 'Borrower',
554 password => hash_password( PATRON_PW ),
557 my $card = $patron2->{cardnumber};
558 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
559 $findpatron = $sip_patron2;
560 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
561 FID_INST_ID. $branchcode. '|'.
562 FID_PATRON_ID. $card. '|'.
563 FID_PATRON_PWD. PATRON_PW. '|';
564 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
566 my $server = { ils => $mocks->{ils} };
568 $msg->handle_patron_info( $server );
569 isnt( $response, undef, 'At least we got a response.' );
570 my $respcode = substr( $response, 0, 2 );
571 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
573 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
574 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
575 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
576 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
577 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
578 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
579 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
580 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
581 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
582 # No check for custom fields here (unofficial PB, PC and PI)
583 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
585 # Test customized patron name in AE with same sip request
586 # This implicitly tests C4::SIP::ILS::Patron->name
587 $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
588 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
590 $msg->handle_patron_info( $server );
591 $respcode = substr( $response, 0, 2 );
592 check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
595 $server->{account}->{hide_fields} = "BD,BE,BF,PB";
596 $msg->handle_patron_info( $server );
597 $respcode = substr( $response, 0, 2 );
598 check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
599 check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
600 check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
601 check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
602 $server->{account}->{hide_fields} = "";
604 # Check empty password and verify CQ again
605 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
606 FID_INST_ID. $branchcode. '|'.
607 FID_PATRON_ID. $card. '|'.
609 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
611 $msg->handle_patron_info( $server );
612 $respcode = substr( $response, 0, 2 );
613 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
614 # Test empty password is OK if account configured to allow
615 $server->{account}->{allow_empty_passwords} = 1;
616 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
618 $msg->handle_patron_info( $server );
619 $respcode = substr( $response, 0, 2 );
620 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
622 t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
623 my $patron = Koha::Patrons->find({ cardnumber => $card });
624 $patron->update({ login_attempts => 0 });
625 is( $patron->account_locked, 0, "Patron account not locked already" );
626 $msg->handle_patron_info( $server );
627 $patron = Koha::Patrons->find({ cardnumber => $card });
628 is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
630 # Finally, we send a wrong card number
631 Koha::Patrons->search({ cardnumber => $card })->delete;
633 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
635 $msg->handle_patron_info( $server );
636 $respcode = substr( $response, 0, 2 );
637 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
638 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
639 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
642 sub test_checkout_v2 {
643 my $builder = t::lib::TestBuilder->new();
644 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
645 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
646 my ( $response, $findpatron );
647 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
650 my $patron1 = $builder->build({
651 source => 'Borrower',
653 password => hash_password( PATRON_PW ),
656 my $card1 = $patron1->{cardnumber};
657 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
658 $findpatron = $sip_patron1;
659 my $item_object = $builder->build_sample_item({
664 homebranch => $branchcode,
665 holdingbranch => $branchcode,
668 my $mockILS = $mocks->{ils};
669 my $server = { ils => $mockILS, account => {} };
670 $mockILS->mock( 'institution', sub { $branchcode; } );
671 $mockILS->mock( 'supports', sub { return; } );
672 $mockILS->mock( 'checkout', sub {
674 return C4::SIP::ILS->checkout(@_);
676 my $today = dt_from_string;
677 t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
678 t::lib::Mocks::mock_preference( 'CheckPrevCheckout', 'hardyes' );
680 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
681 my $return = AddReturn($item_object->barcode, $branchcode);
683 my $siprequest = CHECKOUT . 'YN' . siprequestdate($today) .
684 siprequestdate( $today->clone->add( days => 1) ) .
685 FID_INST_ID . $branchcode . '|'.
686 FID_PATRON_ID . $sip_patron1->id . '|' .
687 FID_ITEM_ID . $item_object->barcode . '|' .
688 FID_TERMINAL_PWD . 'ignored' . '|';
691 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
692 $server->{account}->{prevcheckout_block_checkout} = 1;
693 $msg->handle_checkout( $server );
694 my $respcode = substr( $response, 0, 2 );
695 check_field( $respcode, $response, FID_SCREEN_MSG, 'This item was previously checked out by you', 'Check screen msg', 'equals' );
697 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 0, "Item was not checked out (prevcheckout_block_checkout enabled)");
699 $server->{account}->{prevcheckout_block_checkout} = 0;
700 $msg->handle_checkout( $server );
701 $respcode = substr( $response, 0, 2 );
702 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was checked out (prevcheckout_block_checkout disabled)");
704 $msg->handle_checkout( $server );
705 ok( $response =~ m/AH\d{8} \d{6}/, "Found AH field as timestamp in response");
706 $server->{account}->{format_due_date} = 1;
707 t::lib::Mocks::mock_preference( 'dateFormat', 'sql' );
709 $msg->handle_checkout( $server );
710 ok( $response =~ m/AH\d{4}-\d{2}-\d{2}/, "Found AH field as SQL date in response");
714 sub test_checkin_v2 {
715 my $builder = t::lib::TestBuilder->new();
716 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
717 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
718 my ( $response, $findpatron );
719 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
722 my $patron1 = $builder->build({
723 source => 'Borrower',
725 password => hash_password( PATRON_PW ),
728 my $card1 = $patron1->{cardnumber};
729 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
730 $findpatron = $sip_patron1;
731 my $item_object = $builder->build_sample_item({
736 homebranch => $branchcode,
737 holdingbranch => $branchcode,
740 my $mockILS = $mocks->{ils};
741 my $server = { ils => $mockILS, account => {} };
742 $mockILS->mock( 'institution', sub { $branchcode; } );
743 $mockILS->mock( 'supports', sub { return; } );
744 $mockILS->mock( 'checkin', sub {
746 return C4::SIP::ILS->checkin(@_);
748 my $today = dt_from_string;
750 # Checkin invalid barcode
751 Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
752 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
753 siprequestdate( $today->clone->add( days => 1) ) .
754 FID_INST_ID . $branchcode . '|'.
755 FID_ITEM_ID . 'not_to_be_found' . '|' .
756 FID_TERMINAL_PWD . 'ignored' . '|';
758 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
759 warnings_like { $msg->handle_checkin( $server ); }
760 [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
761 'Checkin of invalid item with two warnings';
762 my $respcode = substr( $response, 0, 2 );
763 is( $respcode, CHECKIN_RESP, 'Response code fine' );
764 is( substr($response,2,1), '0', 'OK flag is false' );
765 is( substr($response,5,1), 'Y', 'Alert flag is set' );
766 check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
768 # Not checked out, toggle option checked_in_ok
769 $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
770 siprequestdate( $today->clone->add( days => 1) ) .
771 FID_INST_ID . $branchcode . '|'.
772 FID_ITEM_ID . $item_object->barcode . '|' .
773 FID_TERMINAL_PWD . 'ignored' . '|';
775 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
776 $msg->handle_checkin( $server );
777 $respcode = substr( $response, 0, 2 );
778 is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
779 is( substr($response,5,1), 'Y', 'Alert flag is set' );
780 check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
782 $server->{account}->{checked_in_ok} = 1;
784 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
785 $msg->handle_checkin( $server );
786 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' );
787 is( substr($response,5,1), 'N', 'Alert flag no longer set' );
788 check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
790 # Move item to another holding branch to trigger CV of 04 with alert flag
791 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
792 $item_object->holdingbranch( $branchcode2 )->store();
794 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
795 $msg->handle_checkin( $server );
796 is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
797 check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
798 $item_object->holdingbranch( $branchcode )->store();
799 t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
801 $server->{account}->{cv_send_00_on_success} = 0;
803 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
804 $msg->handle_checkin( $server );
805 $respcode = substr( $response, 0, 2 );
806 check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
807 $server->{account}->{cv_send_00_on_success} = 1;
809 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
810 $msg->handle_checkin( $server );
811 $respcode = substr( $response, 0, 2 );
812 check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
813 $server->{account}->{checked_in_ok} = 0;
814 $server->{account}->{cv_send_00_on_success} = 0;
816 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
817 $server->{account}->{checked_in_ok} = 0;
818 $server->{account}->{cv_triggers_alert} = 0;
820 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
821 $msg->handle_checkin( $server );
822 $respcode = substr( $response, 0, 2 );
823 is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
824 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
825 $server->{account}->{cv_triggers_alert} = 1;
827 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
828 $msg->handle_checkin( $server );
829 $respcode = substr( $response, 0, 2 );
830 is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
831 $server->{account}->{cv_triggers_alert} = 0;
832 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
834 $server->{account}->{checked_in_ok} = 1;
835 $server->{account}->{ct_always_send} = 0;
837 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
838 $msg->handle_checkin( $server );
839 $respcode = substr( $response, 0, 2 );
840 check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
841 $server->{account}->{ct_always_send} = 1;
843 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
844 $msg->handle_checkin( $server );
845 $respcode = substr( $response, 0, 2 );
846 check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
847 $server->{account}->{checked_in_ok} = 0;
848 $server->{account}->{ct_always_send} = 0;
850 # Checkin at wrong branch: issue item and switch branch, and checkin
851 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
852 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
853 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
855 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
856 $msg->handle_checkin( $server );
857 is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
858 is( substr($response,5,1), 'Y', 'Alert flag is set' );
859 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
860 $branchcode = $item_object->homebranch; # switch back
861 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
863 # Data corrupted: add same issue_id to old_issues
864 Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
866 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
867 warnings_like { $msg->handle_checkin( $server ); }
868 [ qr/Duplicate entry/, qr/data issues/ ],
869 'DBIx error on duplicate issue_id';
870 is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
871 is( substr($response,5,1), 'Y', 'Alert flag is set' );
872 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
874 # Finally checkin without problems (remove duplicate id)
875 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
877 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
878 $msg->handle_checkin( $server );
879 is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
880 is( substr($response,5,1), 'N', 'Alert flag is not set' );
881 is( Koha::Checkouts->find( $issue->issue_id ), undef,
882 'Issue record is gone now' );
884 # Test account option no_holds_check that prevents items on hold from being checked in via SIP
885 $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
886 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item is checked out");
887 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
888 $server->{account}->{holds_block_checkin} = 1;
889 my $reserve_id = AddReserve({
890 branchcode => $branchcode,
891 borrowernumber => $patron1->{borrowernumber},
892 biblionumber => $item_object->biblionumber,
895 my $hold = Koha::Holds->find( $reserve_id );
896 is( $hold->id, $reserve_id, "Hold was created successfully" );
898 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
899 $msg->handle_checkin( $server );
900 is( substr($response,2,1), '0', 'OK flag is false when we check in an item on hold and we do not allow it' );
901 is( substr($response,5,1), 'Y', 'Alert flag is set' );
902 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was not checked in");
903 $hold->discard_changes;
904 is( $hold->found, undef, "Hold was not marked as found by SIP when holds_block_checkin enabled");
905 $server->{account}->{holds_block_checkin} = 0;
907 # Test account option holds_get_captured that automatically sets the hold as found for a hold and possibly sets it to in transit
908 $server->{account}->{holds_get_captured} = 0;
910 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
911 $msg->handle_checkin( $server );
912 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' );
913 is( substr($response,5,1), 'Y', 'Alert flag is set' );
914 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 0, "Item was checked in");
915 $hold->discard_changes;
916 is( $hold->found, undef, "Hold was not marked as found by SIP when holds_get_captured disabled");
918 $server->{account}->{holds_get_captured} = 1;
921 sub test_hold_patron_bcode {
922 my $builder = t::lib::TestBuilder->new();
923 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
924 my ( $response, $findpatron );
925 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
927 my $item = $builder->build_sample_item(
929 library => $branchcode
933 my $server = { ils => $mocks->{ils} };
934 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
936 is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
938 my $resp = C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
939 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
942 sub test_checkout_desensitize {
943 my $builder = t::lib::TestBuilder->new();
944 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
945 my ( $response, $findpatron );
946 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
949 my $patron1 = $builder->build({
950 source => 'Borrower',
952 password => hash_password( PATRON_PW ),
955 my $card1 = $patron1->{cardnumber};
956 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
957 my $patron_category = $sip_patron1->ptype();
958 $findpatron = $sip_patron1;
959 my $item_object = $builder->build_sample_item({
964 homebranch => $branchcode,
965 holdingbranch => $branchcode,
967 my $itemtype = $item_object->effective_itemtype;
969 my $mockILS = $mocks->{ils};
970 my $server = { ils => $mockILS, account => {} };
971 $mockILS->mock( 'institution', sub { $branchcode; } );
972 $mockILS->mock( 'supports', sub { return; } );
973 $mockILS->mock( 'checkout', sub {
975 return C4::SIP::ILS->checkout(@_);
977 my $today = dt_from_string;
978 t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
979 t::lib::Mocks::mock_preference( 'CheckPrevCheckout', 'hardyes' );
981 my $siprequest = CHECKOUT . 'YN' . siprequestdate($today) .
982 siprequestdate( $today->clone->add( days => 1) ) .
983 FID_INST_ID . $branchcode . '|'.
984 FID_PATRON_ID . $sip_patron1->id . '|' .
985 FID_ITEM_ID . $item_object->barcode . '|' .
986 FID_TERMINAL_PWD . 'ignored' . '|';
989 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
990 $server->{account}->{inhouse_patron_categories} = "A,$patron_category,Z";
991 $msg->handle_checkout( $server );
992 my $respcode = substr( $response, 5, 1 );
993 is( $respcode, 'N', "Desensitize flag was not set for patron category in inhouse_patron_categories" );
996 $server->{account}->{inhouse_patron_categories} = "A,B,C";
997 $msg->handle_checkout( $server );
998 $respcode = substr( $response, 5, 1 );
999 is( $respcode, 'Y', "Desensitize flag was set for patron category not in inhouse_patron_categories" );
1002 $server->{account}->{inhouse_patron_categories} = "";
1003 $msg->handle_checkout( $server );
1004 $respcode = substr( $response, 5, 1 );
1005 is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_patron_categories" );
1007 $server->{account}->{inhouse_patron_categories} = "";
1010 $server->{account}->{inhouse_item_types} = "A,$itemtype,Z";
1011 $msg->handle_checkout( $server );
1012 $respcode = substr( $response, 5, 1 );
1013 is( $respcode, 'N', "Desensitize flag was not set for itemtype in inhouse_item_types" );
1016 $server->{account}->{inhouse_item_types} = "A,B,C";
1017 $msg->handle_checkout( $server );
1018 $respcode = substr( $response, 5, 1 );
1019 is( $respcode, 'Y', "Desensitize flag was set for item type not in inhouse_item_types" );
1022 $server->{account}->{inhouse_item_types} = "";
1023 $msg->handle_checkout( $server );
1024 $respcode = substr( $response, 5, 1 );
1025 is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_item_types" );
1028 sub test_renew_desensitize {
1029 my $builder = t::lib::TestBuilder->new();
1030 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
1031 my ( $response, $findpatron );
1032 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
1035 my $patron1 = $builder->build({
1036 source => 'Borrower',
1038 password => hash_password( PATRON_PW ),
1041 my $card1 = $patron1->{cardnumber};
1042 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
1043 my $patron_category = $sip_patron1->ptype();
1044 $findpatron = $sip_patron1;
1045 my $item_object = $builder->build_sample_item({
1050 homebranch => $branchcode,
1051 holdingbranch => $branchcode,
1053 my $itemtype = $item_object->effective_itemtype;
1055 my $mockILS = $mocks->{ils};
1056 my $server = { ils => $mockILS, account => {} };
1057 $mockILS->mock( 'institution', sub { $branchcode; } );
1058 $mockILS->mock( 'supports', sub { return; } );
1059 $mockILS->mock( 'checkout', sub {
1061 return C4::SIP::ILS->checkout(@_);
1063 my $today = dt_from_string;
1064 t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
1066 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
1068 my $siprequest = RENEW . 'YN' . siprequestdate($today) .
1069 siprequestdate( $today->clone->add( days => 1) ) .
1070 FID_INST_ID . $branchcode . '|'.
1071 FID_PATRON_ID . $sip_patron1->id . '|' .
1072 FID_ITEM_ID . $item_object->barcode . '|' .
1073 FID_TERMINAL_PWD . 'ignored' . '|';
1076 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
1077 $server->{account}->{inhouse_patron_categories} = "A,$patron_category,Z";
1078 $msg->handle_checkout( $server );
1079 my $respcode = substr( $response, 5, 1 );
1080 is( $respcode, 'N', "Desensitize flag was not set for patron category in inhouse_patron_categories" );
1083 $server->{account}->{inhouse_patron_categories} = "A,B,C";
1084 $msg->handle_checkout( $server );
1085 $respcode = substr( $response, 5, 1 );
1086 is( $respcode, 'Y', "Desensitize flag was set for patron category not in inhouse_patron_categories" );
1089 $server->{account}->{inhouse_patron_categories} = "";
1090 $msg->handle_checkout( $server );
1091 $respcode = substr( $response, 5, 1 );
1092 is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_patron_categories" );
1094 $server->{account}->{inhouse_patron_categories} = "";
1097 $server->{account}->{inhouse_item_types} = "A,B,C";
1098 $msg->handle_checkout( $server );
1099 $respcode = substr( $response, 5, 1 );
1100 is( $respcode, 'Y', "Desensitize flag was set for item type not in inhouse_item_types" );
1103 $server->{account}->{inhouse_item_types} = "";
1104 $msg->handle_checkout( $server );
1105 $respcode = substr( $response, 5, 1 );
1106 is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_item_types" );
1109 $server->{account}->{inhouse_item_types} = "A,$itemtype,Z";
1110 $msg->handle_checkout( $server );
1111 $respcode = substr( $response, 5, 1 );
1112 is( $respcode, 'N', "Desensitize flag was not set for itemtype in inhouse_item_types" );
1119 my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
1121 # mock write_msg (imported from Sip.pm into Message.pm)
1122 my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
1123 $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
1126 my $mockILS = Test::MockObject->new;
1127 $mockILS->mock( 'check_inst_id', sub {} );
1128 $mockILS->mock( 'institution_id', sub { $$branchcode; } );
1129 $mockILS->mock( 'find_patron', sub { $$findpatron; } );
1131 return { ils => $mockILS, message => $mockMsg };
1135 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
1136 # mode: contains || equals || regex (by default: equals)
1138 # strip fixed part; prefix to simplify next regex
1139 $resp = '|'. substr( $resp, fixed_length( $code ) );
1141 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
1143 } elsif( !defined($expr) ) { # field should not be found
1146 } else { # test fails
1147 is( 0, 1, "Code $fld not found in '$resp'?" );
1151 if( !$mode || $mode eq 'equals' ) { # default
1152 is( $fldval, $expr, $msg );
1153 } elsif( $mode eq 'regex' ) {
1154 is( $fldval =~ /$expr/, 1, $msg );
1156 is( index( $fldval, $expr ) > -1, 1, $msg );
1160 sub siprequestdate {
1162 return $dt->ymd('').(' 'x4).$dt->hms('');
1165 sub fixed_length { #length of fixed fields including response code
1167 ( PATRON_STATUS_RESP ) => 37,
1168 ( PATRON_INFO_RESP ) => 61,
1169 ( CHECKIN_RESP ) => 24,
1170 ( CHECKOUT_RESP ) => 24,