b3a1ff803f40c6288914e5f823d3f79ba8abb5c9
[koha.git] / t / db_dependent / 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                       ReturnLostItem
51                 );
52     
53     can_ok( $self->testing_class, @methods );    
54 }
55
56 =head3 setup_add_biblios
57
58 everything in the C4::Circulation really requires items, so let's do this in the setup phase.
59
60 =cut
61
62 sub setup_add_biblios : Tests( setup => 8 ) {
63     my $self = shift;
64
65     # we want to use a fresh batch of items, so clear these lists:
66     delete $self->{'items'};
67     delete $self->{'biblios'};
68
69     $self->add_biblios( add_items => 1 );
70 }
71
72
73 =head3 checkout_first_item
74
75 named parameters:
76   borrower  => borrower hashref, computed from $self->{'memberid'} if not given
77   barcode   => item barcode, barcode of $self->{'items'}[0] if not given
78   issuedate => YYYY-MM-DD of date to mark issue checked out. defaults to today.
79
80 =cut
81
82 sub checkout_first_item {
83     my $self   = shift;
84     my $params = shift;
85
86     # get passed in borrower, or default to the one in $self.
87     my $borrower = $params->{'borrower'};
88     if ( ! defined $borrower ) {
89         my $borrowernumber = $self->{'memberid'};
90         $borrower = C4::Members::GetMemberDetails( $borrowernumber );
91     }
92
93     # get the barcode passed in, or default to the first one in the items list
94     my $barcode = $params->{'barcode'};
95     if ( ! defined $barcode ) {
96         return unless $self->{'items'}[0]{'itemnumber'};
97         $barcode = $self->get_barcode_from_itemnumber( $self->{'items'}[0]{'itemnumber'} );
98     }
99
100     # get issuedate from parameters. Default to undef, which will be interpreted as today
101     my $issuedate = $params->{'issuedate'};
102
103     my ( $issuingimpossible, $needsconfirmation ) = C4::Circulation::CanBookBeIssued( $borrower, $barcode );
104
105     my $datedue = C4::Circulation::AddIssue(
106         $borrower,    # borrower
107         $barcode,     # barcode
108         undef,        # datedue
109         undef,        # cancelreserve
110         $issuedate    # issuedate
111     );
112
113     my $issues = C4::Circulation::GetItemIssue( $self->{'items'}[0]{'itemnumber'} );
114
115     return $issues->{'date_due'};
116 }
117
118 =head3 get_barcode_from_itemnumber
119
120 pass in an itemnumber, returns a barcode.
121
122 Should this get moved up to KohaTest.pm? Or, is there a better alternative in C4?
123
124 =cut
125
126 sub get_barcode_from_itemnumber {
127     my $self       = shift;
128     my $itemnumber = shift;
129
130     my $sql = <<END_SQL;
131 SELECT barcode
132   FROM items
133   WHERE itemnumber = ?
134 END_SQL
135     my $dbh = C4::Context->dbh()  or return;
136     my $sth = $dbh->prepare($sql) or return;
137     $sth->execute($itemnumber) or return;
138     my ($barcode) = $sth->fetchrow_array;
139     return $barcode;
140 }
141
142 1;
143