Bug 18797: Create the biblioitem entry
[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::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         biblionumber => $builder->build( { source => 'Biblioitem' } )->{biblionumber},
35     }
36 });
37 my $item2 = $builder->build_object({
38     class => 'Koha::Items',
39     value => {
40         barcode => $test_item_24,
41         biblionumber => $builder->build( { source => 'Biblioitem' } )->{biblionumber},
42     }
43 });
44 my $item3 = $builder->build_object({
45     class => 'Koha::Items',
46     value => {
47         barcode => $test_item_48,
48         biblionumber => $builder->build( { source => 'Biblioitem' } )->{biblionumber},
49     }
50 });
51
52 SKIP: {
53     skip 'Missing test borrower or item, skipping tests', 8
54       unless ( defined $borrower1 && defined $item1 );
55
56     for my $item_barcode ( $test_item_fic, $test_item_24, $test_item_48 ) {
57         my $duedate = try_issue( $test_patron, $item_barcode );
58         isa_ok( $duedate, 'DateTime' );
59         if ( $item_barcode eq $test_item_fic ) {
60             is( $duedate->hour(),   23, "daily loan hours = 23" );
61             is( $duedate->minute(), 59, "daily loan mins = 59" );
62         }
63         my $ret_ok = try_return($item_barcode);
64         is( $ret_ok, 1, 'Return succeeded' );
65     }
66 }
67
68 sub try_issue {
69     my ($cardnumber, $item ) = @_;
70     my $issuedate = '2011-05-16';
71     my $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
72     my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued( $patron, $item );
73     my $issue = AddIssue($patron->unblessed, $item, undef, 0, $issuedate);
74     return dt_from_string( $issue->date_due );
75 }
76
77 sub try_return {
78     my $barcode = shift;
79     my ($ret, $messages, $iteminformation, $borrower) = AddReturn($barcode);
80     return $ret;
81 }