From faafdb53b7c3614420bae1df1b023ad4cb64db2e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 8 Nov 2018 11:15:25 -0300 Subject: [PATCH] Bug 19066: Add tests for Koha::Account->pay This patch adds tests for Koha::Account->pay, to make sure the library_id param is used to set the branchcode attribute. The use of userenv to set branchcode if library_id not passed is removed. Responsability is left to the callers to pass library_id. To test: - Run: $ kshell k$ prove t/db_dependent/Koha/Account.t => SUCCESS: Tests pass! Signed-off-by: Martin Renvoize Signed-off-by: Nick Clemens --- Koha/Account.pm | 1 - t/db_dependent/Accounts.t | 2 +- t/db_dependent/Koha/Account.t | 29 ++++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index 16aef7eaa1..c52e5c53f6 100644 --- a/Koha/Account.pm +++ b/Koha/Account.pm @@ -81,7 +81,6 @@ sub pay { my $offset_type = $params->{offset_type} || $type eq 'writeoff' ? 'Writeoff' : 'Payment'; my $userenv = C4::Context->userenv; - $library_id ||= $userenv ? $userenv->{branch} : undef; my $patron = Koha::Patrons->find( $self->{patron_id} ); diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t index b52f45817c..ae71655179 100644 --- a/t/db_dependent/Accounts.t +++ b/t/db_dependent/Accounts.t @@ -292,7 +292,7 @@ subtest "Koha::Account::pay tests" => sub { is( $payment->amount(), '-42.000000', "Payment paid the specified fine" ); $line3 = Koha::Account::Lines->find( $line3->id ); is( $line3->amountoutstanding, '0.000000', "Specified fine is paid" ); - is( $payment->branchcode, $userenv_branchcode, 'Branchcode set correctly' ); + is( $payment->branchcode, undef, 'branchcode passed, then undef' ); }; subtest "Koha::Account::pay particular line tests" => sub { diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t index c828469574..9fbef7c124 100755 --- a/t/db_dependent/Koha/Account.t +++ b/t/db_dependent/Koha/Account.t @@ -19,7 +19,8 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; +use Test::MockModule; use Koha::Account; use Koha::Account::Lines; @@ -404,3 +405,29 @@ subtest 'reconcile_balance' => sub { $schema->storage->txn_rollback; }; }; + +subtest 'pay() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $account = $patron->account; + + my $context = Test::MockModule->new('C4::Context'); + $context->mock( 'userenv', { branch => $library->id } ); + + my $credit_1_id = $account->pay({ amount => 200 }); + my $credit_1 = Koha::Account::Lines->find( $credit_1_id ); + + is( $credit_1->branchcode, undef, 'No branchcode is set if library_id was not passed' ); + + my $credit_2_id = $account->pay({ amount => 150, library_id => $library->id }); + my $credit_2 = Koha::Account::Lines->find( $credit_2_id ); + + is( $credit_2->branchcode, $library->id, 'branchcode set because library_id was passed' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5