Bug 26581: Only reindex records once per checkin
[koha.git] / t / db_dependent / Upload.t
blob23a50caa01232e5120c0ad9d040c089972c715c9
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 13;
6 use Test::Warn;
7 use Try::Tiny;
9 use Test::MockModule;
10 use t::lib::Mocks;
11 use t::lib::TestBuilder;
13 use C4::Context;
14 use Koha::Database;
15 use Koha::DateUtils;
16 use Koha::UploadedFile;
17 use Koha::UploadedFiles;
18 use Koha::Uploader;
20 my $schema = Koha::Database->new->schema;
21 $schema->storage->txn_begin;
22 our $builder = t::lib::TestBuilder->new;
24 our $current_upload = 0;
25 our $uploads = [
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 );
60 # Start testing
61 subtest 'Make a fresh start' => sub {
62 plan tests => 1;
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 {
73 plan tests => 2;
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 {
83 plan tests => 9;
85 my $upl = Koha::Uploader->new({
86 category => $uploads->[$current_upload]->[0]->{cat},
87 });
88 my $cgi= $upl->cgi;
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' }});
97 my $rec = $rs->next;
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' );
101 $rec = $rs->next;
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 {
108 plan tests => 5;
110 my $upl = Koha::Uploader->new({
111 category => $uploads->[$current_upload]->[0]->{cat},
112 public => 1,
114 my $cgi= $upl->cgi;
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 {
130 plan tests => 2;
132 my $upl = Koha::Uploader->new({ tmp => 1 }); #temporary
133 my $cgi= $upl->cgi;
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 {
140 plan tests => 3;
142 my $upl = Koha::Uploader->new({
143 category => $uploads->[$current_upload]->[0]->{cat},
145 my $cgi= $upl->cgi;
146 is( $upl->count, 0, 'Upload 4 failed as expected' );
147 is( $upl->result, undef, 'Result is undefined' );
148 my $e = $upl->err;
149 is( $e->{file2}->{code}, Koha::Uploader::ERR_EXISTS, "Already exists error reported" );
152 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
153 plan tests => 10;
155 # add temporary file with same name and contents (file4)
156 my $upl = Koha::Uploader->new({ tmp => 1 });
157 my $cgi= $upl->cgi;
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 });
171 $upl->cgi;
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
179 # catch warning
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 });
191 try {
192 $delete = $kohaobj->delete({ keep_file => 1 });
193 } catch {
194 ok( $_->isa("DBIx::Class::Exception"), 'Repeated delete unsuccessful' );
198 subtest 'Test delete_missing' => sub {
199 plan tests => 5;
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' });
204 # dry run first
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' );
213 # Repeat it
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 {
219 plan tests => 3;
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 {
231 plan tests => 2;
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 {
242 plan tests => 4;
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
259 $fl = 2**13; # 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 });
267 $builder->build({
268 source => 'UserPermission',
269 value => {
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 {
280 plan tests => 9;
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 {
323 plan tests => 2;
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");
333 # The end
334 $schema->storage->txn_rollback;
336 # Helper routine
337 sub newCGI {
338 my ( $class, $hook ) = @_;
339 my $read = 0;
340 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
341 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
342 $read+= 1000;
343 &$hook( $uh->{name}, 'a'x1000, $read );
346 $current_upload++;
347 return $class;