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