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