Bug 15343 [QA Followup]
[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 };
138 my $sampleissuingrule2 = {
139     branchcode         => $samplebranch2->{branchcode},
140     categorycode       => $samplecat->{categorycode},
141     itemtype           => 'BOOK',
142     maxissueqty        => 2,
143     maxonsiteissueqty  => 1,
144     renewalsallowed    => 'Null',
145     renewalperiod      => 2,
146     norenewalbefore    => 7,
147     auto_renew         => 0,
148     reservesallowed    => 'Null',
149     issuelength        => 2,
150     lengthunit         => 'Null',
151     hardduedate        => 2,
152     hardduedatecompare => 'Null',
153     fine               => 'Null',
154     finedays           => 'Null',
155     firstremind        => 'Null',
156     chargeperiod       => 'Null',
157     chargeperiod_charge_at => 0,
158     rentaldiscount     => 2.00,
159     overduefinescap    => 'Null',
160     accountsent        => 'Null',
161     reservecharge      => 'Null',
162     chargename         => 'Null',
163     restrictedtype     => 'Null',
164     maxsuspensiondays  => 0,
165     onshelfholds       => 1,
166     opacitemholds      => 'Y',
167 };
168 my $sampleissuingrule3 = {
169     branchcode         => $samplebranch1->{branchcode},
170     categorycode       => $samplecat->{categorycode},
171     itemtype           => 'DVD',
172     maxissueqty        => 3,
173     maxonsiteissueqty  => 2,
174     renewalsallowed    => 'Null',
175     renewalperiod      => 3,
176     norenewalbefore    => 8,
177     auto_renew         => 0,
178     reservesallowed    => 'Null',
179     issuelength        => 3,
180     lengthunit         => 'Null',
181     hardduedate        => 3,
182     hardduedatecompare => 'Null',
183     fine               => 'Null',
184     finedays           => 'Null',
185     firstremind        => 'Null',
186     chargeperiod       => 'Null',
187     chargeperiod_charge_at => 0,
188     rentaldiscount     => 3.00,
189     overduefinescap    => 'Null',
190     accountsent        => 'Null',
191     reservecharge      => 'Null',
192     chargename         => 'Null',
193     restrictedtype     => 'Null',
194     maxsuspensiondays  => 0,
195     onshelfholds       => 1,
196     opacitemholds      => 'F',
197 };
198
199 $query = 'INSERT INTO issuingrules (
200                 branchcode,
201                 categorycode,
202                 itemtype,
203                 maxissueqty,
204                 maxonsiteissueqty,
205                 renewalsallowed,
206                 renewalperiod,
207                 norenewalbefore,
208                 auto_renew,
209                 reservesallowed,
210                 issuelength,
211                 lengthunit,
212                 hardduedate,
213                 hardduedatecompare,
214                 fine,
215                 finedays,
216                 firstremind,
217                 chargeperiod,
218                 chargeperiod_charge_at,
219                 rentaldiscount,
220                 overduefinescap,
221                 accountsent,
222                 reservecharge,
223                 chargename,
224                 restrictedtype,
225                 maxsuspensiondays
226                 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
227 my $sth = $dbh->prepare($query);
228 $sth->execute(
229     $sampleissuingrule1->{branchcode},
230     $sampleissuingrule1->{categorycode},
231     $sampleissuingrule1->{itemtype},
232     $sampleissuingrule1->{maxissueqty},
233     $sampleissuingrule1->{maxonsiteissueqty},
234     $sampleissuingrule1->{renewalsallowed},
235     $sampleissuingrule1->{renewalperiod},
236     $sampleissuingrule1->{norenewalbefore},
237     $sampleissuingrule1->{auto_renew},
238     $sampleissuingrule1->{reservesallowed},
239     $sampleissuingrule1->{issuelength},
240     $sampleissuingrule1->{lengthunit},
241     $sampleissuingrule1->{hardduedate},
242     $sampleissuingrule1->{hardduedatecompare},
243     $sampleissuingrule1->{fine},
244     $sampleissuingrule1->{finedays},
245     $sampleissuingrule1->{firstremind},
246     $sampleissuingrule1->{chargeperiod},
247     $sampleissuingrule1->{chargeperiod_charge_at},
248     $sampleissuingrule1->{rentaldiscount},
249     $sampleissuingrule1->{overduefinescap},
250     $sampleissuingrule1->{accountsent},
251     $sampleissuingrule1->{reservecharge},
252     $sampleissuingrule1->{chargename},
253     $sampleissuingrule1->{restrictedtype},
254     $sampleissuingrule1->{maxsuspensiondays},
255 );
256 $sth->execute(
257     $sampleissuingrule2->{branchcode},
258     $sampleissuingrule2->{categorycode},
259     $sampleissuingrule2->{itemtype},
260     $sampleissuingrule2->{maxissueqty},
261     $sampleissuingrule2->{maxonsiteissueqty},
262     $sampleissuingrule2->{renewalsallowed},
263     $sampleissuingrule2->{renewalperiod},
264     $sampleissuingrule2->{norenewalbefore},
265     $sampleissuingrule2->{auto_renew},
266     $sampleissuingrule2->{reservesallowed},
267     $sampleissuingrule2->{issuelength},
268     $sampleissuingrule2->{lengthunit},
269     $sampleissuingrule2->{hardduedate},
270     $sampleissuingrule2->{hardduedatecompare},
271     $sampleissuingrule2->{fine},
272     $sampleissuingrule2->{finedays},
273     $sampleissuingrule2->{firstremind},
274     $sampleissuingrule2->{chargeperiod},
275     $sampleissuingrule2->{chargeperiod_charge_at},
276     $sampleissuingrule2->{rentaldiscount},
277     $sampleissuingrule2->{overduefinescap},
278     $sampleissuingrule2->{accountsent},
279     $sampleissuingrule2->{reservecharge},
280     $sampleissuingrule2->{chargename},
281     $sampleissuingrule2->{restrictedtype},
282     $sampleissuingrule2->{maxsuspensiondays},
283 );
284 $sth->execute(
285     $sampleissuingrule3->{branchcode},
286     $sampleissuingrule3->{categorycode},
287     $sampleissuingrule3->{itemtype},
288     $sampleissuingrule3->{maxissueqty},
289     $sampleissuingrule3->{maxonsiteissueqty},
290     $sampleissuingrule3->{renewalsallowed},
291     $sampleissuingrule3->{renewalperiod},
292     $sampleissuingrule3->{norenewalbefore},
293     $sampleissuingrule3->{auto_renew},
294     $sampleissuingrule3->{reservesallowed},
295     $sampleissuingrule3->{issuelength},
296     $sampleissuingrule3->{lengthunit},
297     $sampleissuingrule3->{hardduedate},
298     $sampleissuingrule3->{hardduedatecompare},
299     $sampleissuingrule3->{fine},
300     $sampleissuingrule3->{finedays},
301     $sampleissuingrule3->{firstremind},
302     $sampleissuingrule3->{chargeperiod},
303     $sampleissuingrule3->{chargeperiod_charge_at},
304     $sampleissuingrule3->{rentaldiscount},
305     $sampleissuingrule3->{overduefinescap},
306     $sampleissuingrule3->{accountsent},
307     $sampleissuingrule3->{reservecharge},
308     $sampleissuingrule3->{chargename},
309     $sampleissuingrule3->{restrictedtype},
310     $sampleissuingrule3->{maxsuspensiondays},
311 );
312
313 is_deeply(
314     GetIssuingRule(
315         $samplecat->{categorycode},
316         'Book', $samplebranch1->{branchcode}
317     ),
318     $sampleissuingrule1,
319     "GetIssuingCharge returns issuingrule1's informations"
320 );
321
322 #Test GetLoanLength
323 is_deeply(
324     C4::Circulation::GetLoanLength(
325         $samplecat->{categorycode},
326         'BOOK', $samplebranch1->{branchcode}
327     ),
328     { issuelength => 5, lengthunit => 'Null', renewalperiod => 5 },
329     "GetLoanLength"
330 );
331 is_deeply(
332     C4::Circulation::GetLoanLength(),
333     {
334         issuelength   => 21,
335         renewalperiod => 21,
336         lengthunit    => 'days',
337     },
338     "Without parameters, GetLoanLength returns hardcoded values"
339 );
340 is_deeply(
341     C4::Circulation::GetLoanLength( -1, -1 ),
342     {
343         issuelength   => 21,
344         renewalperiod => 21,
345         lengthunit    => 'days',
346     },
347     "With wrong parameters, GetLoanLength returns hardcoded values"
348 );
349 is_deeply(
350     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
351     {
352         issuelength   => 21,
353         renewalperiod => 21,
354         lengthunit    => 'days',
355     },
356     "With only one parameter, GetLoanLength returns hardcoded values"
357 );    #NOTE : is that really what is expected?
358 is_deeply(
359     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK' ),
360     {
361         issuelength   => 21,
362         renewalperiod => 21,
363         lengthunit    => 'days',
364     },
365     "With only one parameter, GetLoanLength returns hardcoded values"
366 );    #NOTE : is that really what is expected?
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 );
379
380 #End transaction
381 $dbh->rollback;