From fabbee5077ba778e12d46b21aac8e05204efe789 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 7 Nov 2018 17:05:25 -0300 Subject: [PATCH] Bug 19066: Tests for AddRenewal AddIssuingCharge and ChargeReserveFee This patch adds some tests that cover functions changed by this patchset. A bug in ChargeReserveFee is highlighted. Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Circulation.t | 90 +++++++++++++++++++++++++++++++++++- t/db_dependent/Reserves.t | 27 ++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 02b45c366a..2a2a9e58d5 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -17,7 +17,8 @@ use Modern::Perl; -use Test::More tests => 122; +use Test::More tests => 123; +use Test::MockModule; use Data::Dumper; use DateTime; @@ -2771,6 +2772,93 @@ $schema->storage->txn_rollback; C4::Context->clear_syspref_cache(); $cache->clear_from_cache('single_holidays'); +subtest 'AddRenewal and AddIssuingCharge tests' => sub { + + plan tests => 12; + + $schema->storage->txn_begin; + + my $issuing_charges = 15; + my $title = 'A title'; + my $author = 'Author, An'; + my $barcode = 'WHATARETHEODDS'; + + my $circ = Test::MockModule->new('C4::Circulation'); + $circ->mock( + 'GetIssuingCharges', + sub { + return $issuing_charges; + } + ); + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' }); + my $patron = $builder->build_object({ + class => 'Koha::Patrons', + value => { branchcode => $library->id } + }); + + my ( $biblionumber, $biblioitemnumber ) = add_biblio( $title, $author ); + my ( undef, undef, $item_id ) = AddItem( + { + homebranch => $library->id, + holdingbranch => $library->id, + barcode => $barcode, + replacementprice => 23.00, + itype => $itemtype->id + }, + $biblionumber + ); + my $item = Koha::Items->find( $item_id ); + + my $items = Test::MockModule->new('C4::Items'); + $items->mock( GetItem => $item->unblessed ); + my $context = Test::MockModule->new('C4::Context'); + $context->mock( userenv => { branch => $library->id } ); + + # Check the item out + AddIssue( $patron->unblessed, $item->barcode ); + + t::lib::Mocks::mock_preference( 'RenewalLog', 0 ); + my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } ); + my $old_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } ); + AddRenewal( $patron->id, $item->id, $library->id ); + my $new_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } ); + is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' ); + + t::lib::Mocks::mock_preference( 'RenewalLog', 1 ); + $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } ); + $old_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } ); + AddRenewal( $patron->id, $item->id, $library->id ); + $new_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } ); + is( $new_log_size, $old_log_size + 1, 'renew log successfully added' ); + + my $lines = Koha::Account::Lines->search({ + borrowernumber => $patron->id, + itemnumber => $item->id + }); + + is( $lines->count, 3 ); + + my $line = $lines->next; + is( $line->accounttype, 'Rent', 'The issuing charge generates an accountline' ); + is( $line->branchcode, $library->id, 'AddIssuingCharge correctly sets branchcode' ); + is( $line->description, 'Rental', 'AddIssuingCharge set a hardcoded description for the accountline' ); + + $line = $lines->next; + is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' ); + is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' ); + is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' ); + + $line = $lines->next; + is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' ); + is( $line->branchcode, $library->id, 'AddRenewal correctly sets branchcode' ); + is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' ); + + $schema->storage->txn_rollback; +}; + + sub set_userenv { my ( $library ) = @_; C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', ''); diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index 9fe293df34..60af2ae3c0 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 57; +use Test::More tests => 58; use Test::MockModule; use Test::Warn; @@ -719,6 +719,31 @@ subtest 'ReservesNeedReturns' => sub { is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' ); }; +subtest 'ChargeReserveFee tests' => sub { + + plan tests => 8; + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + + my $fee = 20; + my $title = 'A title'; + + my $context = Test::MockModule->new('C4::Context'); + $context->mock( userenv => { branch => $library->id } ); + + my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title ); + + is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object'); + ok( $line->is_debit, 'Generates a debit line' ); + is( $line->accounttype, 'Res' , 'generates Res accounttype'); + is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron'); + is( $line->amount, $fee , 'amount set correctly'); + is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly'); + is( $line->description, "Reserve Charge - $title" , 'Hardcoded description is generated'); + is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" ); +}; + sub count_hold_print_messages { my $message_count = $dbh->selectall_arrayref(q{ SELECT COUNT(*) -- 2.19.2