From 159ca296b7004d48d8b6f8ca17d5127203ce797a Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 15 Jul 2014 10:56:39 -0400 Subject: [PATCH] Bug 6427 - Unit Tests --- t/db_dependent/Accounts.t | 189 +++++++++++++++++++++- t/db_dependent/Circulation.t | 36 +++-- t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t | 6 +- 3 files changed, 215 insertions(+), 16 deletions(-) diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t index 1c2dd5a..44b7ba4 100644 --- a/t/db_dependent/Accounts.t +++ b/t/db_dependent/Accounts.t @@ -1,16 +1,199 @@ #!/usr/bin/perl # -# This Koha test module is a stub! +# This Koha test module is a stub! # Add more tests here!!! use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 19; + +use C4::Context; BEGIN { - use_ok('C4::Accounts'); + use_ok('Koha::Database'); + use_ok('Koha::Accounts'); + use_ok('Koha::Accounts::DebitTypes'); + use_ok('Koha::Accounts::CreditTypes'); } +## Intial Setup ## +my $borrower = Koha::Database->new()->schema->resultset('Borrower')->create( + { + surname => 'Test', + categorycode => 'S', + branchcode => 'MPL', + account_balance => 0, + } +); + +my $biblio = + Koha::Database->new()->schema->resultset('Biblio') + ->create( { title => "Test Record" } ); +my $biblioitem = + Koha::Database->new()->schema->resultset('Biblioitem') + ->create( { biblionumber => $biblio->biblionumber() } ); +my $item = Koha::Database->new()->schema->resultset('Item')->create( + { + biblionumber => $biblio->biblionumber(), + biblioitemnumber => $biblioitem->biblioitemnumber(), + replacementprice => 25.00, + barcode => q{TEST_ITEM_BARCODE} + } +); + +my $issue = Koha::Database->new()->schema->resultset('Issue')->create( + { + borrowernumber => $borrower->borrowernumber(), + itemnumber => $item->itemnumber(), + } +); +## END initial setup + +ok( Koha::Accounts::DebitTypes::Fine eq 'FINE', 'Test DebitTypes::Fine' ); +ok( Koha::Accounts::DebitTypes::Lost eq 'LOST', 'Test DebitTypes::Lost' ); +ok( + Koha::Accounts::DebitTypes::IsValid('FINE'), + 'Test DebitTypes::IsValid with valid debit type' +); +ok( + !Koha::Accounts::DebitTypes::IsValid('Not A Valid Fee Type'), + 'Test DebitTypes::IsValid with an invalid debit type' +); +my $authorised_value = + Koha::Database->new()->schema->resultset('AuthorisedValue')->create( + { + category => 'MANUAL_INV', + authorised_value => 'TEST', + lib => 'Test', + } + ); +ok( Koha::Accounts::DebitTypes::IsValid('TEST'), + 'Test DebitTypes::IsValid with valid authorised value debit type' ); +$authorised_value->delete(); + +my $debit = AddDebit( + { + borrower => $borrower, + amount => 5.00, + type => Koha::Accounts::DebitTypes::Fine, + branchcode => 'MPL', + } +); +ok( $debit, "AddDebit returned a valid debit id " . $debit->id() ); + +ok( + $borrower->account_balance() == 5.00, + "Borrower's account balance updated correctly. Should be 5.00, is " . $borrower->account_balance() +); + +my $debit2 = AddDebit( + { + borrower => $borrower, + amount => 7.00, + type => Koha::Accounts::DebitTypes::Fine, + branchcode => 'MPL', + } +); + +my $credit = AddCredit( + { + borrower => $borrower, + type => Koha::Accounts::CreditTypes::Payment, + amount => 9.00, + branchcode => 'MPL', + } +); + +RecalculateAccountBalance( { borrower => $borrower } ); +ok( + sprintf( "%.2f", $borrower->account_balance() ) eq "3.00", + "RecalculateAccountBalance updated balance correctly." +); + +Koha::Database->new()->schema->resultset('AccountCredit')->create( + { + borrowernumber => $borrower->borrowernumber(), + type => Koha::Accounts::CreditTypes::Payment, + amount_paid => 3.00, + amount_remaining => 3.00, + } +); +NormalizeBalances( { borrower => $borrower } ); +ok( + $borrower->account_balance() == 0.00, + "NormalizeBalances updated balance correctly." +); + +# Adding advance credit with no balance due +$credit = AddCredit( + { + borrower => $borrower, + type => Koha::Accounts::CreditTypes::Payment, + amount => 9.00, + branchcode => 'MPL', + } +); +ok( + $borrower->account_balance() == -9, +'Adding a $9 credit for borrower with 0 balance results in a -9 dollar account balance' +); + +my $debit3 = AddDebit( + { + borrower => $borrower, + amount => 5.00, + type => Koha::Accounts::DebitTypes::Fine, + branchcode => 'MPL', + } +); +ok( + $borrower->account_balance() == -4, +'Adding a $5 debit when the balance is negative results in the debit being automatically paid, resulting in a balance of -4' +); + +my $debit4 = AddDebit( + { + borrower => $borrower, + amount => 6.00, + type => Koha::Accounts::DebitTypes::Fine, + branchcode => 'MPL', + } +); +ok( + $borrower->account_balance() == 2, +'Adding another debit ( 6.00 ) more than the negative account balance results in a partial credit and a balance due of 2.00' +); +$credit = AddCredit( + { + borrower => $borrower, + type => Koha::Accounts::CreditTypes::WriteOff, + amount => 2.00, + branchcode => 'MPL', + debit_id => $debit4->debit_id(), + } +); +ok( $borrower->account_balance() == 0, + 'WriteOff of remaining 2.00 balance succeeds' ); + +my $debit5 = DebitLostItem( + { + borrower => $borrower, + issue => $issue, + } +); +ok( $borrower->account_balance() == 25, + 'DebitLostItem adds debit for replacement price of item' ); +my $lost_credit = + CreditLostItem( { borrower => $borrower, debit => $debit5 } ); +ok( + $borrower->account_balance() == 0, + 'CreditLostItem adds credit for same about as the debit for the lost tiem' +); +## Post test cleanup ## +$issue->delete(); +$item->delete(); +$biblio->delete(); +$borrower->delete(); diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 1740795..7bd9f52 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -169,7 +169,7 @@ $dbh->do( ); # Test C4::Circulation::ProcessOfflinePayment -my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'"); +my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM account_credits WHERE amount_paid = '123.45' AND type = 'PAYMENT'"); $sth->execute(); my ( $original_count ) = $sth->fetchrow_array(); @@ -182,9 +182,9 @@ my ( $new_count ) = $sth->fetchrow_array(); ok( $new_count == $original_count + 1, 'ProcessOfflinePayment makes payment correctly' ); -C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )"); +C4::Context->dbh->do("DELETE FROM account_credits"); +C4::Context->dbh->do("DELETE FROM account_debits"); C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'"); -C4::Context->dbh->do("DELETE FROM accountlines"); { # CanBookBeRenewed tests @@ -378,8 +378,15 @@ C4::Context->dbh->do("DELETE FROM accountlines"); C4::Context->set_preference('WhenLostForgiveFine','1'); C4::Context->set_preference('WhenLostChargeReplacementFee','1'); - C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber}, - 15.00, q{}, Koha::DateUtils::output_pref($datedue) ); + C4::Overdues::UpdateFine( + { + itemnumber => $itemnumber, + borrowernumber => $renewing_borrower->{borrowernumber}, + amount => 15.00, + due => Koha::DateUtils::output_pref($datedue), + issue_id => GetItemIssue($itemnumber)->{issue_id} + } + ); LostItem( $itemnumber, 1 ); @@ -387,19 +394,28 @@ C4::Context->dbh->do("DELETE FROM accountlines"); ok( !$item->onloan(), "Lost item marked as returned has false onloan value" ); my $total_due = $dbh->selectrow_array( - 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?', + 'SELECT account_balance FROM borrowers WHERE borrowernumber = ?', undef, $renewing_borrower->{borrowernumber} ); ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' ); - C4::Context->dbh->do("DELETE FROM accountlines"); + C4::Context->dbh->do("DELETE FROM account_credits"); + C4::Context->dbh->do("DELETE FROM account_debits"); + C4::Context->dbh->do("UPDATE borrowers SET account_balance = 0"); C4::Context->set_preference('WhenLostForgiveFine','0'); C4::Context->set_preference('WhenLostChargeReplacementFee','0'); - C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber}, - 15.00, q{}, Koha::DateUtils::output_pref($datedue) ); + C4::Overdues::UpdateFine( + { + itemnumber => $itemnumber2, + borrowernumber => $renewing_borrower->{borrowernumber}, + amount => 15.00, + due => Koha::DateUtils::output_pref($datedue), + issue_id => GetItemIssue($itemnumber2)->{issue_id}, + } + ); LostItem( $itemnumber2, 0 ); @@ -407,7 +423,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" ); $total_due = $dbh->selectrow_array( - 'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?', + 'SELECT account_balance FROM borrowers WHERE borrowernumber = ?', undef, $renewing_borrower->{borrowernumber} ); diff --git a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t index 46d9ec1..b8f0708 100644 --- a/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t +++ b/t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t @@ -40,17 +40,17 @@ my %borrower_data = ( my $borrowernumber = C4::Members::AddMember( %borrower_data ); $borrower_data{borrowernumber} = $borrowernumber; -my ( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber ); +my ( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber ); is( $total, $enrolmentfee_K, "New kid pay $enrolmentfee_K" ); $borrower_data{categorycode} = 'J'; C4::Members::ModMember( %borrower_data ); -( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber ); +( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber ); is( $total, $enrolmentfee_K + $enrolmentfee_J, "Kid growing and become a juvenile, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J ) ); # Check with calling directly AddEnrolmentFeeIfNeeded C4::Members::AddEnrolmentFeeIfNeeded( 'YA', $borrowernumber ); -( $total ) = C4::Members::GetMemberAccountRecords( $borrowernumber ); +( $total ) = C4::Members::GetMemberAccountBalance( $borrowernumber ); is( $total, $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA, "Juvenile growing and become an young adult, he should pay " . ( $enrolmentfee_K + $enrolmentfee_J + $enrolmentfee_YA ) ); $dbh->rollback; -- 1.7.2.5