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

(-)a/t/db_dependent/SIP/Message.t (-11 / +135 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 67-79 subtest 'Testing Patron Status Request V2' => sub { Link Here
67
76
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 => 17;
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 214-219 sub test_request_patron_info_v2 { Link Here
214
    check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
230
    check_field( $respcode, $response, FID_SCREEN_MSG, '.+', 'But we have a screen msg', 'regex' );
215
}
231
}
216
232
233
sub test_checkin_v2 {
234
    # create some data
235
    my $patron1 = $builder->build({
236
        source => 'Borrower',
237
        value  => {
238
            password => hash_password( PATRON_PW ),
239
        },
240
    });
241
    my $card1 = $patron1->{cardnumber};
242
    my $sip_patron1 = C4::SIP::ILS::Patron->new( $card1 );
243
    $findpatron = $sip_patron1;
244
    my $item = $builder->build({
245
        source => 'Item',
246
        value => { damaged => 0, withdrawn => 0, itemlost => 0, restricted => 0, homebranch => $branchcode, holdingbranch => $branchcode },
247
    });
248
249
    my $server = { ils => $mockILS, account => {} };
250
    $mockILS->mock( 'institution', sub { $branchcode; } );
251
    $mockILS->mock( 'supports', sub { return; } );
252
    $mockILS->mock( 'checkin', sub {
253
        shift;
254
        return C4::SIP::ILS->checkin(@_);
255
    });
256
    my $today = dt_from_string;
257
258
    # Checkin invalid barcode
259
    Koha::Items->search({ barcode => 'not_to_be_found' })->delete;
260
    my $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
261
        siprequestdate( $today->clone->add( days => 1) ) .
262
        FID_INST_ID . $branchcode . '|'.
263
        FID_ITEM_ID . 'not_to_be_found' . '|' .
264
        FID_TERMINAL_PWD . 'ignored' . '|';
265
    undef $response;
266
    my $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
267
    warnings_like { $msg->handle_checkin( $server ); }
268
        [ qr/No item 'not_to_be_found'/, qr/no item found in object to resensitize/ ],
269
        'Checkin of invalid item with two warnings';
270
    my $respcode = substr( $response, 0, 2 );
271
    is( $respcode, CHECKIN_RESP, 'Response code fine' );
272
    is( substr($response,2,1), '0', 'OK flag is false' );
273
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
274
    check_field( $respcode, $response, FID_SCREEN_MSG, 'Invalid Item', 'Check screen msg', 'regex' );
275
276
    # Not checked out, toggle option checked_in_ok
277
    $siprequest = CHECKIN . 'N' . 'YYYYMMDDZZZZHHMMSS' .
278
        siprequestdate( $today->clone->add( days => 1) ) .
279
        FID_INST_ID . $branchcode . '|'.
280
        FID_ITEM_ID . $item->{barcode} . '|' .
281
        FID_TERMINAL_PWD . 'ignored' . '|';
282
    undef $response;
283
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
284
    $msg->handle_checkin( $server );
285
    $respcode = substr( $response, 0, 2 );
286
    is( substr($response,2,1), '0', 'OK flag is false when checking in an item that was not checked out' );
287
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
288
    check_field( $respcode, $response, FID_SCREEN_MSG, 'not checked out', 'Check screen msg', 'regex' );
289
    # Toggle option
290
    $server->{account}->{checked_in_ok} = 1;
291
    undef $response;
292
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
293
    $msg->handle_checkin( $server );
294
    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' );
295
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
296
    check_field( $respcode, $response, FID_SCREEN_MSG, undef, 'No screen msg' );
297
    $server->{account}->{checked_in_ok} = 0;
298
299
    # Checkin at wrong branch: issue item and switch branch, and checkin
300
    my $issue = Koha::Checkout->new({ branchcode => $branchcode, borrowernumber => $patron1->{borrowernumber}, itemnumber => $item->{itemnumber} })->store;
301
    $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
302
    t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
303
    undef $response;
304
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
305
    $msg->handle_checkin( $server );
306
    is( substr($response,2,1), '0', 'OK flag is false when we check in at the wrong branch and we do not allow it' );
307
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
308
    check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed', 'Check screen msg' );
309
    $branchcode = $item->{homebranch};  # switch back
310
    t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
311
312
    # Data corrupted: add same issue_id to old_issues
313
    Koha::Old::Checkout->new({ issue_id => $issue->issue_id })->store;
314
    undef $response;
315
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
316
    warnings_like { $msg->handle_checkin( $server ); }
317
        [ qr/Duplicate entry/, qr/data corrupted/ ],
318
        'DBIx error on duplicate issue_id';
319
    is( substr($response,2,1), '0', 'OK flag is false when we encounter data corruption in old_issues' );
320
    is( substr($response,5,1), 'Y', 'Alert flag is set' );
321
    check_field( $respcode, $response, FID_SCREEN_MSG, 'Checkin failed: data problem', 'Check screen msg' );
322
323
    # Finally checkin without problems (remove duplicate id)
324
    Koha::Old::Checkouts->search({ issue_id => $issue->issue_id })->delete;
325
    undef $response;
326
    $msg = C4::SIP::Sip::MsgType->new( $siprequest, 0 );
327
    $msg->handle_checkin( $server );
328
    is( substr($response,2,1), '1', 'OK flag is true when we checkin after removing the duplicate' );
329
    is( substr($response,5,1), 'N', 'Alert flag is not set' );
330
    is( Koha::Checkouts->find( $issue->issue_id ), undef,
331
        'Issue record is gone now' );
332
}
333
217
# Helper routines
334
# Helper routines
218
335
219
sub check_field {
336
sub check_field {
Lines 225-230 sub check_field { Link Here
225
    my $fldval;
342
    my $fldval;
226
    if( $resp =~ /\|$fld([^\|]*)\|/ ) {
343
    if( $resp =~ /\|$fld([^\|]*)\|/ ) {
227
        $fldval = $1;
344
        $fldval = $1;
345
    } elsif( !defined($expr) ) { # field should not be found
346
        ok( 1, $msg );
347
        return;
228
    } else { # test fails
348
    } else { # test fails
229
        is( 0, 1, "Code $fld not found in '$resp'?" );
349
        is( 0, 1, "Code $fld not found in '$resp'?" );
230
        return;
350
        return;
Lines 238-240 sub check_field { Link Here
238
        is( index( $fldval, $expr ) > -1, 1, $msg );
358
        is( index( $fldval, $expr ) > -1, 1, $msg );
239
    }
359
    }
240
}
360
}
241
- 
361
362
sub siprequestdate {
363
    my ( $dt ) = @_;
364
    return $dt->ymd('').(' 'x4).$dt->hms('');
365
}

Return to bug 18996