From 16d89103d0628f41bed3b18d9a85ce67f1cd76b1 Mon Sep 17 00:00:00 2001
From: Jesse Weaver <pianohacker@gmail.com>
Date: Tue, 7 Jul 2015 13:02:18 -0600
Subject: [PATCH] Bug 14464 - (QA followup) add unit tests

This followup adds several tests to t/db_dependent/Reserves.t.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 C4/Reserves.pm            |  2 +-
 t/db_dependent/Reserves.t | 81 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index 96570df..2beea87 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -241,7 +241,7 @@ sub AddReserve {
     }
 
     #}
-    ($const eq "o" || $const eq "e") or return;   # FIXME: why not have a useful return value?
+    ($const eq "o" || $const eq "e") or return $reserve_id;
     $query = qq{
         INSERT INTO reserveconstraints
             (borrowernumber,biblionumber,reservedate,biblioitemnumber)
diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t
index fa0cf94..99504b8 100755
--- a/t/db_dependent/Reserves.t
+++ b/t/db_dependent/Reserves.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 56;
+use Test::More tests => 63;
 
 use MARC::Record;
 use DateTime::Duration;
@@ -528,6 +528,85 @@ $dbh->do(
 );
 ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
 
+# Tests for bug 14464
+
+$dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
+my ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
+ok( !$bz14464_fines, 'Bug 14464 - No fines at beginning' );
+
+# First, test cancelling a reserve when there's no charge configured.
+t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
+
+my $bz14464_reserve = AddReserve(
+    'CPL',
+    $borrowernumber,
+    $bibnum,
+    'a',
+    undef,
+    '1',
+    undef,
+    undef,
+    '',
+    $title,
+    $itemnumber,
+    'W'
+);
+
+ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
+
+CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
+
+( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
+ok( !$bz14464_fines, 'Bug 14464 - No fines after cancelling reserve with no charge configured' );
+
+# Then, test cancelling a reserve when there's no charge desired.
+t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
+
+$bz14464_reserve = AddReserve(
+    'CPL',
+    $borrowernumber,
+    $bibnum,
+    'a',
+    undef,
+    '1',
+    undef,
+    undef,
+    '',
+    $title,
+    $itemnumber,
+    'W'
+);
+
+ok( $bz14464_reserve, 'Bug 14464 - 2nd reserve correctly created' );
+
+CancelReserve({ reserve_id => $bz14464_reserve });
+
+( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
+ok( !$bz14464_fines, 'Bug 14464 - No fines after cancelling reserve with no charge desired' );
+
+# Finally, test cancelling a reserve when there's a charge desired and configured.
+$bz14464_reserve = AddReserve(
+    'CPL',
+    $borrowernumber,
+    $bibnum,
+    'a',
+    undef,
+    '1',
+    undef,
+    undef,
+    '',
+    $title,
+    $itemnumber,
+    'W'
+);
+
+ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
+
+CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
+
+( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
+is( int( $bz14464_fines ), 42, 'Bug 14464 - Fine applied after cancelling reserve with charge desired and configured' );
+
 $dbh->rollback;
 
 sub count_hold_print_messages {
-- 
2.1.0