Bug 22435: Update unit tests

This patch updates the unit tests to reflect the changes we have made to
offset types.  We do two things:

1) Clean up calls to 'apply' to drop passing offset_type
2) Update test code to look for APPLY and CREATE instead of specific
   offset types.

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Martin Renvoize 2021-03-17 17:48:23 +00:00 committed by Jonathan Druart
parent b5a09e4654
commit 9b34ca9fe2
7 changed files with 91 additions and 65 deletions

View File

@ -329,7 +329,7 @@ subtest "Koha::Account::pay writeoff tests" => sub {
subtest "More Koha::Account::pay tests" => sub {
plan tests => 8;
plan tests => 12;
# Create a borrower
my $category = $builder->build({ source => 'Category' })->{ categorycode };
@ -363,11 +363,24 @@ subtest "More Koha::Account::pay tests" => sub {
my $account = Koha::Account->new( { patron_id => $borrowernumber } );
my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
# make the full payment
$account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' });
my $payment = $account->pay(
{
lines => [$line],
amount => $amount,
library_id => $branch,
note => 'A payment note'
}
);
my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next();
is( $offset->amount+0, -100, 'Offset amount is -100.00' );
is( $offset->type(), 'Payment', 'Offset type is Payment' );
my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
is( $offsets->count, 2, 'Two offsets recorded');
my $offset = $offsets->next;
is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
is( $offset->amount+0, -100, 'First offset amount is -100.00' );
$offset = $offsets->next;
is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
is( $offset->amount+0, -100, 'Second offset amount is -100.00' );
is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
my $stat = $schema->resultset('Statistic')->search({
branch => $branch,
@ -388,7 +401,7 @@ subtest "More Koha::Account::pay tests" => sub {
subtest "Even more Koha::Account::pay tests" => sub {
plan tests => 8;
plan tests => 12;
# Create a borrower
my $category = $builder->build({ source => 'Category' })->{ categorycode };
@ -423,11 +436,24 @@ subtest "Even more Koha::Account::pay tests" => sub {
my $account = Koha::Account->new( { patron_id => $borrowernumber } );
my $line = Koha::Account::Lines->find( $accountline->{ accountlines_id } );
# make the full payment
$account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' });
my $payment = $account->pay(
{
lines => [$line],
amount => $partialamount,
library_id => $branch,
note => 'A payment note'
}
);
my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next();
is( $offset->amount+0, -60, 'Offset amount is -60.00' );
is( $offset->type, 'Payment', 'Offset type is payment' );
my $offsets = Koha::Account::Offsets->search({ credit_id => $payment->{payment_id} });
is( $offsets->count, 2, 'Two offsets recorded');
my $offset = $offsets->next;
is( $offset->type(), 'CREATE', 'First offset type is CREATE' );
is( $offset->amount+0, -60, 'First offset amount is -60.00' );
$offset = $offsets->next;
is( $offset->type(), 'APPLY', 'Second offset type is APPLY' );
is( $offset->amount+0, -60, 'Second offset amount is -60.00' );
is( $offset->debit_id, $accountline->{accountlines_id}, 'Second offset is against the right accountline');
my $stat = $schema->resultset('Statistic')->search({
branch => $branch,

View File

@ -1374,7 +1374,7 @@ subtest "CanBookBeRenewed tests" => sub {
is( $line->issue_id, $issue->id, 'Account line issue id matches' );
my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
is( $offset->type, 'OVERDUE', 'Account offset type is Fine' );
is( $offset->type, 'CREATE', 'Account offset type is CREATE' );
is( $offset->amount+0, 15, 'Account offset amount is 15.00' );
t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
@ -3386,7 +3386,7 @@ subtest '_FixOverduesOnReturn' => sub {
C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1, 'RETURNED' );
$accountline->_result()->discard_changes();
my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'APPLY' })->next();
is( $accountline->amountoutstanding + 0, 0, 'Fine amountoutstanding has been reduced to 0' );
isnt( $accountline->status, 'UNRETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status not UNRETURNED )');

View File

@ -81,7 +81,7 @@ is( $accountline->amountoutstanding+0, 10, "Found 10.00 amount outstanding" );
is( $accountline->debit_type_code, "LOST", "Debit type is LOST" );
my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id })->next();
is( $offset->type, 'Lost Item', 'Got correct offset type' );
is( $offset->type, 'CREATE', 'Got CREATE offset type' );
is( $offset->amount+0, 10, 'Got amount of $10.00' );
$schema->storage->txn_rollback;

View File

@ -1264,19 +1264,19 @@ subtest 'Koha::Account::payout_amount() tests' => sub {
my $offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
is( $offsets->count, 4, 'Four offsets generated' );
my $offset = $offsets->next;
is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
is( $offset->amount * 1, 10, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_1->id, "Offset added against credit_1");
is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2, 'Correct amount offset against credit_1' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_2->id, "Offset added against credit_2");
is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -3, 'Correct amount offset against credit_2' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_3->id, "Offset added against credit_3");
is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -5, 'Correct amount offset against credit_3' );
my $credit_5 = $account->add_credit( { amount => 5, interface => 'commandline' } );
@ -1297,11 +1297,11 @@ subtest 'Koha::Account::payout_amount() tests' => sub {
$offsets = Koha::Account::Offsets->search( { debit_id => $payout->id } );
is( $offsets->count, 2, 'Two offsets generated' );
$offset = $offsets->next;
is( $offset->type, 'PAYOUT', 'PAYOUT offset added for payout line' );
is( $offset->type, 'CREATE', 'CREATE offset added for payout line' );
is( $offset->amount * 1, 2.50, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->credit_id, $credit_5->id, "Offset added against credit_5");
is( $offset->type, 'PAYOUT', "PAYOUT used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2.50, 'Correct amount offset against credit_5' );
$schema->storage->txn_rollback;
@ -1391,19 +1391,19 @@ subtest 'Koha::Account::payin_amount() tests' => sub {
my $offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
is( $offsets->count, 4, 'Four offsets generated' );
my $offset = $offsets->next;
is( $offset->type, 'Payment', 'Payment offset added for payin line' );
is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
is( $offset->amount * 1, -10, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_1->id, "Offset added against debit_1");
is( $offset->type, 'Payment', "Payment used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2, 'Correct amount offset against debit_1' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_2->id, "Offset added against debit_2");
is( $offset->type, 'Payment', "Payment used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -3, 'Correct amount offset against debit_2' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_3->id, "Offset added against debit_3");
is( $offset->type, 'Payment', "Payment used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -5, 'Correct amount offset against debit_3' );
my $debit_5 = $account->add_debit( { amount => 5, interface => 'commandline', type => 'OVERDUE' } );
@ -1424,11 +1424,11 @@ subtest 'Koha::Account::payin_amount() tests' => sub {
$offsets = Koha::Account::Offsets->search( { credit_id => $payin->id } );
is( $offsets->count, 2, 'Two offsets generated' );
$offset = $offsets->next;
is( $offset->type, 'Payment', 'Payment offset added for payin line' );
is( $offset->type, 'CREATE', 'CREATE offset added for payin line' );
is( $offset->amount * 1, -2.50, 'Correct offset amount recorded' );
$offset = $offsets->next;
is( $offset->debit_id, $debit_5->id, "Offset added against debit_5");
is( $offset->type, 'Payment', "Payment used for offset_type" );
is( $offset->type, 'APPLY', "APPLY used for offset_type" );
is( $offset->amount * 1, -2.50, 'Correct amount offset against debit_5' );
$schema->storage->txn_rollback;

View File

@ -208,7 +208,7 @@ subtest 'apply() tests' => sub {
$debit_1->discard_changes;
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
$credit = $credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
$credit = $credit->apply( { debits => [ $debits->as_list ] } );
is( ref($credit), 'Koha::Account::Line', '->apply returns the updated Koha::Account::Line credit object');
is( $credit->amountoutstanding * -1, 90, 'Remaining credit is correctly calculated' );
@ -232,7 +232,7 @@ subtest 'apply() tests' => sub {
is( $offsets->count, 1, 'Only one offset is generated' );
$THE_offset = $offsets->next;
is( $THE_offset->amount * 1, -90, 'Amount was calculated correctly (less than the available credit)' );
is( $THE_offset->type, 'Credit Applied', 'Defaults to \'Credit Applied\' offset type' );
is( $THE_offset->type, 'APPLY', 'Defaults to \'APPLY\' offset type' );
$debits = Koha::Account::Lines->search({ accountlines_id => $debit_1->id });
throws_ok
@ -266,7 +266,7 @@ subtest 'apply() tests' => sub {
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id, $credit->id ] } });
throws_ok {
$credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } ); }
$credit_2->apply( { debits => [ $debits->as_list ] }
'Koha::Exceptions::Account::IsNotDebit',
'->apply() rolls back if any of the passed lines is not a debit';
@ -276,7 +276,7 @@ subtest 'apply() tests' => sub {
is( $credit_2->discard_changes->amountoutstanding * -1, 20, 'No changes made' );
$debits = Koha::Account::Lines->search({ accountlines_id => { -in => [ $debit_1->id, $debit_2->id, $debit_3->id ] } });
$credit_2 = $credit_2->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
$credit_2 = $credit_2->apply( { debits => [ $debits->as_list ] } );
is( $debit_1->discard_changes->amountoutstanding * 1, 0, 'No changes to already cancelled debit' );
is( $debit_2->discard_changes->amountoutstanding * 1, 0, 'Debit cancelled' );
@ -333,7 +333,7 @@ subtest 'apply() tests' => sub {
}
);
my $debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
$credit_forgive = $credit_forgive->apply( { debits => $debits_renew, offset_type => 'Forgiven' } );
$credit_forgive = $credit_forgive->apply( { debits => $debits_renew } );
is( $called, 0, 'C4::Circulation::AddRenew NOT called when RenewAccruingItemWhenPaid enabled but credit type is "FORGIVEN"' );
$accountline = Koha::Account::Line->new(
@ -352,7 +352,7 @@ subtest 'apply() tests' => sub {
)->store();
my $credit_renew = $account->add_credit({ amount => 100, user_id => $patron->id, interface => 'commandline' });
$debits_renew = Koha::Account::Lines->search({ accountlines_id => $accountline->id })->as_list;
$credit_renew = $credit_renew->apply( { debits => $debits_renew, offset_type => 'Manual Credit' } );
$credit_renew = $credit_renew->apply( { debits => $debits_renew } );
is( $called, 1, 'RenewAccruingItemWhenPaid causes C4::Circulation::AddRenew to be called when appropriate' );
my @messages = @{$credit_renew->messages};
@ -606,7 +606,7 @@ subtest 'adjust() tests' => sub {
# Update fine to partially paid
my $debits = Koha::Account::Lines->search({ accountlines_id => $debit_2->id });
$credit->apply( { debits => [ $debits->as_list ], offset_type => 'Manual Credit' } );
$credit->apply( { debits => [ $debits->as_list ] } );
$debit_2->discard_changes;
is( $debit_2->amount * 1, 150, 'Fine amount unaffected by partial payment' );
@ -1030,7 +1030,7 @@ subtest "payout() tests" => sub {
subtest "reduce() tests" => sub {
plan tests => 29;
plan tests => 34;
$schema->storage->txn_begin;
@ -1153,12 +1153,16 @@ subtest "reduce() tests" => sub {
is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" );
my $offsets = Koha::Account::Offsets->search(
{ credit_id => $reduction->id, debit_id => $debit1->id } );
is( $offsets->count, 1, 'Only one offset is generated' );
{ credit_id => $reduction->id } );
is( $offsets->count, 2, 'Two offsets generated' );
my $THE_offset = $offsets->next;
is( $THE_offset->type, 'CREATE', 'CREATE offset added for discount line');
is( $THE_offset->amount * 1,
-5, 'Correct amount was applied against debit' );
is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" );
-5, 'Correct offset amount recorded');
$THE_offset = $offsets->next;
is( $THE_offset->type, 'APPLY', "APPLY offset added for 'DISCOUNT'" );
is( $THE_offset->amount * 1, -5, 'Correct amount offset against debt');
is( $THE_offset->debit_id, $debit1->accountlines_id, 'APPLY offset recorded the correct debit_id');
# Zero offset created when zero outstanding
# (Refund another 5 on paid debt of 20)
@ -1173,12 +1177,16 @@ subtest "reduce() tests" => sub {
is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED");
$offsets = Koha::Account::Offsets->search(
{ credit_id => $reduction->id, debit_id => $debit1->id } );
is( $offsets->count, 1, 'Only one new offset is generated' );
{ credit_id => $reduction->id } );
is( $offsets->count, 2, 'Two offsets generated' );
$THE_offset = $offsets->next;
is( $THE_offset->type, 'CREATE', 'CREATE offset added for refund line');
is( $THE_offset->amount * 1,
-5, 'Correct offset amount recorded');
$THE_offset = $offsets->next;
is( $THE_offset->type, 'APPLY', "APPLY offset added for 'REFUND'" );
is( $THE_offset->amount * 1,
0, 'Zero offset created for already paid off debit' );
is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" );
# Compound reduction should not allow more than original amount
# (Reduction of 5 + 5 + 20 > 20)
@ -1209,7 +1217,7 @@ subtest "reduce() tests" => sub {
};
subtest "cancel() tests" => sub {
plan tests => 16;
plan tests => 18;
$schema->storage->txn_begin;
@ -1295,13 +1303,15 @@ subtest "cancel() tests" => sub {
is( $account->balance() * 1, 15, "Account balance is 15" );
my $offsets = Koha::Account::Offsets->search(
{ credit_id => $cancellation->id, debit_id => $debit1->id } );
is( $offsets->count, 1, 'Only one offset is generated' );
{ credit_id => $cancellation->id } );
is( $offsets->count, 2, 'Two offsets are generated' );
my $THE_offset = $offsets->next;
is( $THE_offset->type, 'CREATE', 'CREATE offset added for cancel line');
is( $THE_offset->amount * 1, -10, 'Correct offset amount recorded' );
$THE_offset = $offsets->next;
is( $THE_offset->type, 'APPLY', "APPLY offset added" );
is( $THE_offset->amount * 1,
-10, 'Correct amount was applied against debit' );
is( $THE_offset->type, 'CANCELLATION',
"Offset type set to 'CANCELLATION'" );
$schema->storage->txn_rollback;
};

View File

@ -347,8 +347,7 @@ subtest 'store' => sub {
interface => 'test',
}
);
$credit->apply(
{ debits => [ $debts->as_list ], offset_type => 'Writeoff' } );
$credit->apply( { debits => [ $debts->as_list ] } );
# Simulate item marked as found
$item->itemlost(0)->store;
@ -431,8 +430,7 @@ subtest 'store' => sub {
interface => 'test',
}
);
$credit->apply(
{ debits => [ $debts->as_list ], offset_type => 'Payment' } );
$credit->apply( { debits => [ $debts->as_list ] } );
# Simulate item marked as found
$item->itemlost(0)->store;
@ -630,8 +628,7 @@ subtest 'store' => sub {
}
);
$payment->apply(
{ debits => [$lost_fee_line], offset_type => 'Payment' } );
$payment->apply( { debits => [$lost_fee_line] } );
# Partially write off fee
my $write_off_amount = 25;
@ -642,8 +639,7 @@ subtest 'store' => sub {
interface => 'test',
}
);
$write_off->apply(
{ debits => [$lost_fee_line], offset_type => 'Writeoff' } );
$write_off->apply( { debits => [$lost_fee_line] } );
is(
$account->balance,
@ -771,8 +767,7 @@ subtest 'store' => sub {
interface => 'test',
}
);
$payment->apply(
{ debits => [$lost_fee_line], offset_type => 'Payment' } );
$payment->apply( { debits => [$lost_fee_line] } );
is(
$account->balance,
@ -962,8 +957,7 @@ subtest 'store' => sub {
interface => 'test',
}
);
$credit->apply(
{ debits => [ $debts->as_list ], offset_type => 'Payment' } );
$credit->apply( { debits => [ $debts->as_list ] } );
# Simulate item marked as found
$item->itemlost(0)->store;
@ -1054,8 +1048,7 @@ subtest 'store' => sub {
interface => 'test',
}
);
$credit->apply(
{ debits => [ $debts->as_list ], offset_type => 'Payment' } );
$credit->apply( { debits => [ $debts->as_list ] } );
# Fine not forgiven
my $overdue = $account->add_debit(
@ -1165,8 +1158,7 @@ subtest 'store' => sub {
interface => 'test',
}
);
$credit->apply(
{ debits => [ $debts->as_list ], offset_type => 'Payment' } );
$credit->apply( { debits => [ $debts->as_list ] } );
# Add overdue
my $overdue = $account->add_debit(
@ -1196,8 +1188,7 @@ subtest 'store' => sub {
item_id => $item->itemnumber
}
);
$credit->apply(
{ debits => [$overdue], offset_type => 'Forgiven' } );
$credit->apply( { debits => [$overdue] } );
# Simulate item marked as found
$item->itemlost(0)->store;

View File

@ -110,8 +110,7 @@ subtest 'get_balance() tests' => sub {
note => 'He paid!',
description => 'Finally!',
library_id => $patron->branchcode,
account_type => 'PAYMENT',
offset_type => 'Payment'
account_type => 'PAYMENT'
}
);