Bug 25729: Prevent Charges/Fees.t to fail on slow server
[koha.git] / t / db_dependent / Koha / Charges / Fees.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2018 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Test::More tests => 8;
23 use Test::Exception;
24 use Test::Warn;
25
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
28 use t::lib::Dates;
29
30 use Time::Fake;
31 use C4::Calendar;
32 use Koha::DateUtils qw(dt_from_string);
33
34 BEGIN {
35     use_ok('Koha::Charges::Fees');
36 }
37
38 my $schema  = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new();
40 $schema->storage->txn_begin;
41
42 my $patron_category = $builder->build_object(
43     {
44         class => 'Koha::Patron::Categories',
45         value => {
46             category_type => 'P',
47             enrolmentfee  => 0,
48         }
49     }
50 );
51 my $library = $builder->build_object(
52     {
53         class => 'Koha::Libraries',
54     }
55 );
56 my $biblio = $builder->build_object(
57     {
58         class => 'Koha::Biblios',
59     }
60 );
61 my $itemtype = $builder->build_object(
62     {
63         class => 'Koha::ItemTypes',
64         value => {
65             rentalcharge_daily  => '0.00',
66             rentalcharge_hourly => '0.00',
67             rentalcharge        => '0.00',
68             processfee          => '0.00',
69             defaultreplacecost  => '0.00',
70         },
71     }
72 );
73 my $item = $builder->build_object(
74     {
75         class => 'Koha::Items',
76         value => {
77             biblionumber  => $biblio->id,
78             homebranch    => $library->id,
79             holdingbranch => $library->id,
80             itype         => $itemtype->id,
81         }
82     }
83 );
84 my $patron = $builder->build_object(
85     {
86         class => 'Koha::Patrons',
87         value => {
88             dateexpiry   => '9999-12-31',
89             categorycode => $patron_category->id,
90         }
91     }
92 );
93
94 my $now = dt_from_string()->set_time_zone('floating');
95 Time::Fake->offset( $now->epoch );
96
97 my $dt_from = $now->clone->subtract( days => 2 );
98 my $dt_to = $now->clone->add( days => 4 );
99
100 subtest 'new' => sub {
101     plan tests => 9;
102
103     # Mandatory parameters missing
104     throws_ok {
105         Koha::Charges::Fees->new(
106             {
107                 library => $library,
108                 item    => $item,
109                 to_date => $dt_to,
110             }
111           )
112     }
113     'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for patron';
114     throws_ok {
115         Koha::Charges::Fees->new(
116             {
117                 patron  => $patron,
118                 item    => $item,
119                 to_date => $dt_to,
120             }
121           )
122     }
123     'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for library';
124     throws_ok {
125         Koha::Charges::Fees->new(
126             {
127                 patron  => $patron,
128                 library => $library,
129                 to_date => $dt_to,
130             }
131           )
132     }
133     'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for item';
134     throws_ok {
135         Koha::Charges::Fees->new(
136             {
137                 patron  => $patron,
138                 library => $library,
139                 item    => $item,
140             }
141           )
142     }
143     'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for to_date';
144
145     # Mandatory parameter bad
146     dies_ok {
147         Koha::Charges::Fees->new(
148             {
149                 patron  => '12345',
150                 library => $library,
151                 item    => $item,
152                 to_date => $dt_to,
153             }
154           )
155     }
156     'dies for bad patron';
157     dies_ok {
158         Koha::Charges::Fees->new(
159             {
160                 patron  => $patron,
161                 library => '12345',
162                 item    => $item,
163                 to_date => $dt_to,
164             }
165           )
166     }
167     'dies for bad library';
168     dies_ok {
169         Koha::Charges::Fees->new(
170             {
171                 patron  => $patron,
172                 library => $library,
173                 item    => '12345',
174                 to_date => $dt_to,
175             }
176           )
177     }
178     'dies for bad item';
179     dies_ok {
180         Koha::Charges::Fees->new(
181             {
182                 patron  => $patron,
183                 library => $library,
184                 item    => $item,
185                 to_date => 12345
186             }
187           )
188     }
189     'dies for bad to_date';
190
191     # Defaults
192     my $fees = Koha::Charges::Fees->new(
193         {
194             patron  => $patron,
195             library => $library,
196             item    => $item,
197             to_date => $dt_to,
198         }
199     );
200     is( t::lib::Dates::compare($fees->from_date, dt_from_string()), 0,
201         'from_date default set correctly to today' );
202 };
203
204 subtest 'patron accessor' => sub {
205     plan tests => 2;
206
207     my $fees = Koha::Charges::Fees->new(
208         {
209             patron  => $patron,
210             library => $library,
211             item    => $item,
212             to_date => $dt_to,
213         }
214     );
215
216     ok(
217         $fees->patron->isa('Koha::Patron'),
218         'patron accessor returns a Koha::Patron'
219     );
220     warning_is { $fees->patron('12345') }
221     { carped =>
222 "Setting 'patron' to something other than a Koha::Patron is not supported!"
223     }, "Warning thrown when attempting to set patron to string";
224
225 };
226
227 subtest 'library accessor' => sub {
228     plan tests => 2;
229
230     my $fees = Koha::Charges::Fees->new(
231         {
232             patron  => $patron,
233             library => $library,
234             item    => $item,
235             to_date => $dt_to,
236         }
237     );
238
239     ok(
240         $fees->library->isa('Koha::Library'),
241         'library accessor returns a Koha::Library'
242     );
243     warning_is { $fees->library('12345') }
244     { carped =>
245 "Setting 'library' to something other than a Koha::Library is not supported!"
246     }, "Warning thrown when attempting to set library to string";
247 };
248
249 subtest 'item accessor' => sub {
250     plan tests => 2;
251
252     my $fees = Koha::Charges::Fees->new(
253         {
254             patron  => $patron,
255             library => $library,
256             item    => $item,
257             to_date => $dt_to,
258         }
259     );
260
261     ok( $fees->item->isa('Koha::Item'), 'item accessor returns a Koha::Item' );
262     warning_is { $fees->item('12345') }
263     { carped =>
264 "Setting 'item' to something other than a Koha::Item is not supported!"
265     }, "Warning thrown when attempting to set item to string";
266 };
267
268 subtest 'to_date accessor' => sub {
269     plan tests => 2;
270
271     my $fees = Koha::Charges::Fees->new(
272         {
273             patron  => $patron,
274             library => $library,
275             item    => $item,
276             to_date => $dt_to,
277         }
278     );
279
280     ok( $fees->to_date->isa('DateTime'),
281         'to_date accessor returns a DateTime' );
282     warning_is { $fees->to_date(12345) }
283     { carped =>
284 "Setting 'to_date' to something other than a DateTime is not supported!"
285     }, "Warning thrown when attempting to set to_date to integer";
286 };
287
288 subtest 'from_date accessor' => sub {
289     plan tests => 2;
290
291     my $fees = Koha::Charges::Fees->new(
292         {
293             patron  => $patron,
294             library => $library,
295             item    => $item,
296             to_date => $dt_to,
297         }
298     );
299
300     ok(
301         $fees->from_date->isa('DateTime'),
302         'from_date accessor returns a DateTime'
303     );
304     warning_is { $fees->from_date(12345) }
305     { carped =>
306 "Setting 'from_date' to something other than a DateTime is not supported!"
307     }, "Warning thrown when attempting to set from_date to integer";
308 };
309
310 subtest 'accumulate_rentalcharge tests' => sub {
311     plan tests => 7;
312
313     my $fees = Koha::Charges::Fees->new(
314         {
315             patron    => $patron,
316             library   => $library,
317             item      => $item,
318             to_date   => $dt_to,
319             from_date => $dt_from,
320         }
321     );
322
323     # Daily tests
324     $itemtype->rentalcharge_daily(1.00);
325     $itemtype->store();
326     is( $itemtype->rentalcharge_daily,
327         1.00, 'Daily return charge stored correctly' );
328
329     t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
330     my $charge = $fees->accumulate_rentalcharge();
331     is( $charge, 6.00,
332 'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar'
333     );
334
335     t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
336     $charge = $fees->accumulate_rentalcharge();
337     is( $charge, 6.00,
338 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed'
339     );
340
341     my $calendar = C4::Calendar->new( branchcode => $library->id );
342     # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
343     my $closed_day =
344         ( $dt_from->day_of_week == 6 ) ? 0
345       : ( $dt_from->day_of_week == 7 ) ? 1
346       :                                  $dt_from->day_of_week + 1;
347     $calendar->insert_week_day_holiday(
348         weekday     => $closed_day,
349         title       => 'Test holiday',
350         description => 'Test holiday'
351     );
352     $charge = $fees->accumulate_rentalcharge();
353     my $day_names = {
354         0 => 'Sunday',
355         1 => 'Monday',
356         2 => 'Tuesday',
357         3 => 'Wednesday',
358         4 => 'Thursday',
359         5 => 'Friday',
360         6 => 'Saturday'
361     };
362     my $dayname = $day_names->{$closed_day};
363     is( $charge, 5.00,
364 "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname"
365     );
366
367     # Hourly tests
368     my $issuingrule = Koha::IssuingRules->get_effective_issuing_rule(
369         {
370             categorycode => $patron->categorycode,
371             itemtype     => $itemtype->id,
372             branchcode   => $library->id
373         }
374     );
375     $issuingrule->lengthunit('hours');
376     $issuingrule->store();
377     $itemtype->rentalcharge_hourly("0.25");
378     $itemtype->store();
379
380     $dt_to   = $dt_from->clone->add( hours => 96 );
381     $fees    = Koha::Charges::Fees->new(
382         {
383             patron    => $patron,
384             library   => $library,
385             item      => $item,
386             to_date   => $dt_to,
387             from_date => $dt_from,
388         }
389     );
390
391     t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
392     $charge = $fees->accumulate_rentalcharge();
393     is( $charge, 24.00, 'Hourly rental charge calculated correctly (96h * 0.25u)' );
394
395     t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
396     $charge = $fees->accumulate_rentalcharge();
397     is( $charge, 18.00,
398 "Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname (96h - 24h * 0.25u)"
399     );
400
401     $calendar->delete_holiday( weekday => $closed_day );
402     $charge = $fees->accumulate_rentalcharge();
403     is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
404 };
405
406 $schema->storage->txn_rollback;
407 Time::Fake->reset;