Bug 6874: Add unit tests for C4::UploadedFiles
[koha.git] / t / db_dependent / UploadedFiles.t
blob36605d23dd182703e6ee3d74dc97d54a270efe56
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::CGI::Multipart;
6 use Test::More tests => 11;
8 use C4::Context;
9 use C4::UploadedFiles;
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",
18 my $cgi = $tcm->create_cgi;
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);
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");
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");
36 ok(-e $file->{filepath}, "File $file->{filepath} exists");
38 ok(C4::UploadedFiles::DelUploadedFile($id), "DelUploadedFile($id) returned true");
39 ok(! -e $file->{filepath}, "File $file->{filepath} does not exist anymore");
41 C4::Context->set_preference('uploadPath', $uploadPath);
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');