Bug 27993: Add unit 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::CirculationRules;
9 use Koha::Library;
10
11 use t::lib::TestBuilder;
12
13 use Test::More tests => 9;
14
15 BEGIN {
16     use_ok('C4::Circulation');
17 }
18 can_ok(
19     'C4::Circulation',
20     qw(
21       GetHardDueDate
22       GetLoanLength
23       )
24 );
25
26 my $schema = Koha::Database->new->schema;
27 $schema->storage->txn_begin;
28 my $dbh = C4::Context->dbh;
29
30 $dbh->do(q|DELETE FROM issues|);
31 $dbh->do(q|DELETE FROM items|);
32 $dbh->do(q|DELETE FROM borrowers|);
33 $dbh->do(q|DELETE FROM edifact_ean|);
34 $dbh->do(q|DELETE FROM branches|);
35 $dbh->do(q|DELETE FROM categories|);
36 $dbh->do(q|DELETE FROM circulation_rules|);
37
38 #Add sample datas
39
40 #Add branch and category
41 my $samplebranch1 = {
42     branchcode     => 'SAB1',
43     branchname     => 'Sample Branch',
44     branchaddress1 => 'sample adr1',
45     branchaddress2 => 'sample adr2',
46     branchaddress3 => 'sample adr3',
47     branchzip      => 'sample zip',
48     branchcity     => 'sample city',
49     branchstate    => 'sample state',
50     branchcountry  => 'sample country',
51     branchphone    => 'sample phone',
52     branchfax      => 'sample fax',
53     branchemail    => 'sample email',
54     branchurl      => 'sample url',
55     branchip       => 'sample ip',
56     opac_info      => 'sample opac',
57 };
58 my $samplebranch2 = {
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     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 my $builder = t::lib::TestBuilder->new;
107 my $sampleitemtype1 = $builder->build({ source => 'Itemtype' })->{itemtype};
108 my $sampleitemtype2 = $builder->build({ source => 'Itemtype' })->{itemtype};
109
110 #Begin Tests
111
112 my $default = {
113     issuelength => 0,
114     renewalperiod => 0,
115     lengthunit => 'days'
116 };
117
118 #Test get_effective_rules
119 my $sampleissuingrule1 = {
120     branchcode   => $samplebranch1->{branchcode},
121     categorycode => $samplecat->{categorycode},
122     itemtype     => $sampleitemtype1,
123     rules        => {
124         finedays                         => 0,
125         lengthunit                       => 'days',
126         renewalperiod                    => 5,
127         norenewalbefore                  => 6,
128         auto_renew                       => 0,
129         issuelength                      => 5,
130         chargeperiod                     => 0,
131         chargeperiod_charge_at           => 0,
132         rentaldiscount                   => 2,
133         reservesallowed                  => 0,
134         hardduedate                      => '2013-01-01',
135         fine                             => 0,
136         hardduedatecompare               => 5,
137         overduefinescap                  => 0,
138         renewalsallowed                  => 0,
139         firstremind                      => 0,
140         maxsuspensiondays                => 0,
141         onshelfholds                     => 0,
142         opacitemholds                    => 'N',
143         cap_fine_to_replacement_price    => 0,
144         holds_per_record                 => 1,
145         article_requests                 => 'yes',
146         no_auto_renewal_after            => undef,
147         no_auto_renewal_after_hard_limit => undef,
148         suspension_chargeperiod          => 1,
149         holds_per_day                    => undef,
150     }
151 };
152 my $sampleissuingrule2 = {
153     branchcode   => $samplebranch2->{branchcode},
154     categorycode => $samplecat->{categorycode},
155     itemtype     => $sampleitemtype1,
156     rules        => {
157         renewalsallowed               => 0,
158         renewalperiod                 => 2,
159         norenewalbefore               => 7,
160         auto_renew                    => 0,
161         reservesallowed               => 0,
162         issuelength                   => 2,
163         lengthunit                    => 'days',
164         hardduedate                   => 2,
165         hardduedatecompare            => undef,
166         fine                          => undef,
167         finedays                      => undef,
168         firstremind                   => undef,
169         chargeperiod                  => undef,
170         chargeperiod_charge_at        => 0,
171         rentaldiscount                => 2.00,
172         overduefinescap               => undef,
173         maxsuspensiondays             => 0,
174         onshelfholds                  => 1,
175         opacitemholds                 => 'Y',
176         cap_fine_to_replacement_price => 0,
177         holds_per_record              => 1,
178         article_requests              => 'yes',
179     }
180 };
181 my $sampleissuingrule3 = {
182     branchcode   => $samplebranch1->{branchcode},
183     categorycode => $samplecat->{categorycode},
184     itemtype     => $sampleitemtype2,
185     rules        => {
186         renewalsallowed               => 0,
187         renewalperiod                 => 3,
188         norenewalbefore               => 8,
189         auto_renew                    => 0,
190         reservesallowed               => 0,
191         issuelength                   => 3,
192         lengthunit                    => 'days',
193         hardduedate                   => 3,
194         hardduedatecompare            => undef,
195         fine                          => undef,
196         finedays                      => undef,
197         firstremind                   => undef,
198         chargeperiod                  => undef,
199         chargeperiod_charge_at        => 0,
200         rentaldiscount                => 3.00,
201         overduefinescap               => 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
211 Koha::CirculationRules->set_rules( $sampleissuingrule1 );
212 Koha::CirculationRules->set_rules( $sampleissuingrule2 );
213 Koha::CirculationRules->set_rules( $sampleissuingrule3 );
214
215 #Test GetLoanLength
216 is_deeply(
217     C4::Circulation::GetLoanLength(
218         $samplecat->{categorycode},
219         $sampleitemtype1, $samplebranch1->{branchcode}
220     ),
221     { issuelength => 5, lengthunit => 'days', renewalperiod => 5 },
222     "GetLoanLength"
223 );
224
225 is_deeply(
226     C4::Circulation::GetLoanLength(),
227     $default,
228     "Without parameters, GetLoanLength returns hardcoded values"
229 );
230 is_deeply(
231     C4::Circulation::GetLoanLength( -1, -1 ),
232     $default,
233     "With wrong parameters, GetLoanLength returns hardcoded values"
234 );
235 is_deeply(
236     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
237     $default,
238     "With only one parameter, GetLoanLength returns hardcoded values"
239 );    #NOTE : is that really what is expected?
240 is_deeply(
241     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, $sampleitemtype1 ),
242     $default,
243     "With only two parameters, GetLoanLength returns hardcoded values"
244 );    #NOTE : is that really what is expected?
245 is_deeply(
246     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, $sampleitemtype1, $samplebranch1->{branchcode} ),
247     {
248         issuelength   => 5,
249         renewalperiod => 5,
250         lengthunit    => 'days',
251     },
252     "With the correct number of parameters, GetLoanLength returns the expected values"
253 );
254
255 #Test GetHardDueDate
256 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
257     $sampleitemtype1, $samplebranch1->{branchcode} );
258 is_deeply(
259     \@hardduedate,
260     [
261         dt_from_string( $sampleissuingrule1->{rules}->{hardduedate}, 'iso' ),
262         $sampleissuingrule1->{rules}->{hardduedatecompare}
263     ],
264     "GetHardDueDate returns the duedate and the duedatecompare"
265 );