From 1ae7aabe4a261436392b9ffdd607eaad8e9ae63e Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Thu, 11 Apr 2013 12:36:27 +0000
Subject: [PATCH] Bug 6874: Add unit tests for C4::UploadedFiles

Add unit tests for C4::UploadedFiles and move a variable
declaration at subroutine level instead of file level.
Add dependency to Test::CGI::Multipart

Still works, and the newly-provided unit tests have good test
coverage:
C4/UploadedFiles.pm 90.7    65.0    66.7    100.0   100.0    0.2    86.4

Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
Signed-off-by: Mark Tompsett <mtompset@hotmail.com>
---
 C4/Installer/PerlDependencies.pm    |  5 +++++
 cataloguing/value_builder/upload.pl |  6 ++---
 t/db_dependent/UploadedFiles.t      | 44 +++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100644 t/db_dependent/UploadedFiles.t

diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm
index 79cbc14..2e30fe3 100644
--- a/C4/Installer/PerlDependencies.pm
+++ b/C4/Installer/PerlDependencies.pm
@@ -741,6 +741,11 @@ our $PERL_DEPS = {
         'usage'    => 'Discharge generation',
         'required' => '0',
         'min_ver'  => '0.31',
+    }
+    'Test::CGI::Multipart' => {
+        'usage'    => 'Tests',
+        'required' => '0',
+        'min_ver'  => '0.0.3',
     },
 };
 
diff --git a/cataloguing/value_builder/upload.pl b/cataloguing/value_builder/upload.pl
index 03ad77b..7eb15e5 100755
--- a/cataloguing/value_builder/upload.pl
+++ b/cataloguing/value_builder/upload.pl
@@ -26,8 +26,6 @@ use C4::Context;
 use C4::Output;
 use C4::UploadedFiles;
 
-my $upload_path = C4::Context->preference('uploadPath');
-
 sub plugin_parameters {
     my ( $dbh, $record, $tagslib, $i, $tabloop ) = @_;
     return "";
@@ -115,6 +113,7 @@ sub plugin {
             -size => 50,
         );
 
+        my $upload_path = C4::Context->preference('uploadPath');
         my $dirs_tree = [ {
             name => '/',
             value => '/',
@@ -137,7 +136,8 @@ sub plugin {
 
 # Build a hierarchy of directories
 sub finddirs {
-    my $base = shift || $upload_path;
+    my $base = shift;
+    my $upload_path = C4::Context->preference('uploadPath');
     my $found = 0;
     my @dirs;
     my @files = glob("$base/*");
diff --git a/t/db_dependent/UploadedFiles.t b/t/db_dependent/UploadedFiles.t
new file mode 100644
index 0000000..36605d2
--- /dev/null
+++ b/t/db_dependent/UploadedFiles.t
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use Modern::Perl;
+use File::Temp qw/ tempdir /;
+use Test::CGI::Multipart;
+use Test::More tests => 11;
+
+use C4::Context;
+use C4::UploadedFiles;
+
+# This simulates a multipart POST request with a file upload.
+my $tcm = new Test::CGI::Multipart;
+$tcm->upload_file(
+    name => 'testfile',
+    file => 'testfilename.txt',
+    value => "This is the content of testfilename.txt",
+);
+my $cgi = $tcm->create_cgi;
+
+# Save the value of uploadPath and set it to a temporary directory
+my $uploadPath = C4::Context->preference('uploadPath');
+my $tempdir = tempdir(CLEANUP => 1);
+C4::Context->set_preference('uploadPath', $tempdir);
+
+my $testfilename = $cgi->param('testfile');
+my $testfile_fh = $cgi->upload('testfile');
+my $id = C4::UploadedFiles::UploadFile($testfilename, '', $testfile_fh->handle);
+ok($id, "File uploaded, id is $id");
+
+my $file = C4::UploadedFiles::GetUploadedFile($id);
+isa_ok($file, 'HASH', "GetUploadedFiles($id)");
+foreach my $key (qw(id filename filepath dir)) {
+    ok(exists $file->{$key}, "GetUploadedFile($id)->{$key} exists");
+}
+
+ok(-e $file->{filepath}, "File $file->{filepath} exists");
+
+ok(C4::UploadedFiles::DelUploadedFile($id), "DelUploadedFile($id) returned true");
+ok(! -e $file->{filepath}, "File $file->{filepath} does not exist anymore");
+
+C4::Context->set_preference('uploadPath', $uploadPath);
+
+is(C4::UploadedFiles::UploadFile($testfilename, '../', $testfile_fh->handle), undef, 'UploadFile with $dir containing ".." return undef');
+is(C4::UploadedFiles::GetUploadedFile(), undef, 'GetUploadedFile without parameters returns undef');
-- 
2.1.4