View | Details | Raw Unified | Return to bug 18996
Collapse All | Expand All

(-)a/t/db_dependent/SIP/Message.t (-10 / +134 lines)
Lines 21-34 Link Here
21
# along with Koha; if not, see <http://www.gnu.org/licenses>.
21
# along with Koha; if not, see <http://www.gnu.org/licenses>.
22
22
23
use Modern::Perl;
23
use Modern::Perl;
24
use Test::More tests => 2;
24
use Test::More tests => 3;
25
use Test::MockObject;
25
use Test::MockObject;
26
use Test::MockModule;
26
use Test::MockModule;
27
use Test::Warn;
27
28
28
use Koha::Database;
29
use t::lib::Mocks;
29
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
31
32
use Koha::Database;
30
use Koha::AuthUtils qw(hash_password);
33
use Koha::AuthUtils qw(hash_password);
34
use Koha::DateUtils;
35
use Koha::Items;
36
use Koha::Checkouts;
37
use Koha::Old::Checkouts;
31
38
39
use C4::SIP::ILS;
32
use C4::SIP::ILS::Patron;
40
use C4::SIP::ILS::Patron;
33
use C4::SIP::Sip qw(write_msg);
41
use C4::SIP::Sip qw(write_msg);
34
use C4::SIP::Sip::Constants qw(:all);
42
use C4::SIP::Sip::Constants qw(:all);
Lines 36-56 use C4::SIP::Sip::MsgType; Link Here
36
44
37
use constant PATRON_PW => 'do_not_ever_use_this_one';
45
use constant PATRON_PW => 'do_not_ever_use_this_one';
38
46
39
my $fixed_length = { #length of fixed fields including response code
47
our $fixed_length = { #length of fixed fields including response code
40
    ( PATRON_STATUS_RESP ) => 37,
48
    ( PATRON_STATUS_RESP ) => 37,
41
    ( PATRON_INFO_RESP )   => 61,
49
    ( PATRON_INFO_RESP )   => 61,
50
    ( CHECKIN_RESP )       => 24,
42
};
51
};
43
52
44
my $schema = Koha::Database->new->schema;
53
our $schema = Koha::Database->new->schema;
45
my $builder = t::lib::TestBuilder->new();
54
our $builder = t::lib::TestBuilder->new();
46
55
47
# COMMON: Some common stuff for all/most subtests
56
# COMMON: Some common stuff for all/most subtests
48
my ( $response, $findpatron, $branch, $branchcode );
57
our ( $response, $findpatron, $branchcode );
58
$branchcode = $builder->build({ source => 'Branch' })->{branchcode};
49
# mock write_msg (imported from Sip.pm into Message.pm)
59
# mock write_msg (imported from Sip.pm into Message.pm)
50
my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
60
my $mockMsg = Test::MockModule->new( 'C4::SIP::Sip::MsgType' );
51
$mockMsg->mock( 'write_msg', sub { $response = $_[1]; } ); # save response
61
$mockMsg->mock( 'write_msg', sub { $response = $_[1]; } ); # save response
52
# mock ils object
62
# mock ils object
53
my $mockILS = Test::MockObject->new;
63
our $mockILS = Test::MockObject->new;
54
$mockILS->mock( 'check_inst_id', sub {} );
64
$mockILS->mock( 'check_inst_id', sub {} );
55
$mockILS->mock( 'institution_id', sub { $branchcode; } );
65
$mockILS->mock( 'institution_id', sub { $branchcode; } );
56
$mockILS->mock( 'find_patron', sub { $findpatron; } );
66
$mockILS->mock( 'find_patron', sub { $findpatron; } );
Lines 59-65 $mockILS->mock( 'find_patron', sub { $findpatron; } ); Link Here
59
subtest 'Testing Patron Status Request V2' => sub {
69
subtest 'Testing Patron Status Request V2' => sub {
60
    $schema->storage->txn_begin;
70
    $schema->storage->txn_begin;
61
    plan tests => 13;
71
    plan tests => 13;
62
    $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
63
    $C4::SIP::Sip::protocol_version = 2;
72
    $C4::SIP::Sip::protocol_version = 2;
64
    test_request_patron_status_v2();
73
    test_request_patron_status_v2();
65
    $schema->storage->txn_rollback;
74
    $schema->storage->txn_rollback;
Lines 68-79 subtest 'Testing Patron Status Request V2' => sub { Link Here
68
subtest 'Testing Patron Info Request V2' => sub {
77
subtest 'Testing Patron Info Request V2' => sub {
69
    $schema->storage->txn_begin;
78
    $schema->storage->txn_begin;
70
    plan tests => 18;
79
    plan tests => 18;
71
    $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
72
    $C4::SIP::Sip::protocol_version = 2;
80
    $C4::SIP::Sip::protocol_version = 2;
73
    test_request_patron_info_v2();
81
    test_request_patron_info_v2();
74
    $schema->storage->txn_rollback;
82
    $schema->storage->txn_rollback;
75
};
83
};
76
84
85
subtest 'Checkin V2' => sub {
86
    $schema->storage->txn_begin;
87
    plan tests => 21;
88
    $C4::SIP::Sip::protocol_version = 2;
89
    test_checkin_v2();
90
    $schema->storage->txn_rollback;
91
};
92
77
# Here is room for some more subtests
93
# Here is room for some more subtests
78
94
79
# END of main code
95
# END of main code
Lines 223-228 sub test_request_patron_info_v2 { Link Here
223
    check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
239
    check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
224
}
240
}
225
241
242
sub test_checkin_v2 {
243
    # create some data
244
    my $patron1 = $builder->build({
245
        source => 'Borrower',
246
        value  => {
247
            password => hash_password( PATRON_PW ),
248
        },
249
    });
250
    my $card1 = $patron1->{cardnumber};
251
    my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
252
    $findpatron = $sip_patron1;
253
    my $item = $builder->build({
254
        source => 'Item',
255
        value => { damaged => 0, withdrawn => 0, itemlost => 0, restricted => 0, homebranch => $branchcode, holdingbranch => $branchcode },
256
    });
257
258
    my $server = { ils => $mockILS, account => {} };
259
    $mockILS->mock( 'institution', sub { $branchcode; } );
260
    $mockILS->mock( 'supports', sub { return; } );
261
    $mockILS->mock( 'checkin', sub {
262
        shift;
263
        return C4::SIP::ILS->checkin(@_);
264
    });
265
    my $today = dt_from_string;
266
267
    # Checkin invalid barcode
268
    Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
269
    my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
270
        siprequestdate( $today->clone->add( days => 1) ) .
271
        FID_INST_ID . $branchcode . '|'.
272
        FID_ITEM_ID . 'not_to_be_found' . '|' .
273
        FID_TERMINAL_PWD . 'ignored' . '|';
274
    undef $response;
275
    my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
276
    warnings_like { $msg->handle_checkin( $server ); }
277
        [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
278
        'Checkin of invalid item with two warnings';
279
    my $respcode = substr( $response, 0, 2 );
280
    is( $respcode, CHECKIN_RESP, 'Response code fine' );
281
    is( substr($response,2,1), '0', 'OK flag is false' );
282
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
283
    check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
284
285
    # Not checked out, toggle option checked_in_ok
286
    $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
287
        siprequestdate( $today->clone->add( days => 1) ) .
288
        FID_INST_ID . $branchcode . '|'.
289
        FID_ITEM_ID . $item->{barcode} . '|' .
290
        FID_TERMINAL_PWD . 'ignored' . '|';
291
    undef $response;
292
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
293
    $msg->handle_checkin( $server );
294
    $respcode = substr( $response, 0, 2 );
295
    is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
296
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
297
    check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
298
    # Toggle option
299
    $server->{account}->{checked_in_ok} = 1;
300
    undef $response;
301
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
302
    $msg->handle_checkin( $server );
303
    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' );
304
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
305
    check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
306
    $server->{account}->{checked_in_ok} = 0;
307
308
    # Checkin at wrong branch: issue item and switch branch, and checkin
309
    my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item->{itemnumber} })->store;
310
    $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
311
    t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
312
    undef $response;
313
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
314
    $msg->handle_checkin( $server );
315
    is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
316
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
317
    check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
318
    $branchcode = $item->{homebranch};  # switch back
319
    t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
320
321
    # Data corrupted: add same issue_id to old_issues
322
    Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
323
    undef $response;
324
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
325
    warnings_like { $msg->handle_checkin( $server ); }
326
        [ qr/Duplicate entry/, qr/data corrupted/ ],
327
        'DBIx error on duplicate issue_id';
328
    is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
329
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
330
    check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
331
332
    # Finally checkin without problems (remove duplicate id)
333
    Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
334
    undef $response;
335
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
336
    $msg->handle_checkin( $server );
337
    is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
338
    is( substr($response,5,1), 'N', 'Alert flag is not set' );
339
    is( Koha::Checkouts->find( $issue->issue_id ), undef,
340
        'Issue record is gone now' );
341
}
342
226
# Helper routines
343
# Helper routines
227
344
228
sub check_field {
345
sub check_field {
Lines 234-239 sub check_field { Link Here
234
    my $fldval;
351
    my $fldval;
235
    if( $resp =~ /\|$fld([^\|]*)\|/ ) {
352
    if( $resp =~ /\|$fld([^\|]*)\|/ ) {
236
        $fldval = $1;
353
        $fldval = $1;
354
    } elsif( !defined($expr) ) { # field should not be found
355
        ok( 1, $msg );
356
        return;
237
    } else { # test fails
357
    } else { # test fails
238
        is( 0, 1, "Code $fld not found in '$resp'?" );
358
        is( 0, 1, "Code $fld not found in '$resp'?" );
239
        return;
359
        return;
Lines 247-249 sub check_field { Link Here
247
        is( index( $fldval, $expr ) > -1, 1, $msg );
367
        is( index( $fldval, $expr ) > -1, 1, $msg );
248
    }
368
    }
249
}
369
}
250
- 
370
371
sub siprequestdate {
372
    my ( $dt ) = @_;
373
    return $dt->ymd('').(' 'x4).$dt->hms('');
374
}

Return to bug 18996