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>
Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@unc.edu.ar>
This commit is contained in:
Julian Maurice 2013-04-11 12:36:27 +00:00 committed by Tomas Cohen Arazi
parent bfe5680186
commit 209faec183
3 changed files with 52 additions and 3 deletions

View File

@ -747,6 +747,11 @@ our $PERL_DEPS = {
'required' => '1',
'min_ver' => '1.29',
},
'Test::CGI::Multipart' => {
'usage' => 'Tests',
'required' => '0',
'min_ver' => '0.0.3',
},
};
1;

View File

@ -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/*");

View File

@ -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');