Bug 27589: Unit test
[srvgit] / 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 => 9;
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 C4::Reserves qw(AddReserve);
33 use Koha::Database;
34 use Koha::AuthUtils qw(hash_password);
35 use Koha::DateUtils;
36 use Koha::Items;
37 use Koha::Checkouts;
38 use Koha::Old::Checkouts;
39 use Koha::Patrons;
40 use Koha::Holds;
41
42 use C4::SIP::ILS;
43 use C4::SIP::ILS::Patron;
44 use C4::SIP::Sip qw(write_msg);
45 use C4::SIP::Sip::Constants qw(:all);
46 use C4::SIP::Sip::MsgType;
47
48 use constant PATRON_PW => 'do_not_ever_use_this_one';
49
50 # START testing
51 subtest 'Testing Patron Status Request V2' => sub {
52     my $schema = Koha::Database->new->schema;
53     $schema->storage->txn_begin;
54     plan tests => 13;
55     $C4::SIP::Sip::protocol_version = 2;
56     test_request_patron_status_v2();
57     $schema->storage->txn_rollback;
58 };
59
60 subtest 'Testing Patron Info Request V2' => sub {
61     my $schema = Koha::Database->new->schema;
62     $schema->storage->txn_begin;
63     plan tests => 24;
64     $C4::SIP::Sip::protocol_version = 2;
65     test_request_patron_info_v2();
66     $schema->storage->txn_rollback;
67 };
68
69 subtest 'Checkin V2' => sub {
70     my $schema = Koha::Database->new->schema;
71     $schema->storage->txn_begin;
72     plan tests => 35;
73     $C4::SIP::Sip::protocol_version = 2;
74     test_checkin_v2();
75     $schema->storage->txn_rollback;
76 };
77
78 subtest 'Test hold_patron_bcode' => sub {
79     my $schema = Koha::Database->new->schema;
80     $schema->storage->txn_begin;
81     plan tests => 2;
82     $C4::SIP::Sip::protocol_version = 2;
83     test_hold_patron_bcode();
84     $schema->storage->txn_rollback;
85 };
86
87 subtest 'hold_patron_name() tests' => sub {
88
89     plan tests => 2;
90
91     my $schema = Koha::Database->new->schema;
92     $schema->storage->txn_begin;
93
94     my $builder = t::lib::TestBuilder->new();
95
96     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
97     my ( $response, $findpatron );
98     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
99
100     my $item = $builder->build_sample_item(
101         {
102             damaged       => 0,
103             withdrawn     => 0,
104             itemlost      => 0,
105             restricted    => 0,
106             homebranch    => $branchcode,
107             holdingbranch => $branchcode
108         }
109     );
110
111     my $server = { ils => $mocks->{ils} };
112     my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
113
114     is( $sip_item->hold_patron_name, q{}, "SIP item with no hold returns empty string for patron name" );
115
116     my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_name, $server );
117     is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
118
119     $schema->storage->txn_rollback;
120 };
121
122 subtest 'Lastseen response' => sub {
123
124     my $schema = Koha::Database->new->schema;
125     $schema->storage->txn_begin;
126
127     plan tests => 6;
128     my $builder = t::lib::TestBuilder->new();
129     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
130     my ( $response, $findpatron );
131     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
132     my $seen_patron = $builder->build({
133         source => 'Borrower',
134         value  => {
135             lastseen => '2001-01-01',
136             password => hash_password( PATRON_PW ),
137             branchcode => $branchcode,
138         },
139     });
140     my $cardnum = $seen_patron->{cardnumber};
141     my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
142     $findpatron = $sip_patron;
143
144     my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
145         FID_INST_ID. $branchcode. '|'.
146         FID_PATRON_ID. $cardnum. '|'.
147         FID_PATRON_PWD. PATRON_PW. '|';
148     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
149
150     my $server = { ils => $mocks->{ils} };
151     undef $response;
152     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '' );
153     $msg->handle_patron_info( $server );
154
155     isnt( $response, undef, 'At least we got a response.' );
156     my $respcode = substr( $response, 0, 2 );
157     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
158     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->{cardnumber} });
159     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');
160     undef $response;
161     t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '1' );
162     $msg->handle_patron_info( $server );
163
164     isnt( $response, undef, 'At least we got a response.' );
165     $respcode = substr( $response, 0, 2 );
166     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
167     $seen_patron = Koha::Patrons->find({ cardnumber => $seen_patron->cardnumber() });
168     is( output_pref({str => $seen_patron->lastseen(), dateonly => 1}), output_pref({dt => dt_from_string(), dateonly => 1}),'Last seen updated if tracking patrons');
169     $schema->storage->txn_rollback;
170
171 };
172
173 subtest "Test build_additional_item_fields_string" => sub {
174     my $schema = Koha::Database->new->schema;
175     $schema->storage->txn_begin;
176
177     plan tests => 2;
178
179     my $builder = t::lib::TestBuilder->new();
180
181     my $item = $builder->build_sample_item;
182     my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
183
184     my $server = {};
185     $server->{account}->{item_field}->{code} = 'itemnumber';
186     $server->{account}->{item_field}->{field} = 'XY';
187     my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
188     is( $attribute_string, "XY".$item->itemnumber."|", 'Attribute field generated correctly with single param' );
189
190     $server = {};
191     $server->{account}->{item_field}->[0]->{code} = 'itemnumber';
192     $server->{account}->{item_field}->[0]->{field} = 'XY';
193     $server->{account}->{item_field}->[1]->{code} = 'biblionumber';
194     $server->{account}->{item_field}->[1]->{field} = 'YZ';
195     $attribute_string = $ils_item->build_additional_item_fields_string( $server );
196     is( $attribute_string, sprintf("XY%s|YZ%s|", $item->itemnumber, $item->biblionumber), 'Attribute field generated correctly with multiple params' );
197
198     $schema->storage->txn_rollback;
199 };
200
201 subtest "Test cr_item_field" => sub {
202     plan tests => 2;
203
204     my $builder = t::lib::TestBuilder->new();
205     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
206     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
207     my ( $response, $findpatron );
208     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
209
210     # create some data
211     my $patron1 = $builder->build({
212         source => 'Borrower',
213         value  => {
214             password => hash_password( PATRON_PW ),
215         },
216     });
217     my $card1 = $patron1->{cardnumber};
218     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
219     $findpatron = $sip_patron1;
220     my $item_object = $builder->build_sample_item({
221         damaged => 0,
222         withdrawn => 0,
223         itemlost => 0,
224         restricted => 0,
225         homebranch => $branchcode,
226         holdingbranch => $branchcode,
227     });
228
229     my $mockILS = $mocks->{ils};
230     my $server = { ils => $mockILS, account => {} };
231     $mockILS->mock( 'institution', sub { $branchcode; } );
232     $mockILS->mock( 'supports', sub { return; } );
233     $mockILS->mock( 'checkin', sub {
234         shift;
235         return C4::SIP::ILS->checkin(@_);
236     });
237     my $today = dt_from_string;
238
239     my $respcode;
240
241     # Not checked out, toggle option checked_in_ok
242     my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
243         siprequestdate( $today->clone->add( days => 1) ) .
244         FID_INST_ID . $branchcode . '|'.
245         FID_ITEM_ID . $item_object->barcode . '|' .
246         FID_TERMINAL_PWD . 'ignored' . '|';
247     undef $response;
248     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
249
250     $server->{account}->{cr_item_field} = 'itemnumber';
251
252     $msg->handle_checkin( $server );
253
254     my $id = $item_object->id;
255     ok( $response =~ m/CR$id/, "Found correct CR field in response");
256
257     $siprequest = ITEM_INFORMATION . 'YYYYMMDDZZZZHHMMSS' .
258         FID_INST_ID . $branchcode . '|'.
259         FID_ITEM_ID . $item_object->barcode . '|' .
260         FID_TERMINAL_PWD . 'ignored' . '|';
261     undef $response;
262     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
263
264     $mockILS->mock( 'find_item', sub {
265         return C4::SIP::ILS::Item->new( $item_object->barcode );
266     });
267
268     $server->{account}->{cr_item_field} = 'itype';
269
270     $msg->handle_item_information( $server );
271
272     my $itype = $item_object->itype;
273     ok( $response =~ m/CR$itype/, "Found correct CR field in response");
274 };
275
276 subtest 'Patron info summary > 5 should not crash server' => sub {
277
278     my $schema = Koha::Database->new->schema;
279     $schema->storage->txn_begin;
280
281     plan tests => 22;
282     my $builder = t::lib::TestBuilder->new();
283     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
284     my ( $response, $findpatron );
285     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
286     my $seen_patron = $builder->build({
287         source => 'Borrower',
288         value  => {
289             lastseen => '2001-01-01',
290             password => hash_password( PATRON_PW ),
291             branchcode => $branchcode,
292         },
293     });
294     my $cardnum = $seen_patron->{cardnumber};
295     my $sip_patron = C4::SIP::ILS::Patron->new( $cardnum );
296     $findpatron = $sip_patron;
297
298     my @summaries = (
299         '          ',
300         'Y         ',
301         ' Y        ',
302         '  Y       ',
303         '   Y      ',
304         '    Y     ',
305         '     Y    ',
306         '      Y   ',
307         '       Y  ',
308         '        Y ',
309         '         Y',
310     );
311     for my $summary ( @summaries ) {
312         my $siprequest = PATRON_INFO . 'engYYYYMMDDZZZZHHMMSS' . $summary .
313             FID_INST_ID . $branchcode . '|' .
314             FID_PATRON_ID . $cardnum . '|' .
315             FID_PATRON_PWD . PATRON_PW . '|';
316         my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
317
318         my $server = { ils => $mocks->{ils} };
319         undef $response;
320         $msg->handle_patron_info( $server );
321
322         isnt( $response, undef, 'At least we got a response.' );
323         my $respcode = substr( $response, 0, 2 );
324         is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
325     }
326
327     $schema->storage->txn_rollback;
328 };
329
330 # Here is room for some more subtests
331
332 # END of main code
333
334 sub test_request_patron_status_v2 {
335     my $builder = t::lib::TestBuilder->new();
336     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
337     my ( $response, $findpatron );
338     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
339
340     my $patron1 = $builder->build({
341         source => 'Borrower',
342         value  => {
343             password => hash_password( PATRON_PW ),
344         },
345     });
346     my $card1 = $patron1->{cardnumber};
347     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
348     $findpatron = $sip_patron1;
349
350     my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
351         FID_INST_ID. $branchcode. '|'.
352         FID_PATRON_ID. $card1. '|'.
353         FID_PATRON_PWD. PATRON_PW. '|';
354     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
355
356     my $server = { ils => $mocks->{ils} };
357     undef $response;
358     $msg->handle_patron_status( $server );
359
360     isnt( $response, undef, 'At least we got a response.' );
361     my $respcode = substr( $response, 0, 2 );
362     is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
363
364     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
365     check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
366     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
367     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
368     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
369     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
370
371     # Now, we pass a wrong password and verify CQ again
372     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
373         FID_INST_ID. $branchcode. '|'.
374         FID_PATRON_ID. $card1. '|'.
375         FID_PATRON_PWD. 'wrong_password'. '|';
376     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
377     undef $response;
378     $msg->handle_patron_status( $server );
379     $respcode = substr( $response, 0, 2 );
380     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
381
382     # Check empty password and verify CQ again
383     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
384         FID_INST_ID. $branchcode. '|'.
385         FID_PATRON_ID. $card1. '|'.
386         FID_PATRON_PWD. '|';
387     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
388     undef $response;
389     $msg->handle_patron_status( $server );
390     $respcode = substr( $response, 0, 2 );
391     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
392
393     # Finally, we send a wrong card number and check AE, BL
394     # This is done by removing the new patron first
395     Koha::Patrons->search({ cardnumber => $card1 })->delete;
396     undef $findpatron;
397     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
398         FID_INST_ID. $branchcode. '|'.
399         FID_PATRON_ID. $card1. '|'.
400         FID_PATRON_PWD. PATRON_PW. '|';
401     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
402     undef $response;
403     $msg->handle_patron_status( $server );
404     $respcode = substr( $response, 0, 2 );
405     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
406     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
407     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
408 }
409
410 sub test_request_patron_info_v2 {
411     my $builder = t::lib::TestBuilder->new();
412     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
413     my ( $response, $findpatron );
414     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
415
416     my $patron2 = $builder->build({
417         source => 'Borrower',
418         value  => {
419             password => hash_password( PATRON_PW ),
420         },
421     });
422     my $card = $patron2->{cardnumber};
423     my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
424     $findpatron = $sip_patron2;
425     my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
426         FID_INST_ID. $branchcode. '|'.
427         FID_PATRON_ID. $card. '|'.
428         FID_PATRON_PWD. PATRON_PW. '|';
429     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
430
431     my $server = { ils => $mocks->{ils} };
432     undef $response;
433     $msg->handle_patron_info( $server );
434     isnt( $response, undef, 'At least we got a response.' );
435     my $respcode = substr( $response, 0, 2 );
436     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
437
438     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
439     check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
440     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
441     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
442     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
443     check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
444     check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
445     check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
446     check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
447     # No check for custom fields here (unofficial PB, PC and PI)
448     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
449
450     # Test customized patron name in AE with same sip request
451     # This implicitly tests C4::SIP::ILS::Patron->name
452     $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
453     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
454     undef $response;
455     $msg->handle_patron_info( $server );
456     $respcode = substr( $response, 0, 2 );
457     check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
458
459     undef $response;
460     $server->{account}->{hide_fields} = "BD,BE,BF,PB";
461     $msg->handle_patron_info( $server );
462     $respcode = substr( $response, 0, 2 );
463     check_field( $respcode, $response, FID_HOME_ADDR, undef, 'Home address successfully stripped from response' );
464     check_field( $respcode, $response, FID_EMAIL, undef, 'Email address successfully stripped from response' );
465     check_field( $respcode, $response, FID_HOME_PHONE, undef, 'Home phone successfully stripped from response' );
466     check_field( $respcode, $response, FID_PATRON_BIRTHDATE, undef, 'Date of birth successfully stripped from response' );
467     $server->{account}->{hide_fields} = "";
468
469     # Check empty password and verify CQ again
470     $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
471         FID_INST_ID. $branchcode. '|'.
472         FID_PATRON_ID. $card. '|'.
473         FID_PATRON_PWD. '|';
474     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
475     undef $response;
476     $msg->handle_patron_info( $server );
477     $respcode = substr( $response, 0, 2 );
478     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
479     # Test empty password is OK if account configured to allow
480     $server->{account}->{allow_empty_passwords} = 1;
481     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
482     undef $response;
483     $msg->handle_patron_info( $server );
484     $respcode = substr( $response, 0, 2 );
485     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
486
487     t::lib::Mocks::mock_preference( 'FailedLoginAttempts', '1' );
488     my $patron = Koha::Patrons->find({ cardnumber => $card });
489     $patron->update({ login_attempts => 0 });
490     is( $patron->account_locked, 0, "Patron account not locked already" );
491     $msg->handle_patron_info( $server );
492     $patron = Koha::Patrons->find({ cardnumber => $card });
493     is( $patron->account_locked, 0, "Patron account is not locked by patron info messages with empty password" );
494
495     # Finally, we send a wrong card number
496     Koha::Patrons->search({ cardnumber => $card })->delete;
497     undef $findpatron;
498     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
499     undef $response;
500     $msg->handle_patron_info( $server );
501     $respcode = substr( $response, 0, 2 );
502     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
503     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
504     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
505 }
506
507 sub test_checkin_v2 {
508     my $builder = t::lib::TestBuilder->new();
509     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
510     my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
511     my ( $response, $findpatron );
512     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
513
514     # create some data
515     my $patron1 = $builder->build({
516         source => 'Borrower',
517         value  => {
518             password => hash_password( PATRON_PW ),
519         },
520     });
521     my $card1 = $patron1->{cardnumber};
522     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
523     $findpatron = $sip_patron1;
524     my $item_object = $builder->build_sample_item({
525         damaged => 0,
526         withdrawn => 0,
527         itemlost => 0,
528         restricted => 0,
529         homebranch => $branchcode,
530         holdingbranch => $branchcode,
531     });
532
533     my $mockILS = $mocks->{ils};
534     my $server = { ils => $mockILS, account => {} };
535     $mockILS->mock( 'institution', sub { $branchcode; } );
536     $mockILS->mock( 'supports', sub { return; } );
537     $mockILS->mock( 'checkin', sub {
538         shift;
539         return C4::SIP::ILS->checkin(@_);
540     });
541     my $today = dt_from_string;
542
543     # Checkin invalid barcode
544     Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
545     my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
546         siprequestdate( $today->clone->add( days => 1) ) .
547         FID_INST_ID . $branchcode . '|'.
548         FID_ITEM_ID . 'not_to_be_found' . '|' .
549         FID_TERMINAL_PWD . 'ignored' . '|';
550     undef $response;
551     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
552     warnings_like { $msg->handle_checkin( $server ); }
553         [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
554         'Checkin of invalid item with two warnings';
555     my $respcode = substr( $response, 0, 2 );
556     is( $respcode, CHECKIN_RESP, 'Response code fine' );
557     is( substr($response,2,1), '0', 'OK flag is false' );
558     is( substr($response,5,1), 'Y', 'Alert flag is set' );
559     check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
560
561     # Not checked out, toggle option checked_in_ok
562     $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
563         siprequestdate( $today->clone->add( days => 1) ) .
564         FID_INST_ID . $branchcode . '|'.
565         FID_ITEM_ID . $item_object->barcode . '|' .
566         FID_TERMINAL_PWD . 'ignored' . '|';
567     undef $response;
568     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
569     $msg->handle_checkin( $server );
570     $respcode = substr( $response, 0, 2 );
571     is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
572     is( substr($response,5,1), 'Y', 'Alert flag is set' );
573     check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
574     # Toggle option
575     $server->{account}->{checked_in_ok} = 1;
576     undef $response;
577     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
578     $msg->handle_checkin( $server );
579     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' );
580     is( substr($response,5,1), 'N', 'Alert flag no longer set' );
581     check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
582
583     # Move item to another holding branch to trigger CV of 04 with alert flag
584     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
585     $item_object->holdingbranch( $branchcode2 )->store();
586     undef $response;
587     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
588     $msg->handle_checkin( $server );
589     is( substr($response,5,1), 'Y', 'Alert flag is set with check_in_ok, item is checked in but needs transfer' );
590     check_field( $respcode, $response, FID_ALERT_TYPE, '04', 'Got FID_ALERT_TYPE (CV) field with value 04 ( needs transfer )' );
591     $item_object->holdingbranch( $branchcode )->store();
592     t::lib::Mocks::mock_preference( ' AllowReturnToBranch ', 'anywhere' );
593
594     $server->{account}->{cv_send_00_on_success} = 0;
595     undef $response;
596     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
597     $msg->handle_checkin( $server );
598     $respcode = substr( $response, 0, 2 );
599     check_field( $respcode, $response, FID_ALERT_TYPE, undef, 'No FID_ALERT_TYPE (CV) field' );
600     $server->{account}->{cv_send_00_on_success} = 1;
601     undef $response;
602     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
603     $msg->handle_checkin( $server );
604     $respcode = substr( $response, 0, 2 );
605     check_field( $respcode, $response, FID_ALERT_TYPE, '00', 'FID_ALERT_TYPE (CV) field is 00' );
606     $server->{account}->{checked_in_ok} = 0;
607     $server->{account}->{cv_send_00_on_success} = 0;
608
609     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
610     $server->{account}->{checked_in_ok} = 0;
611     $server->{account}->{cv_triggers_alert} = 0;
612     undef $response;
613     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
614     $msg->handle_checkin( $server );
615     $respcode = substr( $response, 0, 2 );
616     is( substr( $response, 5, 1 ), 'Y', 'Checkin without CV triggers alert flag when cv_triggers_alert is off' );
617     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '0' );
618     $server->{account}->{cv_triggers_alert} = 1;
619     undef $response;
620     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
621     $msg->handle_checkin( $server );
622     $respcode = substr( $response, 0, 2 );
623     is( substr( $response, 5, 1 ), 'N', 'Checkin without CV does not trigger alert flag when cv_triggers_alert is on' );
624     $server->{account}->{cv_triggers_alert} = 0;
625     t::lib::Mocks::mock_preference( 'RecordLocalUseOnReturn', '1' );
626
627     $server->{account}->{checked_in_ok} = 1;
628     $server->{account}->{ct_always_send} = 0;
629     undef $response;
630     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
631     $msg->handle_checkin( $server );
632     $respcode = substr( $response, 0, 2 );
633     check_field( $respcode, $response, FID_DESTINATION_LOCATION, undef, 'No FID_DESTINATION_LOCATION (CT) field' );
634     $server->{account}->{ct_always_send} = 1;
635     undef $response;
636     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
637     $msg->handle_checkin( $server );
638     $respcode = substr( $response, 0, 2 );
639     check_field( $respcode, $response, FID_DESTINATION_LOCATION, q{}, 'FID_DESTINATION_LOCATION (CT) field is empty but present' );
640     $server->{account}->{checked_in_ok} = 0;
641     $server->{account}->{ct_always_send} = 0;
642
643     # Checkin at wrong branch: issue item and switch branch, and checkin
644     my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
645     $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
646     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
647     undef $response;
648     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
649     $msg->handle_checkin( $server );
650     is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
651     is( substr($response,5,1), 'Y', 'Alert flag is set' );
652     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
653     $branchcode = $item_object->homebranch;  # switch back
654     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
655
656     # Data corrupted: add same issue_id to old_issues
657     Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
658     undef $response;
659     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
660     warnings_like { $msg->handle_checkin( $server ); }
661         [ qr/Duplicate entry/, qr/data issues/ ],
662         'DBIx error on duplicate issue_id';
663     is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
664     is( substr($response,5,1), 'Y', 'Alert flag is set' );
665     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
666
667     # Finally checkin without problems (remove duplicate id)
668     Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
669     undef $response;
670     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
671     $msg->handle_checkin( $server );
672     is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
673     is( substr($response,5,1), 'N', 'Alert flag is not set' );
674     is( Koha::Checkouts->find( $issue->issue_id ), undef,
675         'Issue record is gone now' );
676
677     # Test account option no_holds_check that prevents items on hold from being checked in via SIP
678     $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
679     is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item is checked out");
680     Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
681     $server->{account}->{holds_block_checkin} = 1;
682     my $reserve_id = AddReserve({
683         branchcode     => $branchcode,
684         borrowernumber => $patron1->{borrowernumber},
685         biblionumber   => $item_object->biblionumber,
686         priority       => 1,
687     });
688     my $hold = Koha::Holds->find( $reserve_id );
689     is( $hold->id, $reserve_id, "Hold was created successfully" );
690     undef $response;
691     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
692     $msg->handle_checkin( $server );
693     is( substr($response,2,1), '0', 'OK flag is false when we check in an item on hold and we do not allow it' );
694     is( substr($response,5,1), 'Y', 'Alert flag is set' );
695     check_field( $respcode, $response, FID_SCREEN_MSG, 'Item is on hold, please return to circulation desk', 'Screen message is correct' );
696     is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was not checked in");
697     $hold->delete();
698     $server->{account}->{holds_block_checkin} = 0;
699
700 }
701
702 sub test_hold_patron_bcode {
703     my $builder = t::lib::TestBuilder->new();
704     my $branchcode  = $builder->build({ source => 'Branch' })->{branchcode};
705     my ( $response, $findpatron );
706     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
707
708     my $item = $builder->build_sample_item(
709         {
710             library => $branchcode
711         }
712     );
713
714     my $server = { ils => $mocks->{ils} };
715     my $sip_item = C4::SIP::ILS::Item->new( $item->barcode );
716
717     is( $sip_item->hold_patron_bcode, q{}, "SIP item with no hold returns empty string" );
718
719     my $resp .= C4::SIP::Sip::maybe_add( FID_CALL_NUMBER, $sip_item->hold_patron_bcode, $server );
720     is( $resp, q{}, "maybe_add returns empty string for SIP item with no hold returns empty string" );
721 }
722
723 # Helper routines
724
725 sub create_mocks {
726     my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
727
728     # mock write_msg (imported from Sip.pm into Message.pm)
729     my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
730     $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
731
732     # mock ils object
733     my $mockILS = Test::MockObject->new;
734     $mockILS->mock( 'check_inst_id', sub {} );
735     $mockILS->mock( 'institution_id', sub { $$branchcode; } );
736     $mockILS->mock( 'find_patron', sub { $$findpatron; } );
737
738     return { ils => $mockILS, message => $mockMsg };
739 }
740
741 sub check_field {
742     my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
743     # mode: contains || equals || regex (by default: equals)
744
745     # strip fixed part; prefix to simplify next regex
746     $resp = '|'. substr( $resp, fixed_length( $code ) );
747     my $fldval;
748     if( $resp =~ /\|$fld([^\|]*)\|/ ) {
749         $fldval = $1;
750     } elsif( !defined($expr) ) { # field should not be found
751         ok( 1, $msg );
752         return;
753     } else { # test fails
754         is( 0, 1, "Code $fld not found in '$resp'?" );
755         return;
756     }
757
758     if( !$mode || $mode eq 'equals' ) { # default
759         is( $fldval, $expr, $msg );
760     } elsif( $mode eq 'regex' ) {
761         is( $fldval =~ /$expr/, 1, $msg );
762     } else { # contains
763         is( index( $fldval, $expr ) > -1, 1, $msg );
764     }
765 }
766
767 sub siprequestdate {
768     my ( $dt ) = @_;
769     return $dt->ymd('').(' 'x4).$dt->hms('');
770 }
771
772 sub fixed_length { #length of fixed fields including response code
773     return {
774       ( PATRON_STATUS_RESP )  => 37,
775       ( PATRON_INFO_RESP )    => 61,
776       ( CHECKIN_RESP )        => 24,
777     }->{$_[0]};
778 }