3 # Tests for SIP::Sip::MsgType
4 # Please help to extend it!
6 # This file is part of Koha.
8 # Copyright 2016 Rijksmuseum
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.
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.
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>.
24 use Test::More tests => 3;
30 use t::lib::TestBuilder;
33 use Koha::AuthUtils qw(hash_password);
37 use Koha::Old::Checkouts;
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;
46 use constant PATRON_PW => 'do_not_ever_use_this_one';
49 subtest 'Testing Patron Status Request V2' => sub {
50 my $schema = Koha::Database->new->schema;
51 $schema->storage->txn_begin;
53 $C4::SIP::Sip::protocol_version = 2;
54 test_request_patron_status_v2();
55 $schema->storage->txn_rollback;
58 subtest 'Testing Patron Info Request V2' => sub {
59 my $schema = Koha::Database->new->schema;
60 $schema->storage->txn_begin;
62 $C4::SIP::Sip::protocol_version = 2;
63 test_request_patron_info_v2();
64 $schema->storage->txn_rollback;
67 subtest 'Checkin V2' => sub {
68 my $schema = Koha::Database->new->schema;
69 $schema->storage->txn_begin;
71 $C4::SIP::Sip::protocol_version = 2;
73 $schema->storage->txn_rollback;
76 # Here is room for some more subtests
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 );
86 my $patron1 = $builder->build({
89 password => hash_password( PATRON_PW ),
92 my $card1 = $patron1->{cardnumber};
93 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
94 $findpatron = $sip_patron1;
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 );
102 my $server = { ils => $mocks->{ils} };
104 $msg->handle_patron_status( $server );
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' );
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' );
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 );
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' );
128 # Check empty password and verify CQ again
129 $siprequest = PATRON_STATUS_REQ. 'engYYYYMMDDZZZZHHMMSS'.
130 FID_INST_ID. $branchcode. '|'.
131 FID_PATRON_ID. $card1. '|'.
133 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
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' );
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;
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 );
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' );
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 );
162 my $patron2 = $builder->build({
163 source => 'Borrower',
165 password => hash_password( PATRON_PW ),
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 );
177 my $server = { ils => $mocks->{ils} };
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' );
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' );
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 );
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' );
205 # Check empty password and verify CQ again
206 $siprequest = PATRON_INFO. 'engYYYYMMDDZZZZHHMMSS'.'Y '.
207 FID_INST_ID. $branchcode. '|'.
208 FID_PATRON_ID. $card. '|'.
210 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
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 );
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' );
223 # Finally, we send a wrong card number
224 Koha::Patrons->search({ cardnumber => $card })->delete;
226 $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
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' );
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 );
242 my $patron1 = $builder->build({
243 source => 'Borrower',
245 password => hash_password( PATRON_PW ),
248 my $card1 = $patron1->{cardnumber};
249 my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
250 $findpatron = $sip_patron1;
251 my $item = $builder->build({
253 value => { damaged => 0, withdrawn => 0, itemlost => 0, restricted => 0, homebranch => $branchcode, holdingbranch => $branchcode },
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 {
262 return C4::SIP::ILS->checkin(@_);
264 my $today = dt_from_string;
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' . '|';
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' );
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' . '|';
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' );
298 $server->{account}->{checked_in_ok} = 1;
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;
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' );
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' );
320 # Data corrupted: add same issue_id to old_issues
321 Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
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' );
331 # Finally checkin without problems (remove duplicate id)
332 Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
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' );
345 my ( $response, $findpatron, $branchcode ) = @_; # referenced variables !
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
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; } );
357 return { ils => $mockILS, message => $mockMsg };
361 my ( $code, $resp, $fld, $expr, $msg, $mode ) = @_;
362 # mode: contains || equals || regex (by default: equals)
364 # strip fixed part; prefix to simplify next regex
365 $resp = '|'. substr( $resp, fixed_length( $code ) );
367 if( $resp =~ /\|$fld([^\|]*)\|/ ) {
369 } elsif( !defined($expr) ) { # field should not be found
372 } else { # test fails
373 is( 0, 1, "Code $fld not found in '$resp'?" );
377 if( !$mode || $mode eq 'equals' ) { # default
378 is( $fldval, $expr, $msg );
379 } elsif( $mode eq 'regex' ) {
380 is( $fldval =~ /$expr/, 1, $msg );
382 is( index( $fldval, $expr ) > -1, 1, $msg );
388 return $dt->ymd('').(' 'x4).$dt->hms('');
391 sub fixed_length { #length of fixed fields including response code
393 ( PATRON_STATUS_RESP ) => 37,
394 ( PATRON_INFO_RESP ) => 61,
395 ( CHECKIN_RESP ) => 24,