4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 11;
10 use t::lib::TestBuilder;
15 use Koha::UploadedFile;
16 use Koha::UploadedFiles;
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $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 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 );
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( Koha::UploadedFile->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}, 1, "Errcode 1 [already exists] 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 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' );
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 is( $delete, 1, 'Delete successful' );
175 isnt( -e $path, 1, 'File no longer found after delete' );
177 # add another record with TestBuilder, so file does not exist
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' );
186 subtest 'Call search_term with[out] private flag' => sub {
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' );
193 is( Koha::UploadedFiles->search_term({
194 term => 'file', include_private => 1,
195 })->count, 4, 'Returns now four results' );
198 subtest 'Simple tests for httpheaders and getCategories' => sub {
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' );
209 subtest 'Testing allows_add_by' => sub {
212 my $patron = $builder->build({
213 source => 'Borrower',
214 value => { flags => 0 }, #no permissions
216 my $patronid = $patron->{borrowernumber};
217 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
218 undef, 'Patron is not allowed to do anything' );
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' );
226 # replace flags by all 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' );
232 # remove all tools and add upload_general_files only
233 $fl = 0; # no modules
234 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
236 source => 'UserPermission',
238 borrowernumber => $patronid,
239 module_bit => { module_bit => { flag => 'tools' } },
240 code => 'upload_general_files',
243 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
244 1, 'Patron is still allowed to add uploaded files' );
247 subtest 'Testing delete_temporary' => sub {
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' );
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;
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;
271 # Now call delete_temporary with 0, 6, 5 and 1 (via override)
272 t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 0 );
273 Koha::UploadedFiles->delete_temporary;
274 is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==0' );
276 t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 6 );
277 Koha::UploadedFiles->delete_temporary;
278 is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
280 t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 5 );
281 Koha::UploadedFiles->delete_temporary;
282 is( Koha::UploadedFiles->search->count, 4, 'Delete with pref==5 makes 4' );
284 Koha::UploadedFiles->delete_temporary({ override_pref => 1 });
285 is( Koha::UploadedFiles->search->count, 3, 'Delete override==1 makes 3' );
286 is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
287 'Still 3 permanent uploads' );
291 $schema->storage->txn_rollback;
295 my ( $class, $hook ) = @_;
297 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
298 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
300 &$hook( $uh->{name}, 'a'x1000, $read );