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 => 9;
30 use t::lib::TestBuilder;
32 use C4::Reserves qw(AddReserve);
34 use Koha::AuthUtils qw(hash_password);
38 use Koha::Old::Checkouts;
43 use C4::SIP::ILS::Patron;
44 use C4::SIP::Sip qw(write_msg);
45 use C4::SIP::Sip::Constants qw(:all);
46 use C4::SIP::Sip::MsgType;
48 use constant PATRON_PW => 'do_not_ever_use_this_one';
51 subtest 'Testing Patron Status Request V2' => sub {
52 my $schema = Koha::Database->new->schema;
53 $schema->storage->txn_begin;
55 $C4::SIP::Sip::protocol_version = 2;
56 test_request_patron_status_v2();
57 $schema->storage->txn_rollback;
60 subtest 'Testing Patron Info Request V2' => sub {
61 my $schema = Koha::Database->new->schema;
62 $schema->storage->txn_begin;
64 $C4::SIP::Sip::protocol_version = 2;
65 test_request_patron_info_v2();
66 $schema->storage->txn_rollback;
69 subtest 'Checkin V2' => sub {
70 my $schema = Koha::Database->new->schema;
71 $schema->storage->txn_begin;
73 $C4::SIP::Sip::protocol_version = 2;
75 $schema->storage->txn_rollback;
78 subtest 'Test hold_patron_bcode' => sub {
79 my $schema = Koha::Database->new->schema;
80 $schema->storage->txn_begin;
82 $C4::SIP::Sip::protocol_version = 2;
83 test_hold_patron_bcode();
84 $schema->storage->txn_rollback;
87 subtest 'hold_patron_name() tests' => sub {
91 my $schema = Koha::Database->new->schema;
92 $schema->storage->txn_begin;
94 my $builder = t::lib::TestBuilder->new();
96 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
97 my ( $response, $findpatron );
98 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
100 my $item = $builder->build_sample_item(
106 homebranch => $branchcode,
107 holdingbranch => $branchcode
111 my $server = { ils => $mocks->{ils} };
112 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
114 is( $sip_item->hold_patron_name, q{}, "SIP item with no hold returns empty string for patron name" );
116 my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_name, $server );
117 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
119 $schema->storage->txn_rollback;
122 subtest 'Lastseen response' => sub {
124 my $schema = Koha::Database->new->schema;
125 $schema->storage->txn_begin;
128 my $builder = t::lib::TestBuilder->new();
129 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
130 my ( $response, $findpatron );
131 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
132 my $seen_patron = $builder->build({
133 source => 'Borrower',
135 lastseen => '2001-01-01',
136 password => hash_password( PATRON_PW ),
137 branchcode => $branchcode,
140 my $cardnum = $seen_patron->{cardnumber};
141 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
142 $findpatron = $sip_patron;
144 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
145 FID_INST_ID. $branchcode. '|'.
146 FID_PATRON_ID. $cardnum. '|'.
147 FID_PATRON_PWD. PATRON_PW. '|';
148 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
150 my $server = { ils => $mocks->{ils} };
152 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
153 $msg->handle_patron_info( $server );
155 isnt( $response, undef, 'At least we got a response.' );
156 my $respcode = substr( $response, 0, 2 );
157 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
158 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
159 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');
161 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
162 $msg->handle_patron_info( $server );
164 isnt( $response, undef, 'At least we got a response.' );
165 $respcode = substr( $response, 0, 2 );
166 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
167 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
168 is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
169 $schema->storage->txn_rollback;
173 subtest "Test build_additional_item_fields_string" => sub {
174 my $schema = Koha::Database->new->schema;
175 $schema->storage->txn_begin;
179 my $builder = t::lib::TestBuilder->new();
181 my $item = $builder->build_sample_item;
182 my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
185 $server->{account}->{item_field}->{code} = 'itemnumber';
186 $server->{account}->{item_field}->{field} = 'XY';
187 my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
188 is( $attribute_string, "XY".$item->itemnumber."|", 'Attribute field generated correctly with single param' );
191 $server->{account}->{item_field}->[0]->{code} = 'itemnumber';
192 $server->{account}->{item_field}->[0]->{field} = 'XY';
193 $server->{account}->{item_field}->[1]->{code} = 'biblionumber';
194 $server->{account}->{item_field}->[1]->{field} = 'YZ';
195 $attribute_string = $ils_item->build_additional_item_fields_string( $server );
196 is( $attribute_string, sprintf("XY%s|YZ%s|", $item->itemnumber, $item->biblionumber), 'Attribute field generated correctly with multiple params' );
198 $schema->storage->txn_rollback;
201 subtest "Test cr_item_field" => sub {
204 my $builder = t::lib::TestBuilder->new();
205 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
206 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
207 my ( $response, $findpatron );
208 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
211 my $patron1 = $builder->build({
212 source => 'Borrower',
214 password => hash_password( PATRON_PW ),
217 my $card1 = $patron1->{cardnumber};
218 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
219 $findpatron = $sip_patron1;
220 my $item_object = $builder->build_sample_item({
225 homebranch => $branchcode,
226 holdingbranch => $branchcode,
229 my $mockILS = $mocks->{ils};
230 my $server = { ils => $mockILS, account => {} };
231 $mockILS->mock( 'institution', sub { $branchcode; } );
232 $mockILS->mock( 'supports', sub { return; } );
233 $mockILS->mock( 'checkin', sub {
235 return C4::SIP::ILS->checkin(@_);
237 my $today = dt_from_string;
241 # Not checked out, toggle option checked_in_ok
242 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
243 siprequestdate( $today->clone->add( days => 1) ) .
244 FID_INST_ID . $branchcode . '|'.
245 FID_ITEM_ID . $item_object->barcode . '|' .
246 FID_TERMINAL_PWD . 'ignored' . '|';
248 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
250 $server->{account}->{cr_item_field} = 'itemnumber';
252 $msg->handle_checkin( $server );
254 my $id = $item_object->id;
255 ok( $response =~ m/CR$id/, "Found correct CR field in response");
257 $siprequest = ITEM_INFORMATION . 'YYYYMMDDZZZZHHMMSS' .
258 FID_INST_ID . $branchcode . '|'.
259 FID_ITEM_ID . $item_object->barcode . '|' .
260 FID_TERMINAL_PWD . 'ignored' . '|';
262 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
264 $mockILS->mock( 'find_item', sub {
265 return C4::SIP::ILS::Item->new( $item_object->barcode );
268 $server->{account}->{cr_item_field} = 'itype';
270 $msg->handle_item_information( $server );
272 my $itype = $item_object->itype;
273 ok( $response =~ m/CR$itype/, "Found correct CR field in response");
276 subtest 'Patron info summary > 5 should not crash server' => sub {
278 my $schema = Koha::Database->new->schema;
279 $schema->storage->txn_begin;
282 my $builder = t::lib::TestBuilder->new();
283 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
284 my ( $response, $findpatron );
285 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
286 my $seen_patron = $builder->build({
287 source => 'Borrower',
289 lastseen => '2001-01-01',
290 password => hash_password( PATRON_PW ),
291 branchcode => $branchcode,
294 my $cardnum = $seen_patron->{cardnumber};
295 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
296 $findpatron = $sip_patron;
311 for my $summary ( @summaries ) {
312 my $siprequest = PATRON_INFO . 'engYYYYMMDDZZZZHHMMSS' . $summary .
313 FID_INST_ID . $branchcode . '|' .
314 FID_PATRON_ID . $cardnum . '|' .
315 FID_PATRON_PWD . PATRON_PW . '|';
316 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
318 my $server = { ils => $mocks->{ils} };
320 $msg->handle_patron_info( $server );
322 isnt( $response, undef, 'At least we got a response.' );
323 my $respcode = substr( $response, 0, 2 );
324 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
327 $schema->storage->txn_rollback;
330 # Here is room for some more subtests
334 sub test_request_patron_status_v2 {
335 my $builder = t::lib::TestBuilder->new();
336 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
337 my ( $response, $findpatron );
338 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
340 my $patron1 = $builder->build({
341 source => 'Borrower',
343 password => hash_password( PATRON_PW ),
346 my $card1 = $patron1->{cardnumber};
347 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
348 $findpatron = $sip_patron1;
350 my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
351 FID_INST_ID. $branchcode. '|'.
352 FID_PATRON_ID. $card1. '|'.
353 FID_PATRON_PWD. PATRON_PW. '|';
354 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
356 my $server = { ils => $mocks->{ils} };
358 $msg->handle_patron_status( $server );
360 isnt( $response, undef, 'At least we got a response.' );
361 my $respcode = substr( $response, 0, 2 );
362 is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
364 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
365 check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
366 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
367 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
368 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
369 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
371 # Now, we pass a wrong password and verify CQ again
372 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
373 FID_INST_ID. $branchcode. '|'.
374 FID_PATRON_ID. $card1. '|'.
375 FID_PATRON_PWD. 'wrong_password'. '|';
376 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
378 $msg->handle_patron_status( $server );
379 $respcode = substr( $response, 0, 2 );
380 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
382 # Check empty password and verify CQ again
383 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
384 FID_INST_ID. $branchcode. '|'.
385 FID_PATRON_ID. $card1. '|'.
387 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
389 $msg->handle_patron_status( $server );
390 $respcode = substr( $response, 0, 2 );
391 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
393 # Finally, we send a wrong card number and check AE, BL
394 # This is done by removing the new patron first
395 Koha::Patrons->search({ cardnumber => $card1 })->delete;
397 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
398 FID_INST_ID. $branchcode. '|'.
399 FID_PATRON_ID. $card1. '|'.
400 FID_PATRON_PWD. PATRON_PW. '|';
401 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
403 $msg->handle_patron_status( $server );
404 $respcode = substr( $response, 0, 2 );
405 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
406 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
407 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
410 sub test_request_patron_info_v2 {
411 my $builder = t::lib::TestBuilder->new();
412 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
413 my ( $response, $findpatron );
414 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
416 my $patron2 = $builder->build({
417 source => 'Borrower',
419 password => hash_password( PATRON_PW ),
422 my $card = $patron2->{cardnumber};
423 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
424 $findpatron = $sip_patron2;
425 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
426 FID_INST_ID. $branchcode. '|'.
427 FID_PATRON_ID. $card. '|'.
428 FID_PATRON_PWD. PATRON_PW. '|';
429 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
431 my $server = { ils => $mocks->{ils} };
433 $msg->handle_patron_info( $server );
434 isnt( $response, undef, 'At least we got a response.' );
435 my $respcode = substr( $response, 0, 2 );
436 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
438 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
439 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
440 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
441 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
442 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
443 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
444 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
445 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
446 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
447 # No check for custom fields here (unofficial PB, PC and PI)
448 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
450 # Test customized patron name in AE with same sip request
451 # This implicitly tests C4::SIP::ILS::Patron->name
452 $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
453 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
455 $msg->handle_patron_info( $server );
456 $respcode = substr( $response, 0, 2 );
457 check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
460 $server->{account}->{hide_fields} = "BD,BE,BF,PB";
461 $msg->handle_patron_info( $server );
462 $respcode = substr( $response, 0, 2 );
463 check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
464 check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
465 check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
466 check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
467 $server->{account}->{hide_fields} = "";
469 # Check empty password and verify CQ again
470 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
471 FID_INST_ID. $branchcode. '|'.
472 FID_PATRON_ID. $card. '|'.
474 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
476 $msg->handle_patron_info( $server );
477 $respcode = substr( $response, 0, 2 );
478 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
479 # Test empty password is OK if account configured to allow
480 $server->{account}->{allow_empty_passwords} = 1;
481 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
483 $msg->handle_patron_info( $server );
484 $respcode = substr( $response, 0, 2 );
485 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
487 t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
488 my $patron = Koha::Patrons->find({ cardnumber => $card });
489 $patron->update({ login_attempts => 0 });
490 is( $patron->account_locked, 0, "Patron account not locked already" );
491 $msg->handle_patron_info( $server );
492 $patron = Koha::Patrons->find({ cardnumber => $card });
493 is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
495 # Finally, we send a wrong card number
496 Koha::Patrons->search({ cardnumber => $card })->delete;
498 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
500 $msg->handle_patron_info( $server );
501 $respcode = substr( $response, 0, 2 );
502 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
503 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
504 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
507 sub test_checkin_v2 {
508 my $builder = t::lib::TestBuilder->new();
509 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
510 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
511 my ( $response, $findpatron );
512 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
515 my $patron1 = $builder->build({
516 source => 'Borrower',
518 password => hash_password( PATRON_PW ),
521 my $card1 = $patron1->{cardnumber};
522 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
523 $findpatron = $sip_patron1;
524 my $item_object = $builder->build_sample_item({
529 homebranch => $branchcode,
530 holdingbranch => $branchcode,
533 my $mockILS = $mocks->{ils};
534 my $server = { ils => $mockILS, account => {} };
535 $mockILS->mock( 'institution', sub { $branchcode; } );
536 $mockILS->mock( 'supports', sub { return; } );
537 $mockILS->mock( 'checkin', sub {
539 return C4::SIP::ILS->checkin(@_);
541 my $today = dt_from_string;
543 # Checkin invalid barcode
544 Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
545 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
546 siprequestdate( $today->clone->add( days => 1) ) .
547 FID_INST_ID . $branchcode . '|'.
548 FID_ITEM_ID . 'not_to_be_found' . '|' .
549 FID_TERMINAL_PWD . 'ignored' . '|';
551 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
552 warnings_like { $msg->handle_checkin( $server ); }
553 [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
554 'Checkin of invalid item with two warnings';
555 my $respcode = substr( $response, 0, 2 );
556 is( $respcode, CHECKIN_RESP, 'Response code fine' );
557 is( substr($response,2,1), '0', 'OK flag is false' );
558 is( substr($response,5,1), 'Y', 'Alert flag is set' );
559 check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
561 # Not checked out, toggle option checked_in_ok
562 $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
563 siprequestdate( $today->clone->add( days => 1) ) .
564 FID_INST_ID . $branchcode . '|'.
565 FID_ITEM_ID . $item_object->barcode . '|' .
566 FID_TERMINAL_PWD . 'ignored' . '|';
568 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
569 $msg->handle_checkin( $server );
570 $respcode = substr( $response, 0, 2 );
571 is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
572 is( substr($response,5,1), 'Y', 'Alert flag is set' );
573 check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
575 $server->{account}->{checked_in_ok} = 1;
577 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
578 $msg->handle_checkin( $server );
579 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' );
580 is( substr($response,5,1), 'N', 'Alert flag no longer set' );
581 check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
583 # Move item to another holding branch to trigger CV of 04 with alert flag
584 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
585 $item_object->holdingbranch( $branchcode2 )->store();
587 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
588 $msg->handle_checkin( $server );
589 is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
590 check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
591 $item_object->holdingbranch( $branchcode )->store();
592 t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
594 $server->{account}->{cv_send_00_on_success} = 0;
596 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
597 $msg->handle_checkin( $server );
598 $respcode = substr( $response, 0, 2 );
599 check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
600 $server->{account}->{cv_send_00_on_success} = 1;
602 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
603 $msg->handle_checkin( $server );
604 $respcode = substr( $response, 0, 2 );
605 check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
606 $server->{account}->{checked_in_ok} = 0;
607 $server->{account}->{cv_send_00_on_success} = 0;
609 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
610 $server->{account}->{checked_in_ok} = 0;
611 $server->{account}->{cv_triggers_alert} = 0;
613 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
614 $msg->handle_checkin( $server );
615 $respcode = substr( $response, 0, 2 );
616 is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
617 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
618 $server->{account}->{cv_triggers_alert} = 1;
620 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
621 $msg->handle_checkin( $server );
622 $respcode = substr( $response, 0, 2 );
623 is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
624 $server->{account}->{cv_triggers_alert} = 0;
625 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
627 $server->{account}->{checked_in_ok} = 1;
628 $server->{account}->{ct_always_send} = 0;
630 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
631 $msg->handle_checkin( $server );
632 $respcode = substr( $response, 0, 2 );
633 check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
634 $server->{account}->{ct_always_send} = 1;
636 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
637 $msg->handle_checkin( $server );
638 $respcode = substr( $response, 0, 2 );
639 check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
640 $server->{account}->{checked_in_ok} = 0;
641 $server->{account}->{ct_always_send} = 0;
643 # Checkin at wrong branch: issue item and switch branch, and checkin
644 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
645 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
646 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
648 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
649 $msg->handle_checkin( $server );
650 is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
651 is( substr($response,5,1), 'Y', 'Alert flag is set' );
652 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
653 $branchcode = $item_object->homebranch; # switch back
654 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
656 # Data corrupted: add same issue_id to old_issues
657 Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
659 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
660 warnings_like { $msg->handle_checkin( $server ); }
661 [ qr/Duplicate entry/, qr/data issues/ ],
662 'DBIx error on duplicate issue_id';
663 is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
664 is( substr($response,5,1), 'Y', 'Alert flag is set' );
665 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
667 # Finally checkin without problems (remove duplicate id)
668 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
670 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
671 $msg->handle_checkin( $server );
672 is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
673 is( substr($response,5,1), 'N', 'Alert flag is not set' );
674 is( Koha::Checkouts->find( $issue->issue_id ), undef,
675 'Issue record is gone now' );
677 # Test account option no_holds_check that prevents items on hold from being checked in via SIP
678 $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
679 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item is checked out");
680 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
681 $server->{account}->{holds_block_checkin} = 1;
682 my $reserve_id = AddReserve({
683 branchcode => $branchcode,
684 borrowernumber => $patron1->{borrowernumber},
685 biblionumber => $item_object->biblionumber,
688 my $hold = Koha::Holds->find( $reserve_id );
689 is( $hold->id, $reserve_id, "Hold was created successfully" );
691 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
692 $msg->handle_checkin( $server );
693 is( substr($response,2,1), '0', 'OK flag is false when we check in an item on hold and we do not allow it' );
694 is( substr($response,5,1), 'Y', 'Alert flag is set' );
695 check_field( $respcode, $response, FID_SCREEN_MSG, 'Item is on hold, please return to circulation desk', 'Screen message is correct' );
696 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was not checked in");
698 $server->{account}->{holds_block_checkin} = 0;
702 sub test_hold_patron_bcode {
703 my $builder = t::lib::TestBuilder->new();
704 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
705 my ( $response, $findpatron );
706 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
708 my $item = $builder->build_sample_item(
710 library => $branchcode
714 my $server = { ils => $mocks->{ils} };
715 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
717 is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
719 my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
720 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
726 my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
728 # mock write_msg (imported from Sip.pm into Message.pm)
729 my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
730 $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
733 my $mockILS = Test::MockObject->new;
734 $mockILS->mock( 'check_inst_id', sub {} );
735 $mockILS->mock( 'institution_id', sub { $$branchcode; } );
736 $mockILS->mock( 'find_patron', sub { $$findpatron; } );
738 return { ils => $mockILS, message => $mockMsg };
742 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
743 # mode: contains || equals || regex (by default: equals)
745 # strip fixed part; prefix to simplify next regex
746 $resp = '|'. substr( $resp, fixed_length( $code ) );
748 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
750 } elsif( !defined($expr) ) { # field should not be found
753 } else { # test fails
754 is( 0, 1, "Code $fld not found in '$resp'?" );
758 if( !$mode || $mode eq 'equals' ) { # default
759 is( $fldval, $expr, $msg );
760 } elsif( $mode eq 'regex' ) {
761 is( $fldval =~ /$expr/, 1, $msg );
763 is( index( $fldval, $expr ) > -1, 1, $msg );
769 return $dt->ymd('').(' 'x4).$dt->hms('');
772 sub fixed_length { #length of fixed fields including response code
774 ( PATRON_STATUS_RESP ) => 37,
775 ( PATRON_INFO_RESP ) => 61,
776 ( CHECKIN_RESP ) => 24,