Bug 24408: Do not compare floats with precision in tests
[koha-ffzg.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::Database;
7 use Koha::DateUtils;
8 use Koha::IssuingRules;
9 use Koha::Library;
10
11 use Test::More tests => 9;
12
13 BEGIN {
14     use_ok('C4::Circulation');
15 }
16 can_ok(
17     'C4::Circulation',
18     qw(
19       GetHardDueDate
20       GetLoanLength
21       )
22 );
23
24 my $schema = Koha::Database->new->schema;
25 $schema->storage->txn_begin;
26 my $dbh = C4::Context->dbh;
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       => undef,
82     enrolmentperioddate   => undef,
83     dateofbirthrequired   => undef,
84     finetype              => undef,
85     bulk                  => undef,
86     enrolmentfee          => undef,
87     overduenoticerequired => undef,
88     issuelimit            => undef,
89     reservefee            => undef,
90     hidelostitems         => 0,
91     category_type         => 'A',
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,
117     restrictedtype     => 0,
118     accountsent        => 0,
119     finedays           => 0,
120     lengthunit         => 'days',
121     renewalperiod      => 5,
122     norenewalbefore    => 6,
123     auto_renew         => 0,
124     issuelength        => 5,
125     chargeperiod       => 0,
126     chargeperiod_charge_at => 0,
127     rentaldiscount     => 2,
128     reservesallowed    => 0,
129     hardduedate        => '2013-01-01',
130     branchcode         => $samplebranch1->{branchcode},
131     fine               => 0,
132     hardduedatecompare => 0,
133     overduefinescap    => 0,
134     renewalsallowed    => 0,
135     firstremind        => 0,
136     itemtype           => 'BOOK',
137     categorycode       => $samplecat->{categorycode},
138     maxsuspensiondays  => 0,
139     onshelfholds       => 0,
140     opacitemholds      => 'N',
141     cap_fine_to_replacement_price => 0,
142     holds_per_record   => 1,
143     article_requests   => 'yes',
144     no_auto_renewal_after => undef,
145     no_auto_renewal_after_hard_limit => undef,
146     suspension_chargeperiod => 1,
147     holds_per_day => undef,
148 };
149 my $sampleissuingrule2 = {
150     branchcode         => $samplebranch2->{branchcode},
151     categorycode       => $samplecat->{categorycode},
152     itemtype           => 'BOOK',
153     renewalsallowed    => 0,
154     renewalperiod      => 2,
155     norenewalbefore    => 7,
156     auto_renew         => 0,
157     reservesallowed    => 0,
158     issuelength        => 2,
159     lengthunit         => 'days',
160     hardduedate        => undef,
161     hardduedatecompare => 0,
162     fine               => undef,
163     finedays           => undef,
164     firstremind        => undef,
165     chargeperiod       => undef,
166     chargeperiod_charge_at => 0,
167     rentaldiscount     => 2.00,
168     overduefinescap    => undef,
169     accountsent        => undef,
170     reservecharge      => undef,
171     restrictedtype     => undef,
172     maxsuspensiondays  => 0,
173     onshelfholds       => 1,
174     opacitemholds      => 'Y',
175     cap_fine_to_replacement_price => 0,
176     holds_per_record   => 1,
177     article_requests   => 'yes',
178 };
179 my $sampleissuingrule3 = {
180     branchcode         => $samplebranch1->{branchcode},
181     categorycode       => $samplecat->{categorycode},
182     itemtype           => 'DVD',
183     renewalsallowed    => 0,
184     renewalperiod      => 3,
185     norenewalbefore    => 8,
186     auto_renew         => 0,
187     reservesallowed    => 0,
188     issuelength        => 3,
189     lengthunit         => 'days',
190     hardduedate        => undef,
191     hardduedatecompare => 0,
192     fine               => undef,
193     finedays           => undef,
194     firstremind        => undef,
195     chargeperiod       => undef,
196     chargeperiod_charge_at => 0,
197     rentaldiscount     => 3.00,
198     overduefinescap    => undef,
199     accountsent        => undef,
200     reservecharge      => undef,
201     restrictedtype     => undef,
202     maxsuspensiondays  => 0,
203     onshelfholds       => 1,
204     opacitemholds      => 'F',
205     cap_fine_to_replacement_price => 0,
206     holds_per_record   => 1,
207     article_requests   => 'yes',
208 };
209
210 $query = 'INSERT INTO issuingrules (
211                 branchcode,
212                 categorycode,
213                 itemtype,
214                 renewalsallowed,
215                 renewalperiod,
216                 norenewalbefore,
217                 auto_renew,
218                 reservesallowed,
219                 issuelength,
220                 lengthunit,
221                 hardduedate,
222                 hardduedatecompare,
223                 fine,
224                 finedays,
225                 firstremind,
226                 chargeperiod,
227                 chargeperiod_charge_at,
228                 rentaldiscount,
229                 overduefinescap,
230                 accountsent,
231                 reservecharge,
232                 restrictedtype,
233                 maxsuspensiondays,
234                 onshelfholds,
235                 opacitemholds,
236                 cap_fine_to_replacement_price,
237                 article_requests
238                 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
239 my $sth = $dbh->prepare($query);
240 $sth->execute(
241     $sampleissuingrule1->{branchcode},
242     $sampleissuingrule1->{categorycode},
243     $sampleissuingrule1->{itemtype},
244     $sampleissuingrule1->{renewalsallowed},
245     $sampleissuingrule1->{renewalperiod},
246     $sampleissuingrule1->{norenewalbefore},
247     $sampleissuingrule1->{auto_renew},
248     $sampleissuingrule1->{reservesallowed},
249     $sampleissuingrule1->{issuelength},
250     $sampleissuingrule1->{lengthunit},
251     $sampleissuingrule1->{hardduedate},
252     $sampleissuingrule1->{hardduedatecompare},
253     $sampleissuingrule1->{fine},
254     $sampleissuingrule1->{finedays},
255     $sampleissuingrule1->{firstremind},
256     $sampleissuingrule1->{chargeperiod},
257     $sampleissuingrule1->{chargeperiod_charge_at},
258     $sampleissuingrule1->{rentaldiscount},
259     $sampleissuingrule1->{overduefinescap},
260     $sampleissuingrule1->{accountsent},
261     $sampleissuingrule1->{reservecharge},
262     $sampleissuingrule1->{restrictedtype},
263     $sampleissuingrule1->{maxsuspensiondays},
264     $sampleissuingrule1->{onshelfholds},
265     $sampleissuingrule1->{opacitemholds},
266     $sampleissuingrule1->{cap_fine_to_replacement_price},
267     $sampleissuingrule1->{article_requests},
268 );
269 $sth->execute(
270     $sampleissuingrule2->{branchcode},
271     $sampleissuingrule2->{categorycode},
272     $sampleissuingrule2->{itemtype},
273     $sampleissuingrule2->{renewalsallowed},
274     $sampleissuingrule2->{renewalperiod},
275     $sampleissuingrule2->{norenewalbefore},
276     $sampleissuingrule2->{auto_renew},
277     $sampleissuingrule2->{reservesallowed},
278     $sampleissuingrule2->{issuelength},
279     $sampleissuingrule2->{lengthunit},
280     $sampleissuingrule2->{hardduedate},
281     $sampleissuingrule2->{hardduedatecompare},
282     $sampleissuingrule2->{fine},
283     $sampleissuingrule2->{finedays},
284     $sampleissuingrule2->{firstremind},
285     $sampleissuingrule2->{chargeperiod},
286     $sampleissuingrule2->{chargeperiod_charge_at},
287     $sampleissuingrule2->{rentaldiscount},
288     $sampleissuingrule2->{overduefinescap},
289     $sampleissuingrule2->{accountsent},
290     $sampleissuingrule2->{reservecharge},
291     $sampleissuingrule2->{restrictedtype},
292     $sampleissuingrule2->{maxsuspensiondays},
293     $sampleissuingrule2->{onshelfholds},
294     $sampleissuingrule2->{opacitemholds},
295     $sampleissuingrule2->{cap_fine_to_replacement_price},
296     $sampleissuingrule2->{article_requests},
297 );
298 $sth->execute(
299     $sampleissuingrule3->{branchcode},
300     $sampleissuingrule3->{categorycode},
301     $sampleissuingrule3->{itemtype},
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->{restrictedtype},
321     $sampleissuingrule3->{maxsuspensiondays},
322     $sampleissuingrule3->{onshelfholds},
323     $sampleissuingrule3->{opacitemholds},
324     $sampleissuingrule3->{cap_fine_to_replacement_price},
325     $sampleissuingrule3->{article_requests},
326 );
327
328 #Test GetLoanLength
329 is_deeply(
330     C4::Circulation::GetLoanLength(
331         $samplecat->{categorycode},
332         'BOOK', $samplebranch1->{branchcode}
333     ),
334     { issuelength => 5, lengthunit => 'days', renewalperiod => 5 },
335     "GetLoanLength"
336 );
337
338 is_deeply(
339     C4::Circulation::GetLoanLength(),
340     $default,
341     "Without parameters, GetLoanLength returns hardcoded values"
342 );
343 is_deeply(
344     C4::Circulation::GetLoanLength( -1, -1 ),
345     $default,
346     "With wrong parameters, GetLoanLength returns hardcoded values"
347 );
348 is_deeply(
349     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
350     $default,
351     "With only one parameter, GetLoanLength returns hardcoded values"
352 );    #NOTE : is that really what is expected?
353 is_deeply(
354     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK' ),
355     $default,
356     "With only two parameters, GetLoanLength returns hardcoded values"
357 );    #NOTE : is that really what is expected?
358 is_deeply(
359     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK', $samplebranch1->{branchcode} ),
360     {
361         issuelength   => 5,
362         renewalperiod => 5,
363         lengthunit    => 'days',
364     },
365     "With the correct number of parameters, GetLoanLength returns the expected values"
366 );
367
368 #Test GetHardDueDate
369 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
370     'BOOK', $samplebranch1->{branchcode} );
371 is_deeply(
372     \@hardduedate,
373     [
374         dt_from_string( $sampleissuingrule1->{hardduedate}, 'iso' ),
375         $sampleissuingrule1->{hardduedatecompare}
376     ],
377     "GetHardDueDate returns the duedate and the duedatecompare"
378 );