From e4bb0ea910d1723351c44921b8e783c0e67f392f 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 Signed-off-by: Kyle M Hall Signed-off-by: Sean McGarvey --- t/db_dependent/Accounts.t | 331 ++++++++++++---------- t/db_dependent/Circulation.t | 36 ++- t/db_dependent/Members/AddEnrolmentFeeIfNeeded.t | 6 +- 3 files changed, 211 insertions(+), 162 deletions(-) diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t index a167f02..4351e7e 100644 --- a/t/db_dependent/Accounts.t +++ b/t/db_dependent/Accounts.t @@ -19,165 +19,198 @@ use strict; use warnings; -use Test::More tests => 15; -use Test::Warn; +use Test::More tests => 19; + +use C4::Context; + +my $dbh = C4::Context->dbh; +$dbh->{RaiseError}=1; +$dbh->{AutoCommit}=0; BEGIN { - use_ok('C4::Accounts'); - use_ok('Koha::Object'); - use_ok('Koha::Borrower'); - use_ok('Data::Dumper'); + use_ok('Koha::Database'); + use_ok('Koha::Accounts'); + use_ok('Koha::Accounts::DebitTypes'); + use_ok('Koha::Accounts::CreditTypes'); } -can_ok( 'C4::Accounts', - qw( recordpayment - makepayment - getnextacctno +## Intial Setup ## +my $borrower = Koha::Database->new()->schema->resultset('Borrower')->create( + { + surname => 'Test', + categorycode => 'S', + branchcode => 'MPL', + account_balance => 0, + } +); - chargelostitem - manualinvoice - getcharges - ModNote - getcredits - getrefunds - ReversePayment - recordpayment_selectaccts - makepartialpayment - WriteOffFee ) +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 $dbh = C4::Context->dbh; -$dbh->{RaiseError}=1; -$dbh->{AutoCommit}=0; -$dbh->do(q|DELETE FROM accountlines|); -$dbh->do(q|DELETE FROM borrowers|); -$dbh->do(q|DELETE FROM issues|); - -# Mock userenv -local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; -my $userenv; -*C4::Context::userenv = \&Mock_userenv; -$userenv = { flags => 1, id => 'my_userid', branch => 'CPL' }; - -# A Borrower for the tests ---------------------- -my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode(); -my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode(); - -my $borrower = Koha::Borrower->new( { - cardnumber => '1234567890', - surname => 'McFly', - firstname => 'Marty', -} ); -$borrower->categorycode( $categorycode ); -$borrower->branchcode( $branchcode ); -$borrower->store; - -my $sth = $dbh->prepare( - "INSERT INTO accountlines ( - borrowernumber, - amountoutstanding ) - VALUES ( ?, ? )" -); -$sth->execute($borrower->borrowernumber, '100'); -$sth->execute($borrower->borrowernumber, '200'); - -$sth = $dbh->prepare("SELECT count(*) FROM accountlines"); -$sth->execute; -my $count = $sth->fetchrow_array; -is ($count, 2, 'There is 2 lines as expected'); - -# Testing recordpayment ------------------------- -# There is $100 in the account -$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); -my $amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); -my $amountleft = 0; -for my $line ( @$amountoutstanding ) { - $amountleft += $line; -} -ok($amountleft == 300, 'The account has 300$ as expected' ); - -# We make a $20 payment -my $borrowernumber = $borrower->borrowernumber; -my $data = '20.00'; -my $sys_paytype; -my $payment_note = '$20.00 payment note'; -recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); -# There is now $280 in the account -$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); -$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); -$amountleft = 0; -for my $line ( @$amountoutstanding ) { - $amountleft += $line; -} -ok($amountleft == 280, 'The account has $280 as expected' ); -# Is the payment note well registered -$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); -$sth->execute($borrower->borrowernumber); -my $note = $sth->fetchrow_array; -is($note,'$20.00 payment note', '$20.00 payment note is registered'); - -# We make a -$30 payment (a NEGATIVE payment) -$data = '-30.00'; -$payment_note = '-$30.00 payment note'; -recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); -# There is now $310 in the account -$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); -$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); -$amountleft = 0; -for my $line ( @$amountoutstanding ) { - $amountleft += $line; -} -ok($amountleft == 310, 'The account has $310 as expected' ); -# Is the payment note well registered -$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); -$sth->execute($borrower->borrowernumber); -$note = $sth->fetchrow_array; -is($note,'-$30.00 payment note', '-$30.00 payment note is registered'); - -#We make a $150 payment ( > 1stLine ) -$data = '150.00'; -$payment_note = '$150.00 payment note'; -recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); -# There is now $160 in the account -$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); -$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); -$amountleft = 0; -for my $line ( @$amountoutstanding ) { - $amountleft += $line; -} -ok($amountleft == 160, 'The account has $160 as expected' ); -# Is the payment note well registered -$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); -$sth->execute($borrower->borrowernumber); -$note = $sth->fetchrow_array; -is($note,'$150.00 payment note', '$150.00 payment note is registered'); - -#We make a $200 payment ( > amountleft ) -$data = '200.00'; -$payment_note = '$200.00 payment note'; -recordpayment($borrowernumber, $data, $sys_paytype, $payment_note); -# There is now -$40 in the account -$sth = $dbh->prepare("SELECT amountoutstanding FROM accountlines WHERE borrowernumber=?"); -$amountoutstanding = $dbh->selectcol_arrayref($sth, {}, $borrower->borrowernumber); -$amountleft = 0; -for my $line ( @$amountoutstanding ) { - $amountleft += $line; -} -ok($amountleft == -40, 'The account has -$40 as expected, (credit situation)' ); -# Is the payment note well registered -$sth = $dbh->prepare("SELECT note FROM accountlines WHERE borrowernumber=? ORDER BY accountlines_id DESC LIMIT 1"); -$sth->execute($borrower->borrowernumber); -$note = $sth->fetchrow_array; -is($note,'$200.00 payment note', '$200.00 payment note is registered'); +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() +); -$dbh->rollback; +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' +); -# Sub ------------------------------------------- +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' +); -# C4::Context->userenv -sub Mock_userenv { - return $userenv; -} \ No newline at end of file +## 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 30a5fe7..4084d87 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -170,7 +170,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(); @@ -183,9 +183,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 @@ -429,8 +429,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 ); @@ -438,19 +445,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 ); @@ -458,7 +474,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