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