Bug 17669: [QA Follow-up] Allow zero in temp-uploads-days
[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 => 11;
6 use Test::Warn;
7
8 use Test::MockModule;
9 use t::lib::Mocks;
10 use t::lib::TestBuilder;
11
12 use C4::Context;
13 use Koha::Database;
14 use Koha::DateUtils;
15 use Koha::UploadedFile;
16 use Koha::UploadedFiles;
17 use Koha::Uploader;
18
19 my $schema  = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $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 File::Spec and CGI
52 my $tempdir = tempdir( CLEANUP => 1 );
53 t::lib::Mocks::mock_config('upload_path', $tempdir);
54 my $specmod = Test::MockModule->new( 'File::Spec' );
55 $specmod->mock( 'tmpdir' => 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( Koha::UploadedFile->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}, 1, "Errcode 1 [already exists] reported" );
149 };
150
151 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
152     plan tests => 8;
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     is( $delete, 1, '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     is( $delete, 1, '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 { Koha::UploadedFiles->find( $upload01->{id} )->delete; }
181         qr/file was missing/,
182         'delete warns when file is missing';
183     is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
184 };
185
186 subtest 'Call search_term with[out] private flag' => sub {
187     plan tests => 3;
188
189     my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
190     is( @recs, 1, 'Returns only one public result' );
191     is( $recs[0]->filename, 'file3', 'Should be file3' );
192
193     is( Koha::UploadedFiles->search_term({
194         term => 'file', include_private => 1,
195     })->count, 4, 'Returns now four results' );
196 };
197
198 subtest 'Simple tests for httpheaders and getCategories' => sub {
199     plan tests => 2;
200
201     my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next;
202     my @hdrs = $rec->httpheaders;
203     is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
204     $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
205     my $cat = Koha::UploadedFiles->getCategories;
206     is( @$cat >= 1, 1, 'getCategories returned at least one category' );
207 };
208
209 subtest 'Testing allows_add_by' => sub {
210     plan tests => 4;
211
212     my $patron = $builder->build({
213         source => 'Borrower',
214         value  => { flags => 0 }, #no permissions
215     });
216     my $patronid = $patron->{borrowernumber};
217     is( Koha::Uploader->allows_add_by( $patron->{userid} ),
218         undef, 'Patron is not allowed to do anything' );
219
220     # add some permissions: edit_catalogue
221     my $fl = 2**9; # edit_catalogue
222     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
223     is( Koha::Uploader->allows_add_by( $patron->{userid} ),
224         undef, 'Patron is still not allowed to add uploaded files' );
225
226     # replace flags by all tools
227     $fl = 2**13; # tools
228     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
229     is( Koha::Uploader->allows_add_by( $patron->{userid} ),
230         1, 'Patron should be allowed now to add uploaded files' );
231
232     # remove all tools and add upload_general_files only
233     $fl = 0; # no modules
234     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
235     $builder->build({
236         source => 'UserPermission',
237         value  => {
238             borrowernumber => $patronid,
239             module_bit     => { module_bit => { flag => 'tools' } },
240             code           => 'upload_general_files',
241         },
242     });
243     is( Koha::Uploader->allows_add_by( $patron->{userid} ),
244         1, 'Patron is still allowed to add uploaded files' );
245 };
246
247 subtest 'Testing delete_temporary' => sub {
248     plan tests => 6;
249
250     # Add two temporary files: result should be 3 + 3
251     Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7
252     is( Koha::UploadedFiles->search->count, 6, 'Test starting count' );
253     is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
254         'Includes 3 permanent' );
255
256     # Move all permanents to today - 1
257     # Move temp 1 to today - 3, and temp 2,3 to today - 5
258     my $today = dt_from_string;
259     $today->subtract( minutes => 2 ); # should be enough :)
260     my $dt = $today->clone->subtract( days => 1 );
261     foreach my $rec ( Koha::UploadedFiles->search({ permanent => 1 }) ) {
262         $rec->dtcreated($dt)->store;
263     }
264     my @recs = Koha::UploadedFiles->search({ permanent => 0 });
265     $dt = $today->clone->subtract( days => 3 );
266     $recs[0]->dtcreated($dt)->store;
267     $dt = $today->clone->subtract( days => 5 );
268     $recs[1]->dtcreated($dt)->store;
269     $recs[2]->dtcreated($dt)->store;
270
271     # Now call delete_temporary with 6, 5 and 0
272     t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 6 );
273     Koha::UploadedFiles->delete_temporary;
274     is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
275
276     # use override parameter
277     Koha::UploadedFiles->delete_temporary({ override_pref => 5 });
278     is( Koha::UploadedFiles->search->count, 4, 'Delete with override==5' );
279
280     t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 0 );
281     Koha::UploadedFiles->delete_temporary;
282     is( Koha::UploadedFiles->search->count, 3, 'Delete with pref==0 makes 3' );
283     is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
284         'Still 3 permanent uploads' );
285 };
286
287 # The end
288 $schema->storage->txn_rollback;
289
290 # Helper routine
291 sub newCGI {
292     my ( $class, $hook ) = @_;
293     my $read = 0;
294     foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
295         for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
296             $read+= 1000;
297             &$hook( $uh->{name}, 'a'x1000, $read );
298         }
299     }
300     $current_upload++;
301     return $class;
302 }