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