From 0a7a42ec8ecf5ed77972774fa3516a759cc86260 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 21 Aug 2015 13:00:11 +0200 Subject: [PATCH] Bug 14702: Unit tests for GetReserveFee and ChargeReservesFee Content-Type: text/plain; charset=utf-8 Test plan: Run the test: t/db_dependent/Reserves_Fee.t --- t/db_dependent/Reserves_Fee.t | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100755 t/db_dependent/Reserves_Fee.t diff --git a/t/db_dependent/Reserves_Fee.t b/t/db_dependent/Reserves_Fee.t new file mode 100755 index 0000000..d43d159 --- /dev/null +++ b/t/db_dependent/Reserves_Fee.t @@ -0,0 +1,110 @@ +#!/usr/bin/perl + +# This script includes tests for GetReserveFee and ChargeReserveFee + +use Modern::Perl; +use Test::More tests => 5; +use Test::MockModule; + +use C4::Circulation; +use C4::Reserves qw|AddReserve|; +use t::lib::TestBuilder; + +my $mContext = new Test::MockModule('C4::Context'); +$mContext->mock( 'userenv', sub { + return { branch => 'CPL' }; +}); + +my $builder = t::lib::TestBuilder->new(); +my $dbh = C4::Context->dbh; # after start transaction of testbuilder + +# Category with hold fee, two patrons +$builder->build({ + source => 'Category', + value => { + categorycode => 'XYZ1', + reservefee => 2.5, + }, +}); +my $patron1 = $builder->build({ + source => 'Borrower', + value => { + categorycode => 'XYZ1', + }, +}); +my $patron2 = $builder->build({ + source => 'Borrower', + value => { + categorycode => 'XYZ1', + }, +}); + +# One biblio and two items +my $biblio = $builder->build({ + source => 'Biblio', + value => { + title => 'Title 1', + }, +}); +my $item1 = $builder->build({ + source => 'Item', + value => { + biblionumber => $biblio->{biblionumber}, + }, +}); +my $item2 = $builder->build({ + source => 'Item', + value => { + biblionumber => $biblio->{biblionumber}, + }, +}); + + +# Actual testing starts here! +# Add reserve for patron1, no fee expected +# Note: AddReserve calls GetReserveFee and ChargeReserveFee +my $acc1 = acctlines( $patron1->{borrowernumber} ); +my $res1 = addreserve( $patron1->{borrowernumber} ); +is( acctlines( $patron1->{borrowernumber} ), $acc1, 'No fee charged for patron 1' ); + +# Issue item1 to patron1. Since there is still a reserve too, we should +# expect a charge for patron2. +C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter +my $acc2 = acctlines( $patron2->{borrowernumber} ); +my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} ); +is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' ); +C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->{title} ); +is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' ); + +# If we delete the reserve, there should be no charge +$dbh->do( "DELETE FROM reserves WHERE reserve_id=?", undef, ( $res1 ) ); +$fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} ); +is( $fee, 0, 'Patron 2 will not be charged now' ); + +# If we delete the second item, there should be a charge +$dbh->do( "DELETE FROM items WHERE itemnumber=?", undef, ( $item2->{itemnumber} ) ); +$fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} ); +is( $fee > 0, 1, 'Patron 2 should be charged again this time' ); +# End of tests + + +sub acctlines { #calculate number of accountlines for a patron + my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) ); + return $temp[0]; +} + +sub addreserve { + return AddReserve( + 'CPL', + $_[0], + $biblio->{biblionumber}, + undef, + '1', + undef, + undef, + '', + $biblio->{title}, + undef, + '' + ); +} -- 1.7.10.4