Bug 16186: t/db_dependent/Circulation_Issuingrule.t is failing
[koha.git] / t / db_dependent / Circulation_Issuingrule.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Context;
5 use DateTime;
6 use Koha::DateUtils;
7 use Koha::Library;
8
9 use Test::More tests => 9;
10
11 BEGIN {
12     use_ok('C4::Circulation');
13 }
14 can_ok(
15     'C4::Circulation',
16     qw(
17       GetHardDueDate
18       GetIssuingRule
19       GetLoanLength
20       )
21 );
22
23 #Start transaction
24 my $dbh = C4::Context->dbh;
25 $dbh->{RaiseError} = 1;
26 $dbh->{AutoCommit} = 0;
27
28 $dbh->do(q|DELETE FROM issues|);
29 $dbh->do(q|DELETE FROM items|);
30 $dbh->do(q|DELETE FROM borrowers|);
31 $dbh->do(q|DELETE FROM branches|);
32 $dbh->do(q|DELETE FROM categories|);
33 $dbh->do(q|DELETE FROM issuingrules|);
34
35 #Add sample datas
36
37 #Add branch and category
38 my $samplebranch1 = {
39     branchcode     => 'SAB1',
40     branchname     => 'Sample Branch',
41     branchaddress1 => 'sample adr1',
42     branchaddress2 => 'sample adr2',
43     branchaddress3 => 'sample adr3',
44     branchzip      => 'sample zip',
45     branchcity     => 'sample city',
46     branchstate    => 'sample state',
47     branchcountry  => 'sample country',
48     branchphone    => 'sample phone',
49     branchfax      => 'sample fax',
50     branchemail    => 'sample email',
51     branchurl      => 'sample url',
52     branchip       => 'sample ip',
53     branchprinter  => undef,
54     opac_info      => 'sample opac',
55 };
56 my $samplebranch2 = {
57     branchcode     => 'SAB2',
58     branchname     => 'Sample Branch2',
59     branchaddress1 => 'sample adr1_2',
60     branchaddress2 => 'sample adr2_2',
61     branchaddress3 => 'sample adr3_2',
62     branchzip      => 'sample zip2',
63     branchcity     => 'sample city2',
64     branchstate    => 'sample state2',
65     branchcountry  => 'sample country2',
66     branchphone    => 'sample phone2',
67     branchfax      => 'sample fax2',
68     branchemail    => 'sample email2',
69     branchurl      => 'sample url2',
70     branchip       => 'sample ip2',
71     branchprinter  => undef,
72     opac_info      => 'sample opac2',
73 };
74 Koha::Library->new($samplebranch1)->store;
75 Koha::Library->new($samplebranch2)->store;
76
77 my $samplecat = {
78     categorycode          => 'CAT1',
79     description           => 'Description1',
80     enrolmentperiod       => 'Null',
81     enrolmentperioddate   => 'Null',
82     dateofbirthrequired   => 'Null',
83     finetype              => 'Null',
84     bulk                  => 'Null',
85     enrolmentfee          => 'Null',
86     overduenoticerequired => 'Null',
87     issuelimit            => 'Null',
88     reservefee            => 'Null',
89     hidelostitems         => 0,
90     category_type         => 'Null'
91 };
92 my $query =
93 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
94 $dbh->do(
95     $query, {},
96     $samplecat->{categorycode},          $samplecat->{description},
97     $samplecat->{enrolmentperiod},       $samplecat->{enrolmentperioddate},
98     $samplecat->{dateofbirthrequired},   $samplecat->{finetype},
99     $samplecat->{bulk},                  $samplecat->{enrolmentfee},
100     $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
101     $samplecat->{reservefee},            $samplecat->{hidelostitems},
102     $samplecat->{category_type}
103 );
104
105 #Begin Tests
106
107 #Test GetIssuingRule
108 my $sampleissuingrule1 = {
109     reservecharge      => '0.000000',
110     chargename         => 'Null',
111     restrictedtype     => 0,
112     accountsent        => 0,
113     maxissueqty        => 5,
114     maxonsiteissueqty  => 4,
115     finedays           => 0,
116     lengthunit         => 'Null',
117     renewalperiod      => 5,
118     norenewalbefore    => 6,
119     auto_renew         => 0,
120     issuelength        => 5,
121     chargeperiod       => 0,
122     chargeperiod_charge_at => 0,
123     rentaldiscount     => '2.000000',
124     reservesallowed    => 0,
125     hardduedate        => '2013-01-01',
126     branchcode         => $samplebranch1->{branchcode},
127     fine               => '0.000000',
128     hardduedatecompare => 5,
129     overduefinescap    => '0.000000',
130     renewalsallowed    => 0,
131     firstremind        => 0,
132     itemtype           => 'BOOK',
133     categorycode       => $samplecat->{categorycode},
134     maxsuspensiondays  => 0,
135     onshelfholds       => 0,
136     opacitemholds      => 'N',
137     cap_fine_to_replacement_price => 0,
138 };
139 my $sampleissuingrule2 = {
140     branchcode         => $samplebranch2->{branchcode},
141     categorycode       => $samplecat->{categorycode},
142     itemtype           => 'BOOK',
143     maxissueqty        => 2,
144     maxonsiteissueqty  => 1,
145     renewalsallowed    => 'Null',
146     renewalperiod      => 2,
147     norenewalbefore    => 7,
148     auto_renew         => 0,
149     reservesallowed    => 'Null',
150     issuelength        => 2,
151     lengthunit         => 'Null',
152     hardduedate        => 2,
153     hardduedatecompare => 'Null',
154     fine               => 'Null',
155     finedays           => 'Null',
156     firstremind        => 'Null',
157     chargeperiod       => 'Null',
158     chargeperiod_charge_at => 0,
159     rentaldiscount     => 2.00,
160     overduefinescap    => 'Null',
161     accountsent        => 'Null',
162     reservecharge      => 'Null',
163     chargename         => 'Null',
164     restrictedtype     => 'Null',
165     maxsuspensiondays  => 0,
166     onshelfholds       => 1,
167     opacitemholds      => 'Y',
168     cap_fine_to_replacement_price => 0,
169 };
170 my $sampleissuingrule3 = {
171     branchcode         => $samplebranch1->{branchcode},
172     categorycode       => $samplecat->{categorycode},
173     itemtype           => 'DVD',
174     maxissueqty        => 3,
175     maxonsiteissueqty  => 2,
176     renewalsallowed    => 'Null',
177     renewalperiod      => 3,
178     norenewalbefore    => 8,
179     auto_renew         => 0,
180     reservesallowed    => 'Null',
181     issuelength        => 3,
182     lengthunit         => 'Null',
183     hardduedate        => 3,
184     hardduedatecompare => 'Null',
185     fine               => 'Null',
186     finedays           => 'Null',
187     firstremind        => 'Null',
188     chargeperiod       => 'Null',
189     chargeperiod_charge_at => 0,
190     rentaldiscount     => 3.00,
191     overduefinescap    => 'Null',
192     accountsent        => 'Null',
193     reservecharge      => 'Null',
194     chargename         => 'Null',
195     restrictedtype     => 'Null',
196     maxsuspensiondays  => 0,
197     onshelfholds       => 1,
198     opacitemholds      => 'F',
199     cap_fine_to_replacement_price => 0,
200 };
201
202 $query = 'INSERT INTO issuingrules (
203                 branchcode,
204                 categorycode,
205                 itemtype,
206                 maxissueqty,
207                 maxonsiteissueqty,
208                 renewalsallowed,
209                 renewalperiod,
210                 norenewalbefore,
211                 auto_renew,
212                 reservesallowed,
213                 issuelength,
214                 lengthunit,
215                 hardduedate,
216                 hardduedatecompare,
217                 fine,
218                 finedays,
219                 firstremind,
220                 chargeperiod,
221                 chargeperiod_charge_at,
222                 rentaldiscount,
223                 overduefinescap,
224                 accountsent,
225                 reservecharge,
226                 chargename,
227                 restrictedtype,
228                 maxsuspensiondays,
229                 onshelfholds,
230                 opacitemholds,
231                 cap_fine_to_replacement_price
232                 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
233 my $sth = $dbh->prepare($query);
234 $sth->execute(
235     $sampleissuingrule1->{branchcode},
236     $sampleissuingrule1->{categorycode},
237     $sampleissuingrule1->{itemtype},
238     $sampleissuingrule1->{maxissueqty},
239     $sampleissuingrule1->{maxonsiteissueqty},
240     $sampleissuingrule1->{renewalsallowed},
241     $sampleissuingrule1->{renewalperiod},
242     $sampleissuingrule1->{norenewalbefore},
243     $sampleissuingrule1->{auto_renew},
244     $sampleissuingrule1->{reservesallowed},
245     $sampleissuingrule1->{issuelength},
246     $sampleissuingrule1->{lengthunit},
247     $sampleissuingrule1->{hardduedate},
248     $sampleissuingrule1->{hardduedatecompare},
249     $sampleissuingrule1->{fine},
250     $sampleissuingrule1->{finedays},
251     $sampleissuingrule1->{firstremind},
252     $sampleissuingrule1->{chargeperiod},
253     $sampleissuingrule1->{chargeperiod_charge_at},
254     $sampleissuingrule1->{rentaldiscount},
255     $sampleissuingrule1->{overduefinescap},
256     $sampleissuingrule1->{accountsent},
257     $sampleissuingrule1->{reservecharge},
258     $sampleissuingrule1->{chargename},
259     $sampleissuingrule1->{restrictedtype},
260     $sampleissuingrule1->{maxsuspensiondays},
261     $sampleissuingrule1->{onshelfholds},
262     $sampleissuingrule1->{opacitemholds},
263     $sampleissuingrule1->{cap_fine_to_replacement_price},
264 );
265 $sth->execute(
266     $sampleissuingrule2->{branchcode},
267     $sampleissuingrule2->{categorycode},
268     $sampleissuingrule2->{itemtype},
269     $sampleissuingrule2->{maxissueqty},
270     $sampleissuingrule2->{maxonsiteissueqty},
271     $sampleissuingrule2->{renewalsallowed},
272     $sampleissuingrule2->{renewalperiod},
273     $sampleissuingrule2->{norenewalbefore},
274     $sampleissuingrule2->{auto_renew},
275     $sampleissuingrule2->{reservesallowed},
276     $sampleissuingrule2->{issuelength},
277     $sampleissuingrule2->{lengthunit},
278     $sampleissuingrule2->{hardduedate},
279     $sampleissuingrule2->{hardduedatecompare},
280     $sampleissuingrule2->{fine},
281     $sampleissuingrule2->{finedays},
282     $sampleissuingrule2->{firstremind},
283     $sampleissuingrule2->{chargeperiod},
284     $sampleissuingrule2->{chargeperiod_charge_at},
285     $sampleissuingrule2->{rentaldiscount},
286     $sampleissuingrule2->{overduefinescap},
287     $sampleissuingrule2->{accountsent},
288     $sampleissuingrule2->{reservecharge},
289     $sampleissuingrule2->{chargename},
290     $sampleissuingrule2->{restrictedtype},
291     $sampleissuingrule2->{maxsuspensiondays},
292     $sampleissuingrule2->{onshelfholds},
293     $sampleissuingrule2->{opacitemholds},
294     $sampleissuingrule2->{cap_fine_to_replacement_price},
295 );
296 $sth->execute(
297     $sampleissuingrule3->{branchcode},
298     $sampleissuingrule3->{categorycode},
299     $sampleissuingrule3->{itemtype},
300     $sampleissuingrule3->{maxissueqty},
301     $sampleissuingrule3->{maxonsiteissueqty},
302     $sampleissuingrule3->{renewalsallowed},
303     $sampleissuingrule3->{renewalperiod},
304     $sampleissuingrule3->{norenewalbefore},
305     $sampleissuingrule3->{auto_renew},
306     $sampleissuingrule3->{reservesallowed},
307     $sampleissuingrule3->{issuelength},
308     $sampleissuingrule3->{lengthunit},
309     $sampleissuingrule3->{hardduedate},
310     $sampleissuingrule3->{hardduedatecompare},
311     $sampleissuingrule3->{fine},
312     $sampleissuingrule3->{finedays},
313     $sampleissuingrule3->{firstremind},
314     $sampleissuingrule3->{chargeperiod},
315     $sampleissuingrule3->{chargeperiod_charge_at},
316     $sampleissuingrule3->{rentaldiscount},
317     $sampleissuingrule3->{overduefinescap},
318     $sampleissuingrule3->{accountsent},
319     $sampleissuingrule3->{reservecharge},
320     $sampleissuingrule3->{chargename},
321     $sampleissuingrule3->{restrictedtype},
322     $sampleissuingrule3->{maxsuspensiondays},
323     $sampleissuingrule3->{onshelfholds},
324     $sampleissuingrule3->{opacitemholds},
325     $sampleissuingrule3->{cap_fine_to_replacement_price},
326 );
327
328 is_deeply(
329     GetIssuingRule(
330         $samplecat->{categorycode},
331         'Book', $samplebranch1->{branchcode}
332     ),
333     $sampleissuingrule1,
334     "GetIssuingCharge returns issuingrule1's informations"
335 );
336
337 #Test GetLoanLength
338 is_deeply(
339     C4::Circulation::GetLoanLength(
340         $samplecat->{categorycode},
341         'BOOK', $samplebranch1->{branchcode}
342     ),
343     { issuelength => 5, lengthunit => 'Null', renewalperiod => 5 },
344     "GetLoanLength"
345 );
346 is_deeply(
347     C4::Circulation::GetLoanLength(),
348     {
349         issuelength   => 21,
350         renewalperiod => 21,
351         lengthunit    => 'days',
352     },
353     "Without parameters, GetLoanLength returns hardcoded values"
354 );
355 is_deeply(
356     C4::Circulation::GetLoanLength( -1, -1 ),
357     {
358         issuelength   => 21,
359         renewalperiod => 21,
360         lengthunit    => 'days',
361     },
362     "With wrong parameters, GetLoanLength returns hardcoded values"
363 );
364 is_deeply(
365     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
366     {
367         issuelength   => 21,
368         renewalperiod => 21,
369         lengthunit    => 'days',
370     },
371     "With only one parameter, GetLoanLength returns hardcoded values"
372 );    #NOTE : is that really what is expected?
373 is_deeply(
374     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK' ),
375     {
376         issuelength   => 21,
377         renewalperiod => 21,
378         lengthunit    => 'days',
379     },
380     "With only one parameter, GetLoanLength returns hardcoded values"
381 );    #NOTE : is that really what is expected?
382
383 #Test GetHardDueDate
384 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
385     'BOOK', $samplebranch1->{branchcode} );
386 is_deeply(
387     \@hardduedate,
388     [
389         dt_from_string( $sampleissuingrule1->{hardduedate}, 'iso' ),
390         $sampleissuingrule1->{hardduedatecompare}
391     ],
392     "GetHardDueDate returns the duedate and the duedatecompare"
393 );
394
395 #End transaction
396 $dbh->rollback;