bug 2503: tests for C4::Circulation
[koha.git] / t / lib / KohaTest / Circulation.pm
1 package KohaTest::Circulation;
2 use base qw( KohaTest );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Circulation;
10 sub testing_class { 'C4::Circulation' };
11
12
13 sub methods : Test( 1 ) {
14     my $self = shift;
15     my @methods = qw( barcodedecode 
16                       decode 
17                       transferbook 
18                       TooMany 
19                       itemissues 
20                       CanBookBeIssued 
21                       AddIssue 
22                       GetLoanLength 
23                       GetIssuingRule 
24                       GetBranchBorrowerCircRule
25                       AddReturn 
26                       MarkIssueReturned 
27                       FixOverduesOnReturn 
28                       FixAccountForLostAndReturned 
29                       GetItemIssue 
30                       GetItemIssues 
31                       GetBiblioIssues 
32                       GetUpcomingDueIssues
33                       CanBookBeRenewed 
34                       AddRenewal 
35                       GetRenewCount 
36                       GetIssuingCharges 
37                       AddIssuingCharge 
38                       GetTransfers 
39                       GetTransfersFromTo 
40                       DeleteTransfer 
41                       AnonymiseIssueHistory 
42                       updateWrongTransfer 
43                       UpdateHoldingbranch 
44                       CalcDateDue  
45                       CheckValidDatedue 
46                       CheckRepeatableHolidays
47                       CheckSpecialHolidays
48                       CheckRepeatableSpecialHolidays
49                       CheckValidBarcode
50                 );
51     
52     can_ok( $self->testing_class, @methods );    
53 }
54
55 =head3 setup_add_biblios
56
57 everything in the C4::Circulation really requires items, so let's do this in the setup phase.
58
59 =cut
60
61 sub setup_add_biblios : Tests( setup => 8 ) {
62     my $self = shift;
63
64     # we want to use a fresh batch of items, so clear these lists:
65     delete $self->{'items'};
66     delete $self->{'biblios'};
67
68     $self->add_biblios( add_items => 1 );
69 }
70
71
72 =head3 checkout_first_item
73
74 named parameters:
75   borrower  => borrower hashref, computed from $self->{'memberid'} if not given
76   barcode   => item barcode, barcode of $self->{'items'}[0] if not given
77   issuedate => YYYY-MM-DD of date to mark issue checked out. defaults to today.
78
79 =cut
80
81 sub checkout_first_item {
82     my $self   = shift;
83     my $params = shift;
84
85     # get passed in borrower, or default to the one in $self.
86     my $borrower = $params->{'borrower'};
87     if ( ! defined $borrower ) {
88         my $borrowernumber = $self->{'memberid'};
89         $borrower = C4::Members::GetMemberDetails( $borrowernumber );
90     }
91
92     # get the barcode passed in, or default to the first one in the items list
93     my $barcode = $params->{'barcode'};
94     if ( ! defined $barcode ) {
95         return unless $self->{'items'}[0]{'itemnumber'};
96         $barcode = $self->get_barcode_from_itemnumber( $self->{'items'}[0]{'itemnumber'} );
97     }
98
99     # get issuedate from parameters. Default to undef, which will be interpreted as today
100     my $issuedate = $params->{'issuedate'};
101
102     my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $borrower, $barcode );
103
104     my $datedue = C4::Circulation::AddIssue(
105         $borrower,    # borrower
106         $barcode,     # barcode
107         undef,        # datedue
108         undef,        # cancelreserve
109         $issuedate    # issuedate
110     );
111
112     my $issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
113
114     return $issues->{'date_due'};
115 }
116
117 =head3 get_barcode_from_itemnumber
118
119 pass in an itemnumber, returns a barcode.
120
121 Should this get moved up to KohaTest.pm? Or, is there a better alternative in C4?
122
123 =cut
124
125 sub get_barcode_from_itemnumber {
126     my $self       = shift;
127     my $itemnumber = shift;
128
129     my $sql = <<END_SQL;
130 SELECT barcode
131   FROM items
132   WHERE itemnumber = ?
133 END_SQL
134     my $dbh = C4::Context->dbh()  or return;
135     my $sth = $dbh->prepare($sql) or return;
136     $sth->execute($itemnumber) or return;
137     my ($barcode) = $sth->fetchrow_array;
138     return $barcode;
139 }
140
141 1;
142