bug 2503: tests for C4::Circulation
[koha.git] / t / lib / KohaTest / Circulation / AddIssue.pm
1 package KohaTest::Circulation::AddIssue;
2 use base qw(KohaTest::Circulation);
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 =head2 basic_usage
10
11 basic usage of C4::Circulation::AddIssue
12
13 Note: This logic is repeated in
14 KohaTest::Circulation::checkout_first_item, but without tests. This
15 includes tests at each step to make it easier to track down what's
16 broken as we go along.
17
18 =cut
19
20 sub basic_usage : Test( 11 ) {
21     my $self = shift;
22
23     my $borrowernumber = $self->{'memberid'};
24     ok( $borrowernumber, "we're going to work with borrower: $borrowernumber" );
25
26     my $borrower = C4::Members::GetMemberDetails( $borrowernumber );
27     ok( $borrower, '...and we were able to look up that borrower' );
28     is( $borrower->{'borrowernumber'}, $borrowernumber, '...and they have the right borrowernumber' );
29
30     my $itemnumber = $self->{'items'}[0]{'itemnumber'};
31     ok( $itemnumber, "We're going to checkout itemnumber $itemnumber" );
32     my $barcode = $self->get_barcode_from_itemnumber($itemnumber);
33     ok( $barcode, "...which has barcode $barcode" );
34
35     my $before_issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
36     # Note that we can't check for $before_issues as undef because GetItemIssue always returns a populated hashref
37     ok( ! defined $before_issues->{'borrowernumber'}, '...and is not currently checked out' )
38       or diag( Data::Dumper->Dump( [ $before_issues ], [ 'before_issues' ] ) );
39
40     my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $borrower, $barcode );
41     is( scalar keys %$issuingimpossible, 0, 'the item CanBookBeIssued' )
42       or diag( Data::Dumper->Dump( [ $issuingimpossible, $needsconfirmation ], [ qw( issuingimpossible needsconfirmation ) ] ) );
43     is( scalar keys %$needsconfirmation, 0, '...and the transaction does not needsconfirmation' )
44       or diag( Data::Dumper->Dump( [ $issuingimpossible, $needsconfirmation ], [ qw( issuingimpossible needsconfirmation ) ] ) );
45
46     my $datedue = C4::Circulation::AddIssue( $borrower, $barcode );
47     {
48         local $TODO = 'AddIssue does not actually return the due date';
49         ok( $datedue, "the item has been issued and it is due: $datedue" );
50     }
51     
52     my $after_issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
53     is( $after_issues->{'borrowernumber'}, $borrowernumber, '...and now it is checked out to our borrower' )
54       or diag( Data::Dumper->Dump( [ $after_issues ], [ 'after_issues' ] ) );
55
56     my $loanlength = Date::Calc::Delta_Days( split( /-/, $after_issues->{'issuedate'} ), split( /-/, $after_issues->{'date_due'} ) );
57     ok( $loanlength, "the loanlength is $loanlength days" );
58
59     # save this here since we refer to it in set_issuedate.
60     $self->{'loanlength'} = $loanlength;
61
62 }
63
64 =head2 set_issuedate
65
66 Make sure that we can set the issuedate of an issue.
67
68 Also, since we are specifying an issuedate and not a due date, the due
69 date should be calculated from the issuedate, not today.
70
71 =cut
72
73 sub set_issuedate : Test( 7 ) {
74     my $self = shift;
75
76     my $before_issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
77     ok( ! defined $before_issues->{'borrowernumber'}, 'At this beginning, this item was not checked out.' )
78       or diag( Data::Dumper->Dump( [ $before_issues ], [ 'before_issues' ] ) );
79
80     my $issuedate = $self->random_date();
81     ok( $issuedate, "Check out an item on $issuedate" );
82     my $datedue = $self->checkout_first_item( { issuedate => $issuedate } );
83     ok( $datedue, "...and it's due on $datedue" );
84
85     my $after_issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
86     is( $after_issues->{'borrowernumber'}, $self->{'memberid'}, 'We found this item checked out to our member.' )
87       or diag( Data::Dumper->Dump( [ $after_issues ], [ 'issues' ] ) );
88     is( $after_issues->{'issuedate'}, $issuedate, "...and it was issued on $issuedate" )
89       or diag( Data::Dumper->Dump( [ $after_issues ], [ 'after_issues' ] ) );
90     
91     my $loanlength = Date::Calc::Delta_Days( split( /-/, $after_issues->{'issuedate'} ), split( /-/, $after_issues->{'date_due'} ) );
92     ok( $loanlength, "the loanlength is $loanlength days" );
93     is( $loanlength, $self->{'loanlength'} );
94 }
95
96 sub set_issuedate_on_renewal : Test( 6 ) {
97     my $self = shift;
98
99     my $before_issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
100     ok( ! defined $before_issues->{'borrowernumber'}, 'At this beginning, this item was not checked out.' )
101       or diag( Data::Dumper->Dump( [ $before_issues ], [ 'before_issues' ] ) );
102
103     my $datedue = $self->checkout_first_item( { issuedate => $self->yesterday() } );
104     ok( $datedue, "The item is checked out and it's due on $datedue" );
105
106     my $issuedate = $self->random_date();
107     ok( $issuedate, "Check out an item again on $issuedate" );
108     # This will actually be a renewal
109     $datedue = $self->checkout_first_item( { issuedate => $issuedate } );
110     ok( $datedue, "...and it's due on $datedue" );
111
112     my $after_issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
113     is( $after_issues->{'borrowernumber'}, $self->{'memberid'}, 'We found this item checked out to our member.' )
114       or diag( Data::Dumper->Dump( [ $after_issues ], [ 'issues' ] ) );
115     is( $after_issues->{'issuedate'}, $issuedate, "...and it was issued on $issuedate" )
116       or diag( Data::Dumper->Dump( [ $after_issues ], [ 'after_issues' ] ) );
117     
118 }
119
120 1;