Bug 10683: (follow-up) improvements to the unit tests
[koha.git] / t / db_dependent / Circulation_issue.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Koha::DateUtils;
5 use DateTime::Duration;
6 use C4::Biblio;
7 use C4::Members;
8 use C4::Branch;
9 use C4::Circulation;
10 use C4::Items;
11 use C4::Context;
12
13 use Test::More tests => 24;
14
15 BEGIN {
16     use_ok('C4::Circulation');
17 }
18 can_ok(
19     'C4::Circulation',
20     qw(AddIssue
21       AddIssuingCharge
22       AddRenewal
23       AddReturn
24       GetBiblioIssues
25       GetIssuingCharges
26       GetIssuingRule
27       GetItemIssue
28       GetItemIssues
29       GetOpenIssue
30       GetRenewCount
31       GetUpcomingDueIssues
32       )
33 );
34
35 #Start transaction
36 my $dbh = C4::Context->dbh;
37 $dbh->{RaiseError} = 1;
38 $dbh->{AutoCommit} = 0;
39
40 $dbh->do(q|DELETE FROM issues|);
41 $dbh->do(q|DELETE FROM items|);
42 $dbh->do(q|DELETE FROM borrowers|);
43 $dbh->do(q|DELETE FROM branches|);
44 $dbh->do(q|DELETE FROM categories|);
45 $dbh->do(q|DELETE FROM accountlines|);
46 $dbh->do(q|DELETE FROM issuingrules|);
47
48 #Add sample datas
49
50 #Add Dates
51
52 my $dt_today    = dt_from_string;
53 my $today       = output_pref( $dt_today, 'iso', '24hr', 1 );
54
55 my $dt_today2 = dt_from_string;
56 my $dur10 = DateTime::Duration->new( days => -10 );
57 $dt_today2->add_duration($dur10);
58 my $daysago10 = output_pref( $dt_today2, 'iso', '24hr', 1 );
59
60 #Add branch and category
61 my $samplebranch1 = {
62     add            => 1,
63     branchcode     => 'CPL',
64     branchname     => 'Sample Branch',
65     branchaddress1 => 'sample adr1',
66     branchaddress2 => 'sample adr2',
67     branchaddress3 => 'sample adr3',
68     branchzip      => 'sample zip',
69     branchcity     => 'sample city',
70     branchstate    => 'sample state',
71     branchcountry  => 'sample country',
72     branchphone    => 'sample phone',
73     branchfax      => 'sample fax',
74     branchemail    => 'sample email',
75     branchurl      => 'sample url',
76     branchip       => 'sample ip',
77     branchprinter  => undef,
78     opac_info      => 'sample opac',
79 };
80 my $samplebranch2 = {
81     add            => 1,
82     branchcode     => 'MPL',
83     branchname     => 'Sample Branch2',
84     branchaddress1 => 'sample adr1_2',
85     branchaddress2 => 'sample adr2_2',
86     branchaddress3 => 'sample adr3_2',
87     branchzip      => 'sample zip2',
88     branchcity     => 'sample city2',
89     branchstate    => 'sample state2',
90     branchcountry  => 'sample country2',
91     branchphone    => 'sample phone2',
92     branchfax      => 'sample fax2',
93     branchemail    => 'sample email2',
94     branchurl      => 'sample url2',
95     branchip       => 'sample ip2',
96     branchprinter  => undef,
97     opac_info      => 'sample opac2',
98 };
99 ModBranch($samplebranch1);
100 ModBranch($samplebranch2);
101
102 my $samplecat = {
103     categorycode          => 'CAT1',
104     description           => 'Description1',
105     enrolmentperiod       => 'Null',
106     enrolmentperioddate   => 'Null',
107     dateofbirthrequired   => 'Null',
108     finetype              => 'Null',
109     bulk                  => 'Null',
110     enrolmentfee          => 'Null',
111     overduenoticerequired => 'Null',
112     issuelimit            => 'Null',
113     reservefee            => 'Null',
114     hidelostitems         => 0,
115     category_type         => 'Null'
116 };
117 my $query =
118 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
119 $dbh->do(
120     $query, {},
121     $samplecat->{categorycode},          $samplecat->{description},
122     $samplecat->{enrolmentperiod},       $samplecat->{enrolmentperioddate},
123     $samplecat->{dateofbirthrequired},   $samplecat->{finetype},
124     $samplecat->{bulk},                  $samplecat->{enrolmentfee},
125     $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
126     $samplecat->{reservefee},            $samplecat->{hidelostitems},
127     $samplecat->{category_type}
128 );
129
130 #Add biblio and item
131 my $record = MARC::Record->new();
132 $record->append_fields(
133     MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
134 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
135
136 my @sampleitem1 = C4::Items::AddItem(
137     {
138         barcode        => 'barcode_1',
139         itemcallnumber => 'callnumber1',
140         homebranch     => $samplebranch1->{branchcode},
141         holdingbranch  => $samplebranch1->{branchcode},
142         issue          => 1,
143         reserve        => 1
144     },
145     $biblionumber
146 );
147 my $item_id1    = $sampleitem1[2];
148 my @sampleitem2 = C4::Items::AddItem(
149     {
150         barcode        => 'barcode_2',
151         itemcallnumber => 'callnumber2',
152         homebranch     => $samplebranch2->{branchcode},
153         holdingbranch  => $samplebranch2->{branchcode},
154         notforloan     => 1,
155         issue          => 1
156     },
157     $biblionumber
158 );
159 my $item_id2 = $sampleitem2[2];
160
161 #Add borrower
162 my $borrower_id1 = C4::Members::AddMember(
163     firstname    => 'firstname1',
164     surname      => 'surname1 ',
165     categorycode => $samplecat->{categorycode},
166     branchcode   => $samplebranch1->{branchcode},
167 );
168 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
169 my $borrower_id2 = C4::Members::AddMember(
170     firstname    => 'firstname2',
171     surname      => 'surname2 ',
172     categorycode => $samplecat->{categorycode},
173     branchcode   => $samplebranch2->{branchcode},
174 );
175 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
176
177 # NEED TO BE FIXED !!!
178 # The first parameter for set_userenv is the class ref
179 #my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
180 my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
181
182 C4::Context->_new_userenv('DUMMY_SESSION_ID');
183 C4::Context->set_userenv(@USERENV);
184
185 my $userenv = C4::Context->userenv
186   or BAIL_OUT("No userenv");
187
188 #Begin Tests
189
190 #Test AddIssue
191 $query = " SELECT count(*) FROM issues";
192 my $sth = $dbh->prepare($query);
193 $sth->execute;
194 my $countissue = $sth -> fetchrow_array;
195 is ($countissue ,0, "there is no issue");
196 my $datedue1 = C4::Circulation::AddIssue( $borrower_1, "code", $daysago10,0, $today, '' );
197 like(
198     $datedue1,
199     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
200     "AddRenewal returns a date"
201 );
202 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
203
204 my $datedue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
205 is( $datedue2, undef, "AddIssue returns undef if no datedue is specified" );
206 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
207
208 $sth->execute;
209 $countissue = $sth -> fetchrow_array;
210 #FIXME: Currently AddIssue doesn't add correctly issues
211 #is ($countissue,2,"2 issues have been added");
212
213 #Test AddIssuingCharge
214 $query = " SELECT count(*) FROM accountlines";
215 $sth = $dbh->prepare($query);
216 $sth->execute;
217 my $countaccount = $sth -> fetchrow_array;
218 is ($countaccount,0,"0 accountline exists");
219 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
220     1, "An issuing charge has been added" );
221 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
222 $sth->execute;
223 $countaccount = $sth -> fetchrow_array;
224 is ($countaccount,1,"1 accountline has been added");
225
226 #Test AddRenewal
227 my $datedue3 =
228   AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
229     $datedue1, $daysago10 );
230 like(
231     $datedue3,
232     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
233     "AddRenewal returns a date"
234 );
235
236 #Test GetBiblioIssues
237 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
238
239 #Test GetItemIssue
240 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
241 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
242 #is(GetItemIssue($item_id1),{},"Item1's issues");
243
244 #Test GetItemIssues
245 #FIXME: this routine currently doesn't work be
246 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
247
248 #Test GetOpenIssue
249 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
250 is( GetOpenIssue(-1), undef,
251     "With wrong parameter GetOpenIssue returns undef" );
252 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
253
254 my @renewcount;
255 #Test GetRenewCount
256 $datedue2 = C4::Circulation::AddIssue( $borrower_1, 'barcode_1' );
257 isnt( $datedue2, undef, "AddIssue does not return undef if datedue is specified" );
258 #Without anything in DB
259 @renewcount = C4::Circulation::GetRenewCount();
260 is_deeply(
261     \@renewcount,
262     [ 0, undef, 0 ], # FIXME Need to be fixed
263     "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
264 );
265 @renewcount = C4::Circulation::GetRenewCount(-1);
266 is_deeply(
267     \@renewcount,
268     [ 0, undef, 0 ], # FIXME Need to be fixed
269     "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
270 );
271 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
272 is_deeply(
273     \@renewcount,
274     [ 0, undef, 0 ], # FIXME Need to be fixed
275     "Without issuing rules and with a valid parameter, renewcount = 0, renewsallowed = undef, renewsleft = 0"
276 );
277
278 #With something in DB
279 # Add a default rule: No renewal allowed
280 $dbh->do(q|
281     INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
282     VALUES ( '*', '*', '*', 10, 0 )
283 |);
284 @renewcount = C4::Circulation::GetRenewCount();
285 is_deeply(
286     \@renewcount,
287     [ 0, 0, 0 ],
288     "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
289 );
290 @renewcount = C4::Circulation::GetRenewCount(-1);
291 is_deeply(
292     \@renewcount,
293     [ 0, 0, 0 ],
294     "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
295 );
296 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
297 is_deeply(
298     \@renewcount,
299     [ 0, 0, 0 ],
300     "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
301 );
302
303 # Add a default rule: renewal is allowed
304 $dbh->do(q|
305     UPDATE issuingrules SET renewalsallowed = 3
306 |);
307 @renewcount = C4::Circulation::GetRenewCount();
308 is_deeply(
309     \@renewcount,
310     [ 0, 3, 3 ],
311     "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
312 );
313 @renewcount = C4::Circulation::GetRenewCount(-1);
314 is_deeply(
315     \@renewcount,
316     [ 0, 3, 3 ],
317     "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
318 );
319 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
320 is_deeply(
321     \@renewcount,
322     [ 0, 3, 3 ],
323     "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
324 );
325
326 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
327     $datedue3, $daysago10 );
328 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
329 is_deeply(
330     \@renewcount,
331     [ 1, 3, 2 ],
332     "With issuing rules (renewal allowed, 2 remaining) and with a valid parameter, Getrenewcount of item1 returns 2 renews left"
333 );
334
335
336 #End transaction
337 $dbh->rollback;