Koha/t/db_dependent/Circulation/CalcFine.t
Kyle M Hall 1763b136d1
Bug 34279: Don't enforce overduefinescap unless it is greater than 0
When creating a circ rule, we can set overduefinescap to blank or 0 and no cap is enforced. If we edit that rule, the blank/0 is converted to "0.00" which perl considers true, thus zero-ing out any calculated fine.

Considering we've always ignored an overdue fines cap of 0, we should also ignore 0.00. However, perl is evaluating it as a string which makes it true instead of false as 0 is.

Test Plan:
1) Apply the first patch ( unit tests )
2) prove t/db_dependent/Circulation/CalcFine.t
3) Note the test fails
4) Apply the second patch as well
5) prove t/db_dependent/Circulation/CalcFine.t
6) Note the test passes

Test Plan 2:
1) Create an all/all/all rule with an overduefinescap of 0.00, with a
   daily fine. Enable CalculateFinesOnReturn
2) Backdate a checkout so it is overdue
3) Return this item, note the lack of a fine
4) Apply this patch set
5) Backdate a checkout and return it again
6) Note the fine is generated!

Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2023-07-19 13:00:41 -03:00

374 lines
10 KiB
Perl
Executable file

#!/usr/bin/perl
use Modern::Perl;
use Test::More tests => 7;
use Test::Warn;
use C4::Context;
use C4::Overdues qw( CalcFine );
use Koha::DateUtils qw( dt_from_string );
use t::lib::TestBuilder;
use t::lib::Mocks;
my $schema = Koha::Database->schema;
$schema->storage->txn_begin;
our $dbh = C4::Context->dbh;
$dbh->do(q|DELETE FROM issues|);
t::lib::Mocks::mock_preference('item-level_itypes', '1');
my $builder = t::lib::TestBuilder->new();
my $branch = $builder->build(
{
source => 'Branch',
}
);
my $category = $builder->build(
{
source => 'Category',
}
);
my $patron = $builder->build(
{
source => 'Borrower',
value => {
categorycode => $category->{categorycode},
branchcode => $branch->{branchcode},
},
}
);
my $itemtype = $builder->build(
{
source => 'Itemtype',
value => {
defaultreplacecost => 6,
},
}
);
my $item = $builder->build_sample_item(
{
library => $branch->{branchcode},
replacementprice => '5.00',
itype => $itemtype->{itemtype},
}
);
subtest 'Test basic functionality' => sub {
plan tests => 1;
Koha::CirculationRules->set_rules(
{
branchcode => undef,
categorycode => undef,
itemtype => undef,
rules => {
fine => '1.00',
lengthunit => 'days',
finedays => 0,
firstremind => 0,
chargeperiod => 1,
overduefinescap => undef,
cap_fine_to_replacement_price => 0,
}
},
);
my $start_dt = DateTime->new(
year => 2000,
month => 1,
day => 1,
);
my $end_dt = DateTime->new(
year => 2000,
month => 1,
day => 30,
);
my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( $amount, 29, 'Amount is calculated correctly' );
teardown();
};
subtest 'Overdue fines cap should be disabled when value is 0' => sub {
plan tests => 1;
Koha::CirculationRules->set_rules(
{
branchcode => undef,
categorycode => undef,
itemtype => undef,
rules => {
fine => '1.00',
lengthunit => 'days',
finedays => 0,
firstremind => 0,
chargeperiod => 1,
overduefinescap => "0",
cap_fine_to_replacement_price => 0,
}
},
);
my $start_dt = DateTime->new(
year => 2000,
month => 1,
day => 1,
);
my $end_dt = DateTime->new(
year => 2000,
month => 1,
day => 30,
);
my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( $amount, 29, 'Amount is calculated correctly' );
teardown();
};
subtest 'Overdue fines cap should be disabled when value is 0.00' => sub {
plan tests => 1;
Koha::CirculationRules->set_rules(
{
branchcode => undef,
categorycode => undef,
itemtype => undef,
rules => {
fine => '1.00',
lengthunit => 'days',
finedays => 0,
firstremind => 0,
chargeperiod => 1,
overduefinescap => "0.00",
cap_fine_to_replacement_price => 0,
}
},
);
my $start_dt = DateTime->new(
year => 2000,
month => 1,
day => 1,
);
my $end_dt = DateTime->new(
year => 2000,
month => 1,
day => 30,
);
my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( $amount, 29, 'Amount is calculated correctly' );
teardown();
};
subtest 'Test with fine amount empty' => sub {
plan tests => 1;
Koha::CirculationRules->set_rules(
{
branchcode => undef,
categorycode => undef,
itemtype => undef,
rules => {
fine => '',
lengthunit => 'days',
finedays => 0,
firstremind => 0,
chargeperiod => 1,
overduefinescap => undef,
cap_fine_to_replacement_price => 1,
},
}
);
my $start_dt = DateTime->new(
year => 2000,
month => 1,
day => 1,
);
my $end_dt = DateTime->new(
year => 2000,
month => 1,
day => 30,
);
warning_is {
my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
}
undef, "No warning when fine amount is ''";
teardown();
};
subtest 'Test cap_fine_to_replacement_price' => sub {
plan tests => 2;
t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
Koha::CirculationRules->set_rules(
{
branchcode => undef,
categorycode => undef,
itemtype => undef,
rules => {
fine => '1.00',
lengthunit => 'days',
finedays => 0,
firstremind => 0,
chargeperiod => 1,
overduefinescap => undef,
cap_fine_to_replacement_price => 1,
},
}
);
my $start_dt = DateTime->new(
year => 2000,
month => 1,
day => 1,
);
my $end_dt = DateTime->new(
year => 2000,
month => 1,
day => 30,
);
my $item = $builder->build_sample_item(
{
library => $branch->{branchcode},
replacementprice => 5,
itype => $itemtype->{itemtype},
}
);
my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( int($amount), 5, 'Amount is calculated correctly' );
# Use default replacement cost (useDefaultReplacementCost) is item's replacement price is 0
$item->replacementprice(0)->store;
($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( int($amount), 6, 'Amount is calculated correctly' );
teardown();
};
subtest 'Test cap_fine_to_replacement_pricew with overduefinescap' => sub {
plan tests => 2;
t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
Koha::CirculationRules->set_rules(
{
branchcode => undef,
categorycode => undef,
itemtype => undef,
rules => {
fine => '1.00',
lengthunit => 'days',
finedays => 0,
firstremind => 0,
chargeperiod => 1,
overduefinescap => 3,
cap_fine_to_replacement_price => 1,
},
}
);
my $start_dt = DateTime->new(
year => 2000,
month => 1,
day => 1,
);
my $end_dt = DateTime->new(
year => 2000,
month => 1,
day => 30,
);
my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( int($amount), 3, 'Got the lesser of overduefinescap and replacement price where overduefinescap < replacement price' );
Koha::CirculationRules->set_rule({ rule_name => 'overduefinescap', rule_value => 6, branchcode => undef, categorycode => undef, itemtype => undef });
($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( int($amount), 5, 'Get the lesser of overduefinescap and replacement price where overduefinescap > replacement price' );
teardown();
};
subtest 'Recall overdue fines' => sub {
plan tests => 2;
Koha::CirculationRules->set_rules(
{
branchcode => undef,
categorycode => undef,
itemtype => undef,
rules => {
fine => '1.00',
lengthunit => 'days',
finedays => 0,
firstremind => 0,
chargeperiod => 1,
recall_overdue_fine => '5.00',
},
}
);
my $start_dt = DateTime->new(
year => 2000,
month => 1,
day => 1,
);
my $end_dt = DateTime->new(
year => 2000,
month => 1,
day => 6,
);
my $recall = Koha::Recall->new({
patron_id => $patron->{borrowernumber},
created_date => dt_from_string,
biblio_id => $item->biblionumber,
pickup_library_id => $branch->{branchcode},
item_id => $item->itemnumber,
expiration_date => undef,
item_level => 1
})->store;
$recall->set_overdue;
my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( int($amount), 25, 'Use recall fine amount specified in circulation rules' );
$recall->set_fulfilled;
($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
is( int($amount), 5, 'With no recall, use normal fine amount' );
teardown();
};
sub teardown {
$dbh->do(q|DELETE FROM circulation_rules|);
}