Bug 16344: Fix test in GetHardDueDate.t
[srvgit] / 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::IssuingRules;
8 use Koha::Library;
9
10 use Test::More tests => 10;
11
12 BEGIN {
13     use_ok('C4::Circulation');
14 }
15 can_ok(
16     'C4::Circulation',
17     qw(
18       GetHardDueDate
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     article_requests   => 'yes',
147     no_auto_renewal_after => undef,
148     no_auto_renewal_after_hard_limit => undef,
149 };
150 my $sampleissuingrule2 = {
151     branchcode         => $samplebranch2->{branchcode},
152     categorycode       => $samplecat->{categorycode},
153     itemtype           => 'BOOK',
154     maxissueqty        => 2,
155     maxonsiteissueqty  => 1,
156     renewalsallowed    => 'Null',
157     renewalperiod      => 2,
158     norenewalbefore    => 7,
159     auto_renew         => 0,
160     reservesallowed    => 'Null',
161     issuelength        => 2,
162     lengthunit         => 'days',
163     hardduedate        => 2,
164     hardduedatecompare => 'Null',
165     fine               => 'Null',
166     finedays           => 'Null',
167     firstremind        => 'Null',
168     chargeperiod       => 'Null',
169     chargeperiod_charge_at => 0,
170     rentaldiscount     => 2.00,
171     overduefinescap    => 'Null',
172     accountsent        => 'Null',
173     reservecharge      => 'Null',
174     chargename         => 'Null',
175     restrictedtype     => 'Null',
176     maxsuspensiondays  => 0,
177     onshelfholds       => 1,
178     opacitemholds      => 'Y',
179     cap_fine_to_replacement_price => 0,
180     holds_per_record   => 1,
181     article_requests   => 'yes',
182 };
183 my $sampleissuingrule3 = {
184     branchcode         => $samplebranch1->{branchcode},
185     categorycode       => $samplecat->{categorycode},
186     itemtype           => 'DVD',
187     maxissueqty        => 3,
188     maxonsiteissueqty  => 2,
189     renewalsallowed    => 'Null',
190     renewalperiod      => 3,
191     norenewalbefore    => 8,
192     auto_renew         => 0,
193     reservesallowed    => 'Null',
194     issuelength        => 3,
195     lengthunit         => 'days',
196     hardduedate        => 3,
197     hardduedatecompare => 'Null',
198     fine               => 'Null',
199     finedays           => 'Null',
200     firstremind        => 'Null',
201     chargeperiod       => 'Null',
202     chargeperiod_charge_at => 0,
203     rentaldiscount     => 3.00,
204     overduefinescap    => 'Null',
205     accountsent        => 'Null',
206     reservecharge      => 'Null',
207     chargename         => 'Null',
208     restrictedtype     => 'Null',
209     maxsuspensiondays  => 0,
210     onshelfholds       => 1,
211     opacitemholds      => 'F',
212     cap_fine_to_replacement_price => 0,
213     holds_per_record   => 1,
214     article_requests   => 'yes',
215 };
216
217 $query = 'INSERT INTO issuingrules (
218                 branchcode,
219                 categorycode,
220                 itemtype,
221                 maxissueqty,
222                 maxonsiteissueqty,
223                 renewalsallowed,
224                 renewalperiod,
225                 norenewalbefore,
226                 auto_renew,
227                 reservesallowed,
228                 issuelength,
229                 lengthunit,
230                 hardduedate,
231                 hardduedatecompare,
232                 fine,
233                 finedays,
234                 firstremind,
235                 chargeperiod,
236                 chargeperiod_charge_at,
237                 rentaldiscount,
238                 overduefinescap,
239                 accountsent,
240                 reservecharge,
241                 chargename,
242                 restrictedtype,
243                 maxsuspensiondays,
244                 onshelfholds,
245                 opacitemholds,
246                 cap_fine_to_replacement_price,
247                 article_requests
248                 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
249 my $sth = $dbh->prepare($query);
250 $sth->execute(
251     $sampleissuingrule1->{branchcode},
252     $sampleissuingrule1->{categorycode},
253     $sampleissuingrule1->{itemtype},
254     $sampleissuingrule1->{maxissueqty},
255     $sampleissuingrule1->{maxonsiteissueqty},
256     $sampleissuingrule1->{renewalsallowed},
257     $sampleissuingrule1->{renewalperiod},
258     $sampleissuingrule1->{norenewalbefore},
259     $sampleissuingrule1->{auto_renew},
260     $sampleissuingrule1->{reservesallowed},
261     $sampleissuingrule1->{issuelength},
262     $sampleissuingrule1->{lengthunit},
263     $sampleissuingrule1->{hardduedate},
264     $sampleissuingrule1->{hardduedatecompare},
265     $sampleissuingrule1->{fine},
266     $sampleissuingrule1->{finedays},
267     $sampleissuingrule1->{firstremind},
268     $sampleissuingrule1->{chargeperiod},
269     $sampleissuingrule1->{chargeperiod_charge_at},
270     $sampleissuingrule1->{rentaldiscount},
271     $sampleissuingrule1->{overduefinescap},
272     $sampleissuingrule1->{accountsent},
273     $sampleissuingrule1->{reservecharge},
274     $sampleissuingrule1->{chargename},
275     $sampleissuingrule1->{restrictedtype},
276     $sampleissuingrule1->{maxsuspensiondays},
277     $sampleissuingrule1->{onshelfholds},
278     $sampleissuingrule1->{opacitemholds},
279     $sampleissuingrule1->{cap_fine_to_replacement_price},
280     $sampleissuingrule1->{article_requests},
281 );
282 $sth->execute(
283     $sampleissuingrule2->{branchcode},
284     $sampleissuingrule2->{categorycode},
285     $sampleissuingrule2->{itemtype},
286     $sampleissuingrule2->{maxissueqty},
287     $sampleissuingrule2->{maxonsiteissueqty},
288     $sampleissuingrule2->{renewalsallowed},
289     $sampleissuingrule2->{renewalperiod},
290     $sampleissuingrule2->{norenewalbefore},
291     $sampleissuingrule2->{auto_renew},
292     $sampleissuingrule2->{reservesallowed},
293     $sampleissuingrule2->{issuelength},
294     $sampleissuingrule2->{lengthunit},
295     $sampleissuingrule2->{hardduedate},
296     $sampleissuingrule2->{hardduedatecompare},
297     $sampleissuingrule2->{fine},
298     $sampleissuingrule2->{finedays},
299     $sampleissuingrule2->{firstremind},
300     $sampleissuingrule2->{chargeperiod},
301     $sampleissuingrule2->{chargeperiod_charge_at},
302     $sampleissuingrule2->{rentaldiscount},
303     $sampleissuingrule2->{overduefinescap},
304     $sampleissuingrule2->{accountsent},
305     $sampleissuingrule2->{reservecharge},
306     $sampleissuingrule2->{chargename},
307     $sampleissuingrule2->{restrictedtype},
308     $sampleissuingrule2->{maxsuspensiondays},
309     $sampleissuingrule2->{onshelfholds},
310     $sampleissuingrule2->{opacitemholds},
311     $sampleissuingrule2->{cap_fine_to_replacement_price},
312     $sampleissuingrule2->{article_requests},
313 );
314 $sth->execute(
315     $sampleissuingrule3->{branchcode},
316     $sampleissuingrule3->{categorycode},
317     $sampleissuingrule3->{itemtype},
318     $sampleissuingrule3->{maxissueqty},
319     $sampleissuingrule3->{maxonsiteissueqty},
320     $sampleissuingrule3->{renewalsallowed},
321     $sampleissuingrule3->{renewalperiod},
322     $sampleissuingrule3->{norenewalbefore},
323     $sampleissuingrule3->{auto_renew},
324     $sampleissuingrule3->{reservesallowed},
325     $sampleissuingrule3->{issuelength},
326     $sampleissuingrule3->{lengthunit},
327     $sampleissuingrule3->{hardduedate},
328     $sampleissuingrule3->{hardduedatecompare},
329     $sampleissuingrule3->{fine},
330     $sampleissuingrule3->{finedays},
331     $sampleissuingrule3->{firstremind},
332     $sampleissuingrule3->{chargeperiod},
333     $sampleissuingrule3->{chargeperiod_charge_at},
334     $sampleissuingrule3->{rentaldiscount},
335     $sampleissuingrule3->{overduefinescap},
336     $sampleissuingrule3->{accountsent},
337     $sampleissuingrule3->{reservecharge},
338     $sampleissuingrule3->{chargename},
339     $sampleissuingrule3->{restrictedtype},
340     $sampleissuingrule3->{maxsuspensiondays},
341     $sampleissuingrule3->{onshelfholds},
342     $sampleissuingrule3->{opacitemholds},
343     $sampleissuingrule3->{cap_fine_to_replacement_price},
344     $sampleissuingrule3->{article_requests},
345 );
346
347 is_deeply(
348     Koha::IssuingRules->find({ categorycode => $samplecat->{categorycode}, itemtype => 'Book', branchcode => $samplebranch1->{branchcode} })->unblessed,
349     $sampleissuingrule1,
350     "GetIssuingCharge returns issuingrule1's informations"
351 );
352
353 #Test GetLoanLength
354 is_deeply(
355     C4::Circulation::GetLoanLength(
356         $samplecat->{categorycode},
357         'BOOK', $samplebranch1->{branchcode}
358     ),
359     { issuelength => 5, lengthunit => 'days', renewalperiod => 5 },
360     "GetLoanLength"
361 );
362
363 is_deeply(
364     C4::Circulation::GetLoanLength(),
365     $default,
366     "Without parameters, GetLoanLength returns hardcoded values"
367 );
368 is_deeply(
369     C4::Circulation::GetLoanLength( -1, -1 ),
370     $default,
371     "With wrong parameters, GetLoanLength returns hardcoded values"
372 );
373 is_deeply(
374     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
375     $default,
376     "With only one parameter, GetLoanLength returns hardcoded values"
377 );    #NOTE : is that really what is expected?
378 is_deeply(
379     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK' ),
380     $default,
381     "With only two parameters, GetLoanLength returns hardcoded values"
382 );    #NOTE : is that really what is expected?
383 is_deeply(
384     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK', $samplebranch1->{branchcode} ),
385     {
386         issuelength   => 5,
387         renewalperiod => 5,
388         lengthunit    => 'days',
389     },
390     "With the correct number of parameters, GetLoanLength returns the expected values"
391 );
392
393 #Test GetHardDueDate
394 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
395     'BOOK', $samplebranch1->{branchcode} );
396 is_deeply(
397     \@hardduedate,
398     [
399         dt_from_string( $sampleissuingrule1->{hardduedate}, 'iso' ),
400         $sampleissuingrule1->{hardduedatecompare}
401     ],
402     "GetHardDueDate returns the duedate and the duedatecompare"
403 );
404
405 #End transaction
406 $dbh->rollback;