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 => 11;
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 'Checkin V2' => sub {
81 my $schema = Koha::Database->new->schema;
82 $schema->storage->txn_begin;
84 $C4::SIP::Sip::protocol_version = 2;
86 $schema->storage->txn_rollback;
89 subtest 'Test hold_patron_bcode' => sub {
90 my $schema = Koha::Database->new->schema;
91 $schema->storage->txn_begin;
93 $C4::SIP::Sip::protocol_version = 2;
94 test_hold_patron_bcode();
95 $schema->storage->txn_rollback;
98 subtest 'hold_patron_name() tests' => sub {
102 my $schema = Koha::Database->new->schema;
103 $schema->storage->txn_begin;
105 my $builder = t::lib::TestBuilder->new();
107 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
108 my ( $response, $findpatron );
109 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
111 my $item = $builder->build_sample_item(
117 homebranch => $branchcode,
118 holdingbranch => $branchcode
122 my $server = { ils => $mocks->{ils} };
123 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
125 is( $sip_item->hold_patron_name, q{}, "SIP item with no hold returns empty string for patron name" );
127 my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_name, $server );
128 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
130 $schema->storage->txn_rollback;
133 subtest 'Lastseen response' => sub {
135 my $schema = Koha::Database->new->schema;
136 $schema->storage->txn_begin;
139 my $builder = t::lib::TestBuilder->new();
140 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
141 my ( $response, $findpatron );
142 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
143 my $seen_patron = $builder->build({
144 source => 'Borrower',
146 lastseen => '2001-01-01',
147 password => hash_password( PATRON_PW ),
148 branchcode => $branchcode,
151 my $cardnum = $seen_patron->{cardnumber};
152 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
153 $findpatron = $sip_patron;
155 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
156 FID_INST_ID. $branchcode. '|'.
157 FID_PATRON_ID. $cardnum. '|'.
158 FID_PATRON_PWD. PATRON_PW. '|';
159 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
161 my $server = { ils => $mocks->{ils} };
163 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
164 $msg->handle_patron_info( $server );
166 isnt( $response, undef, 'At least we got a response.' );
167 my $respcode = substr( $response, 0, 2 );
168 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
169 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
170 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');
172 t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
173 $msg->handle_patron_info( $server );
175 isnt( $response, undef, 'At least we got a response.' );
176 $respcode = substr( $response, 0, 2 );
177 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
178 $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
179 is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
180 $schema->storage->txn_rollback;
184 subtest "Test build_additional_item_fields_string" => sub {
185 my $schema = Koha::Database->new->schema;
186 $schema->storage->txn_begin;
190 my $builder = t::lib::TestBuilder->new();
192 my $item = $builder->build_sample_item;
193 my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
196 $server->{account}->{item_field}->{code} = 'itemnumber';
197 $server->{account}->{item_field}->{field} = 'XY';
198 my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
199 is( $attribute_string, "XY".$item->itemnumber."|", 'Attribute field generated correctly with single param' );
202 $server->{account}->{item_field}->[0]->{code} = 'itemnumber';
203 $server->{account}->{item_field}->[0]->{field} = 'XY';
204 $server->{account}->{item_field}->[1]->{code} = 'biblionumber';
205 $server->{account}->{item_field}->[1]->{field} = 'YZ';
206 $attribute_string = $ils_item->build_additional_item_fields_string( $server );
207 is( $attribute_string, sprintf("XY%s|YZ%s|", $item->itemnumber, $item->biblionumber), 'Attribute field generated correctly with multiple params' );
209 $schema->storage->txn_rollback;
212 subtest "Test cr_item_field" => sub {
215 my $builder = t::lib::TestBuilder->new();
216 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
217 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
218 my ( $response, $findpatron );
219 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
222 my $patron1 = $builder->build({
223 source => 'Borrower',
225 password => hash_password( PATRON_PW ),
228 my $card1 = $patron1->{cardnumber};
229 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
230 $findpatron = $sip_patron1;
231 my $item_object = $builder->build_sample_item({
236 homebranch => $branchcode,
237 holdingbranch => $branchcode,
240 my $mockILS = $mocks->{ils};
241 my $server = { ils => $mockILS, account => {} };
242 $mockILS->mock( 'institution', sub { $branchcode; } );
243 $mockILS->mock( 'supports', sub { return; } );
244 $mockILS->mock( 'checkin', sub {
246 return C4::SIP::ILS->checkin(@_);
248 my $today = dt_from_string;
252 # Not checked out, toggle option checked_in_ok
253 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
254 siprequestdate( $today->clone->add( days => 1) ) .
255 FID_INST_ID . $branchcode . '|'.
256 FID_ITEM_ID . $item_object->barcode . '|' .
257 FID_TERMINAL_PWD . 'ignored' . '|';
259 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
261 $server->{account}->{cr_item_field} = 'itemnumber';
263 $msg->handle_checkin( $server );
265 my $id = $item_object->id;
266 ok( $response =~ m/CR$id/, "Found correct CR field in response");
268 $siprequest = ITEM_INFORMATION . 'YYYYMMDDZZZZHHMMSS' .
269 FID_INST_ID . $branchcode . '|'.
270 FID_ITEM_ID . $item_object->barcode . '|' .
271 FID_TERMINAL_PWD . 'ignored' . '|';
273 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
275 $mockILS->mock( 'find_item', sub {
276 return C4::SIP::ILS::Item->new( $item_object->barcode );
279 $server->{account}->{cr_item_field} = 'itype';
281 $msg->handle_item_information( $server );
283 my $itype = $item_object->itype;
284 ok( $response =~ m/CR$itype/, "Found correct CR field in response");
287 subtest 'Patron info summary > 5 should not crash server' => sub {
289 my $schema = Koha::Database->new->schema;
290 $schema->storage->txn_begin;
293 my $builder = t::lib::TestBuilder->new();
294 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
295 my ( $response, $findpatron );
296 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
297 my $seen_patron = $builder->build({
298 source => 'Borrower',
300 lastseen => '2001-01-01',
301 password => hash_password( PATRON_PW ),
302 branchcode => $branchcode,
305 my $cardnum = $seen_patron->{cardnumber};
306 my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
307 $findpatron = $sip_patron;
322 for my $summary ( @summaries ) {
323 my $siprequest = PATRON_INFO . 'engYYYYMMDDZZZZHHMMSS' . $summary .
324 FID_INST_ID . $branchcode . '|' .
325 FID_PATRON_ID . $cardnum . '|' .
326 FID_PATRON_PWD . PATRON_PW . '|';
327 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
329 my $server = { ils => $mocks->{ils} };
331 $msg->handle_patron_info( $server );
333 isnt( $response, undef, 'At least we got a response.' );
334 my $respcode = substr( $response, 0, 2 );
335 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
338 $schema->storage->txn_rollback;
341 subtest 'SC status tests' => sub {
343 my $schema = Koha::Database->new->schema;
344 $schema->storage->txn_begin;
348 my $builder = t::lib::TestBuilder->new();
349 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
350 my $sip_user = $builder->build_object({ class => "Koha::Patrons" });
352 my ( $response, $findpatron );
353 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
354 my $mockILS = $mocks->{ils};
355 $mockILS->mock( 'checkout_ok', sub {1} );
356 $mockILS->mock( 'checkin_ok', sub {1} );
357 $mockILS->mock( 'status_update_ok', sub {1} );
358 $mockILS->mock( 'offline_ok', sub {1} );
359 $mockILS->mock( 'supports', sub {1} );
360 my $server = Test::MockObject->new();
361 $server->mock( 'get_timeout', sub {'100'});
362 $server->{ils} = $mockILS;
363 $server->{sip_username} = $sip_user->userid;
364 $server->{account} = {};
365 $server->{policy} = { renewal =>1,retries=>'000'};
367 my $siprequest = SC_STATUS . '0' . '030' . '2.00';
368 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
369 $msg->handle_sc_status( $server );
371 like( $response, qr/98YYYYYY100000[0-9 ]{19}.00AO|BXYYYYYYYYYYYYYYYY|/, 'At least we got a response.' );
375 dies_ok{ $msg->handle_sc_status( $server ) } ,"Dies if sip user cannot be found";
379 # Here is room for some more subtests
383 sub test_request_patron_status_v2 {
384 my $builder = t::lib::TestBuilder->new();
385 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
386 my ( $response, $findpatron );
387 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
389 my $patron1 = $builder->build({
390 source => 'Borrower',
392 password => hash_password( PATRON_PW ),
395 my $card1 = $patron1->{cardnumber};
396 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
397 $findpatron = $sip_patron1;
399 my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
400 FID_INST_ID. $branchcode. '|'.
401 FID_PATRON_ID. $card1. '|'.
402 FID_PATRON_PWD. PATRON_PW. '|';
403 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
405 my $server = { ils => $mocks->{ils} };
407 $msg->handle_patron_status( $server );
409 isnt( $response, undef, 'At least we got a response.' );
410 my $respcode = substr( $response, 0, 2 );
411 is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
413 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
414 check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
415 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
416 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
417 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
418 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
420 # Now, we pass a wrong password and verify CQ again
421 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
422 FID_INST_ID. $branchcode. '|'.
423 FID_PATRON_ID. $card1. '|'.
424 FID_PATRON_PWD. 'wrong_password'. '|';
425 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
427 $msg->handle_patron_status( $server );
428 $respcode = substr( $response, 0, 2 );
429 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
431 # Check empty password and verify CQ again
432 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
433 FID_INST_ID. $branchcode. '|'.
434 FID_PATRON_ID. $card1. '|'.
436 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
438 $msg->handle_patron_status( $server );
439 $respcode = substr( $response, 0, 2 );
440 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
442 # Finally, we send a wrong card number and check AE, BL
443 # This is done by removing the new patron first
444 Koha::Patrons->search({ cardnumber => $card1 })->delete;
446 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
447 FID_INST_ID. $branchcode. '|'.
448 FID_PATRON_ID. $card1. '|'.
449 FID_PATRON_PWD. PATRON_PW. '|';
450 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
452 $msg->handle_patron_status( $server );
453 $respcode = substr( $response, 0, 2 );
454 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
455 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
456 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
459 sub test_request_patron_info_v2 {
460 my $builder = t::lib::TestBuilder->new();
461 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
462 my ( $response, $findpatron );
463 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
465 my $patron2 = $builder->build({
466 source => 'Borrower',
468 password => hash_password( PATRON_PW ),
471 my $card = $patron2->{cardnumber};
472 my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
473 $findpatron = $sip_patron2;
474 my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
475 FID_INST_ID. $branchcode. '|'.
476 FID_PATRON_ID. $card. '|'.
477 FID_PATRON_PWD. PATRON_PW. '|';
478 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
480 my $server = { ils => $mocks->{ils} };
482 $msg->handle_patron_info( $server );
483 isnt( $response, undef, 'At least we got a response.' );
484 my $respcode = substr( $response, 0, 2 );
485 is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
487 check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
488 check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
489 check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
490 check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
491 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
492 check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
493 check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
494 check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
495 check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
496 # No check for custom fields here (unofficial PB, PC and PI)
497 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
499 # Test customized patron name in AE with same sip request
500 # This implicitly tests C4::SIP::ILS::Patron->name
501 $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
502 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
504 $msg->handle_patron_info( $server );
505 $respcode = substr( $response, 0, 2 );
506 check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
509 $server->{account}->{hide_fields} = "BD,BE,BF,PB";
510 $msg->handle_patron_info( $server );
511 $respcode = substr( $response, 0, 2 );
512 check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
513 check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
514 check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
515 check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
516 $server->{account}->{hide_fields} = "";
518 # Check empty password and verify CQ again
519 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
520 FID_INST_ID. $branchcode. '|'.
521 FID_PATRON_ID. $card. '|'.
523 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
525 $msg->handle_patron_info( $server );
526 $respcode = substr( $response, 0, 2 );
527 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
528 # Test empty password is OK if account configured to allow
529 $server->{account}->{allow_empty_passwords} = 1;
530 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
532 $msg->handle_patron_info( $server );
533 $respcode = substr( $response, 0, 2 );
534 check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
536 t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
537 my $patron = Koha::Patrons->find({ cardnumber => $card });
538 $patron->update({ login_attempts => 0 });
539 is( $patron->account_locked, 0, "Patron account not locked already" );
540 $msg->handle_patron_info( $server );
541 $patron = Koha::Patrons->find({ cardnumber => $card });
542 is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
544 # Finally, we send a wrong card number
545 Koha::Patrons->search({ cardnumber => $card })->delete;
547 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
549 $msg->handle_patron_info( $server );
550 $respcode = substr( $response, 0, 2 );
551 check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
552 check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
553 check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
556 sub test_checkout_v2 {
557 my $builder = t::lib::TestBuilder->new();
558 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
559 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
560 my ( $response, $findpatron );
561 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
564 my $patron1 = $builder->build({
565 source => 'Borrower',
567 password => hash_password( PATRON_PW ),
570 my $card1 = $patron1->{cardnumber};
571 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
572 $findpatron = $sip_patron1;
573 my $item_object = $builder->build_sample_item({
578 homebranch => $branchcode,
579 holdingbranch => $branchcode,
582 my $mockILS = $mocks->{ils};
583 my $server = { ils => $mockILS, account => {} };
584 $mockILS->mock( 'institution', sub { $branchcode; } );
585 $mockILS->mock( 'supports', sub { return; } );
586 $mockILS->mock( 'checkout', sub {
588 return C4::SIP::ILS->checkout(@_);
590 my $today = dt_from_string;
591 t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
592 t::lib::Mocks::mock_preference( 'CheckPrevCheckout', 'hardyes' );
594 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
595 my $return = AddReturn($item_object->barcode, $branchcode);
597 my $siprequest = CHECKOUT . 'YN' . siprequestdate($today) .
598 siprequestdate( $today->clone->add( days => 1) ) .
599 FID_INST_ID . $branchcode . '|'.
600 FID_PATRON_ID . $sip_patron1->id . '|' .
601 FID_ITEM_ID . $item_object->barcode . '|' .
602 FID_TERMINAL_PWD . 'ignored' . '|';
605 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
606 $server->{account}->{prevcheckout_block_checkout} = 1;
607 $msg->handle_checkout( $server );
608 my $respcode = substr( $response, 0, 2 );
609 check_field( $respcode, $response, FID_SCREEN_MSG, 'This item was previously checked out by you', 'Check screen msg', 'equals' );
611 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 0, "Item was not checked out (prevcheckout_block_checkout enabled)");
613 $server->{account}->{prevcheckout_block_checkout} = 0;
614 $msg->handle_checkout( $server );
615 $respcode = substr( $response, 0, 2 );
616 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was checked out (prevcheckout_block_checkout disabled)");
619 sub test_checkin_v2 {
620 my $builder = t::lib::TestBuilder->new();
621 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
622 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
623 my ( $response, $findpatron );
624 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
627 my $patron1 = $builder->build({
628 source => 'Borrower',
630 password => hash_password( PATRON_PW ),
633 my $card1 = $patron1->{cardnumber};
634 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
635 $findpatron = $sip_patron1;
636 my $item_object = $builder->build_sample_item({
641 homebranch => $branchcode,
642 holdingbranch => $branchcode,
645 my $mockILS = $mocks->{ils};
646 my $server = { ils => $mockILS, account => {} };
647 $mockILS->mock( 'institution', sub { $branchcode; } );
648 $mockILS->mock( 'supports', sub { return; } );
649 $mockILS->mock( 'checkin', sub {
651 return C4::SIP::ILS->checkin(@_);
653 my $today = dt_from_string;
655 # Checkin invalid barcode
656 Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
657 my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
658 siprequestdate( $today->clone->add( days => 1) ) .
659 FID_INST_ID . $branchcode . '|'.
660 FID_ITEM_ID . 'not_to_be_found' . '|' .
661 FID_TERMINAL_PWD . 'ignored' . '|';
663 my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
664 warnings_like { $msg->handle_checkin( $server ); }
665 [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
666 'Checkin of invalid item with two warnings';
667 my $respcode = substr( $response, 0, 2 );
668 is( $respcode, CHECKIN_RESP, 'Response code fine' );
669 is( substr($response,2,1), '0', 'OK flag is false' );
670 is( substr($response,5,1), 'Y', 'Alert flag is set' );
671 check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
673 # Not checked out, toggle option checked_in_ok
674 $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
675 siprequestdate( $today->clone->add( days => 1) ) .
676 FID_INST_ID . $branchcode . '|'.
677 FID_ITEM_ID . $item_object->barcode . '|' .
678 FID_TERMINAL_PWD . 'ignored' . '|';
680 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
681 $msg->handle_checkin( $server );
682 $respcode = substr( $response, 0, 2 );
683 is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
684 is( substr($response,5,1), 'Y', 'Alert flag is set' );
685 check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
687 $server->{account}->{checked_in_ok} = 1;
689 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
690 $msg->handle_checkin( $server );
691 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' );
692 is( substr($response,5,1), 'N', 'Alert flag no longer set' );
693 check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
695 # Move item to another holding branch to trigger CV of 04 with alert flag
696 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
697 $item_object->holdingbranch( $branchcode2 )->store();
699 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
700 $msg->handle_checkin( $server );
701 is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
702 check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
703 $item_object->holdingbranch( $branchcode )->store();
704 t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
706 $server->{account}->{cv_send_00_on_success} = 0;
708 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
709 $msg->handle_checkin( $server );
710 $respcode = substr( $response, 0, 2 );
711 check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
712 $server->{account}->{cv_send_00_on_success} = 1;
714 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
715 $msg->handle_checkin( $server );
716 $respcode = substr( $response, 0, 2 );
717 check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
718 $server->{account}->{checked_in_ok} = 0;
719 $server->{account}->{cv_send_00_on_success} = 0;
721 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
722 $server->{account}->{checked_in_ok} = 0;
723 $server->{account}->{cv_triggers_alert} = 0;
725 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
726 $msg->handle_checkin( $server );
727 $respcode = substr( $response, 0, 2 );
728 is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
729 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
730 $server->{account}->{cv_triggers_alert} = 1;
732 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
733 $msg->handle_checkin( $server );
734 $respcode = substr( $response, 0, 2 );
735 is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
736 $server->{account}->{cv_triggers_alert} = 0;
737 t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
739 $server->{account}->{checked_in_ok} = 1;
740 $server->{account}->{ct_always_send} = 0;
742 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
743 $msg->handle_checkin( $server );
744 $respcode = substr( $response, 0, 2 );
745 check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
746 $server->{account}->{ct_always_send} = 1;
748 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
749 $msg->handle_checkin( $server );
750 $respcode = substr( $response, 0, 2 );
751 check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
752 $server->{account}->{checked_in_ok} = 0;
753 $server->{account}->{ct_always_send} = 0;
755 # Checkin at wrong branch: issue item and switch branch, and checkin
756 my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
757 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
758 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
760 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
761 $msg->handle_checkin( $server );
762 is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
763 is( substr($response,5,1), 'Y', 'Alert flag is set' );
764 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
765 $branchcode = $item_object->homebranch; # switch back
766 t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
768 # Data corrupted: add same issue_id to old_issues
769 Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
771 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
772 warnings_like { $msg->handle_checkin( $server ); }
773 [ qr/Duplicate entry/, qr/data issues/ ],
774 'DBIx error on duplicate issue_id';
775 is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
776 is( substr($response,5,1), 'Y', 'Alert flag is set' );
777 check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
779 # Finally checkin without problems (remove duplicate id)
780 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
782 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
783 $msg->handle_checkin( $server );
784 is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
785 is( substr($response,5,1), 'N', 'Alert flag is not set' );
786 is( Koha::Checkouts->find( $issue->issue_id ), undef,
787 'Issue record is gone now' );
789 # Test account option no_holds_check that prevents items on hold from being checked in via SIP
790 $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
791 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item is checked out");
792 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
793 $server->{account}->{holds_block_checkin} = 1;
794 my $reserve_id = AddReserve({
795 branchcode => $branchcode,
796 borrowernumber => $patron1->{borrowernumber},
797 biblionumber => $item_object->biblionumber,
800 my $hold = Koha::Holds->find( $reserve_id );
801 is( $hold->id, $reserve_id, "Hold was created successfully" );
803 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
804 $msg->handle_checkin( $server );
805 is( substr($response,2,1), '0', 'OK flag is false when we check in an item on hold and we do not allow it' );
806 is( substr($response,5,1), 'Y', 'Alert flag is set' );
807 check_field( $respcode, $response, FID_SCREEN_MSG, 'Item is on hold, please return to circulation desk', 'Screen message is correct' );
808 is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was not checked in");
810 $server->{account}->{holds_block_checkin} = 0;
814 sub test_hold_patron_bcode {
815 my $builder = t::lib::TestBuilder->new();
816 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
817 my ( $response, $findpatron );
818 my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
820 my $item = $builder->build_sample_item(
822 library => $branchcode
826 my $server = { ils => $mocks->{ils} };
827 my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
829 is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
831 my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
832 is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
838 my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
840 # mock write_msg (imported from Sip.pm into Message.pm)
841 my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
842 $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
845 my $mockILS = Test::MockObject->new;
846 $mockILS->mock( 'check_inst_id', sub {} );
847 $mockILS->mock( 'institution_id', sub { $$branchcode; } );
848 $mockILS->mock( 'find_patron', sub { $$findpatron; } );
850 return { ils => $mockILS, message => $mockMsg };
854 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
855 # mode: contains || equals || regex (by default: equals)
857 # strip fixed part; prefix to simplify next regex
858 $resp = '|'. substr( $resp, fixed_length( $code ) );
860 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
862 } elsif( !defined($expr) ) { # field should not be found
865 } else { # test fails
866 is( 0, 1, "Code $fld not found in '$resp'?" );
870 if( !$mode || $mode eq 'equals' ) { # default
871 is( $fldval, $expr, $msg );
872 } elsif( $mode eq 'regex' ) {
873 is( $fldval =~ /$expr/, 1, $msg );
875 is( index( $fldval, $expr ) > -1, 1, $msg );
881 return $dt->ymd('').(' 'x4).$dt->hms('');
884 sub fixed_length { #length of fixed fields including response code
886 ( PATRON_STATUS_RESP ) => 37,
887 ( PATRON_INFO_RESP ) => 61,
888 ( CHECKIN_RESP ) => 24,
889 ( CHECKOUT_RESP ) => 24,