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