|
Lines 2-8
Link Here
|
| 2 |
|
2 |
|
| 3 |
use Modern::Perl; |
3 |
use Modern::Perl; |
| 4 |
use File::Temp qw/ tempdir /; |
4 |
use File::Temp qw/ tempdir /; |
| 5 |
use Test::More tests => 13; |
5 |
use Test::More tests => 12; |
| 6 |
use Test::Warn; |
6 |
use Test::Warn; |
| 7 |
use Try::Tiny; |
7 |
use Try::Tiny; |
| 8 |
|
8 |
|
|
Lines 24-57
our $builder = t::lib::TestBuilder->new;
Link Here
|
| 24 |
our $current_upload = 0; |
24 |
our $current_upload = 0; |
| 25 |
our $uploads = [ |
25 |
our $uploads = [ |
| 26 |
[ |
26 |
[ |
| 27 |
{ name => 'file1', cat => 'A', size => 6000 }, |
27 |
{ name => 'file1', storage => 'DEFAULT', dir => 'A', size => 6000 }, |
| 28 |
{ name => 'file2', cat => 'A', size => 8000 }, |
28 |
{ name => 'file2', storage => 'DEFAULT', dir => 'A', size => 8000 }, |
| 29 |
], |
29 |
], |
| 30 |
[ |
30 |
[ |
| 31 |
{ name => 'file3', cat => 'B', size => 1000 }, |
31 |
{ name => 'file3', storage => 'DEFAULT', dir => 'B', size => 1000 }, |
| 32 |
], |
32 |
], |
| 33 |
[ |
33 |
[ |
| 34 |
{ name => 'file4', cat => undef, size => 5000 }, # temporary |
34 |
{ name => 'file4', storage => 'TMP', dir => undef, size => 5000 }, |
| 35 |
], |
35 |
], |
| 36 |
[ |
36 |
[ |
| 37 |
{ name => 'file2', cat => 'A', size => 8000 }, |
37 |
{ name => 'file2', storage => 'DEFAULT', dir => 'A', size => 8000 }, |
| 38 |
# uploading a duplicate in cat A should fail |
38 |
# uploading a duplicate in dir A should fail |
| 39 |
], |
39 |
], |
| 40 |
[ |
40 |
[ |
| 41 |
{ name => 'file4', cat => undef, size => 5000 }, # temp duplicate |
41 |
{ name => 'file4', storage => 'TMP', dir => undef, size => 5000 }, # temp duplicate |
| 42 |
], |
42 |
], |
| 43 |
[ |
43 |
[ |
| 44 |
{ name => 'file5', cat => undef, size => 7000 }, |
44 |
{ name => 'file5', storage => 'DEFAULT', dir => undef, size => 7000 }, |
| 45 |
], |
45 |
], |
| 46 |
[ |
46 |
[ |
| 47 |
{ name => 'file6', cat => undef, size => 6500 }, |
47 |
{ name => 'file6', storage => 'TMP', dir => undef, size => 6500 }, |
| 48 |
{ name => 'file7', cat => undef, size => 6501 }, |
48 |
{ name => 'file7', storage => 'TMP', dir => undef, size => 6501 }, |
| 49 |
], |
49 |
], |
| 50 |
]; |
50 |
]; |
| 51 |
|
51 |
|
| 52 |
# Redirect upload dir structure and mock C4::Context and CGI |
52 |
# Redirect upload dir structure and mock C4::Context and CGI |
| 53 |
my $tempdir = tempdir( CLEANUP => 1 ); |
53 |
my $tempdir = tempdir( CLEANUP => 1 ); |
| 54 |
t::lib::Mocks::mock_config('upload_path', $tempdir); |
54 |
my $storage_config = [ |
|
|
55 |
{ |
| 56 |
name => 'DEFAULT', |
| 57 |
adapter => 'directory', |
| 58 |
adapter_params => { |
| 59 |
path => $tempdir, |
| 60 |
}, |
| 61 |
hash_filename => 1, |
| 62 |
}, |
| 63 |
]; |
| 64 |
t::lib::Mocks::mock_config('storage', $storage_config); |
| 55 |
my $specmod = Test::MockModule->new( 'C4::Context' ); |
65 |
my $specmod = Test::MockModule->new( 'C4::Context' ); |
| 56 |
$specmod->mock( 'temporary_directory' => sub { return $tempdir; } ); |
66 |
$specmod->mock( 'temporary_directory' => sub { return $tempdir; } ); |
| 57 |
my $cgimod = Test::MockModule->new( 'CGI' ); |
67 |
my $cgimod = Test::MockModule->new( 'CGI' ); |
|
Lines 69-89
subtest 'Make a fresh start' => sub {
Link Here
|
| 69 |
is( Koha::UploadedFiles->count, 0, 'No records left' ); |
79 |
is( Koha::UploadedFiles->count, 0, 'No records left' ); |
| 70 |
}; |
80 |
}; |
| 71 |
|
81 |
|
| 72 |
subtest 'permanent_directory and temporary_directory' => sub { |
|
|
| 73 |
plan tests => 2; |
| 74 |
|
| 75 |
# Check mocked directories |
| 76 |
is( Koha::UploadedFile->permanent_directory, $tempdir, |
| 77 |
'Check permanent directory' ); |
| 78 |
is( C4::Context::temporary_directory, $tempdir, |
| 79 |
'Check temporary directory' ); |
| 80 |
}; |
| 81 |
|
| 82 |
subtest 'Add two uploads in category A' => sub { |
82 |
subtest 'Add two uploads in category A' => sub { |
| 83 |
plan tests => 9; |
83 |
plan tests => 9; |
| 84 |
|
84 |
|
| 85 |
my $upl = Koha::Uploader->new({ |
85 |
my $upl = Koha::Uploader->new({ |
| 86 |
category => $uploads->[$current_upload]->[0]->{cat}, |
86 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
|
|
87 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 87 |
}); |
88 |
}); |
| 88 |
my $cgi= $upl->cgi; |
89 |
my $cgi= $upl->cgi; |
| 89 |
my $res= $upl->result; |
90 |
my $res= $upl->result; |
|
Lines 96-121
subtest 'Add two uploads in category A' => sub {
Link Here
|
| 96 |
}, { order_by => { -asc => 'filename' }}); |
97 |
}, { order_by => { -asc => 'filename' }}); |
| 97 |
my $rec = $rs->next; |
98 |
my $rec = $rs->next; |
| 98 |
is( $rec->filename, 'file1', 'Check file name' ); |
99 |
is( $rec->filename, 'file1', 'Check file name' ); |
| 99 |
is( $rec->uploadcategorycode, 'A', 'Check category A' ); |
100 |
is( $rec->dir, 'A', 'Check dir A' ); |
| 100 |
is( $rec->filesize, 6000, 'Check size of file1' ); |
101 |
is( $rec->filesize, 6000, 'Check size of file1' ); |
| 101 |
$rec = $rs->next; |
102 |
$rec = $rs->next; |
| 102 |
is( $rec->filename, 'file2', 'Check file name 2' ); |
103 |
is( $rec->filename, 'file2', 'Check file name 2' ); |
| 103 |
is( $rec->filesize, 8000, 'Check size of file2' ); |
104 |
is( $rec->filesize, 8000, 'Check size of file2' ); |
| 104 |
is( $rec->public, undef, 'Check public undefined' ); |
105 |
is( $rec->public, 0, 'Check public 0' ); |
| 105 |
}; |
106 |
}; |
| 106 |
|
107 |
|
| 107 |
subtest 'Add another upload, check file_handle' => sub { |
108 |
subtest 'Add another upload, check file_handle' => sub { |
| 108 |
plan tests => 5; |
109 |
plan tests => 5; |
| 109 |
|
110 |
|
| 110 |
my $upl = Koha::Uploader->new({ |
111 |
my $upl = Koha::Uploader->new({ |
| 111 |
category => $uploads->[$current_upload]->[0]->{cat}, |
112 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
|
|
113 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 112 |
public => 1, |
114 |
public => 1, |
| 113 |
}); |
115 |
}); |
| 114 |
my $cgi= $upl->cgi; |
116 |
my $cgi= $upl->cgi; |
| 115 |
is( $upl->count, 1, 'Upload 2 includes one file' ); |
117 |
is( $upl->count, 1, 'Upload 2 includes one file' ); |
| 116 |
my $res= $upl->result; |
118 |
my $res= $upl->result; |
| 117 |
my $rec = Koha::UploadedFiles->find( $res ); |
119 |
my $rec = Koha::UploadedFiles->find( $res ); |
| 118 |
is( $rec->uploadcategorycode, 'B', 'Check category B' ); |
120 |
is( $rec->dir, 'B', 'Check dir B' ); |
| 119 |
is( $rec->public, 1, 'Check public == 1' ); |
121 |
is( $rec->public, 1, 'Check public == 1' ); |
| 120 |
my $fh = $rec->file_handle; |
122 |
my $fh = $rec->file_handle; |
| 121 |
is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' ); |
123 |
is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' ); |
|
Lines 129-146
subtest 'Add another upload, check file_handle' => sub {
Link Here
|
| 129 |
subtest 'Add temporary upload' => sub { |
131 |
subtest 'Add temporary upload' => sub { |
| 130 |
plan tests => 2; |
132 |
plan tests => 2; |
| 131 |
|
133 |
|
| 132 |
my $upl = Koha::Uploader->new({ tmp => 1 }); #temporary |
134 |
my $upl = Koha::Uploader->new({ |
|
|
135 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 136 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 137 |
}); |
| 133 |
my $cgi= $upl->cgi; |
138 |
my $cgi= $upl->cgi; |
| 134 |
is( $upl->count, 1, 'Upload 3 includes one temporary file' ); |
139 |
is( $upl->count, 1, 'Upload 3 includes one temporary file' ); |
| 135 |
my $rec = Koha::UploadedFiles->find( $upl->result ); |
140 |
my $rec = Koha::UploadedFiles->find( $upl->result ); |
| 136 |
is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' ); |
141 |
is( $rec->dir, '', 'Check dir is empty' ); |
| 137 |
}; |
142 |
}; |
| 138 |
|
143 |
|
| 139 |
subtest 'Add same file in same category' => sub { |
144 |
subtest 'Add same file in same category' => sub { |
| 140 |
plan tests => 3; |
145 |
plan tests => 3; |
| 141 |
|
146 |
|
| 142 |
my $upl = Koha::Uploader->new({ |
147 |
my $upl = Koha::Uploader->new({ |
| 143 |
category => $uploads->[$current_upload]->[0]->{cat}, |
148 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
|
|
149 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 144 |
}); |
150 |
}); |
| 145 |
my $cgi= $upl->cgi; |
151 |
my $cgi= $upl->cgi; |
| 146 |
is( $upl->count, 0, 'Upload 4 failed as expected' ); |
152 |
is( $upl->count, 0, 'Upload 4 failed as expected' ); |
|
Lines 153-183
subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
Link Here
|
| 153 |
plan tests => 10; |
159 |
plan tests => 10; |
| 154 |
|
160 |
|
| 155 |
# add temporary file with same name and contents (file4) |
161 |
# add temporary file with same name and contents (file4) |
| 156 |
my $upl = Koha::Uploader->new({ tmp => 1 }); |
162 |
my $upl = Koha::Uploader->new({ |
|
|
163 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 164 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 165 |
}); |
| 157 |
my $cgi= $upl->cgi; |
166 |
my $cgi= $upl->cgi; |
| 158 |
is( $upl->count, 1, 'Add duplicate temporary file (file4)' ); |
167 |
is( $upl->count, 1, 'Add duplicate temporary file (file4)' ); |
| 159 |
my $id = $upl->result; |
168 |
my $id = $upl->result; |
| 160 |
my $path = Koha::UploadedFiles->find( $id )->full_path; |
169 |
my $uploaded_file = Koha::UploadedFiles->find( $id ); |
|
|
170 |
my $storage = Koha::Storage->get_instance($uploaded_file->storage); |
| 161 |
|
171 |
|
| 162 |
# testing delete via UploadedFiles (plural) |
172 |
# testing delete via UploadedFiles (plural) |
| 163 |
my $delete = Koha::UploadedFiles->search({ id => $id })->delete; |
173 |
my $delete = Koha::UploadedFiles->search({ id => $id })->delete; |
| 164 |
isnt( $delete, "0E0", 'Delete successful' ); |
174 |
isnt( $delete, "0E0", 'Delete successful' ); |
| 165 |
isnt( -e $path, 1, 'File no longer found after delete' ); |
175 |
isnt( $storage->exists($uploaded_file->filepath), 1, 'File no longer found after delete' ); |
| 166 |
is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' ); |
176 |
is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' ); |
| 167 |
|
177 |
|
| 168 |
# testing delete via UploadedFile (singular) |
178 |
# testing delete via UploadedFile (singular) |
| 169 |
# Note that find returns a Koha::Object |
179 |
# Note that find returns a Koha::Object |
| 170 |
$upl = Koha::Uploader->new({ tmp => 1 }); |
180 |
$upl = Koha::Uploader->new({ |
|
|
181 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 182 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 183 |
}); |
| 171 |
$upl->cgi; |
184 |
$upl->cgi; |
| 172 |
my $kohaobj = Koha::UploadedFiles->find( $upl->result ); |
185 |
my $kohaobj = Koha::UploadedFiles->find( $upl->result ); |
| 173 |
$path = $kohaobj->full_path; |
186 |
$storage = Koha::Storage->get_instance($kohaobj->storage); |
| 174 |
$delete = $kohaobj->delete; |
187 |
$delete = $kohaobj->delete; |
| 175 |
ok( $delete, 'Delete successful' ); |
188 |
ok( $delete, 'Delete successful' ); |
| 176 |
isnt( -e $path, 1, 'File no longer found after delete' ); |
189 |
isnt($storage->exists($kohaobj->filepath), 1, 'File no longer found after delete' ); |
| 177 |
|
190 |
|
| 178 |
# add another record with TestBuilder, so file does not exist |
191 |
# add another record with TestBuilder, so file does not exist |
| 179 |
# catch warning |
192 |
# catch warning |
| 180 |
my $upload01 = $builder->build({ source => 'UploadedFile' }); |
193 |
my $upload01 = $builder->build({ source => 'UploadedFile', value => {storage => 'TMP'} }); |
| 181 |
warning_like { $delete = Koha::UploadedFiles->find( $upload01->{id} )->delete; } |
194 |
warning_like { $delete = Koha::UploadedFiles->find( $upload01->{id} )->delete; } |
| 182 |
qr/file was missing/, |
195 |
qr/file was missing/, |
| 183 |
'delete warns when file is missing'; |
196 |
'delete warns when file is missing'; |
|
Lines 199-206
subtest 'Test delete_missing' => sub {
Link Here
|
| 199 |
plan tests => 5; |
212 |
plan tests => 5; |
| 200 |
|
213 |
|
| 201 |
# If we add files via TestBuilder, they do not exist |
214 |
# If we add files via TestBuilder, they do not exist |
| 202 |
my $upload01 = $builder->build({ source => 'UploadedFile' }); |
215 |
my $upload01 = $builder->build({ source => 'UploadedFile', value => {storage => 'TMP'} }); |
| 203 |
my $upload02 = $builder->build({ source => 'UploadedFile' }); |
216 |
my $upload02 = $builder->build({ source => 'UploadedFile', value => {storage => 'TMP'} }); |
| 204 |
# dry run first |
217 |
# dry run first |
| 205 |
my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 }); |
218 |
my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 }); |
| 206 |
is( $deleted, 2, 'Expect two records with missing files' ); |
219 |
is( $deleted, 2, 'Expect two records with missing files' ); |
|
Lines 227-241
subtest 'Call search_term with[out] private flag' => sub {
Link Here
|
| 227 |
})->count, 4, 'Returns now four results' ); |
240 |
})->count, 4, 'Returns now four results' ); |
| 228 |
}; |
241 |
}; |
| 229 |
|
242 |
|
| 230 |
subtest 'Simple tests for httpheaders and getCategories' => sub { |
243 |
subtest 'Simple tests for httpheaders' => sub { |
| 231 |
plan tests => 2; |
244 |
plan tests => 1; |
| 232 |
|
245 |
|
| 233 |
my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next; |
246 |
my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next; |
| 234 |
my @hdrs = $rec->httpheaders; |
247 |
my @hdrs = $rec->httpheaders; |
| 235 |
is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders'); |
248 |
is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders'); |
| 236 |
$builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } }); |
249 |
$builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } }); |
| 237 |
my $cat = Koha::UploadedFiles->getCategories; |
|
|
| 238 |
is( @$cat >= 1, 1, 'getCategories returned at least one category' ); |
| 239 |
}; |
250 |
}; |
| 240 |
|
251 |
|
| 241 |
subtest 'Testing allows_add_by' => sub { |
252 |
subtest 'Testing allows_add_by' => sub { |
|
Lines 280-286
subtest 'Testing delete_temporary' => sub {
Link Here
|
| 280 |
plan tests => 9; |
291 |
plan tests => 9; |
| 281 |
|
292 |
|
| 282 |
# Add two temporary files: result should be 3 + 3 |
293 |
# Add two temporary files: result should be 3 + 3 |
| 283 |
Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7 |
294 |
Koha::Uploader->new({ |
|
|
295 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 296 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 297 |
})->cgi; # add file6 and file7 |
| 284 |
is( Koha::UploadedFiles->search->count, 6, 'Test starting count' ); |
298 |
is( Koha::UploadedFiles->search->count, 6, 'Test starting count' ); |
| 285 |
is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3, |
299 |
is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3, |
| 286 |
'Includes 3 permanent' ); |
300 |
'Includes 3 permanent' ); |
|
Lines 321-328
subtest 'Testing delete_temporary' => sub {
Link Here
|
| 321 |
|
335 |
|
| 322 |
subtest 'Testing download headers' => sub { |
336 |
subtest 'Testing download headers' => sub { |
| 323 |
plan tests => 2; |
337 |
plan tests => 2; |
| 324 |
my $test_pdf = Koha::UploadedFile->new({ filename => 'pdf.pdf', uploadcategorycode => 'B', filesize => 1000 }); |
338 |
my $test_pdf = Koha::UploadedFile->new({ filename => 'pdf.pdf', storage => 'DEFAULT', dir => 'B', filesize => 1000 }); |
| 325 |
my $test_not = Koha::UploadedFile->new({ filename => 'pdf.not', uploadcategorycode => 'B', filesize => 1000 }); |
339 |
my $test_not = Koha::UploadedFile->new({ filename => 'pdf.not', storage => 'DEFAULT', dir => 'B', filesize => 1000 }); |
| 326 |
my @pdf_expect = ( '-type'=>'application/pdf','Content-Disposition'=>'inline; filename=pdf.pdf' ); |
340 |
my @pdf_expect = ( '-type'=>'application/pdf','Content-Disposition'=>'inline; filename=pdf.pdf' ); |
| 327 |
my @not_expect = ( '-type'=>'application/octet-stream','-attachment'=>'pdf.not' ); |
341 |
my @not_expect = ( '-type'=>'application/octet-stream','-attachment'=>'pdf.not' ); |
| 328 |
my @pdf_head = $test_pdf->httpheaders; |
342 |
my @pdf_head = $test_pdf->httpheaders; |