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