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 => 6;
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;
170 # Here is room for some more subtests
174 sub test_request_patron_status_v2 {
175 my $builder = t::lib::TestBuilder->new();
176 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
177 my ( $response, $findpatron );
178 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
180 my $patron1 = $builder->build({
181 source => 'Borrower',
183 password => hash_password( PATRON_PW ),
186 my $card1 = $patron1->{cardnumber};
187 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
188 $findpatron = $sip_patron1;
190 my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
191 FID_INST_ID. $branchcode. '|'.
192 FID_PATRON_ID. $card1. '|'.
193 FID_PATRON_PWD. PATRON_PW. '|';
194 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
196 my $server = { ils => $mocks->{ils} };
198 $msg->handle_patron_status( $server );
200 isnt( $response, undef, 'At least we got a response.' );
201 my $respcode = substr( $response, 0, 2 );
202 is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
204 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
205 check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
206 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
207 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
208 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
209 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
211 # Now, we pass a wrong password and verify CQ again
212 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
213 FID_INST_ID. $branchcode. '|'.
214 FID_PATRON_ID. $card1. '|'.
215 FID_PATRON_PWD. 'wrong_password'. '|';
216 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
218 $msg->handle_patron_status( $server );
219 $respcode = substr( $response, 0, 2 );
220 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
222 # Check empty password and verify CQ again
223 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
224 FID_INST_ID. $branchcode. '|'.
225 FID_PATRON_ID. $card1. '|'.
227 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
229 $msg->handle_patron_status( $server );
230 $respcode = substr( $response, 0, 2 );
231 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
233 # Finally, we send a wrong card number and check AE, BL
234 # This is done by removing the new patron first
235 Koha::Patrons->search({ cardnumber => $card1 })->delete;
237 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
238 FID_INST_ID. $branchcode. '|'.
239 FID_PATRON_ID. $card1. '|'.
240 FID_PATRON_PWD. PATRON_PW. '|';
241 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
243 $msg->handle_patron_status( $server );
244 $respcode = substr( $response, 0, 2 );
245 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
246 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
247 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
250 sub test_request_patron_info_v2 {
251 my $builder = t::lib::TestBuilder->new();
252 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
253 my ( $response, $findpatron );
254 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
256 my $patron2 = $builder->build({
257 source => 'Borrower',
259 password => hash_password( PATRON_PW ),
262 my $card = $patron2->{cardnumber};
263 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
264 $findpatron = $sip_patron2;
265 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
266 FID_INST_ID. $branchcode. '|'.
267 FID_PATRON_ID. $card. '|'.
268 FID_PATRON_PWD. PATRON_PW. '|';
269 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
271 my $server = { ils => $mocks->{ils} };
273 $msg->handle_patron_info( $server );
274 isnt( $response, undef, 'At least we got a response.' );
275 my $respcode = substr( $response, 0, 2 );
276 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
278 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
279 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
280 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
281 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
282 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
283 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
284 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
285 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
286 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
287 # No check for custom fields here (unofficial PB, PC and PI)
288 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
290 # Test customized patron name in AE with same sip request
291 # This implicitly tests C4::SIP::ILS::Patron->name
292 $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
293 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
295 $msg->handle_patron_info( $server );
296 $respcode = substr( $response, 0, 2 );
297 check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
300 $server->{account}->{hide_fields} = "BD,BE,BF,PB";
301 $msg->handle_patron_info( $server );
302 $respcode = substr( $response, 0, 2 );
303 check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
304 check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
305 check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
306 check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
307 $server->{account}->{hide_fields} = "";
309 # Check empty password and verify CQ again
310 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
311 FID_INST_ID. $branchcode. '|'.
312 FID_PATRON_ID. $card. '|'.
314 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
316 $msg->handle_patron_info( $server );
317 $respcode = substr( $response, 0, 2 );
318 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
319 # Test empty password is OK if account configured to allow
320 $server->{account}->{allow_empty_passwords} = 1;
321 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
323 $msg->handle_patron_info( $server );
324 $respcode = substr( $response, 0, 2 );
325 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
327 t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
328 my $patron = Koha::Patrons->find({ cardnumber => $card });
329 $patron->update({ login_attempts => 0 });
330 is( $patron->account_locked, 0, "Patron account not locked already" );
331 $msg->handle_patron_info( $server );
332 $patron = Koha::Patrons->find({ cardnumber => $card });
333 is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
335 # Finally, we send a wrong card number
336 Koha::Patrons->search({ cardnumber => $card })->delete;
338 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
340 $msg->handle_patron_info( $server );
341 $respcode = substr( $response, 0, 2 );
342 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
343 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
344 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
347 sub test_checkin_v2 {
348 my $builder = t::lib::TestBuilder->new();
349 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
350 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
351 my ( $response, $findpatron );
352 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
355 my $patron1 = $builder->build({
356 source => 'Borrower',
358 password => hash_password( PATRON_PW ),
361 my $card1 = $patron1->{cardnumber};
362 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
363 $findpatron = $sip_patron1;
364 my $item_object = $builder->build_sample_item({
369 homebranch => $branchcode,
370 holdingbranch => $branchcode,
373 my $mockILS = $mocks->{ils};
374 my $server = { ils => $mockILS, account => {} };
375 $mockILS->mock( 'institution', sub { $branchcode; } );
376 $mockILS->mock( 'supports', sub { return; } );
377 $mockILS->mock( 'checkin', sub {
379 return C4::SIP::ILS->checkin(@_);
381 my $today = dt_from_string;
383 # Checkin invalid barcode
384 Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
385 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
386 siprequestdate( $today->clone->add( days => 1) ) .
387 FID_INST_ID . $branchcode . '|'.
388 FID_ITEM_ID . 'not_to_be_found' . '|' .
389 FID_TERMINAL_PWD . 'ignored' . '|';
391 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
392 warnings_like { $msg->handle_checkin( $server ); }
393 [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
394 'Checkin of invalid item with two warnings';
395 my $respcode = substr( $response, 0, 2 );
396 is( $respcode, CHECKIN_RESP, 'Response code fine' );
397 is( substr($response,2,1), '0', 'OK flag is false' );
398 is( substr($response,5,1), 'Y', 'Alert flag is set' );
399 check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
401 # Not checked out, toggle option checked_in_ok
402 $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
403 siprequestdate( $today->clone->add( days => 1) ) .
404 FID_INST_ID . $branchcode . '|'.
405 FID_ITEM_ID . $item_object->barcode . '|' .
406 FID_TERMINAL_PWD . 'ignored' . '|';
408 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
409 $msg->handle_checkin( $server );
410 $respcode = substr( $response, 0, 2 );
411 is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
412 is( substr($response,5,1), 'Y', 'Alert flag is set' );
413 check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
415 $server->{account}->{checked_in_ok} = 1;
417 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
418 $msg->handle_checkin( $server );
419 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' );
420 is( substr($response,5,1), 'N', 'Alert flag no longer set' );
421 check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
423 # Move item to another holding branch to trigger CV of 04 with alert flag
424 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
425 $item_object->holdingbranch( $branchcode2 )->store();
427 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
428 $msg->handle_checkin( $server );
429 is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
430 check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
431 $item_object->holdingbranch( $branchcode )->store();
432 t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
434 $server->{account}->{cv_send_00_on_success} = 0;
436 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
437 $msg->handle_checkin( $server );
438 $respcode = substr( $response, 0, 2 );
439 check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
440 $server->{account}->{cv_send_00_on_success} = 1;
442 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
443 $msg->handle_checkin( $server );
444 $respcode = substr( $response, 0, 2 );
445 check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
446 $server->{account}->{checked_in_ok} = 0;
447 $server->{account}->{cv_send_00_on_success} = 0;
449 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
450 $server->{account}->{checked_in_ok} = 0;
451 $server->{account}->{cv_triggers_alert} = 0;
453 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
454 $msg->handle_checkin( $server );
455 $respcode = substr( $response, 0, 2 );
456 is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
457 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
458 $server->{account}->{cv_triggers_alert} = 1;
460 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
461 $msg->handle_checkin( $server );
462 $respcode = substr( $response, 0, 2 );
463 is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
464 $server->{account}->{cv_triggers_alert} = 0;
465 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
467 $server->{account}->{checked_in_ok} = 1;
468 $server->{account}->{ct_always_send} = 0;
470 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
471 $msg->handle_checkin( $server );
472 $respcode = substr( $response, 0, 2 );
473 check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
474 $server->{account}->{ct_always_send} = 1;
476 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
477 $msg->handle_checkin( $server );
478 $respcode = substr( $response, 0, 2 );
479 check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
480 $server->{account}->{checked_in_ok} = 0;
481 $server->{account}->{ct_always_send} = 0;
483 # Checkin at wrong branch: issue item and switch branch, and checkin
484 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
485 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
486 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
488 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
489 $msg->handle_checkin( $server );
490 is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
491 is( substr($response,5,1), 'Y', 'Alert flag is set' );
492 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
493 $branchcode = $item_object->homebranch; # switch back
494 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
496 # Data corrupted: add same issue_id to old_issues
497 Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
499 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
500 warnings_like { $msg->handle_checkin( $server ); }
501 [ qr/Duplicate entry/, qr/data corrupted/ ],
502 'DBIx error on duplicate issue_id';
503 is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
504 is( substr($response,5,1), 'Y', 'Alert flag is set' );
505 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
507 # Finally checkin without problems (remove duplicate id)
508 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
510 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
511 $msg->handle_checkin( $server );
512 is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
513 is( substr($response,5,1), 'N', 'Alert flag is not set' );
514 is( Koha::Checkouts->find( $issue->issue_id ), undef,
515 'Issue record is gone now' );
518 sub test_hold_patron_bcode {
519 my $builder = t::lib::TestBuilder->new();
520 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
521 my ( $response, $findpatron );
522 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
524 my $item = $builder->build({
526 value => { damaged => 0, withdrawn => 0, itemlost => 0, restricted => 0, homebranch => $branchcode, holdingbranch => $branchcode },
528 my $item_object = Koha::Items->find( $item->{itemnumber} );
530 my $server = { ils => $mocks->{ils} };
531 my $sip_item = C4::SIP::ILS::Item->new( $item->{barcode} );
533 is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
535 my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
536 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
542 my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
544 # mock write_msg (imported from Sip.pm into Message.pm)
545 my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
546 $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
549 my $mockILS = Test::MockObject->new;
550 $mockILS->mock( 'check_inst_id', sub {} );
551 $mockILS->mock( 'institution_id', sub { $$branchcode; } );
552 $mockILS->mock( 'find_patron', sub { $$findpatron; } );
554 return { ils => $mockILS, message => $mockMsg };
558 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
559 # mode: contains || equals || regex (by default: equals)
561 # strip fixed part; prefix to simplify next regex
562 $resp = '|'. substr( $resp, fixed_length( $code ) );
564 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
566 } elsif( !defined($expr) ) { # field should not be found
569 } else { # test fails
570 is( 0, 1, "Code $fld not found in '$resp'?" );
574 if( !$mode || $mode eq 'equals' ) { # default
575 is( $fldval, $expr, $msg );
576 } elsif( $mode eq 'regex' ) {
577 is( $fldval =~ /$expr/, 1, $msg );
579 is( index( $fldval, $expr ) > -1, 1, $msg );
585 return $dt->ymd('').(' 'x4).$dt->hms('');
588 sub fixed_length { #length of fixed fields including response code
590 ( PATRON_STATUS_RESP ) => 37,
591 ( PATRON_INFO_RESP ) => 61,
592 ( CHECKIN_RESP ) => 24,