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