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