Bug 29144: Add $contents->find_best_match, $library->opac_info
[koha.git] / t / db_dependent / Upload.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 13;
6 use Test::Warn;
7 use Try::Tiny;
8
9 use Test::MockModule;
10 use t::lib::Mocks;
11 use t::lib::TestBuilder;
12
13 use C4::Context;
14 use Koha::Database;
15 use Koha::DateUtils qw( dt_from_string );
16 use Koha::UploadedFiles;
17 use Koha::Uploader;
18
19 my $schema  = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 our $builder = t::lib::TestBuilder->new;
22
23 our $current_upload = 0;
24 our $uploads = [
25     [
26         { name => 'file1', cat => 'A', size => 6000 },
27         { name => 'file2', cat => 'A', size => 8000 },
28     ],
29     [
30         { name => 'file3', cat => 'B', size => 1000 },
31     ],
32     [
33         { name => 'file4', cat => undef, size => 5000 }, # temporary
34     ],
35     [
36         { name => 'file2', cat => 'A', size => 8000 },
37         # uploading a duplicate in cat A should fail
38     ],
39     [
40         { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
41     ],
42     [
43         { name => 'file5', cat => undef, size => 7000 },
44     ],
45     [
46         { name => 'file6', cat => undef, size => 6500 },
47         { name => 'file7', cat => undef, size => 6501 },
48     ],
49 ];
50
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 );
58
59 # Start testing
60 subtest 'Make a fresh start' => sub {
61     plan tests => 1;
62
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' );
69 };
70
71 subtest 'permanent_directory and temporary_directory' => sub {
72     plan tests => 2;
73
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' );
79 };
80
81 subtest 'Add two uploads in category A' => sub {
82     plan tests => 9;
83
84     my $upl = Koha::Uploader->new({
85         category => $uploads->[$current_upload]->[0]->{cat},
86     });
87     my $cgi= $upl->cgi;
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' );
92
93     my $rs = Koha::UploadedFiles->search({
94         id => [ split ',', $res ]
95     }, { order_by => { -asc => 'filename' }});
96     my $rec = $rs->next;
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' );
100     $rec = $rs->next;
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' );
104 };
105
106 subtest 'Add another upload, check file_handle' => sub {
107     plan tests => 5;
108
109     my $upl = Koha::Uploader->new({
110         category => $uploads->[$current_upload]->[0]->{cat},
111         public => 1,
112     });
113     my $cgi= $upl->cgi;
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' );
121
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;
126 };
127
128 subtest 'Add temporary upload' => sub {
129     plan tests => 2;
130
131     my $upl = Koha::Uploader->new({ tmp => 1 }); #temporary
132     my $cgi= $upl->cgi;
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' );
136 };
137
138 subtest 'Add same file in same category' => sub {
139     plan tests => 3;
140
141     my $upl = Koha::Uploader->new({
142         category => $uploads->[$current_upload]->[0]->{cat},
143     });
144     my $cgi= $upl->cgi;
145     is( $upl->count, 0, 'Upload 4 failed as expected' );
146     is( $upl->result, undef, 'Result is undefined' );
147     my $e = $upl->err;
148     is( $e->{file2}->{code}, Koha::Uploader::ERR_EXISTS, "Already exists error reported" );
149 };
150
151 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
152     plan tests => 10;
153
154     # add temporary file with same name and contents (file4)
155     my $upl = Koha::Uploader->new({ tmp => 1 });
156     my $cgi= $upl->cgi;
157     is( $upl->count, 1, 'Add duplicate temporary file (file4)' );
158     my $id = $upl->result;
159     my $path = Koha::UploadedFiles->find( $id )->full_path;
160
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' );
166
167     # testing delete via UploadedFile (singular)
168     # Note that find returns a Koha::Object
169     $upl = Koha::Uploader->new({ tmp => 1 });
170     $upl->cgi;
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' );
176
177     # add another record with TestBuilder, so file does not exist
178     # catch warning
179     my $upload01 = $builder->build({ source => 'UploadedFile' });
180     warning_like { $delete = Koha::UploadedFiles->find( $upload01->{id} )->delete; }
181         qr/file was missing/,
182         'delete warns when file is missing';
183     ok( $delete, 'Deleting record was successful' );
184     is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
185
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 });
190     try {
191         $delete = $kohaobj->delete({ keep_file => 1 });
192     } catch {
193         ok( $_->isa("DBIx::Class::Exception"), 'Repeated delete unsuccessful' );
194     }
195 };
196
197 subtest 'Test delete_missing' => sub {
198     plan tests => 5;
199
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' });
203     # dry run first
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' );
212     # Repeat it
213     $deleted = Koha::UploadedFiles->delete_missing;
214     is( $deleted, "0E0", "Return value of 0E0 expected" );
215 };
216
217 subtest 'Call search_term with[out] private flag' => sub {
218     plan tests => 3;
219
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' );
223
224     is( Koha::UploadedFiles->search_term({
225         term => 'file', include_private => 1,
226     })->count, 4, 'Returns now four results' );
227 };
228
229 subtest 'Simple tests for httpheaders and getCategories' => sub {
230     plan tests => 2;
231
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' );
238 };
239
240 subtest 'Testing allows_add_by' => sub {
241     plan tests => 4;
242
243     my $patron = $builder->build({
244         source => 'Borrower',
245         value  => { flags => 0 }, #no permissions
246     });
247     my $patronid = $patron->{borrowernumber};
248     is( Koha::Uploader->allows_add_by( $patron->{userid} ),
249         undef, 'Patron is not allowed to do anything' );
250
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' );
256
257     # replace flags by all tools
258     $fl = 2**13; # 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' );
262
263     # remove all tools and add upload_general_files only
264     $fl = 0; # no modules
265     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
266     $builder->build({
267         source => 'UserPermission',
268         value  => {
269             borrowernumber => $patronid,
270             module_bit     => { module_bit => { flag => 'tools' } },
271             code           => 'upload_general_files',
272         },
273     });
274     is( Koha::Uploader->allows_add_by( $patron->{userid} ),
275         1, 'Patron is still allowed to add uploaded files' );
276 };
277
278 subtest 'Testing delete_temporary' => sub {
279     plan tests => 9;
280
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' );
286
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;
294     }
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;
301
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' );
307
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' );
312
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' );
319 };
320
321 subtest 'Testing download headers' => sub {
322     plan tests => 2;
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");
331 };
332 # The end
333 $schema->storage->txn_rollback;
334
335 # Helper routine
336 sub newCGI {
337     my ( $class, $hook ) = @_;
338     my $read = 0;
339     foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
340         for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
341             $read+= 1000;
342             &$hook( $uh->{name}, 'a'x1000, $read );
343         }
344     }
345     $current_upload++;
346     return $class;
347 }