4 use File
::Temp qw
/ tempdir /;
5 use Test
::More tests
=> 13;
11 use t
::lib
::TestBuilder
;
16 use Koha
::UploadedFile
;
17 use Koha
::UploadedFiles
;
20 my $schema = Koha
::Database
->new->schema;
21 $schema->storage->txn_begin;
22 our $builder = t
::lib
::TestBuilder
->new;
24 our $current_upload = 0;
27 { name
=> 'file1', cat
=> 'A', size
=> 6000 },
28 { name
=> 'file2', cat
=> 'A', size
=> 8000 },
31 { name
=> 'file3', cat
=> 'B', size
=> 1000 },
34 { name
=> 'file4', cat
=> undef, size
=> 5000 }, # temporary
37 { name
=> 'file2', cat
=> 'A', size
=> 8000 },
38 # uploading a duplicate in cat A should fail
41 { name
=> 'file4', cat
=> undef, size
=> 5000 }, # temp duplicate
44 { name
=> 'file5', cat
=> undef, size
=> 7000 },
47 { name
=> 'file6', cat
=> undef, size
=> 6500 },
48 { name
=> 'file7', cat
=> undef, size
=> 6501 },
52 # Redirect upload dir structure and mock C4::Context and CGI
53 my $tempdir = tempdir
( CLEANUP
=> 1 );
54 t
::lib
::Mocks
::mock_config
('upload_path', $tempdir);
55 my $specmod = Test
::MockModule
->new( 'C4::Context' );
56 $specmod->mock( 'temporary_directory' => sub { return $tempdir; } );
57 my $cgimod = Test
::MockModule
->new( 'CGI' );
58 $cgimod->mock( 'new' => \
&newCGI
);
61 subtest
'Make a fresh start' => sub {
64 # Delete existing records (for later tests)
65 # Passing keep_file suppresses warnings (and does not delete files)
66 # Note that your files are not in danger, since we redirected
67 # all files to a new empty temp folder
68 Koha
::UploadedFiles
->delete({ keep_file
=> 1 });
69 is
( Koha
::UploadedFiles
->count, 0, 'No records left' );
72 subtest
'permanent_directory and temporary_directory' => sub {
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' );
82 subtest
'Add two uploads in category A' => sub {
85 my $upl = Koha
::Uploader
->new({
86 category
=> $uploads->[$current_upload]->[0]->{cat
},
89 my $res= $upl->result;
90 is
( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
91 is
( $upl->count, 2, 'Count returns 2 also' );
92 is
( $upl->err, undef, 'No errors reported' );
94 my $rs = Koha
::UploadedFiles
->search({
95 id
=> [ split ',', $res ]
96 }, { order_by
=> { -asc
=> 'filename' }});
98 is
( $rec->filename, 'file1', 'Check file name' );
99 is
( $rec->uploadcategorycode, 'A', 'Check category A' );
100 is
( $rec->filesize, 6000, 'Check size of file1' );
102 is
( $rec->filename, 'file2', 'Check file name 2' );
103 is
( $rec->filesize, 8000, 'Check size of file2' );
104 is
( $rec->public, undef, 'Check public undefined' );
107 subtest
'Add another upload, check file_handle' => sub {
110 my $upl = Koha
::Uploader
->new({
111 category
=> $uploads->[$current_upload]->[0]->{cat
},
115 is
( $upl->count, 1, 'Upload 2 includes one file' );
116 my $res= $upl->result;
117 my $rec = Koha
::UploadedFiles
->find( $res );
118 is
( $rec->uploadcategorycode, 'B', 'Check category B' );
119 is
( $rec->public, 1, 'Check public == 1' );
120 my $fh = $rec->file_handle;
121 is
( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
123 my $orgname = $rec->filename;
124 $rec->filename( 'doesprobablynotexist' )->store;
125 is
( $rec->file_handle, undef, 'Sabotage with file handle' );
126 $rec->filename( $orgname )->store;
129 subtest
'Add temporary upload' => sub {
132 my $upl = Koha
::Uploader
->new({ tmp
=> 1 }); #temporary
134 is
( $upl->count, 1, 'Upload 3 includes one temporary file' );
135 my $rec = Koha
::UploadedFiles
->find( $upl->result );
136 is
( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
139 subtest
'Add same file in same category' => sub {
142 my $upl = Koha
::Uploader
->new({
143 category
=> $uploads->[$current_upload]->[0]->{cat
},
146 is
( $upl->count, 0, 'Upload 4 failed as expected' );
147 is
( $upl->result, undef, 'Result is undefined' );
149 is
( $e->{file2
}->{code
}, Koha
::Uploader
::ERR_EXISTS
, "Already exists error reported" );
152 subtest
'Test delete via UploadedFile as well as UploadedFiles' => sub {
155 # add temporary file with same name and contents (file4)
156 my $upl = Koha
::Uploader
->new({ tmp
=> 1 });
158 is
( $upl->count, 1, 'Add duplicate temporary file (file4)' );
159 my $id = $upl->result;
160 my $path = Koha
::UploadedFiles
->find( $id )->full_path;
162 # testing delete via UploadedFiles (plural)
163 my $delete = Koha
::UploadedFiles
->search({ id
=> $id })->delete;
164 isnt
( $delete, "0E0", 'Delete successful' );
165 isnt
( -e
$path, 1, 'File no longer found after delete' );
166 is
( Koha
::UploadedFiles
->find( $id ), undef, 'Record also gone' );
168 # testing delete via UploadedFile (singular)
169 # Note that find returns a Koha::Object
170 $upl = Koha
::Uploader
->new({ tmp
=> 1 });
172 my $kohaobj = Koha
::UploadedFiles
->find( $upl->result );
173 $path = $kohaobj->full_path;
174 $delete = $kohaobj->delete;
175 ok
( $delete, 'Delete successful' );
176 isnt
( -e
$path, 1, 'File no longer found after delete' );
178 # add another record with TestBuilder, so file does not exist
180 my $upload01 = $builder->build({ source
=> 'UploadedFile' });
181 warning_like
{ $delete = Koha
::UploadedFiles
->find( $upload01->{id
} )->delete; }
182 qr/file was missing/,
183 'delete warns when file is missing';
184 ok
( $delete, 'Deleting record was successful' );
185 is
( Koha
::UploadedFiles
->count, 4, 'Back to four uploads now' );
187 # add another one with TestBuilder and delete twice (file does not exist)
188 $upload01 = $builder->build({ source
=> 'UploadedFile' });
189 $kohaobj = Koha
::UploadedFiles
->find( $upload01->{id
} );
190 $delete = $kohaobj->delete({ keep_file
=> 1 });
192 $delete = $kohaobj->delete({ keep_file
=> 1 });
194 ok
( $_->isa("DBIx::Class::Exception"), 'Repeated delete unsuccessful' );
198 subtest
'Test delete_missing' => sub {
201 # If we add files via TestBuilder, they do not exist
202 my $upload01 = $builder->build({ source
=> 'UploadedFile' });
203 my $upload02 = $builder->build({ source
=> 'UploadedFile' });
205 my $deleted = Koha
::UploadedFiles
->delete_missing({ keep_record
=> 1 });
206 is
( $deleted, 2, 'Expect two records with missing files' );
207 isnt
( Koha
::UploadedFiles
->find( $upload01->{id
} ), undef, 'Not deleted' );
208 $deleted = Koha
::UploadedFiles
->delete_missing;
209 ok
( $deleted =~ /^(2)$/, 'Deleted two records with missing files' );
210 is
( Koha
::UploadedFiles
->search({
211 id
=> [ $upload01->{id
}, $upload02->{id
} ],
212 })->count, 0, 'Records are gone' );
214 $deleted = Koha
::UploadedFiles
->delete_missing;
215 is
( $deleted, "0E0", "Return value of 0E0 expected" );
218 subtest
'Call search_term with[out] private flag' => sub {
221 my @recs = Koha
::UploadedFiles
->search_term({ term
=> 'file' });
222 is
( @recs, 1, 'Returns only one public result' );
223 is
( $recs[0]->filename, 'file3', 'Should be file3' );
225 is
( Koha
::UploadedFiles
->search_term({
226 term
=> 'file', include_private
=> 1,
227 })->count, 4, 'Returns now four results' );
230 subtest
'Simple tests for httpheaders and getCategories' => sub {
233 my $rec = Koha
::UploadedFiles
->search_term({ term
=> 'file' })->next;
234 my @hdrs = $rec->httpheaders;
235 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' } });
237 my $cat = Koha
::UploadedFiles
->getCategories;
238 is
( @
$cat >= 1, 1, 'getCategories returned at least one category' );
241 subtest
'Testing allows_add_by' => sub {
244 my $patron = $builder->build({
245 source
=> 'Borrower',
246 value
=> { flags
=> 0 }, #no permissions
248 my $patronid = $patron->{borrowernumber
};
249 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
250 undef, 'Patron is not allowed to do anything' );
252 # add some permissions: edit_catalogue
253 my $fl = 2**9; # edit_catalogue
254 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
255 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
256 undef, 'Patron is still not allowed to add uploaded files' );
258 # replace flags by all tools
260 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
261 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
262 1, 'Patron should be allowed now to add uploaded files' );
264 # remove all tools and add upload_general_files only
265 $fl = 0; # no modules
266 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
268 source
=> 'UserPermission',
270 borrowernumber
=> $patronid,
271 module_bit
=> { module_bit
=> { flag
=> 'tools' } },
272 code
=> 'upload_general_files',
275 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
276 1, 'Patron is still allowed to add uploaded files' );
279 subtest
'Testing delete_temporary' => sub {
282 # Add two temporary files: result should be 3 + 3
283 Koha
::Uploader
->new({ tmp
=> 1 })->cgi; # add file6 and file7
284 is
( Koha
::UploadedFiles
->search->count, 6, 'Test starting count' );
285 is
( Koha
::UploadedFiles
->search({ permanent
=> 1 })->count, 3,
286 'Includes 3 permanent' );
288 # Move all permanents to today - 1
289 # Move temp 1 to today - 3, and temp 2,3 to today - 5
290 my $today = dt_from_string
;
291 $today->subtract( minutes
=> 2 ); # should be enough :)
292 my $dt = $today->clone->subtract( days
=> 1 );
293 foreach my $rec ( Koha
::UploadedFiles
->search({ permanent
=> 1 }) ) {
294 $rec->dtcreated($dt)->store;
296 my @recs = Koha
::UploadedFiles
->search({ permanent
=> 0 });
297 $dt = $today->clone->subtract( days
=> 3 );
298 $recs[0]->dtcreated($dt)->store;
299 $dt = $today->clone->subtract( days
=> 5 );
300 $recs[1]->dtcreated($dt)->store;
301 $recs[2]->dtcreated($dt)->store;
303 # Now call delete_temporary with 6, 5 and 0
304 t
::lib
::Mocks
::mock_preference
('UploadPurgeTemporaryFilesDays', 6 );
305 my $delete = Koha
::UploadedFiles
->delete_temporary;
306 is
( $delete, '0E0', 'Check return value with 6' );
307 is
( Koha
::UploadedFiles
->search->count, 6, 'Delete with pref==6' );
309 # use override parameter
310 $delete = Koha
::UploadedFiles
->delete_temporary({ override_pref
=> 5 });
311 is
( $delete, 2, 'Check return value with 5' );
312 is
( Koha
::UploadedFiles
->search->count, 4, 'Delete with override==5' );
314 t
::lib
::Mocks
::mock_preference
('UploadPurgeTemporaryFilesDays', 0 );
315 $delete = Koha
::UploadedFiles
->delete_temporary;
316 is
( $delete, 1, 'Check return value with 0' );
317 is
( Koha
::UploadedFiles
->search->count, 3, 'Delete with pref==0 makes 3' );
318 is
( Koha
::UploadedFiles
->search({ permanent
=> 1 })->count, 3,
319 'Still 3 permanent uploads' );
322 subtest
'Testing download headers' => sub {
324 my $test_pdf = Koha
::UploadedFile
->new({ filename
=> 'pdf.pdf', uploadcategorycode
=> 'B', filesize
=> 1000 });
325 my $test_not = Koha
::UploadedFile
->new({ filename
=> 'pdf.not', uploadcategorycode
=> 'B', filesize
=> 1000 });
326 my @pdf_expect = ( '-type'=>'application/pdf','Content-Disposition'=>'inline; filename=pdf.pdf' );
327 my @not_expect = ( '-type'=>'application/octet-stream','-attachment'=>'pdf.not' );
328 my @pdf_head = $test_pdf->httpheaders;
329 my @not_head = $test_not->httpheaders;
330 is_deeply
(\
@pdf_head, \
@pdf_expect,"Get inline pdf headers for pdf");
331 is_deeply
(\
@not_head, \
@not_expect,"Get download headers for non pdf");
334 $schema->storage->txn_rollback;
338 my ( $class, $hook ) = @_;
340 foreach my $uh ( @
{$uploads->[ $current_upload ]} ) {
341 for( my $i=0; $i< $uh->{size
}; $i+=1000 ) {
343 &$hook( $uh->{name
}, 'a'x1000
, $read );