Bug 26370: (QA follow-up) Rename option never_demagnitize to inhouse_patron_categories
[koha.git] / t / db_dependent / SIP / Message.t
1 #!/usr/bin/perl
2
3 # Tests for SIP::Sip::MsgType
4 # Please help to extend it!
5
6 # This file is part of Koha.
7 #
8 # Copyright 2016 Rijksmuseum
9 #
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.
14 #
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.
19 #
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>.
22
23 use Modern::Perl;
24 use Test::More tests => 13;
25 use Test::Exception;
26 use Test::MockObject;
27 use Test::MockModule;
28 use Test::Warn;
29
30 use t::lib::Mocks;
31 use t::lib::TestBuilder;
32
33 use C4::Reserves qw( AddReserve );
34 use C4::Circulation qw( AddReturn );
35 use Koha::Database;
36 use Koha::AuthUtils qw(hash_password);
37 use Koha::DateUtils qw( dt_from_string output_pref );
38 use Koha::Items;
39 use Koha::Checkouts;
40 use Koha::Old::Checkouts;
41 use Koha::Patrons;
42 use Koha::Holds;
43
44 use C4::SIP::ILS;
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;
49
50 use constant PATRON_PW => 'do_not_ever_use_this_one';
51
52 # START testing
53 subtest 'Testing Patron Status Request V2' => sub {
54     my $schema = Koha::Database->new->schema;
55     $schema->storage->txn_begin;
56     plan tests => 13;
57     $C4::SIP::Sip::protocol_version = 2;
58     test_request_patron_status_v2();
59     $schema->storage->txn_rollback;
60 };
61
62 subtest 'Testing Patron Info Request V2' => sub {
63     my $schema = Koha::Database->new->schema;
64     $schema->storage->txn_begin;
65     plan tests => 24;
66     $C4::SIP::Sip::protocol_version = 2;
67     test_request_patron_info_v2();
68     $schema->storage->txn_rollback;
69 };
70
71 subtest 'Checkout V2' => sub {
72     my $schema = Koha::Database->new->schema;
73     $schema->storage->txn_begin;
74     plan tests => 5;
75     $C4::SIP::Sip::protocol_version = 2;
76     test_checkout_v2();
77     $schema->storage->txn_rollback;
78 };
79
80 subtest 'Test checkout desensitize' => sub {
81     my $schema = Koha::Database->new->schema;
82     $schema->storage->txn_begin;
83     plan tests => 3;
84     $C4::SIP::Sip::protocol_version = 2;
85     test_checkout_desensitize();
86     $schema->storage->txn_rollback;
87 };
88
89 subtest 'Test renew desensitize' => sub {
90     my $schema = Koha::Database->new->schema;
91     $schema->storage->txn_begin;
92     plan tests => 3;
93     $C4::SIP::Sip::protocol_version = 2;
94     test_renew_desensitize();
95     $schema->storage->txn_rollback;
96 };
97
98 subtest 'Checkin V2' => sub {
99     my $schema = Koha::Database->new->schema;
100     $schema->storage->txn_begin;
101     plan tests => 35;
102     $C4::SIP::Sip::protocol_version = 2;
103     test_checkin_v2();
104     $schema->storage->txn_rollback;
105 };
106
107 subtest 'Test hold_patron_bcode' => sub {
108     my $schema = Koha::Database->new->schema;
109     $schema->storage->txn_begin;
110     plan tests => 2;
111     $C4::SIP::Sip::protocol_version = 2;
112     test_hold_patron_bcode();
113     $schema->storage->txn_rollback;
114 };
115
116 subtest 'hold_patron_name() tests' => sub {
117
118     plan tests => 2;
119
120     my $schema = Koha::Database->new->schema;
121     $schema->storage->txn_begin;
122
123     my $builder = t::lib::TestBuilder->new();
124
125     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
126     my ( $response, $findpatron );
127     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
128
129     my $item = $builder->build_sample_item(
130         {
131             damaged       => 0,
132             withdrawn     => 0,
133             itemlost      => 0,
134             restricted    => 0,
135             homebranch    => $branchcode,
136             holdingbranch => $branchcode
137         }
138     );
139
140     my $server = { ils => $mocks->{ils} };
141     my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
142
143     is( $sip_item->hold_patron_name, q{}, "SIP item with no hold returns empty string for patron name" );
144
145     my $resp = C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_name, $server );
146     is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
147
148     $schema->storage->txn_rollback;
149 };
150
151 subtest 'Lastseen response' => sub {
152
153     my $schema = Koha::Database->new->schema;
154     $schema->storage->txn_begin;
155
156     plan tests => 6;
157     my $builder = t::lib::TestBuilder->new();
158     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
159     my ( $response, $findpatron );
160     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
161     my $seen_patron = $builder->build({
162         source => 'Borrower',
163         value  => {
164             lastseen => '2001-01-01',
165             password => hash_password( PATRON_PW ),
166             branchcode => $branchcode,
167         },
168     });
169     my $cardnum = $seen_patron->{cardnumber};
170     my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
171     $findpatron = $sip_patron;
172
173     my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
174         FID_INST_ID. $branchcode. '|'.
175         FID_PATRON_ID. $cardnum. '|'.
176         FID_PATRON_PWD. PATRON_PW. '|';
177     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
178
179     my $server = { ils => $mocks->{ils} };
180     undef $response;
181     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
182     $msg->handle_patron_info( $server );
183
184     isnt( $response, undef, 'At least we got a response.' );
185     my $respcode = substr( $response, 0, 2 );
186     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
187     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
188     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');
189     undef $response;
190     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
191     $msg->handle_patron_info( $server );
192
193     isnt( $response, undef, 'At least we got a response.' );
194     $respcode = substr( $response, 0, 2 );
195     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
196     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
197     is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
198     $schema->storage->txn_rollback;
199
200 };
201
202 subtest "Test build_additional_item_fields_string" => sub {
203     my $schema = Koha::Database->new->schema;
204     $schema->storage->txn_begin;
205
206     plan tests => 2;
207
208     my $builder = t::lib::TestBuilder->new();
209
210     my $item = $builder->build_sample_item;
211     my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
212
213     my $server = {};
214     $server->{account}->{item_field}->{code} = 'itemnumber';
215     $server->{account}->{item_field}->{field} = 'XY';
216     my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
217     is( $attribute_string, "XY".$item->itemnumber."|", 'Attribute field generated correctly with single param' );
218
219     $server = {};
220     $server->{account}->{item_field}->[0]->{code} = 'itemnumber';
221     $server->{account}->{item_field}->[0]->{field} = 'XY';
222     $server->{account}->{item_field}->[1]->{code} = 'biblionumber';
223     $server->{account}->{item_field}->[1]->{field} = 'YZ';
224     $attribute_string = $ils_item->build_additional_item_fields_string( $server );
225     is( $attribute_string, sprintf("XY%s|YZ%s|", $item->itemnumber, $item->biblionumber), 'Attribute field generated correctly with multiple params' );
226
227     $schema->storage->txn_rollback;
228 };
229
230 subtest "Test cr_item_field" => sub {
231     plan tests => 2;
232
233     my $builder = t::lib::TestBuilder->new();
234     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
235     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
236     my ( $response, $findpatron );
237     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
238
239     # create some data
240     my $patron1 = $builder->build({
241         source => 'Borrower',
242         value  => {
243             password => hash_password( PATRON_PW ),
244         },
245     });
246     my $card1 = $patron1->{cardnumber};
247     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
248     $findpatron = $sip_patron1;
249     my $item_object = $builder->build_sample_item({
250         damaged => 0,
251         withdrawn => 0,
252         itemlost => 0,
253         restricted => 0,
254         homebranch => $branchcode,
255         holdingbranch => $branchcode,
256     });
257
258     my $mockILS = $mocks->{ils};
259     my $server = { ils => $mockILS, account => {} };
260     $mockILS->mock( 'institution', sub { $branchcode; } );
261     $mockILS->mock( 'supports', sub { return; } );
262     $mockILS->mock( 'checkin', sub {
263         shift;
264         return C4::SIP::ILS->checkin(@_);
265     });
266     my $today = dt_from_string;
267
268     my $respcode;
269
270     # Not checked out, toggle option checked_in_ok
271     my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
272         siprequestdate( $today->clone->add( days => 1) ) .
273         FID_INST_ID . $branchcode . '|'.
274         FID_ITEM_ID . $item_object->barcode . '|' .
275         FID_TERMINAL_PWD . 'ignored' . '|';
276     undef $response;
277     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
278
279     $server->{account}->{cr_item_field} = 'itemnumber';
280
281     $msg->handle_checkin( $server );
282
283     my $id = $item_object->id;
284     ok( $response =~ m/CR$id/, "Found correct CR field in response");
285
286     $siprequest = ITEM_INFORMATION . 'YYYYMMDDZZZZHHMMSS' .
287         FID_INST_ID . $branchcode . '|'.
288         FID_ITEM_ID . $item_object->barcode . '|' .
289         FID_TERMINAL_PWD . 'ignored' . '|';
290     undef $response;
291     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
292
293     $mockILS->mock( 'find_item', sub {
294         return C4::SIP::ILS::Item->new( $item_object->barcode );
295     });
296
297     $server->{account}->{cr_item_field} = 'itype';
298
299     $msg->handle_item_information( $server );
300
301     my $itype = $item_object->itype;
302     ok( $response =~ m/CR$itype/, "Found correct CR field in response");
303 };
304
305 subtest 'Patron info summary > 5 should not crash server' => sub {
306
307     my $schema = Koha::Database->new->schema;
308     $schema->storage->txn_begin;
309
310     plan tests => 22;
311     my $builder = t::lib::TestBuilder->new();
312     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
313     my ( $response, $findpatron );
314     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
315     my $seen_patron = $builder->build({
316         source => 'Borrower',
317         value  => {
318             lastseen => '2001-01-01',
319             password => hash_password( PATRON_PW ),
320             branchcode => $branchcode,
321         },
322     });
323     my $cardnum = $seen_patron->{cardnumber};
324     my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
325     $findpatron = $sip_patron;
326
327     my @summaries = (
328         '          ',
329         'Y         ',
330         ' Y        ',
331         '  Y       ',
332         '   Y      ',
333         '    Y     ',
334         '     Y    ',
335         '      Y   ',
336         '       Y  ',
337         '        Y ',
338         '         Y',
339     );
340     for my $summary ( @summaries ) {
341         my $siprequest = PATRON_INFO . 'engYYYYMMDDZZZZHHMMSS' . $summary .
342             FID_INST_ID . $branchcode . '|' .
343             FID_PATRON_ID . $cardnum . '|' .
344             FID_PATRON_PWD . PATRON_PW . '|';
345         my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
346
347         my $server = { ils => $mocks->{ils} };
348         undef $response;
349         $msg->handle_patron_info( $server );
350
351         isnt( $response, undef, 'At least we got a response.' );
352         my $respcode = substr( $response, 0, 2 );
353         is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
354     }
355
356     $schema->storage->txn_rollback;
357 };
358
359 subtest 'SC status tests' => sub {
360
361     my $schema = Koha::Database->new->schema;
362     $schema->storage->txn_begin;
363
364     plan tests => 2;
365
366     my $builder = t::lib::TestBuilder->new();
367     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
368     my $sip_user = $builder->build_object({ class => "Koha::Patrons" });
369
370     my ( $response, $findpatron );
371     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
372     my $mockILS = $mocks->{ils};
373     $mockILS->mock( 'checkout_ok', sub {1} );
374     $mockILS->mock( 'checkin_ok', sub {1} );
375     $mockILS->mock( 'status_update_ok', sub {1} );
376     $mockILS->mock( 'offline_ok', sub {1} );
377     $mockILS->mock( 'supports', sub {1} );
378     my $server = Test::MockObject->new();
379     $server->mock( 'get_timeout', sub {'100'});
380     $server->{ils} = $mockILS;
381     $server->{sip_username} = $sip_user->userid;
382     $server->{account} = {};
383     $server->{policy} = { renewal =>1,retries=>'000'};
384
385     my $siprequest = SC_STATUS . '0' . '030' . '2.00';
386     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
387     $msg->handle_sc_status( $server );
388
389     like( $response, qr/98YYYYYY100000[0-9 ]{19}.00AO|BXYYYYYYYYYYYYYYYY|/, 'At least we got a response.' );
390
391     $sip_user->delete;
392
393     dies_ok{ $msg->handle_sc_status( $server ) } ,"Dies if sip user cannot be found";
394
395 };
396
397 # Here is room for some more subtests
398
399 # END of main code
400
401 sub test_request_patron_status_v2 {
402     my $builder = t::lib::TestBuilder->new();
403     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
404     my ( $response, $findpatron );
405     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
406
407     my $patron1 = $builder->build({
408         source => 'Borrower',
409         value  => {
410             password => hash_password( PATRON_PW ),
411         },
412     });
413     my $card1 = $patron1->{cardnumber};
414     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
415     $findpatron = $sip_patron1;
416
417     my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
418         FID_INST_ID. $branchcode. '|'.
419         FID_PATRON_ID. $card1. '|'.
420         FID_PATRON_PWD. PATRON_PW. '|';
421     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
422
423     my $server = { ils => $mocks->{ils} };
424     undef $response;
425     $msg->handle_patron_status( $server );
426
427     isnt( $response, undef, 'At least we got a response.' );
428     my $respcode = substr( $response, 0, 2 );
429     is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
430
431     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
432     check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
433     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
434     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
435     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
436     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
437
438     # Now, we pass a wrong password and verify CQ again
439     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
440         FID_INST_ID. $branchcode. '|'.
441         FID_PATRON_ID. $card1. '|'.
442         FID_PATRON_PWD. 'wrong_password'. '|';
443     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
444     undef $response;
445     $msg->handle_patron_status( $server );
446     $respcode = substr( $response, 0, 2 );
447     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
448
449     # Check empty password and verify CQ again
450     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
451         FID_INST_ID. $branchcode. '|'.
452         FID_PATRON_ID. $card1. '|'.
453         FID_PATRON_PWD. '|';
454     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
455     undef $response;
456     $msg->handle_patron_status( $server );
457     $respcode = substr( $response, 0, 2 );
458     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
459
460     # Finally, we send a wrong card number and check AE, BL
461     # This is done by removing the new patron first
462     Koha::Patrons->search({ cardnumber => $card1 })->delete;
463     undef $findpatron;
464     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
465         FID_INST_ID. $branchcode. '|'.
466         FID_PATRON_ID. $card1. '|'.
467         FID_PATRON_PWD. PATRON_PW. '|';
468     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
469     undef $response;
470     $msg->handle_patron_status( $server );
471     $respcode = substr( $response, 0, 2 );
472     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
473     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
474     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
475 }
476
477 sub test_request_patron_info_v2 {
478     my $builder = t::lib::TestBuilder->new();
479     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
480     my ( $response, $findpatron );
481     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
482
483     my $patron2 = $builder->build({
484         source => 'Borrower',
485         value  => {
486             password => hash_password( PATRON_PW ),
487         },
488     });
489     my $card = $patron2->{cardnumber};
490     my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
491     $findpatron = $sip_patron2;
492     my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
493         FID_INST_ID. $branchcode. '|'.
494         FID_PATRON_ID. $card. '|'.
495         FID_PATRON_PWD. PATRON_PW. '|';
496     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
497
498     my $server = { ils => $mocks->{ils} };
499     undef $response;
500     $msg->handle_patron_info( $server );
501     isnt( $response, undef, 'At least we got a response.' );
502     my $respcode = substr( $response, 0, 2 );
503     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
504
505     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
506     check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
507     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
508     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
509     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
510     check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
511     check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
512     check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
513     check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
514     # No check for custom fields here (unofficial PB, PC and PI)
515     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
516
517     # Test customized patron name in AE with same sip request
518     # This implicitly tests C4::SIP::ILS::Patron->name
519     $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
520     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
521     undef $response;
522     $msg->handle_patron_info( $server );
523     $respcode = substr( $response, 0, 2 );
524     check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
525
526     undef $response;
527     $server->{account}->{hide_fields} = "BD,BE,BF,PB";
528     $msg->handle_patron_info( $server );
529     $respcode = substr( $response, 0, 2 );
530     check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
531     check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
532     check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
533     check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
534     $server->{account}->{hide_fields} = "";
535
536     # Check empty password and verify CQ again
537     $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
538         FID_INST_ID. $branchcode. '|'.
539         FID_PATRON_ID. $card. '|'.
540         FID_PATRON_PWD. '|';
541     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
542     undef $response;
543     $msg->handle_patron_info( $server );
544     $respcode = substr( $response, 0, 2 );
545     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
546     # Test empty password is OK if account configured to allow
547     $server->{account}->{allow_empty_passwords} = 1;
548     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
549     undef $response;
550     $msg->handle_patron_info( $server );
551     $respcode = substr( $response, 0, 2 );
552     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
553
554     t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
555     my $patron = Koha::Patrons->find({ cardnumber => $card });
556     $patron->update({ login_attempts => 0 });
557     is( $patron->account_locked, 0, "Patron account not locked already" );
558     $msg->handle_patron_info( $server );
559     $patron = Koha::Patrons->find({ cardnumber => $card });
560     is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
561
562     # Finally, we send a wrong card number
563     Koha::Patrons->search({ cardnumber => $card })->delete;
564     undef $findpatron;
565     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
566     undef $response;
567     $msg->handle_patron_info( $server );
568     $respcode = substr( $response, 0, 2 );
569     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
570     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
571     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
572 }
573
574 sub test_checkout_v2 {
575     my $builder = t::lib::TestBuilder->new();
576     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
577     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
578     my ( $response, $findpatron );
579     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
580
581     # create some data
582     my $patron1 = $builder->build({
583         source => 'Borrower',
584         value  => {
585             password => hash_password( PATRON_PW ),
586         },
587     });
588     my $card1 = $patron1->{cardnumber};
589     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
590     $findpatron = $sip_patron1;
591     my $item_object = $builder->build_sample_item({
592         damaged => 0,
593         withdrawn => 0,
594         itemlost => 0,
595         restricted => 0,
596         homebranch => $branchcode,
597         holdingbranch => $branchcode,
598     });
599
600     my $mockILS = $mocks->{ils};
601     my $server = { ils => $mockILS, account => {} };
602     $mockILS->mock( 'institution', sub { $branchcode; } );
603     $mockILS->mock( 'supports', sub { return; } );
604     $mockILS->mock( 'checkout', sub {
605         shift;
606         return C4::SIP::ILS->checkout(@_);
607     });
608     my $today = dt_from_string;
609     t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
610     t::lib::Mocks::mock_preference( 'CheckPrevCheckout',  'hardyes' );
611
612     my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
613     my $return = AddReturn($item_object->barcode, $branchcode);
614
615     my $siprequest = CHECKOUT . 'YN' . siprequestdate($today) .
616     siprequestdate( $today->clone->add( days => 1) ) .
617     FID_INST_ID . $branchcode . '|'.
618     FID_PATRON_ID . $sip_patron1->id . '|' .
619     FID_ITEM_ID . $item_object->barcode . '|' .
620     FID_TERMINAL_PWD . 'ignored' . '|';
621     undef $response;
622
623     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
624     $server->{account}->{prevcheckout_block_checkout} = 1;
625     $msg->handle_checkout( $server );
626     my $respcode = substr( $response, 0, 2 );
627     check_field( $respcode, $response, FID_SCREEN_MSG, 'This item was previously checked out by you', 'Check screen msg', 'equals' );
628
629     is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 0, "Item was not checked out (prevcheckout_block_checkout enabled)");
630
631     $server->{account}->{prevcheckout_block_checkout} = 0;
632     $msg->handle_checkout( $server );
633     $respcode = substr( $response, 0, 2 );
634     is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was checked out (prevcheckout_block_checkout disabled)");
635
636     $msg->handle_checkout( $server );
637     ok( $response =~ m/AH\d{8}    \d{6}/, "Found AH field as timestamp in response");
638     $server->{account}->{format_due_date} = 1;
639     t::lib::Mocks::mock_preference( 'dateFormat',  'sql' );
640     undef $response;
641     $msg->handle_checkout( $server );
642     ok( $response =~ m/AH\d{4}-\d{2}-\d{2}/, "Found AH field as SQL date in response");
643
644 }
645
646 sub test_checkin_v2 {
647     my $builder = t::lib::TestBuilder->new();
648     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
649     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
650     my ( $response, $findpatron );
651     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
652
653     # create some data
654     my $patron1 = $builder->build({
655         source => 'Borrower',
656         value  => {
657             password => hash_password( PATRON_PW ),
658         },
659     });
660     my $card1 = $patron1->{cardnumber};
661     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
662     $findpatron = $sip_patron1;
663     my $item_object = $builder->build_sample_item({
664         damaged => 0,
665         withdrawn => 0,
666         itemlost => 0,
667         restricted => 0,
668         homebranch => $branchcode,
669         holdingbranch => $branchcode,
670     });
671
672     my $mockILS = $mocks->{ils};
673     my $server = { ils => $mockILS, account => {} };
674     $mockILS->mock( 'institution', sub { $branchcode; } );
675     $mockILS->mock( 'supports', sub { return; } );
676     $mockILS->mock( 'checkin', sub {
677         shift;
678         return C4::SIP::ILS->checkin(@_);
679     });
680     my $today = dt_from_string;
681
682     # Checkin invalid barcode
683     Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
684     my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
685         siprequestdate( $today->clone->add( days => 1) ) .
686         FID_INST_ID . $branchcode . '|'.
687         FID_ITEM_ID . 'not_to_be_found' . '|' .
688         FID_TERMINAL_PWD . 'ignored' . '|';
689     undef $response;
690     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
691     warnings_like { $msg->handle_checkin( $server ); }
692         [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
693         'Checkin of invalid item with two warnings';
694     my $respcode = substr( $response, 0, 2 );
695     is( $respcode, CHECKIN_RESP, 'Response code fine' );
696     is( substr($response,2,1), '0', 'OK flag is false' );
697     is( substr($response,5,1), 'Y', 'Alert flag is set' );
698     check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
699
700     # Not checked out, toggle option checked_in_ok
701     $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
702         siprequestdate( $today->clone->add( days => 1) ) .
703         FID_INST_ID . $branchcode . '|'.
704         FID_ITEM_ID . $item_object->barcode . '|' .
705         FID_TERMINAL_PWD . 'ignored' . '|';
706     undef $response;
707     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
708     $msg->handle_checkin( $server );
709     $respcode = substr( $response, 0, 2 );
710     is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
711     is( substr($response,5,1), 'Y', 'Alert flag is set' );
712     check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
713     # Toggle option
714     $server->{account}->{checked_in_ok} = 1;
715     undef $response;
716     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
717     $msg->handle_checkin( $server );
718     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' );
719     is( substr($response,5,1), 'N', 'Alert flag no longer set' );
720     check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
721
722     # Move item to another holding branch to trigger CV of 04 with alert flag
723     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
724     $item_object->holdingbranch( $branchcode2 )->store();
725     undef $response;
726     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
727     $msg->handle_checkin( $server );
728     is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
729     check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
730     $item_object->holdingbranch( $branchcode )->store();
731     t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
732
733     $server->{account}->{cv_send_00_on_success} = 0;
734     undef $response;
735     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
736     $msg->handle_checkin( $server );
737     $respcode = substr( $response, 0, 2 );
738     check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
739     $server->{account}->{cv_send_00_on_success} = 1;
740     undef $response;
741     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
742     $msg->handle_checkin( $server );
743     $respcode = substr( $response, 0, 2 );
744     check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
745     $server->{account}->{checked_in_ok} = 0;
746     $server->{account}->{cv_send_00_on_success} = 0;
747
748     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
749     $server->{account}->{checked_in_ok} = 0;
750     $server->{account}->{cv_triggers_alert} = 0;
751     undef $response;
752     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
753     $msg->handle_checkin( $server );
754     $respcode = substr( $response, 0, 2 );
755     is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
756     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
757     $server->{account}->{cv_triggers_alert} = 1;
758     undef $response;
759     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
760     $msg->handle_checkin( $server );
761     $respcode = substr( $response, 0, 2 );
762     is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
763     $server->{account}->{cv_triggers_alert} = 0;
764     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
765
766     $server->{account}->{checked_in_ok} = 1;
767     $server->{account}->{ct_always_send} = 0;
768     undef $response;
769     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
770     $msg->handle_checkin( $server );
771     $respcode = substr( $response, 0, 2 );
772     check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
773     $server->{account}->{ct_always_send} = 1;
774     undef $response;
775     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
776     $msg->handle_checkin( $server );
777     $respcode = substr( $response, 0, 2 );
778     check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
779     $server->{account}->{checked_in_ok} = 0;
780     $server->{account}->{ct_always_send} = 0;
781
782     # Checkin at wrong branch: issue item and switch branch, and checkin
783     my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
784     $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
785     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
786     undef $response;
787     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
788     $msg->handle_checkin( $server );
789     is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
790     is( substr($response,5,1), 'Y', 'Alert flag is set' );
791     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
792     $branchcode = $item_object->homebranch;  # switch back
793     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
794
795     # Data corrupted: add same issue_id to old_issues
796     Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
797     undef $response;
798     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
799     warnings_like { $msg->handle_checkin( $server ); }
800         [ qr/Duplicate entry/, qr/data issues/ ],
801         'DBIx error on duplicate issue_id';
802     is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
803     is( substr($response,5,1), 'Y', 'Alert flag is set' );
804     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
805
806     # Finally checkin without problems (remove duplicate id)
807     Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
808     undef $response;
809     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
810     $msg->handle_checkin( $server );
811     is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
812     is( substr($response,5,1), 'N', 'Alert flag is not set' );
813     is( Koha::Checkouts->find( $issue->issue_id ), undef,
814         'Issue record is gone now' );
815
816     # Test account option no_holds_check that prevents items on hold from being checked in via SIP
817     $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
818     is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item is checked out");
819     Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
820     $server->{account}->{holds_block_checkin} = 1;
821     my $reserve_id = AddReserve({
822         branchcode     => $branchcode,
823         borrowernumber => $patron1->{borrowernumber},
824         biblionumber   => $item_object->biblionumber,
825         priority       => 1,
826     });
827     my $hold = Koha::Holds->find( $reserve_id );
828     is( $hold->id, $reserve_id, "Hold was created successfully" );
829     undef $response;
830     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
831     $msg->handle_checkin( $server );
832     is( substr($response,2,1), '0', 'OK flag is false when we check in an item on hold and we do not allow it' );
833     is( substr($response,5,1), 'Y', 'Alert flag is set' );
834     check_field( $respcode, $response, FID_SCREEN_MSG, 'Item is on hold, please return to circulation desk', 'Screen message is correct' );
835     is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was not checked in");
836     $hold->delete();
837     $server->{account}->{holds_block_checkin} = 0;
838
839 }
840
841 sub test_hold_patron_bcode {
842     my $builder = t::lib::TestBuilder->new();
843     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
844     my ( $response, $findpatron );
845     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
846
847     my $item = $builder->build_sample_item(
848         {
849             library => $branchcode
850         }
851     );
852
853     my $server = { ils => $mocks->{ils} };
854     my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
855
856     is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
857
858     my $resp = C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
859     is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
860 }
861
862 sub test_checkout_desensitize {
863     my $builder = t::lib::TestBuilder->new();
864     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
865     my ( $response, $findpatron );
866     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
867
868     # create some data
869     my $patron1 = $builder->build({
870         source => 'Borrower',
871         value  => {
872             password => hash_password( PATRON_PW ),
873         },
874     });
875     my $card1 = $patron1->{cardnumber};
876     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
877     my $patron_category = $sip_patron1->ptype();
878     $findpatron = $sip_patron1;
879     my $item_object = $builder->build_sample_item({
880         damaged => 0,
881         withdrawn => 0,
882         itemlost => 0,
883         restricted => 0,
884         homebranch => $branchcode,
885         holdingbranch => $branchcode,
886     });
887
888     my $mockILS = $mocks->{ils};
889     my $server = { ils => $mockILS, account => {} };
890     $mockILS->mock( 'institution', sub { $branchcode; } );
891     $mockILS->mock( 'supports', sub { return; } );
892     $mockILS->mock( 'checkout', sub {
893         shift;
894         return C4::SIP::ILS->checkout(@_);
895     });
896     my $today = dt_from_string;
897     t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
898     t::lib::Mocks::mock_preference( 'CheckPrevCheckout',  'hardyes' );
899
900     my $siprequest = CHECKOUT . 'YN' . siprequestdate($today) .
901     siprequestdate( $today->clone->add( days => 1) ) .
902     FID_INST_ID . $branchcode . '|'.
903     FID_PATRON_ID . $sip_patron1->id . '|' .
904     FID_ITEM_ID . $item_object->barcode . '|' .
905     FID_TERMINAL_PWD . 'ignored' . '|';
906
907     undef $response;
908     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
909     $server->{account}->{inhouse_patron_categories} = "A,$patron_category,Z";
910     $msg->handle_checkout( $server );
911     my $respcode = substr( $response, 5, 1 );
912     is( $respcode, 'N', "Desensitize flag was not set for patron category in inhouse_patron_categories" );
913
914     undef $response;
915     $server->{account}->{inhouse_patron_categories} = "A,B,C";
916     $msg->handle_checkout( $server );
917     $respcode = substr( $response, 5, 1 );
918     is( $respcode, 'Y', "Desensitize flag was set for patron category not in inhouse_patron_categories" );
919
920     undef $response;
921     $server->{account}->{inhouse_patron_categories} = "";
922     $msg->handle_checkout( $server );
923     $respcode = substr( $response, 5, 1 );
924     is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_patron_categories" );
925 }
926
927 sub test_renew_desensitize {
928     my $builder = t::lib::TestBuilder->new();
929     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
930     my ( $response, $findpatron );
931     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
932
933     # create some data
934     my $patron1 = $builder->build({
935         source => 'Borrower',
936         value  => {
937             password => hash_password( PATRON_PW ),
938         },
939     });
940     my $card1 = $patron1->{cardnumber};
941     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
942     my $patron_category = $sip_patron1->ptype();
943     $findpatron = $sip_patron1;
944     my $item_object = $builder->build_sample_item({
945         damaged => 0,
946         withdrawn => 0,
947         itemlost => 0,
948         restricted => 0,
949         homebranch => $branchcode,
950         holdingbranch => $branchcode,
951     });
952
953     my $mockILS = $mocks->{ils};
954     my $server = { ils => $mockILS, account => {} };
955     $mockILS->mock( 'institution', sub { $branchcode; } );
956     $mockILS->mock( 'supports', sub { return; } );
957     $mockILS->mock( 'checkout', sub {
958         shift;
959         return C4::SIP::ILS->checkout(@_);
960     });
961     my $today = dt_from_string;
962     t::lib::Mocks::mock_userenv({ branchcode => $branchcode, flags => 1 });
963
964     my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
965
966     my $siprequest = RENEW . 'YN' . siprequestdate($today) .
967     siprequestdate( $today->clone->add( days => 1) ) .
968     FID_INST_ID . $branchcode . '|'.
969     FID_PATRON_ID . $sip_patron1->id . '|' .
970     FID_ITEM_ID . $item_object->barcode . '|' .
971     FID_TERMINAL_PWD . 'ignored' . '|';
972
973     undef $response;
974     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
975     $server->{account}->{inhouse_patron_categories} = "A,$patron_category,Z";
976     $msg->handle_checkout( $server );
977     my $respcode = substr( $response, 5, 1 );
978     is( $respcode, 'N', "Desensitize flag was not set for patron category in inhouse_patron_categories" );
979
980     undef $response;
981     $server->{account}->{inhouse_patron_categories} = "A,B,C";
982     $msg->handle_checkout( $server );
983     $respcode = substr( $response, 5, 1 );
984     is( $respcode, 'Y', "Desensitize flag was set for patron category not in inhouse_patron_categories" );
985
986     undef $response;
987     $server->{account}->{inhouse_patron_categories} = "";
988     $msg->handle_checkout( $server );
989     $respcode = substr( $response, 5, 1 );
990     is( $respcode, 'Y', "Desensitize flag was set for empty inhouse_patron_categories" );
991 }
992
993 # Helper routines
994
995 sub create_mocks {
996     my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
997
998     # mock write_msg (imported from Sip.pm into Message.pm)
999     my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
1000     $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
1001
1002     # mock ils object
1003     my $mockILS = Test::MockObject->new;
1004     $mockILS->mock( 'check_inst_id', sub {} );
1005     $mockILS->mock( 'institution_id', sub { $$branchcode; } );
1006     $mockILS->mock( 'find_patron', sub { $$findpatron; } );
1007
1008     return { ils => $mockILS, message => $mockMsg };
1009 }
1010
1011 sub check_field {
1012     my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
1013     # mode: contains || equals || regex (by default: equals)
1014
1015     # strip fixed part; prefix to simplify next regex
1016     $resp = '|'. substr( $resp, fixed_length( $code ) );
1017     my $fldval;
1018     if( $resp =~ /\|$fld([^\|]*)\|/ ) {
1019         $fldval = $1;
1020     } elsif( !defined($expr) ) { # field should not be found
1021         ok( 1, $msg );
1022         return;
1023     } else { # test fails
1024         is( 0, 1, "Code $fld not found in '$resp'?" );
1025         return;
1026     }
1027
1028     if( !$mode || $mode eq 'equals' ) { # default
1029         is( $fldval, $expr, $msg );
1030     } elsif( $mode eq 'regex' ) {
1031         is( $fldval =~ /$expr/, 1, $msg );
1032     } else { # contains
1033         is( index( $fldval, $expr ) > -1, 1, $msg );
1034     }
1035 }
1036
1037 sub siprequestdate {
1038     my ( $dt ) = @_;
1039     return $dt->ymd('').(' 'x4).$dt->hms('');
1040 }
1041
1042 sub fixed_length { #length of fixed fields including response code
1043     return {
1044       ( PATRON_STATUS_RESP )  => 37,
1045       ( PATRON_INFO_RESP )    => 61,
1046       ( CHECKIN_RESP )        => 24,
1047       ( CHECKOUT_RESP )       => 24,
1048     }->{$_[0]};
1049 }