Bug 23057: Unit tests
[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 => 4;
25 use Test::MockObject;
26 use Test::MockModule;
27 use Test::Warn;
28
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 use Koha::Database;
33 use Koha::AuthUtils qw(hash_password);
34 use Koha::DateUtils;
35 use Koha::Items;
36 use Koha::Checkouts;
37 use Koha::Old::Checkouts;
38 use Koha::Patrons;
39
40 use C4::SIP::ILS;
41 use C4::SIP::ILS::Patron;
42 use C4::SIP::Sip qw(write_msg);
43 use C4::SIP::Sip::Constants qw(:all);
44 use C4::SIP::Sip::MsgType;
45
46 use constant PATRON_PW => 'do_not_ever_use_this_one';
47
48 # START testing
49 subtest 'Testing Patron Status Request V2' => sub {
50     my $schema = Koha::Database->new->schema;
51     $schema->storage->txn_begin;
52     plan tests => 13;
53     $C4::SIP::Sip::protocol_version = 2;
54     test_request_patron_status_v2();
55     $schema->storage->txn_rollback;
56 };
57
58 subtest 'Testing Patron Info Request V2' => sub {
59     my $schema = Koha::Database->new->schema;
60     $schema->storage->txn_begin;
61     plan tests => 24;
62     $C4::SIP::Sip::protocol_version = 2;
63     test_request_patron_info_v2();
64     $schema->storage->txn_rollback;
65 };
66
67 subtest 'Checkin V2' => sub {
68     my $schema = Koha::Database->new->schema;
69     $schema->storage->txn_begin;
70     plan tests => 29;
71     $C4::SIP::Sip::protocol_version = 2;
72     test_checkin_v2();
73     $schema->storage->txn_rollback;
74 };
75
76 subtest 'Lastseen response' => sub {
77
78     my $schema = Koha::Database->new->schema;
79     $schema->storage->txn_begin;
80
81     plan tests => 6;
82     my $builder = t::lib::TestBuilder->new();
83     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
84     my ( $response, $findpatron );
85     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
86     my $seen_patron = $builder->build({
87         source => 'Borrower',
88         value  => {
89             lastseen => '2001-01-01',
90             password => hash_password( PATRON_PW ),
91             branchcode => $branchcode,
92         },
93     });
94     my $cardnum = $seen_patron->{cardnumber};
95     my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
96     $findpatron = $sip_patron;
97
98     my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
99         FID_INST_ID. $branchcode. '|'.
100         FID_PATRON_ID. $cardnum. '|'.
101         FID_PATRON_PWD. PATRON_PW. '|';
102     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
103
104     my $server = { ils => $mocks->{ils} };
105     undef $response;
106     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
107     $msg->handle_patron_info( $server );
108
109     isnt( $response, undef, 'At least we got a response.' );
110     my $respcode = substr( $response, 0, 2 );
111     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
112     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
113     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');
114     undef $response;
115     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
116     $msg->handle_patron_info( $server );
117
118     isnt( $response, undef, 'At least we got a response.' );
119     $respcode = substr( $response, 0, 2 );
120     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
121     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
122     is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
123     $schema->storage->txn_rollback;
124
125 };
126 # Here is room for some more subtests
127
128 # END of main code
129
130 sub test_request_patron_status_v2 {
131     my $builder = t::lib::TestBuilder->new();
132     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
133     my ( $response, $findpatron );
134     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
135
136     my $patron1 = $builder->build({
137         source => 'Borrower',
138         value  => {
139             password => hash_password( PATRON_PW ),
140         },
141     });
142     my $card1 = $patron1->{cardnumber};
143     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
144     $findpatron = $sip_patron1;
145
146     my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
147         FID_INST_ID. $branchcode. '|'.
148         FID_PATRON_ID. $card1. '|'.
149         FID_PATRON_PWD. PATRON_PW. '|';
150     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
151
152     my $server = { ils => $mocks->{ils} };
153     undef $response;
154     $msg->handle_patron_status( $server );
155
156     isnt( $response, undef, 'At least we got a response.' );
157     my $respcode = substr( $response, 0, 2 );
158     is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
159
160     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
161     check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
162     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
163     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
164     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
165     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
166
167     # Now, we pass a wrong password and verify CQ again
168     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
169         FID_INST_ID. $branchcode. '|'.
170         FID_PATRON_ID. $card1. '|'.
171         FID_PATRON_PWD. 'wrong_password'. '|';
172     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
173     undef $response;
174     $msg->handle_patron_status( $server );
175     $respcode = substr( $response, 0, 2 );
176     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
177
178     # Check empty password and verify CQ again
179     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
180         FID_INST_ID. $branchcode. '|'.
181         FID_PATRON_ID. $card1. '|'.
182         FID_PATRON_PWD. '|';
183     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
184     undef $response;
185     $msg->handle_patron_status( $server );
186     $respcode = substr( $response, 0, 2 );
187     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
188
189     # Finally, we send a wrong card number and check AE, BL
190     # This is done by removing the new patron first
191     Koha::Patrons->search({ cardnumber => $card1 })->delete;
192     undef $findpatron;
193     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
194         FID_INST_ID. $branchcode. '|'.
195         FID_PATRON_ID. $card1. '|'.
196         FID_PATRON_PWD. PATRON_PW. '|';
197     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
198     undef $response;
199     $msg->handle_patron_status( $server );
200     $respcode = substr( $response, 0, 2 );
201     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
202     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
203     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
204 }
205
206 sub test_request_patron_info_v2 {
207     my $builder = t::lib::TestBuilder->new();
208     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
209     my ( $response, $findpatron );
210     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
211
212     my $patron2 = $builder->build({
213         source => 'Borrower',
214         value  => {
215             password => hash_password( PATRON_PW ),
216         },
217     });
218     my $card = $patron2->{cardnumber};
219     my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
220     $findpatron = $sip_patron2;
221     my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
222         FID_INST_ID. $branchcode. '|'.
223         FID_PATRON_ID. $card. '|'.
224         FID_PATRON_PWD. PATRON_PW. '|';
225     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
226
227     my $server = { ils => $mocks->{ils} };
228     undef $response;
229     $msg->handle_patron_info( $server );
230     isnt( $response, undef, 'At least we got a response.' );
231     my $respcode = substr( $response, 0, 2 );
232     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
233
234     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
235     check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
236     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
237     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
238     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
239     check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
240     check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
241     check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
242     check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
243     # No check for custom fields here (unofficial PB, PC and PI)
244     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
245
246     # Test customized patron name in AE with same sip request
247     # This implicitly tests C4::SIP::ILS::Patron->name
248     $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
249     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
250     undef $response;
251     $msg->handle_patron_info( $server );
252     $respcode = substr( $response, 0, 2 );
253     check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
254
255     undef $response;
256     $server->{account}->{hide_fields} = "BD,BE,BF,PB";
257     $msg->handle_patron_info( $server );
258     $respcode = substr( $response, 0, 2 );
259     check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
260     check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
261     check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
262     check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
263     $server->{account}->{hide_fields} = "";
264
265     # Check empty password and verify CQ again
266     $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
267         FID_INST_ID. $branchcode. '|'.
268         FID_PATRON_ID. $card. '|'.
269         FID_PATRON_PWD. '|';
270     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
271     undef $response;
272     $msg->handle_patron_info( $server );
273     $respcode = substr( $response, 0, 2 );
274     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
275     # Test empty password is OK if account configured to allow
276     $server->{account}->{allow_empty_passwords} = 1;
277     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
278     undef $response;
279     $msg->handle_patron_info( $server );
280     $respcode = substr( $response, 0, 2 );
281     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
282
283     t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
284     my $patron = Koha::Patrons->find({ cardnumber => $card });
285     $patron->update({ login_attempts => 0 });
286     is( $patron->account_locked, 0, "Patron account not locked already" );
287     $msg->handle_patron_info( $server );
288     $patron = Koha::Patrons->find({ cardnumber => $card });
289     is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
290
291     # Finally, we send a wrong card number
292     Koha::Patrons->search({ cardnumber => $card })->delete;
293     undef $findpatron;
294     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
295     undef $response;
296     $msg->handle_patron_info( $server );
297     $respcode = substr( $response, 0, 2 );
298     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
299     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
300     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
301 }
302
303 sub test_checkin_v2 {
304     my $builder = t::lib::TestBuilder->new();
305     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
306     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
307     my ( $response, $findpatron );
308     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
309
310     # create some data
311     my $patron1 = $builder->build({
312         source => 'Borrower',
313         value  => {
314             password => hash_password( PATRON_PW ),
315         },
316     });
317     my $card1 = $patron1->{cardnumber};
318     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
319     $findpatron = $sip_patron1;
320     my $item = $builder->build({
321         source => 'Item',
322         value => { damaged => 0, withdrawn => 0, itemlost => 0, restricted => 0, homebranch => $branchcode, holdingbranch => $branchcode },
323     });
324     my $item_object = Koha::Items->find( $item->{itemnumber} );
325
326     my $mockILS = $mocks->{ils};
327     my $server = { ils => $mockILS, account => {} };
328     $mockILS->mock( 'institution', sub { $branchcode; } );
329     $mockILS->mock( 'supports', sub { return; } );
330     $mockILS->mock( 'checkin', sub {
331         shift;
332         return C4::SIP::ILS->checkin(@_);
333     });
334     my $today = dt_from_string;
335
336     # Checkin invalid barcode
337     Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
338     my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
339         siprequestdate( $today->clone->add( days => 1) ) .
340         FID_INST_ID . $branchcode . '|'.
341         FID_ITEM_ID . 'not_to_be_found' . '|' .
342         FID_TERMINAL_PWD . 'ignored' . '|';
343     undef $response;
344     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
345     warnings_like { $msg->handle_checkin( $server ); }
346         [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
347         'Checkin of invalid item with two warnings';
348     my $respcode = substr( $response, 0, 2 );
349     is( $respcode, CHECKIN_RESP, 'Response code fine' );
350     is( substr($response,2,1), '0', 'OK flag is false' );
351     is( substr($response,5,1), 'Y', 'Alert flag is set' );
352     check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
353
354     # Not checked out, toggle option checked_in_ok
355     $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
356         siprequestdate( $today->clone->add( days => 1) ) .
357         FID_INST_ID . $branchcode . '|'.
358         FID_ITEM_ID . $item->{barcode} . '|' .
359         FID_TERMINAL_PWD . 'ignored' . '|';
360     undef $response;
361     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
362     $msg->handle_checkin( $server );
363     $respcode = substr( $response, 0, 2 );
364     is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
365     is( substr($response,5,1), 'Y', 'Alert flag is set' );
366     check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
367     # Toggle option
368     $server->{account}->{checked_in_ok} = 1;
369     undef $response;
370     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
371     $msg->handle_checkin( $server );
372     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' );
373     is( substr($response,5,1), 'N', 'Alert flag no longer set' );
374     check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
375
376     # Move item to another holding branch to trigger CV of 04 with alert flag
377     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
378     $item_object->holdingbranch( $branchcode2 )->store();
379     undef $response;
380     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
381     $msg->handle_checkin( $server );
382     is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
383     check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
384     $item_object->holdingbranch( $branchcode )->store();
385     t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
386
387     $server->{account}->{cv_send_00_on_success} = 0;
388     undef $response;
389     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
390     $msg->handle_checkin( $server );
391     $respcode = substr( $response, 0, 2 );
392     check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
393     $server->{account}->{cv_send_00_on_success} = 1;
394     undef $response;
395     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
396     $msg->handle_checkin( $server );
397     $respcode = substr( $response, 0, 2 );
398     check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
399     $server->{account}->{checked_in_ok} = 0;
400     $server->{account}->{cv_send_00_on_success} = 0;
401
402     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
403     $server->{account}->{checked_in_ok} = 0;
404     $server->{account}->{cv_triggers_alert} = 0;
405     undef $response;
406     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
407     $msg->handle_checkin( $server );
408     $respcode = substr( $response, 0, 2 );
409     is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
410     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
411     $server->{account}->{cv_triggers_alert} = 1;
412     undef $response;
413     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
414     $msg->handle_checkin( $server );
415     $respcode = substr( $response, 0, 2 );
416     is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
417     $server->{account}->{cv_triggers_alert} = 0;
418     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
419
420     $server->{account}->{checked_in_ok} = 1;
421     $server->{account}->{ct_always_send} = 0;
422     undef $response;
423     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
424     $msg->handle_checkin( $server );
425     $respcode = substr( $response, 0, 2 );
426     check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
427     $server->{account}->{ct_always_send} = 1;
428     undef $response;
429     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
430     $msg->handle_checkin( $server );
431     $respcode = substr( $response, 0, 2 );
432     check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
433     $server->{account}->{checked_in_ok} = 0;
434     $server->{account}->{ct_always_send} = 0;
435
436     # Checkin at wrong branch: issue item and switch branch, and checkin
437     my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item->{itemnumber} })->store;
438     $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
439     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
440     undef $response;
441     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
442     $msg->handle_checkin( $server );
443     is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
444     is( substr($response,5,1), 'Y', 'Alert flag is set' );
445     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
446     $branchcode = $item->{homebranch};  # switch back
447     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
448
449     # Data corrupted: add same issue_id to old_issues
450     Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
451     undef $response;
452     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
453     warnings_like { $msg->handle_checkin( $server ); }
454         [ qr/Duplicate entry/, qr/data corrupted/ ],
455         'DBIx error on duplicate issue_id';
456     is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
457     is( substr($response,5,1), 'Y', 'Alert flag is set' );
458     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
459
460     # Finally checkin without problems (remove duplicate id)
461     Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
462     undef $response;
463     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
464     $msg->handle_checkin( $server );
465     is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
466     is( substr($response,5,1), 'N', 'Alert flag is not set' );
467     is( Koha::Checkouts->find( $issue->issue_id ), undef,
468         'Issue record is gone now' );
469 }
470
471 # Helper routines
472
473 sub create_mocks {
474     my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
475
476     # mock write_msg (imported from Sip.pm into Message.pm)
477     my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
478     $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
479
480     # mock ils object
481     my $mockILS = Test::MockObject->new;
482     $mockILS->mock( 'check_inst_id', sub {} );
483     $mockILS->mock( 'institution_id', sub { $$branchcode; } );
484     $mockILS->mock( 'find_patron', sub { $$findpatron; } );
485
486     return { ils => $mockILS, message => $mockMsg };
487 }
488
489 sub check_field {
490     my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
491     # mode: contains || equals || regex (by default: equals)
492
493     # strip fixed part; prefix to simplify next regex
494     $resp = '|'. substr( $resp, fixed_length( $code ) );
495     my $fldval;
496     if( $resp =~ /\|$fld([^\|]*)\|/ ) {
497         $fldval = $1;
498     } elsif( !defined($expr) ) { # field should not be found
499         ok( 1, $msg );
500         return;
501     } else { # test fails
502         is( 0, 1, "Code $fld not found in '$resp'?" );
503         return;
504     }
505
506     if( !$mode || $mode eq 'equals' ) { # default
507         is( $fldval, $expr, $msg );
508     } elsif( $mode eq 'regex' ) {
509         is( $fldval =~ /$expr/, 1, $msg );
510     } else { # contains
511         is( index( $fldval, $expr ) > -1, 1, $msg );
512     }
513 }
514
515 sub siprequestdate {
516     my ( $dt ) = @_;
517     return $dt->ymd('').(' 'x4).$dt->hms('');
518 }
519
520 sub fixed_length { #length of fixed fields including response code
521     return {
522       ( PATRON_STATUS_RESP )  => 37,
523       ( PATRON_INFO_RESP )    => 61,
524       ( CHECKIN_RESP )        => 24,
525     }->{$_[0]};
526 }