Bug 9016: (follow-up) fix 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 => 25;
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 => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 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 => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 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, 'barcode_1', $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 is ($countissue,1,"1 issues have been added");
211
212 #Test AddIssuingCharge
213 $query = " SELECT count(*) FROM accountlines";
214 $sth = $dbh->prepare($query);
215 $sth->execute;
216 my $countaccount = $sth -> fetchrow_array;
217 is ($countaccount,0,"0 accountline exists");
218 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
219     1, "An issuing charge has been added" );
220 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
221 $sth->execute;
222 $countaccount = $sth -> fetchrow_array;
223 is ($countaccount,1,"1 accountline has been added");
224
225 #Test AddRenewal
226 my $datedue3 =
227   AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
228     $datedue1, $daysago10 );
229 like(
230     $datedue3,
231     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
232     "AddRenewal returns a date"
233 );
234
235 #Test GetBiblioIssues
236 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
237
238 #Test GetItemIssue
239 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
240 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
241 #is(GetItemIssue($item_id1),{},"Item1's issues");
242
243 #Test GetItemIssues
244 #FIXME: this routine currently doesn't work be
245 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
246
247 #Test GetOpenIssue
248 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
249 is( GetOpenIssue(-1), undef,
250     "With wrong parameter GetOpenIssue returns undef" );
251 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
252
253 my @renewcount;
254 #Test GetRenewCount
255 $datedue2 = C4::Circulation::AddIssue( $borrower_1, 'barcode_1' );
256 isnt( $datedue2, undef, "AddIssue does not return undef if datedue is specified" );
257 #Without anything in DB
258 @renewcount = C4::Circulation::GetRenewCount();
259 is_deeply(
260     \@renewcount,
261     [ 0, undef, 0 ], # FIXME Need to be fixed
262     "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
263 );
264 @renewcount = C4::Circulation::GetRenewCount(-1);
265 is_deeply(
266     \@renewcount,
267     [ 0, undef, 0 ], # FIXME Need to be fixed
268     "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
269 );
270 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
271 is_deeply(
272     \@renewcount,
273     [ 2, undef, 0 ],
274     "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
275 );
276
277 #With something in DB
278 # Add a default rule: No renewal allowed
279 $dbh->do(q|
280     INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
281     VALUES ( '*', '*', '*', 10, 0 )
282 |);
283 @renewcount = C4::Circulation::GetRenewCount();
284 is_deeply(
285     \@renewcount,
286     [ 0, 0, 0 ],
287     "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
288 );
289 @renewcount = C4::Circulation::GetRenewCount(-1);
290 is_deeply(
291     \@renewcount,
292     [ 0, 0, 0 ],
293     "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
294 );
295 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
296 is_deeply(
297     \@renewcount,
298     [ 2, 0, 0 ],
299     "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
300 );
301
302 # Add a default rule: renewal is allowed
303 $dbh->do(q|
304     UPDATE issuingrules SET renewalsallowed = 3
305 |);
306 @renewcount = C4::Circulation::GetRenewCount();
307 is_deeply(
308     \@renewcount,
309     [ 0, 3, 3 ],
310     "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
311 );
312 @renewcount = C4::Circulation::GetRenewCount(-1);
313 is_deeply(
314     \@renewcount,
315     [ 0, 3, 3 ],
316     "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
317 );
318 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
319 is_deeply(
320     \@renewcount,
321     [ 2, 3, 1 ],
322     "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
323 );
324
325 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
326     $datedue3, $daysago10 );
327 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
328 is_deeply(
329     \@renewcount,
330     [ 3, 3, 0 ],
331     "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
332 );
333
334
335 #End transaction
336 $dbh->rollback;