From 139f46042609c2887b65c315b1cf9fe3d5c182ae 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! --- Koha/Account.pm | 1 - t/db_dependent/Koha/Account.t | 29 ++++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Koha/Account.pm b/Koha/Account.pm index 3aec6a26e4..6be82c5bf0 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/Koha/Account.t b/t/db_dependent/Koha/Account.t index ff400c7552..02581915b4 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 => 4; +use Test::More tests => 5; +use Test::MockModule; use Koha::Account; use Koha::Account::Lines; @@ -225,3 +226,29 @@ subtest 'lines() tests' => 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.19.1