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 => 8;
30 use t::lib::TestBuilder;
33 use Koha::AuthUtils qw(hash_password);
37 use Koha::Old::Checkouts;
41 use C4::SIP::ILS::Patron;
42 use C4::SIP::Sip qw(write_msg);
43 use C4::SIP::Sip::Constants qw(:all);
44 use C4::SIP::Sip::MsgType;
46 use constant PATRON_PW => 'do_not_ever_use_this_one';
49 subtest 'Testing Patron Status Request V2' => sub {
50 my $schema = Koha::Database->new->schema;
51 $schema->storage->txn_begin;
53 $C4::SIP::Sip::protocol_version = 2;
54 test_request_patron_status_v2();
55 $schema->storage->txn_rollback;
58 subtest 'Testing Patron Info Request V2' => sub {
59 my $schema = Koha::Database->new->schema;
60 $schema->storage->txn_begin;
62 $C4::SIP::Sip::protocol_version = 2;
63 test_request_patron_info_v2();
64 $schema->storage->txn_rollback;
67 subtest 'Checkin V2' => sub {
68 my $schema = Koha::Database->new->schema;
69 $schema->storage->txn_begin;
71 $C4::SIP::Sip::protocol_version = 2;
73 $schema->storage->txn_rollback;
76 subtest 'Test hold_patron_bcode' => sub {
77 my $schema = Koha::Database->new->schema;
78 $schema->storage->txn_begin;
80 $C4::SIP::Sip::protocol_version = 2;
81 test_hold_patron_bcode();
82 $schema->storage->txn_rollback;
85 subtest 'hold_patron_name() tests' => sub {
89 my $schema = Koha::Database->new->schema;
90 $schema->storage->txn_begin;
92 my $builder = t::lib::TestBuilder->new();
94 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
95 my ( $response, $findpatron );
96 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
98 my $item = $builder->build_sample_item(
104 homebranch => $branchcode,
105 holdingbranch => $branchcode
109 my $server = { ils => $mocks->{ils} };
110 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
112 is( $sip_item->hold_patron_name, q{}, "SIP item with no hold returns empty string for patron name" );
114 my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_name, $server );
115 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
117 $schema->storage->txn_rollback;
120 subtest 'Lastseen response' => sub {
122 my $schema = Koha::Database->new->schema;
123 $schema->storage->txn_begin;
126 my $builder = t::lib::TestBuilder->new();
127 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
128 my ( $response, $findpatron );
129 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
130 my $seen_patron = $builder->build({
131 source => 'Borrower',
133 lastseen => '2001-01-01',
134 password => hash_password( PATRON_PW ),
135 branchcode => $branchcode,
138 my $cardnum = $seen_patron->{cardnumber};
139 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
140 $findpatron = $sip_patron;
142 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
143 FID_INST_ID. $branchcode. '|'.
144 FID_PATRON_ID. $cardnum. '|'.
145 FID_PATRON_PWD. PATRON_PW. '|';
146 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
148 my $server = { ils => $mocks->{ils} };
150 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
151 $msg->handle_patron_info( $server );
153 isnt( $response, undef, 'At least we got a response.' );
154 my $respcode = substr( $response, 0, 2 );
155 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
156 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
157 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');
159 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
160 $msg->handle_patron_info( $server );
162 isnt( $response, undef, 'At least we got a response.' );
163 $respcode = substr( $response, 0, 2 );
164 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
165 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
166 is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
167 $schema->storage->txn_rollback;
171 subtest "Test build_additional_item_fields_string" => sub {
172 my $schema = Koha::Database->new->schema;
173 $schema->storage->txn_begin;
177 my $builder = t::lib::TestBuilder->new();
179 my $item = $builder->build( { source => 'Item' } );
180 my $ils_item = C4::SIP::ILS::Item->new( $item->{barcode} );
183 $server->{account}->{item_field}->{code} = 'itemnumber';
184 $server->{account}->{item_field}->{field} = 'XY';
185 my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
186 is( $attribute_string, "XY$item->{itemnumber}|", 'Attribute field generated correctly with single param' );
189 $server->{account}->{item_field}->[0]->{code} = 'itemnumber';
190 $server->{account}->{item_field}->[0]->{field} = 'XY';
191 $server->{account}->{item_field}->[1]->{code} = 'biblionumber';
192 $server->{account}->{item_field}->[1]->{field} = 'YZ';
193 $attribute_string = $ils_item->build_additional_item_fields_string( $server );
194 is( $attribute_string, "XY$item->{itemnumber}|YZ$item->{biblionumber}|", 'Attribute field generated correctly with multiple params' );
196 $schema->storage->txn_rollback;
199 subtest 'Patron info summary > 5 should not crash server' => sub {
201 my $schema = Koha::Database->new->schema;
202 $schema->storage->txn_begin;
205 my $builder = t::lib::TestBuilder->new();
206 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
207 my ( $response, $findpatron );
208 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
209 my $seen_patron = $builder->build({
210 source => 'Borrower',
212 lastseen => '2001-01-01',
213 password => hash_password( PATRON_PW ),
214 branchcode => $branchcode,
217 my $cardnum = $seen_patron->{cardnumber};
218 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
219 $findpatron = $sip_patron;
234 for my $summary ( @summaries ) {
235 my $siprequest = PATRON_INFO . 'engYYYYMMDDZZZZHHMMSS' . $summary .
236 FID_INST_ID . $branchcode . '|' .
237 FID_PATRON_ID . $cardnum . '|' .
238 FID_PATRON_PWD . PATRON_PW . '|';
239 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
241 my $server = { ils => $mocks->{ils} };
243 $msg->handle_patron_info( $server );
245 isnt( $response, undef, 'At least we got a response.' );
246 my $respcode = substr( $response, 0, 2 );
247 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
250 $schema->storage->txn_rollback;
253 # Here is room for some more subtests
257 sub test_request_patron_status_v2 {
258 my $builder = t::lib::TestBuilder->new();
259 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
260 my ( $response, $findpatron );
261 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
263 my $patron1 = $builder->build({
264 source => 'Borrower',
266 password => hash_password( PATRON_PW ),
269 my $card1 = $patron1->{cardnumber};
270 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
271 $findpatron = $sip_patron1;
273 my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
274 FID_INST_ID. $branchcode. '|'.
275 FID_PATRON_ID. $card1. '|'.
276 FID_PATRON_PWD. PATRON_PW. '|';
277 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
279 my $server = { ils => $mocks->{ils} };
281 $msg->handle_patron_status( $server );
283 isnt( $response, undef, 'At least we got a response.' );
284 my $respcode = substr( $response, 0, 2 );
285 is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
287 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
288 check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
289 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
290 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
291 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
292 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
294 # Now, we pass a wrong password and verify CQ again
295 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
296 FID_INST_ID. $branchcode. '|'.
297 FID_PATRON_ID. $card1. '|'.
298 FID_PATRON_PWD. 'wrong_password'. '|';
299 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
301 $msg->handle_patron_status( $server );
302 $respcode = substr( $response, 0, 2 );
303 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
305 # Check empty password and verify CQ again
306 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
307 FID_INST_ID. $branchcode. '|'.
308 FID_PATRON_ID. $card1. '|'.
310 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
312 $msg->handle_patron_status( $server );
313 $respcode = substr( $response, 0, 2 );
314 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
316 # Finally, we send a wrong card number and check AE, BL
317 # This is done by removing the new patron first
318 Koha::Patrons->search({ cardnumber => $card1 })->delete;
320 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
321 FID_INST_ID. $branchcode. '|'.
322 FID_PATRON_ID. $card1. '|'.
323 FID_PATRON_PWD. PATRON_PW. '|';
324 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
326 $msg->handle_patron_status( $server );
327 $respcode = substr( $response, 0, 2 );
328 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
329 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
330 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
333 sub test_request_patron_info_v2 {
334 my $builder = t::lib::TestBuilder->new();
335 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
336 my ( $response, $findpatron );
337 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
339 my $patron2 = $builder->build({
340 source => 'Borrower',
342 password => hash_password( PATRON_PW ),
345 my $card = $patron2->{cardnumber};
346 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
347 $findpatron = $sip_patron2;
348 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
349 FID_INST_ID. $branchcode. '|'.
350 FID_PATRON_ID. $card. '|'.
351 FID_PATRON_PWD. PATRON_PW. '|';
352 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
354 my $server = { ils => $mocks->{ils} };
356 $msg->handle_patron_info( $server );
357 isnt( $response, undef, 'At least we got a response.' );
358 my $respcode = substr( $response, 0, 2 );
359 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
361 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
362 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
363 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
364 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
365 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
366 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
367 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
368 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
369 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
370 # No check for custom fields here (unofficial PB, PC and PI)
371 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
373 # Test customized patron name in AE with same sip request
374 # This implicitly tests C4::SIP::ILS::Patron->name
375 $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
376 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
378 $msg->handle_patron_info( $server );
379 $respcode = substr( $response, 0, 2 );
380 check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
383 $server->{account}->{hide_fields} = "BD,BE,BF,PB";
384 $msg->handle_patron_info( $server );
385 $respcode = substr( $response, 0, 2 );
386 check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
387 check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
388 check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
389 check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
390 $server->{account}->{hide_fields} = "";
392 # Check empty password and verify CQ again
393 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
394 FID_INST_ID. $branchcode. '|'.
395 FID_PATRON_ID. $card. '|'.
397 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
399 $msg->handle_patron_info( $server );
400 $respcode = substr( $response, 0, 2 );
401 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
402 # Test empty password is OK if account configured to allow
403 $server->{account}->{allow_empty_passwords} = 1;
404 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
406 $msg->handle_patron_info( $server );
407 $respcode = substr( $response, 0, 2 );
408 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
410 t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
411 my $patron = Koha::Patrons->find({ cardnumber => $card });
412 $patron->update({ login_attempts => 0 });
413 is( $patron->account_locked, 0, "Patron account not locked already" );
414 $msg->handle_patron_info( $server );
415 $patron = Koha::Patrons->find({ cardnumber => $card });
416 is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
418 # Finally, we send a wrong card number
419 Koha::Patrons->search({ cardnumber => $card })->delete;
421 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
423 $msg->handle_patron_info( $server );
424 $respcode = substr( $response, 0, 2 );
425 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
426 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
427 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
430 sub test_checkin_v2 {
431 my $builder = t::lib::TestBuilder->new();
432 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
433 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
434 my ( $response, $findpatron );
435 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
438 my $patron1 = $builder->build({
439 source => 'Borrower',
441 password => hash_password( PATRON_PW ),
444 my $card1 = $patron1->{cardnumber};
445 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
446 $findpatron = $sip_patron1;
447 my $item_object = $builder->build_sample_item({
452 homebranch => $branchcode,
453 holdingbranch => $branchcode,
456 my $mockILS = $mocks->{ils};
457 my $server = { ils => $mockILS, account => {} };
458 $mockILS->mock( 'institution', sub { $branchcode; } );
459 $mockILS->mock( 'supports', sub { return; } );
460 $mockILS->mock( 'checkin', sub {
462 return C4::SIP::ILS->checkin(@_);
464 my $today = dt_from_string;
466 # Checkin invalid barcode
467 Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
468 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
469 siprequestdate( $today->clone->add( days => 1) ) .
470 FID_INST_ID . $branchcode . '|'.
471 FID_ITEM_ID . 'not_to_be_found' . '|' .
472 FID_TERMINAL_PWD . 'ignored' . '|';
474 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
475 warnings_like { $msg->handle_checkin( $server ); }
476 [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
477 'Checkin of invalid item with two warnings';
478 my $respcode = substr( $response, 0, 2 );
479 is( $respcode, CHECKIN_RESP, 'Response code fine' );
480 is( substr($response,2,1), '0', 'OK flag is false' );
481 is( substr($response,5,1), 'Y', 'Alert flag is set' );
482 check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
484 # Not checked out, toggle option checked_in_ok
485 $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
486 siprequestdate( $today->clone->add( days => 1) ) .
487 FID_INST_ID . $branchcode . '|'.
488 FID_ITEM_ID . $item_object->barcode . '|' .
489 FID_TERMINAL_PWD . 'ignored' . '|';
491 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
492 $msg->handle_checkin( $server );
493 $respcode = substr( $response, 0, 2 );
494 is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
495 is( substr($response,5,1), 'Y', 'Alert flag is set' );
496 check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
498 $server->{account}->{checked_in_ok} = 1;
500 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
501 $msg->handle_checkin( $server );
502 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' );
503 is( substr($response,5,1), 'N', 'Alert flag no longer set' );
504 check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
506 # Move item to another holding branch to trigger CV of 04 with alert flag
507 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
508 $item_object->holdingbranch( $branchcode2 )->store();
510 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
511 $msg->handle_checkin( $server );
512 is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
513 check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
514 $item_object->holdingbranch( $branchcode )->store();
515 t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
517 $server->{account}->{cv_send_00_on_success} = 0;
519 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
520 $msg->handle_checkin( $server );
521 $respcode = substr( $response, 0, 2 );
522 check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
523 $server->{account}->{cv_send_00_on_success} = 1;
525 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
526 $msg->handle_checkin( $server );
527 $respcode = substr( $response, 0, 2 );
528 check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
529 $server->{account}->{checked_in_ok} = 0;
530 $server->{account}->{cv_send_00_on_success} = 0;
532 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
533 $server->{account}->{checked_in_ok} = 0;
534 $server->{account}->{cv_triggers_alert} = 0;
536 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
537 $msg->handle_checkin( $server );
538 $respcode = substr( $response, 0, 2 );
539 is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
540 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
541 $server->{account}->{cv_triggers_alert} = 1;
543 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
544 $msg->handle_checkin( $server );
545 $respcode = substr( $response, 0, 2 );
546 is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
547 $server->{account}->{cv_triggers_alert} = 0;
548 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
550 $server->{account}->{checked_in_ok} = 1;
551 $server->{account}->{ct_always_send} = 0;
553 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
554 $msg->handle_checkin( $server );
555 $respcode = substr( $response, 0, 2 );
556 check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
557 $server->{account}->{ct_always_send} = 1;
559 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
560 $msg->handle_checkin( $server );
561 $respcode = substr( $response, 0, 2 );
562 check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
563 $server->{account}->{checked_in_ok} = 0;
564 $server->{account}->{ct_always_send} = 0;
566 # Checkin at wrong branch: issue item and switch branch, and checkin
567 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
568 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
569 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
571 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
572 $msg->handle_checkin( $server );
573 is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
574 is( substr($response,5,1), 'Y', 'Alert flag is set' );
575 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
576 $branchcode = $item_object->homebranch; # switch back
577 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
579 # Data corrupted: add same issue_id to old_issues
580 Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
582 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
583 warnings_like { $msg->handle_checkin( $server ); }
584 [ qr/Duplicate entry/, qr/data corrupted/ ],
585 'DBIx error on duplicate issue_id';
586 is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
587 is( substr($response,5,1), 'Y', 'Alert flag is set' );
588 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
590 # Finally checkin without problems (remove duplicate id)
591 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
593 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
594 $msg->handle_checkin( $server );
595 is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
596 is( substr($response,5,1), 'N', 'Alert flag is not set' );
597 is( Koha::Checkouts->find( $issue->issue_id ), undef,
598 'Issue record is gone now' );
601 sub test_hold_patron_bcode {
602 my $builder = t::lib::TestBuilder->new();
603 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
604 my ( $response, $findpatron );
605 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
607 my $item = $builder->build({
609 value => { damaged => 0, withdrawn => 0, itemlost => 0, restricted => 0, homebranch => $branchcode, holdingbranch => $branchcode },
611 my $item_object = Koha::Items->find( $item->{itemnumber} );
613 my $server = { ils => $mocks->{ils} };
614 my $sip_item = C4::SIP::ILS::Item->new( $item->{barcode} );
616 is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
618 my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
619 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
625 my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
627 # mock write_msg (imported from Sip.pm into Message.pm)
628 my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
629 $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
632 my $mockILS = Test::MockObject->new;
633 $mockILS->mock( 'check_inst_id', sub {} );
634 $mockILS->mock( 'institution_id', sub { $$branchcode; } );
635 $mockILS->mock( 'find_patron', sub { $$findpatron; } );
637 return { ils => $mockILS, message => $mockMsg };
641 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
642 # mode: contains || equals || regex (by default: equals)
644 # strip fixed part; prefix to simplify next regex
645 $resp = '|'. substr( $resp, fixed_length( $code ) );
647 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
649 } elsif( !defined($expr) ) { # field should not be found
652 } else { # test fails
653 is( 0, 1, "Code $fld not found in '$resp'?" );
657 if( !$mode || $mode eq 'equals' ) { # default
658 is( $fldval, $expr, $msg );
659 } elsif( $mode eq 'regex' ) {
660 is( $fldval =~ /$expr/, 1, $msg );
662 is( index( $fldval, $expr ) > -1, 1, $msg );
668 return $dt->ymd('').(' 'x4).$dt->hms('');
671 sub fixed_length { #length of fixed fields including response code
673 ( PATRON_STATUS_RESP ) => 37,
674 ( PATRON_INFO_RESP ) => 61,
675 ( CHECKIN_RESP ) => 24,