@@ -, +, @@ --- 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 --- a/C4/Installer/PerlDependencies.pm +++ a/C4/Installer/PerlDependencies.pm @@ -689,6 +689,11 @@ our $PERL_DEPS = { 'required' => '1', 'min_ver' => '0.22', }, + 'Test::CGI::Multipart' => { + 'usage' => 'Tests', + 'required' => '0', + 'min_ver' => '0.0.3', + }, }; 1; --- a/cataloguing/value_builder/upload.pl +++ a/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/*"); --- a/t/db_dependent/UploadedFiles.t +++ a/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'); --