4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 13;
11 use t::lib::TestBuilder;
15 use Koha::DateUtils qw( dt_from_string );
16 use Koha::UploadedFiles;
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 our $builder = t::lib::TestBuilder->new;
23 our $current_upload = 0;
26 { name => 'file1', cat => 'A', size => 6000 },
27 { name => 'file2', cat => 'A', size => 8000 },
30 { name => 'file3', cat => 'B', size => 1000 },
33 { name => 'file4', cat => undef, size => 5000 }, # temporary
36 { name => 'file2', cat => 'A', size => 8000 },
37 # uploading a duplicate in cat A should fail
40 { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
43 { name => 'file5', cat => undef, size => 7000 },
46 { name => 'file6', cat => undef, size => 6500 },
47 { name => 'file7', cat => undef, size => 6501 },
51 # Redirect upload dir structure and mock C4::Context and CGI
52 my $tempdir = tempdir( CLEANUP => 1 );
53 t::lib::Mocks::mock_config('upload_path', $tempdir);
54 my $specmod = Test::MockModule->new( 'C4::Context' );
55 $specmod->mock( 'temporary_directory' => sub { return $tempdir; } );
56 my $cgimod = Test::MockModule->new( 'CGI' );
57 $cgimod->mock( 'new' => \&newCGI );
60 subtest 'Make a fresh start' => sub {
63 # Delete existing records (for later tests)
64 # Passing keep_file suppresses warnings (and does not delete files)
65 # Note that your files are not in danger, since we redirected
66 # all files to a new empty temp folder
67 Koha::UploadedFiles->delete({ keep_file => 1 });
68 is( Koha::UploadedFiles->count, 0, 'No records left' );
71 subtest 'permanent_directory and temporary_directory' => sub {
74 # Check mocked directories
75 is( Koha::UploadedFile->permanent_directory, $tempdir,
76 'Check permanent directory' );
77 is( C4::Context::temporary_directory, $tempdir,
78 'Check temporary directory' );
81 subtest 'Add two uploads in category A' => sub {
84 my $upl = Koha::Uploader->new({
85 category => $uploads->[$current_upload]->[0]->{cat},
88 my $res= $upl->result;
89 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
90 is( $upl->count, 2, 'Count returns 2 also' );
91 is( $upl->err, undef, 'No errors reported' );
93 my $rs = Koha::UploadedFiles->search({
94 id => [ split ',', $res ]
95 }, { order_by => { -asc => 'filename' }});
97 is( $rec->filename, 'file1', 'Check file name' );
98 is( $rec->uploadcategorycode, 'A', 'Check category A' );
99 is( $rec->filesize, 6000, 'Check size of file1' );
101 is( $rec->filename, 'file2', 'Check file name 2' );
102 is( $rec->filesize, 8000, 'Check size of file2' );
103 is( $rec->public, undef, 'Check public undefined' );
106 subtest 'Add another upload, check file_handle' => sub {
109 my $upl = Koha::Uploader->new({
110 category => $uploads->[$current_upload]->[0]->{cat},
114 is( $upl->count, 1, 'Upload 2 includes one file' );
115 my $res= $upl->result;
116 my $rec = Koha::UploadedFiles->find( $res );
117 is( $rec->uploadcategorycode, 'B', 'Check category B' );
118 is( $rec->public, 1, 'Check public == 1' );
119 my $fh = $rec->file_handle;
120 is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
122 my $orgname = $rec->filename;
123 $rec->filename( 'doesprobablynotexist' )->store;
124 is( $rec->file_handle, undef, 'Sabotage with file handle' );
125 $rec->filename( $orgname )->store;
128 subtest 'Add temporary upload' => sub {
131 my $upl = Koha::Uploader->new({ tmp => 1 }); #temporary
133 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
134 my $rec = Koha::UploadedFiles->find( $upl->result );
135 is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
138 subtest 'Add same file in same category' => sub {
141 my $upl = Koha::Uploader->new({
142 category => $uploads->[$current_upload]->[0]->{cat},
145 is( $upl->count, 0, 'Upload 4 failed as expected' );
146 is( $upl->result, undef, 'Result is undefined' );
148 is( $e->{file2}->{code}, Koha::Uploader::ERR_EXISTS, "Already exists error reported" );
151 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
154 # add temporary file with same name and contents (file4)
155 my $upl = Koha::Uploader->new({ tmp => 1 });
157 is( $upl->count, 1, 'Add duplicate temporary file (file4)' );
158 my $id = $upl->result;
159 my $path = Koha::UploadedFiles->find( $id )->full_path;
161 # testing delete via UploadedFiles (plural)
162 my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
163 isnt( $delete, "0E0", 'Delete successful' );
164 isnt( -e $path, 1, 'File no longer found after delete' );
165 is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
167 # testing delete via UploadedFile (singular)
168 # Note that find returns a Koha::Object
169 $upl = Koha::Uploader->new({ tmp => 1 });
171 my $kohaobj = Koha::UploadedFiles->find( $upl->result );
172 $path = $kohaobj->full_path;
173 $delete = $kohaobj->delete;
174 ok( $delete, 'Delete successful' );
175 isnt( -e $path, 1, 'File no longer found after delete' );
177 # add another record with TestBuilder, so file does not exist
178 # catch warning (which occurs when deleting permanent file)
179 my $upload01 = $builder->build({ source => 'UploadedFile', value => { permanent => 1 } });
180 warning_like { $delete = Koha::UploadedFiles->find( $upload01->{id} )->delete; }
181 qr/file was missing/,
182 'delete warns when permanent file is missing';
183 ok( $delete, 'Deleting record was successful' );
184 is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
186 # add another one with TestBuilder and delete twice (file does not exist)
187 $upload01 = $builder->build({ source => 'UploadedFile' });
188 $kohaobj = Koha::UploadedFiles->find( $upload01->{id} );
189 $delete = $kohaobj->delete({ keep_file => 1 });
191 $delete = $kohaobj->delete({ keep_file => 1 });
193 ok( $_->isa("DBIx::Class::Exception"), 'Repeated delete unsuccessful' );
197 subtest 'Test delete_missing' => sub {
200 # If we add files via TestBuilder, they do not exist
201 my $upload01 = $builder->build({ source => 'UploadedFile' });
202 my $upload02 = $builder->build({ source => 'UploadedFile' });
204 my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
205 is( $deleted, 2, 'Expect two records with missing files' );
206 isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
207 $deleted = Koha::UploadedFiles->delete_missing;
208 ok( $deleted =~ /^(2)$/, 'Deleted two records with missing files' );
209 is( Koha::UploadedFiles->search({
210 id => [ $upload01->{id}, $upload02->{id} ],
211 })->count, 0, 'Records are gone' );
213 $deleted = Koha::UploadedFiles->delete_missing;
214 is( $deleted, "0E0", "Return value of 0E0 expected" );
217 subtest 'Call search_term with[out] private flag' => sub {
220 my @recs = Koha::UploadedFiles->search_term({ term => 'file' })->as_list;
221 is( @recs, 1, 'Returns only one public result' );
222 is( $recs[0]->filename, 'file3', 'Should be file3' );
224 is( Koha::UploadedFiles->search_term({
225 term => 'file', include_private => 1,
226 })->count, 4, 'Returns now four results' );
229 subtest 'Simple tests for httpheaders and getCategories' => sub {
232 my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next;
233 my @hdrs = $rec->httpheaders;
234 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
235 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
236 my $cat = Koha::UploadedFiles->getCategories;
237 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
240 subtest 'Testing allows_add_by' => sub {
243 my $patron = $builder->build({
244 source => 'Borrower',
245 value => { flags => 0 }, #no permissions
247 my $patronid = $patron->{borrowernumber};
248 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
249 undef, 'Patron is not allowed to do anything' );
251 # add some permissions: edit_catalogue
252 my $fl = 2**9; # edit_catalogue
253 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
254 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
255 undef, 'Patron is still not allowed to add uploaded files' );
257 # replace flags by all tools
259 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
260 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
261 1, 'Patron should be allowed now to add uploaded files' );
263 # remove all tools and add upload_general_files only
264 $fl = 0; # no modules
265 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
267 source => 'UserPermission',
269 borrowernumber => $patronid,
270 module_bit => { module_bit => { flag => 'tools' } },
271 code => 'upload_general_files',
274 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
275 1, 'Patron is still allowed to add uploaded files' );
278 subtest 'Testing delete_temporary' => sub {
281 # Add two temporary files: result should be 3 + 3
282 Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7
283 is( Koha::UploadedFiles->search->count, 6, 'Test starting count' );
284 is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
285 'Includes 3 permanent' );
287 # Move all permanents to today - 1
288 # Move temp 1 to today - 3, and temp 2,3 to today - 5
289 my $today = dt_from_string;
290 $today->subtract( minutes => 2 ); # should be enough :)
291 my $dt = $today->clone->subtract( days => 1 );
292 foreach my $rec ( Koha::UploadedFiles->search({ permanent => 1 })->as_list ) {
293 $rec->dtcreated($dt)->store;
295 my @recs = Koha::UploadedFiles->search({ permanent => 0 })->as_list;
296 $dt = $today->clone->subtract( days => 3 );
297 $recs[0]->dtcreated($dt)->store;
298 $dt = $today->clone->subtract( days => 5 );
299 $recs[1]->dtcreated($dt)->store;
300 $recs[2]->dtcreated($dt)->store;
302 # Now call delete_temporary with 6, 5 and 0
303 t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 6 );
304 my $delete = Koha::UploadedFiles->delete_temporary;
305 is( $delete, '0E0', 'Check return value with 6' );
306 is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
308 # use override parameter
309 $delete = Koha::UploadedFiles->delete_temporary({ override_pref => 5 });
310 is( $delete, 2, 'Check return value with 5' );
311 is( Koha::UploadedFiles->search->count, 4, 'Delete with override==5' );
313 t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 0 );
314 $delete = Koha::UploadedFiles->delete_temporary;
315 is( $delete, 1, 'Check return value with 0' );
316 is( Koha::UploadedFiles->search->count, 3, 'Delete with pref==0 makes 3' );
317 is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
318 'Still 3 permanent uploads' );
321 subtest 'Testing download headers' => sub {
323 my $test_pdf = Koha::UploadedFile->new({ filename => 'pdf.pdf', uploadcategorycode => 'B', filesize => 1000 });
324 my $test_not = Koha::UploadedFile->new({ filename => 'pdf.not', uploadcategorycode => 'B', filesize => 1000 });
325 my @pdf_expect = ( '-type'=>'application/pdf','Content-Disposition'=>'inline; filename="pdf.pdf"' );
326 my @not_expect = ( '-type'=>'application/octet-stream','-attachment'=>'pdf.not' );
327 my @pdf_head = $test_pdf->httpheaders;
328 my @not_head = $test_not->httpheaders;
329 is_deeply(\@pdf_head, \@pdf_expect,"Get inline pdf headers for pdf");
330 is_deeply(\@not_head, \@not_expect,"Get download headers for non pdf");
333 $schema->storage->txn_rollback;
337 my ( $class, $hook ) = @_;
339 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
340 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
342 &$hook( $uh->{name}, 'a'x1000, $read );