Bug 18996: [QA Follow-up] Remove global variables from Message.t
[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 => 3;
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 => 18;
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 => 21;
71     $C4::SIP::Sip::protocol_version = 2;
72     test_checkin_v2();
73     $schema->storage->txn_rollback;
74 };
75
76 # Here is room for some more subtests
77
78 # END of main code
79
80 sub test_request_patron_status_v2 {
81     my $builder = t::lib::TestBuilder->new();
82     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
83     my ( $response, $findpatron );
84     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
85
86     my $patron1 = $builder->build({
87         source => 'Borrower',
88         value  => {
89             password => hash_password( PATRON_PW ),
90         },
91     });
92     my $card1 = $patron1->{cardnumber};
93     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
94     $findpatron = $sip_patron1;
95
96     my $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
97         FID_INST_ID. $branchcode. '|'.
98         FID_PATRON_ID. $card1. '|'.
99         FID_PATRON_PWD. PATRON_PW. '|';
100     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
101
102     my $server = { ils => $mocks->{ils} };
103     undef $response;
104     $msg->handle_patron_status( $server );
105
106     isnt( $response, undef, 'At least we got a response.' );
107     my $respcode = substr( $response, 0, 2 );
108     is( $respcode, PATRON_STATUS_RESP, 'Response code fine' );
109
110     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
111     check_field( $respcode, $response, FID_PATRON_ID, $card1, 'Verified patron id' );
112     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron1->{surname}, 'Verified patron name', 'contains' );
113     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
114     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
115     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'Verified non-empty screen message', 'regex' );
116
117     # Now, we pass a wrong password and verify CQ again
118     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
119         FID_INST_ID. $branchcode. '|'.
120         FID_PATRON_ID. $card1. '|'.
121         FID_PATRON_PWD. 'wrong_password'. '|';
122     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
123     undef $response;
124     $msg->handle_patron_status( $server );
125     $respcode = substr( $response, 0, 2 );
126     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'Verified code CQ for wrong pw' );
127
128     # Check empty password and verify CQ again
129     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
130         FID_INST_ID. $branchcode. '|'.
131         FID_PATRON_ID. $card1. '|'.
132         FID_PATRON_PWD. '|';
133     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
134     undef $response;
135     $msg->handle_patron_status( $server );
136     $respcode = substr( $response, 0, 2 );
137     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
138
139     # Finally, we send a wrong card number and check AE, BL
140     # This is done by removing the new patron first
141     Koha::Patrons->search({ cardnumber => $card1 })->delete;
142     undef $findpatron;
143     $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
144         FID_INST_ID. $branchcode. '|'.
145         FID_PATRON_ID. $card1. '|'.
146         FID_PATRON_PWD. PATRON_PW. '|';
147     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
148     undef $response;
149     $msg->handle_patron_status( $server );
150     $respcode = substr( $response, 0, 2 );
151     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
152     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
153     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
154 }
155
156 sub test_request_patron_info_v2 {
157     my $builder = t::lib::TestBuilder->new();
158     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
159     my ( $response, $findpatron );
160     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
161
162     my $patron2 = $builder->build({
163         source => 'Borrower',
164         value  => {
165             password => hash_password( PATRON_PW ),
166         },
167     });
168     my $card = $patron2->{cardnumber};
169     my $sip_patron2 = C4::SIP::ILS::Patron->new( $card );
170     $findpatron = $sip_patron2;
171     my $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
172         FID_INST_ID. $branchcode. '|'.
173         FID_PATRON_ID. $card. '|'.
174         FID_PATRON_PWD. PATRON_PW. '|';
175     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
176
177     my $server = { ils => $mocks->{ils} };
178     undef $response;
179     $msg->handle_patron_info( $server );
180     isnt( $response, undef, 'At least we got a response.' );
181     my $respcode = substr( $response, 0, 2 );
182     is( $respcode, PATRON_INFO_RESP, 'Response code fine' );
183
184     check_field( $respcode, $response, FID_INST_ID, $branchcode , 'Verified institution id' );
185     check_field( $respcode, $response, FID_PATRON_ID, $card, 'Verified patron id' );
186     check_field( $respcode, $response, FID_PERSONAL_NAME, $patron2->{surname}, 'Verified patron name', 'contains' );
187     check_field( $respcode, $response, FID_VALID_PATRON, 'Y', 'Verified code BL' );
188     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'Verified code CQ' );
189     check_field( $respcode, $response, FID_FEE_LMT, '.*', 'Checked existence of fee limit', 'regex' );
190     check_field( $respcode, $response, FID_HOME_ADDR, $patron2->{address}, 'Address in BD', 'contains' );
191     check_field( $respcode, $response, FID_EMAIL, $patron2->{email}, 'Verified email in BE' );
192     check_field( $respcode, $response, FID_HOME_PHONE, $patron2->{phone}, 'Verified home phone in BF' );
193     # No check for custom fields here (unofficial PB, PC and PI)
194     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'We have a screen msg', 'regex' );
195
196     # Test customized patron name in AE with same sip request
197     # This implicitly tests C4::SIP::ILS::Patron->name
198     $server->{account}->{ae_field_template} = "X[% patron.surname %]Y";
199     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
200     undef $response;
201     $msg->handle_patron_info( $server );
202     $respcode = substr( $response, 0, 2 );
203     check_field( $respcode, $response, FID_PERSONAL_NAME, 'X' . $patron2->{surname} . 'Y', 'Check customized patron name' );
204
205     # Check empty password and verify CQ again
206     $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y         '.
207         FID_INST_ID. $branchcode. '|'.
208         FID_PATRON_ID. $card. '|'.
209         FID_PATRON_PWD. '|';
210     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
211     undef $response;
212     $msg->handle_patron_info( $server );
213     $respcode = substr( $response, 0, 2 );
214     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'N', 'code CQ should be N for empty AD' );
215     # Test empty password is OK if account configured to allow
216     $server->{account}->{allow_empty_passwords} = 1;
217     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
218     undef $response;
219     $msg->handle_patron_info( $server );
220     $respcode = substr( $response, 0, 2 );
221     check_field( $respcode, $response, FID_VALID_PATRON_PWD, 'Y', 'code CQ should be Y if empty AD allowed' );
222
223     # Finally, we send a wrong card number
224     Koha::Patrons->search({ cardnumber => $card })->delete;
225     undef $findpatron;
226     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
227     undef $response;
228     $msg->handle_patron_info( $server );
229     $respcode = substr( $response, 0, 2 );
230     check_field( $respcode, $response, FID_VALID_PATRON, 'N', 'Verified code BL for wrong cardnumber' );
231     check_field( $respcode, $response, FID_PERSONAL_NAME, '', 'Name should be empty now' );
232     check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
233 }
234
235 sub test_checkin_v2 {
236     my $builder = t::lib::TestBuilder->new();
237     my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
238     my ( $response, $findpatron );
239     my $mocks = create_mocks( \$response, \$findpatron, \$branchcode );
240
241     # create some data
242     my $patron1 = $builder->build({
243         source => 'Borrower',
244         value  => {
245             password => hash_password( PATRON_PW ),
246         },
247     });
248     my $card1 = $patron1->{cardnumber};
249     my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
250     $findpatron = $sip_patron1;
251     my $item = $builder->build({
252         source => 'Item',
253         value => { damaged => 0, withdrawn => 0, itemlost => 0, restricted => 0, homebranch => $branchcode, holdingbranch => $branchcode },
254     });
255
256     my $mockILS = $mocks->{ils};
257     my $server = { ils => $mockILS, account => {} };
258     $mockILS->mock( 'institution', sub { $branchcode; } );
259     $mockILS->mock( 'supports', sub { return; } );
260     $mockILS->mock( 'checkin', sub {
261         shift;
262         return C4::SIP::ILS->checkin(@_);
263     });
264     my $today = dt_from_string;
265
266     # Checkin invalid barcode
267     Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
268     my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
269         siprequestdate( $today->clone->add( days => 1) ) .
270         FID_INST_ID . $branchcode . '|'.
271         FID_ITEM_ID . 'not_to_be_found' . '|' .
272         FID_TERMINAL_PWD . 'ignored' . '|';
273     undef $response;
274     my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
275     warnings_like { $msg->handle_checkin( $server ); }
276         [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
277         'Checkin of invalid item with two warnings';
278     my $respcode = substr( $response, 0, 2 );
279     is( $respcode, CHECKIN_RESP, 'Response code fine' );
280     is( substr($response,2,1), '0', 'OK flag is false' );
281     is( substr($response,5,1), 'Y', 'Alert flag is set' );
282     check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
283
284     # Not checked out, toggle option checked_in_ok
285     $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
286         siprequestdate( $today->clone->add( days => 1) ) .
287         FID_INST_ID . $branchcode . '|'.
288         FID_ITEM_ID . $item->{barcode} . '|' .
289         FID_TERMINAL_PWD . 'ignored' . '|';
290     undef $response;
291     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
292     $msg->handle_checkin( $server );
293     $respcode = substr( $response, 0, 2 );
294     is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
295     is( substr($response,5,1), 'Y', 'Alert flag is set' );
296     check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
297     # Toggle option
298     $server->{account}->{checked_in_ok} = 1;
299     undef $response;
300     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
301     $msg->handle_checkin( $server );
302     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' );
303     is( substr($response,5,1), 'Y', 'Alert flag is set' );
304     check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
305     $server->{account}->{checked_in_ok} = 0;
306
307     # Checkin at wrong branch: issue item and switch branch, and checkin
308     my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item->{itemnumber} })->store;
309     $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
310     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
311     undef $response;
312     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
313     $msg->handle_checkin( $server );
314     is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
315     is( substr($response,5,1), 'Y', 'Alert flag is set' );
316     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
317     $branchcode = $item->{homebranch};  # switch back
318     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
319
320     # Data corrupted: add same issue_id to old_issues
321     Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
322     undef $response;
323     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
324     warnings_like { $msg->handle_checkin( $server ); }
325         [ qr/Duplicate entry/, qr/data corrupted/ ],
326         'DBIx error on duplicate issue_id';
327     is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
328     is( substr($response,5,1), 'Y', 'Alert flag is set' );
329     check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
330
331     # Finally checkin without problems (remove duplicate id)
332     Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
333     undef $response;
334     $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
335     $msg->handle_checkin( $server );
336     is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
337     is( substr($response,5,1), 'N', 'Alert flag is not set' );
338     is( Koha::Checkouts->find( $issue->issue_id ), undef,
339         'Issue record is gone now' );
340 }
341
342 # Helper routines
343
344 sub create_mocks {
345     my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
346
347     # mock write_msg (imported from Sip.pm into Message.pm)
348     my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
349     $mockMsg->mock( 'write_msg', sub { $$response = $_[1]; } ); # save response
350
351     # mock ils object
352     my $mockILS = Test::MockObject->new;
353     $mockILS->mock( 'check_inst_id', sub {} );
354     $mockILS->mock( 'institution_id', sub { $$branchcode; } );
355     $mockILS->mock( 'find_patron', sub { $$findpatron; } );
356
357     return { ils => $mockILS, message => $mockMsg };
358 }
359
360 sub check_field {
361     my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
362     # mode: contains || equals || regex (by default: equals)
363
364     # strip fixed part; prefix to simplify next regex
365     $resp = '|'. substr( $resp, fixed_length( $code ) );
366     my $fldval;
367     if( $resp =~ /\|$fld([^\|]*)\|/ ) {
368         $fldval = $1;
369     } elsif( !defined($expr) ) { # field should not be found
370         ok( 1, $msg );
371         return;
372     } else { # test fails
373         is( 0, 1, "Code $fld not found in '$resp'?" );
374         return;
375     }
376
377     if( !$mode || $mode eq 'equals' ) { # default
378         is( $fldval, $expr, $msg );
379     } elsif( $mode eq 'regex' ) {
380         is( $fldval =~ /$expr/, 1, $msg );
381     } else { # contains
382         is( index( $fldval, $expr ) > -1, 1, $msg );
383     }
384 }
385
386 sub siprequestdate {
387     my ( $dt ) = @_;
388     return $dt->ymd('').(' 'x4).$dt->hms('');
389 }
390
391 sub fixed_length { #length of fixed fields including response code
392     return {
393       ( PATRON_STATUS_RESP )  => 37,
394       ( PATRON_INFO_RESP )    => 61,
395       ( CHECKIN_RESP )        => 24,
396     }->{$_[0]};
397 }