0ac7e6c4b65c2c12dd8e0253084e477e5362ebd6
[srvgit] / t / db_dependent / rollingloans.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Context;
5 use C4::Circulation;
6 use C4::Members;
7 use C4::Items;
8 use Koha::DateUtils;
9 use Koha::Patrons;
10 use t::lib::TestBuilder;
11 use t::lib::Mocks qw(mock_preference);
12
13 use Test::More tests => 8;
14
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17
18 C4::Context->_new_userenv(1234567);
19 C4::Context->set_userenv(91, 'CLIstaff', '23529001223661', 'CPL',
20                          'CPL', 'CPL', '', 'cc@cscnet.co.uk');
21
22 t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems',0);
23 my $test_patron = '23529001223651';
24 my $test_item_fic = '502326000402';
25 my $test_item_24 = '502326000404';
26 my $test_item_48 = '502326000403';
27
28 my $builder = t::lib::TestBuilder->new;
29 my $borrower1 = $builder->build_object({ class => 'Koha::Patrons', value => { cardnumber => $test_patron } });
30 my $item1 = $builder->build_object({
31     class => 'Koha::Items',
32     value => {
33         barcode => $test_item_fic,
34     }
35 });
36 my $item2 = $builder->build_object({
37     class => 'Koha::Items',
38     value => {
39         barcode => $test_item_24,
40     }
41 });
42 my $item3 = $builder->build_object({
43     class => 'Koha::Items',
44     value => {
45         barcode => $test_item_48,
46     }
47 });
48
49 SKIP: {
50     skip 'Missing test borrower or item, skipping tests', 8
51       unless ( defined $borrower1 && defined $item1 );
52
53     for my $item_barcode ( $test_item_fic, $test_item_24, $test_item_48 ) {
54         my $duedate = try_issue( $test_patron, $item_barcode );
55         isa_ok( $duedate, 'DateTime' );
56         if ( $item_barcode eq $test_item_fic ) {
57             is( $duedate->hour(),   23, "daily loan hours = 23" );
58             is( $duedate->minute(), 59, "daily loan mins = 59" );
59         }
60         my $ret_ok = try_return($item_barcode);
61         is( $ret_ok, 1, 'Return succeeded' );
62     }
63 }
64
65 sub try_issue {
66     my ($cardnumber, $item ) = @_;
67     my $issuedate = '2011-05-16';
68     my $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
69     my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued( $patron, $item );
70     my $issue = AddIssue($patron->unblessed, $item, undef, 0, $issuedate);
71     return dt_from_string( $issue->date_due );
72 }
73
74 sub try_return {
75     my $barcode = shift;
76     my ($ret, $messages, $iteminformation, $borrower) = AddReturn($barcode);
77     return $ret;
78 }