From 729211f84ee59444273ff7065ae5a7b96196365c Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 11 Nov 2019 15:45:59 +0000 Subject: [PATCH] Bug 24009: Add Unit Tests Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/Account.t | 164 +++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 4 deletions(-) diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t index 02383c7d80..00ea9b9006 100755 --- a/t/db_dependent/Koha/Account.t +++ b/t/db_dependent/Koha/Account.t @@ -24,6 +24,7 @@ use Test::MockModule; use Test::Exception; use Koha::Account; +use Koha::Account::DebitTypes; use Koha::Account::Lines; use Koha::Account::Offsets; @@ -53,7 +54,7 @@ subtest 'new' => sub { subtest 'outstanding_debits() tests' => sub { - plan tests => 22; + plan tests => 23; $schema->storage->txn_begin; @@ -62,9 +63,16 @@ subtest 'outstanding_debits() tests' => sub { my @generated_lines; push @generated_lines, $account->add_debit({ amount => 1, interface => 'commandline', type => 'OVERDUE' }); - push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'OVERDUE' }); - push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'OVERDUE' }); - push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'OVERDUE' }); + push @generated_lines, $account->add_debit({ amount => 2, interface => 'commandline', type => 'RESERVE' }); + push @generated_lines, $account->add_debit({ amount => 3, interface => 'commandline', type => 'RENT' }); + Koha::Account::DebitTypes->find_or_create( + { + code => 'COPY', + description => 'Fee for copie', + is_system => 0 + } + )->store; + push @generated_lines, $account->add_debit({ amount => 4, interface => 'commandline', type => 'COPY' }); my $lines = $account->outstanding_debits(); my @lines_arr = $account->outstanding_debits(); @@ -142,6 +150,154 @@ subtest 'outstanding_debits() tests' => sub { is( $lines->count, 0, 'No credits are confused with debits because of the amountoutstanding value' ); $schema->storage->txn_rollback; + + subtest 'filter_by' => sub { + plan tests => 21; + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $account = $patron->account; + + my $due = 1; + my $res = 3; + my $rent = 5; + my $manual = 7; + + my @generated_lines; + push @generated_lines, $account->add_debit({ amount => $due, interface => 'commandline', type => 'OVERDUE' }); + push @generated_lines, $account->add_debit({ amount => $res, interface => 'commandline', type => 'RESERVE' }); + push @generated_lines, $account->add_debit({ amount => $rent, interface => 'commandline', type => 'RENT' }); + Koha::Account::DebitTypes->find_or_create( + { + code => 'COPY', + description => 'Fee for copie', + is_system => 0 + } + )->store; + push @generated_lines, $account->add_debit({ amount => $manual, interface => 'commandline', type => 'COPY' }); + + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 ); + t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 ); + my ( $total, $non_issues_charges ) = ( $account->balance, $account->outstanding_debits({filter_by => 'blocks_issue'})->total_outstanding ); + my $other_charges = $total - $non_issues_charges; + is( + $account->balance, + $due + $res + $rent + $manual, + 'Total charges should be Due + Res + Rent + Manual' + ); + is( $non_issues_charges, $due, + 'If 0|0|0 there should only have been overdue charges' ); + is( $other_charges, 15, 'If 0|0|0 there should only have other charges' ); + + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 ); + t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 ); + ( $total, $non_issues_charges ) = ( $account->balance, $account->outstanding_debits({filter_by => 'blocks_issue'})->total_outstanding ); + $other_charges = $total - $non_issues_charges; + is( + $total, + $due + $res + $rent + $manual, + 'Total charges should be Res + Rent + Manual' + ); + is( $non_issues_charges, $due + $manual, + 'If 0|0|1 Only Overdue + Manual should be a non issue charge' ); + is( + $other_charges, + $res + $rent, + 'If 0|0|1 Res + Rent should be other charges' + ); + + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 ); + t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 ); + ( $total, $non_issues_charges ) = ( $account->balance, $account->outstanding_debits({filter_by => 'blocks_issue'})->total_outstanding ); + $other_charges = $total - $non_issues_charges; + is( + $total, + $due + $res + $rent + $manual, + 'Total charges should be Res + Rent + Manual' + ); + is( $non_issues_charges, $due + $rent, + 'If 0|1|0 Only Overdue + Rental should be a non issue charge' ); + is( + $other_charges, + $res + $manual, + 'If 0|1|0 Rent + Manual should be other charges' + ); + + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 0 ); + t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 ); + ( $total, $non_issues_charges ) = ( $account->balance, $account->outstanding_debits({filter_by => 'blocks_issue'})->total_outstanding ); + $other_charges = $total - $non_issues_charges; + is( + $total, + $due + $res + $rent + $manual, + 'Total charges should be Res + Rent + Manual' + ); + is( + $non_issues_charges, + $due + $rent + $manual, + 'If 0|1|1 Due + Rent + Manual should be non issues charges' + ); + is( $other_charges, $res, 'If 0|1|1 there should only have other charges' ); + + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 ); + t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 0 ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 ); + ( $total, $non_issues_charges ) = ( $account->balance, $account->outstanding_debits({filter_by => 'blocks_issue'})->total_outstanding ); + $other_charges = $total - $non_issues_charges; + is( + $total, + $due + $res + $rent + $manual, + 'Total charges should be Res + Rent + Manual' + ); + is( $non_issues_charges, $due + $res, + 'If 1|0|0 Only Overdue + Res should be non issues charges' ); + is( + $other_charges, + $rent + $manual, + 'If 1|0|0 Rent + Manual should be other charges' + ); + + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 ); + t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 0 ); + ( $total, $non_issues_charges ) = ( $account->balance, $account->outstanding_debits({filter_by => 'blocks_issue'})->total_outstanding ); + $other_charges = $total - $non_issues_charges; + is( + $total, + $due + $res + $rent + $manual, + 'Total charges should be Res + Rent + Manual' + ); + is( + $non_issues_charges, + $due + $res + $rent, + 'If 1|1|0 Overdue + Res + Rent should be non issues charges' + ); + is( $other_charges, $manual, + 'If 1|1|0 Only Manual should be other charges' ); + + t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', 1 ); + t::lib::Mocks::mock_preference( 'RentalsInNoissuesCharge', 1 ); + t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', 1 ); + ( $total, $non_issues_charges ) = ( $account->balance, $account->outstanding_debits({filter_by => 'blocks_issue'})->total_outstanding ); + $other_charges = $total - $non_issues_charges; + is( + $total, + $due + $res + $rent + $manual, + 'Total charges should be Res + Rent + Manual' + ); + is( + $non_issues_charges, + $due + $res + $rent + $manual, + 'If 1|1|1 Res + Rent + Manual should be non issues charges' + ); + is( $other_charges, 0, 'If 1|1|1 there should not have any other charges' ); + + $schema->storage->txn_rollback; + } }; subtest 'outstanding_credits() tests' => sub { -- 2.24.0