Bug 15685: DBIC Schema changes
[koha.git] / t / db_dependent / Upload.t
blob8d773628f2d1aa33edd311ff23a5cec09c5d3496
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 12;
6 use Test::Warn;
8 use Test::MockModule;
9 use t::lib::Mocks;
10 use t::lib::TestBuilder;
12 use C4::Context;
13 use Koha::Database;
14 use Koha::DateUtils;
15 use Koha::UploadedFile;
16 use Koha::UploadedFiles;
17 use Koha::Uploader;
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 our $builder = t::lib::TestBuilder->new;
23 our $current_upload = 0;
24 our $uploads = [
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 );
59 # Start testing
60 subtest 'Make a fresh start' => sub {
61 plan tests => 1;
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 {
72 plan tests => 2;
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 {
82 plan tests => 9;
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' );
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' );
106 subtest 'Add another upload, check file_handle' => sub {
107 plan tests => 5;
109 my $upl = Koha::Uploader->new({
110 category => $uploads->[$current_upload]->[0]->{cat},
111 public => 1,
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' );
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 {
129 plan tests => 2;
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' );
138 subtest 'Add same file in same category' => sub {
139 plan tests => 3;
141 my $upl = Koha::Uploader->new({
142 category => $uploads->[$current_upload]->[0]->{cat},
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" );
151 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
152 plan tests => 10;
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;
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' );
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=~/^-?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
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=~/^-?1$/, 'Deleting record was successful' );
184 is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
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 $delete = $kohaobj->delete({ keep_file => 1 });
191 ok( $delete =~ /^(0E0|-1)$/, 'Repeated delete unsuccessful' );
192 # NOTE: Koha::Object->delete does not return 0E0 (yet?)
195 subtest 'Test delete_missing' => sub {
196 plan tests => 5;
198 # If we add files via TestBuilder, they do not exist
199 my $upload01 = $builder->build({ source => 'UploadedFile' });
200 my $upload02 = $builder->build({ source => 'UploadedFile' });
201 # dry run first
202 my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
203 is( $deleted, 2, 'Expect two records with missing files' );
204 isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
205 $deleted = Koha::UploadedFiles->delete_missing;
206 ok( $deleted =~ /^(2|-1)$/, 'Deleted two records with missing files' );
207 is( Koha::UploadedFiles->search({
208 id => [ $upload01->{id}, $upload02->{id} ],
209 })->count, 0, 'Records are gone' );
210 # Repeat it
211 $deleted = Koha::UploadedFiles->delete_missing;
212 is( $deleted, "0E0", "Return value of 0E0 expected" );
215 subtest 'Call search_term with[out] private flag' => sub {
216 plan tests => 3;
218 my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
219 is( @recs, 1, 'Returns only one public result' );
220 is( $recs[0]->filename, 'file3', 'Should be file3' );
222 is( Koha::UploadedFiles->search_term({
223 term => 'file', include_private => 1,
224 })->count, 4, 'Returns now four results' );
227 subtest 'Simple tests for httpheaders and getCategories' => sub {
228 plan tests => 2;
230 my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next;
231 my @hdrs = $rec->httpheaders;
232 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
233 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
234 my $cat = Koha::UploadedFiles->getCategories;
235 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
238 subtest 'Testing allows_add_by' => sub {
239 plan tests => 4;
241 my $patron = $builder->build({
242 source => 'Borrower',
243 value => { flags => 0 }, #no permissions
245 my $patronid = $patron->{borrowernumber};
246 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
247 undef, 'Patron is not allowed to do anything' );
249 # add some permissions: edit_catalogue
250 my $fl = 2**9; # edit_catalogue
251 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
252 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
253 undef, 'Patron is still not allowed to add uploaded files' );
255 # replace flags by all tools
256 $fl = 2**13; # tools
257 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
258 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
259 1, 'Patron should be allowed now to add uploaded files' );
261 # remove all tools and add upload_general_files only
262 $fl = 0; # no modules
263 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
264 $builder->build({
265 source => 'UserPermission',
266 value => {
267 borrowernumber => $patronid,
268 module_bit => { module_bit => { flag => 'tools' } },
269 code => 'upload_general_files',
272 is( Koha::Uploader->allows_add_by( $patron->{userid} ),
273 1, 'Patron is still allowed to add uploaded files' );
276 subtest 'Testing delete_temporary' => sub {
277 plan tests => 9;
279 # Add two temporary files: result should be 3 + 3
280 Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7
281 is( Koha::UploadedFiles->search->count, 6, 'Test starting count' );
282 is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
283 'Includes 3 permanent' );
285 # Move all permanents to today - 1
286 # Move temp 1 to today - 3, and temp 2,3 to today - 5
287 my $today = dt_from_string;
288 $today->subtract( minutes => 2 ); # should be enough :)
289 my $dt = $today->clone->subtract( days => 1 );
290 foreach my $rec ( Koha::UploadedFiles->search({ permanent => 1 }) ) {
291 $rec->dtcreated($dt)->store;
293 my @recs = Koha::UploadedFiles->search({ permanent => 0 });
294 $dt = $today->clone->subtract( days => 3 );
295 $recs[0]->dtcreated($dt)->store;
296 $dt = $today->clone->subtract( days => 5 );
297 $recs[1]->dtcreated($dt)->store;
298 $recs[2]->dtcreated($dt)->store;
300 # Now call delete_temporary with 6, 5 and 0
301 t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 6 );
302 my $delete = Koha::UploadedFiles->delete_temporary;
303 ok( $delete =~ /^(-1|0E0)$/, 'Check return value with 6' );
304 is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
306 # use override parameter
307 $delete = Koha::UploadedFiles->delete_temporary({ override_pref => 5 });
308 ok( $delete =~ /^(2|-1)$/, 'Check return value with 5' );
309 is( Koha::UploadedFiles->search->count, 4, 'Delete with override==5' );
311 t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 0 );
312 $delete = Koha::UploadedFiles->delete_temporary;
313 ok( $delete =~ /^(-1|1)$/, 'Check return value with 0' );
314 is( Koha::UploadedFiles->search->count, 3, 'Delete with pref==0 makes 3' );
315 is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
316 'Still 3 permanent uploads' );
319 # The end
320 $schema->storage->txn_rollback;
322 # Helper routine
323 sub newCGI {
324 my ( $class, $hook ) = @_;
325 my $read = 0;
326 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
327 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
328 $read+= 1000;
329 &$hook( $uh->{name}, 'a'x1000, $read );
332 $current_upload++;
333 return $class;