|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use File::Temp qw/ tempdir /; |
| 5 |
use Test::CGI::Multipart; |
| 6 |
use Test::More tests => 11; |
| 7 |
|
| 8 |
use C4::Context; |
| 9 |
use C4::UploadedFiles; |
| 10 |
|
| 11 |
# This simulates a multipart POST request with a file upload. |
| 12 |
my $tcm = new Test::CGI::Multipart; |
| 13 |
$tcm->upload_file( |
| 14 |
name => 'testfile', |
| 15 |
file => 'testfilename.txt', |
| 16 |
value => "This is the content of testfilename.txt", |
| 17 |
); |
| 18 |
my $cgi = $tcm->create_cgi; |
| 19 |
|
| 20 |
# Save the value of uploadPath and set it to a temporary directory |
| 21 |
my $uploadPath = C4::Context->preference('uploadPath'); |
| 22 |
my $tempdir = tempdir(CLEANUP => 1); |
| 23 |
C4::Context->set_preference('uploadPath', $tempdir); |
| 24 |
|
| 25 |
my $testfilename = $cgi->param('testfile'); |
| 26 |
my $testfile_fh = $cgi->upload('testfile'); |
| 27 |
my $id = C4::UploadedFiles::UploadFile($testfilename, '', $testfile_fh->handle); |
| 28 |
ok($id, "File uploaded, id is $id"); |
| 29 |
|
| 30 |
my $file = C4::UploadedFiles::GetUploadedFile($id); |
| 31 |
isa_ok($file, 'HASH', "GetUploadedFiles($id)"); |
| 32 |
foreach my $key (qw(id filename filepath dir)) { |
| 33 |
ok(exists $file->{$key}, "GetUploadedFile($id)->{$key} exists"); |
| 34 |
} |
| 35 |
|
| 36 |
ok(-e $file->{filepath}, "File $file->{filepath} exists"); |
| 37 |
|
| 38 |
ok(C4::UploadedFiles::DelUploadedFile($id), "DelUploadedFile($id) returned true"); |
| 39 |
ok(! -e $file->{filepath}, "File $file->{filepath} does not exist anymore"); |
| 40 |
|
| 41 |
C4::Context->set_preference('uploadPath', $uploadPath); |
| 42 |
|
| 43 |
is(C4::UploadedFiles::UploadFile($testfilename, '../', $testfile_fh->handle), undef, 'UploadFile with $dir containing ".." return undef'); |
| 44 |
is(C4::UploadedFiles::GetUploadedFile(), undef, 'GetUploadedFile without parameters returns undef'); |