From 211578544f8bb21d9c899963fc108bd753befe70 Mon Sep 17 00:00:00 2001
From: Mark Tompsett <mtompset@hotmail.com>
Date: Wed, 6 May 2015 13:56:30 -0400
Subject: [PATCH] [SIGNED OFF] Bug 14103: Improved testing coverage

As per Chris' comment #17, this improves the testing coverage.

Same test plan as comment #16, but add:

TEST PLAN SUPPLEMENT
--------------------
1) $ prove -v t/db_dependent/Borrower_Discharge.t
   -- notice only 5 tests, and generate_as_pdf not tested.
2) Apply this patch.
3) Retest as per comment #16
   -- this will test the error and no error cases
      tweaked in Koha/Borrower/Discharge.pm
4) remove PDF::HTML
   $ sudo apt-get purge libpdf-fromhtml-perl
5) $ prove -v t/db_dependent/Borrower_Discharge.t
   -- 7 tests, all successful
6) add PDF::HTML
   $ sudo dpkg -i /path/to/libpdf-fromhtml-perl...
   -- depends on how you get it. vary accordingly.
7) $ prove -v t/db_dependent/Borrower_Discharge.t
   -- 7 tests, all successful
   -- Note that is was a different 7th test. :)
8) koha qa test tools

Signed-off-by: Chris Nighswonger <cnighswonger@foundations.edu>
---
 Koha/Borrower/Discharge.pm          | 21 ++++++++++++++++-----
 t/db_dependent/Borrower_Discharge.t | 17 +++++++++++++++++
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/Koha/Borrower/Discharge.pm b/Koha/Borrower/Discharge.pm
index 9ca1587..f39475b 100644
--- a/Koha/Borrower/Discharge.pm
+++ b/Koha/Borrower/Discharge.pm
@@ -3,7 +3,9 @@ package Koha::Borrower::Discharge;
 use Modern::Perl;
 use CGI;
 use File::Temp qw( :POSIX );
+use Carp;
 
+use C4::Templates qw ( gettemplate );
 use C4::Members qw( GetPendingIssues );
 use C4::Reserves qw( GetReservesFromBorrowernumber CancelReserve );
 
@@ -113,11 +115,20 @@ sub generate_as_pdf {
     open my $html_fh, '>:encoding(utf8)', $html_path;
     say $html_fh $html_content;
     close $html_fh;
-    require PDF::FromHTML;
-    my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );
-    $pdf->load_file( $html_path );
-    $pdf->convert;
-    $pdf->write_file( $pdf_path );
+    my $output = eval { require PDF::FromHTML; return; } || $@;
+    if ($output && $params->{testing}) {
+        carp $output;
+        $pdf_path = undef;
+    }
+    elsif ($output) {
+        die $output;
+    }
+    else {
+        my $pdf = PDF::FromHTML->new( encoding => 'utf-8' );
+        $pdf->load_file( $html_path );
+        $pdf->convert;
+        $pdf->write_file( $pdf_path );
+    }
 
     return $pdf_path;
 }
diff --git a/t/db_dependent/Borrower_Discharge.t b/t/db_dependent/Borrower_Discharge.t
index 7d22b0b..cb5179f 100644
--- a/t/db_dependent/Borrower_Discharge.t
+++ b/t/db_dependent/Borrower_Discharge.t
@@ -2,6 +2,7 @@
 
 use Modern::Perl;
 use Test::More;
+use Test::Warn;
 use MARC::Record;
 
 use C4::Biblio qw( AddBiblio );
@@ -46,6 +47,22 @@ AddReturn( $barcode );
 # Discharge possible without issue
 is( Koha::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrowernumber }), 1, 'A patron without issues can be discharged' );
 
+is(Koha::Borrower::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
+
+# Check if PDF::FromHTML is installed.
+my $check = eval { require PDF::FromHTML; };
+
+# Tests for if PDF::FromHTML is installed
+if ($check) {
+    isnt( Koha::Borrower::Discharge::generate_as_pdf({ borrowernumber => $borrowernumber }), undef, "Temporary PDF generated." );
+}
+# Tests for if PDF::FromHTML is not installed
+else {
+    warning_like { Koha::Borrower::Discharge::generate_as_pdf({ borrowernumber => $borrowernumber, testing => 1 }) }
+          [ qr/Can't locate PDF\/FromHTML.pm in \@INC/ ],
+          "Expected failure because of missing PDF::FromHTML.";
+}
+
 # FIXME
 # At this point, there is a problem with the AutoCommit off
 # The transaction is bloked into DBIx::Class::Storage::DBI::_dbh_execute
-- 
1.9.1