4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 9;
9 use t::lib::TestBuilder;
14 use Koha::UploadedFile;
15 use Koha::UploadedFiles;
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
20 our $current_upload = 0;
23 { name => 'file1', cat => 'A', size => 6000 },
24 { name => 'file2', cat => 'A', size => 8000 },
27 { name => 'file3', cat => 'B', size => 1000 },
30 { name => 'file4', cat => undef, size => 5000 }, # temporary
33 { name => 'file2', cat => 'A', size => 8000 },
34 # uploading a duplicate in cat A should fail
37 { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
40 { name => 'file5', cat => undef, size => 7000 },
44 # Redirect upload dir structure and mock File::Spec and CGI
45 my $tempdir = tempdir( CLEANUP => 1 );
46 t::lib::Mocks::mock_config('upload_path', $tempdir);
47 my $specmod = Test::MockModule->new( 'File::Spec' );
48 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
49 my $cgimod = Test::MockModule->new( 'CGI' );
50 $cgimod->mock( 'new' => \&newCGI );
53 subtest 'Test01' => sub {
57 subtest 'Test02' => sub {
61 subtest 'Test03' => sub {
65 subtest 'Test04' => sub {
69 subtest 'Test05' => sub {
73 subtest 'Test06' => sub {
77 subtest 'Test07' => sub {
81 subtest 'Test08: allows_add_by' => sub {
85 $schema->storage->txn_rollback;
88 # Delete existing records (for later tests)
89 # Passing keep_file suppresses warnings
90 Koha::UploadedFiles->new->delete({ keep_file => 1 });
92 # Check mocked directories
93 is( Koha::UploadedFile->permanent_directory, $tempdir,
94 'Check permanent directory' );
95 is( Koha::UploadedFile->temporary_directory, $tempdir,
96 'Check temporary directory' );
98 my $upl = Koha::Upload->new({
99 category => $uploads->[$current_upload]->[0]->{cat},
102 my $res= $upl->result;
103 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
104 is( $upl->count, 2, 'Count returns 2 also' );
105 is( $upl->err, undef, 'No errors reported' );
107 my $rs = Koha::UploadedFiles->search({
108 id => [ split ',', $res ]
109 }, { order_by => { -asc => 'filename' }});
111 is( $rec->filename, 'file1', 'Check file name' );
112 is( $rec->uploadcategorycode, 'A', 'Check category A' );
113 is( $rec->filesize, 6000, 'Check size of file1' );
115 is( $rec->filename, 'file2', 'Check file name 2' );
116 is( $rec->filesize, 8000, 'Check size of file2' );
117 is( $rec->public, undef, 'Check public undefined' );
121 my $upl = Koha::Upload->new({
122 category => $uploads->[$current_upload]->[0]->{cat},
126 is( $upl->count, 1, 'Upload 2 includes one file' );
127 my $res= $upl->result;
128 my $rec = Koha::UploadedFiles->find( $res );
129 is( $rec->uploadcategorycode, 'B', 'Check category B' );
130 is( $rec->public, 1, 'Check public == 1' );
131 my $fh = $rec->file_handle;
132 is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
134 my $orgname = $rec->filename;
135 $rec->filename( 'doesprobablynotexist' )->store;
136 is( $rec->file_handle, undef, 'Sabotage with file handle' );
137 $rec->filename( $orgname )->store;
141 my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
143 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
144 my $rec = Koha::UploadedFiles->find( $upl->result );
145 is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
148 sub test04 { # Fail on a file already there
149 my $upl = Koha::Upload->new({
150 category => $uploads->[$current_upload]->[0]->{cat},
153 is( $upl->count, 0, 'Upload 4 failed as expected' );
154 is( $upl->result, undef, 'Result is undefined' );
156 is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
159 sub test05 { # add temporary file with same name and contents, delete it
160 my $upl = Koha::Upload->new({ tmp => 1 });
162 is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
163 my $id = $upl->result;
164 my $path = Koha::UploadedFiles->find( $id )->full_path;
166 # testing delete via UploadedFiles (plural)
167 my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
168 is( $delete, 1, 'Delete successful' );
169 isnt( -e $path, 1, 'File no longer found after delete' );
170 is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
172 # testing delete via UploadedFile (singular)
173 # Note that find returns a Koha::Object
174 $upl = Koha::Upload->new({ tmp => 1 });
176 my $kohaobj = Koha::UploadedFiles->find( $upl->result );
177 my $name = $kohaobj->filename;
178 $path = $kohaobj->full_path;
179 $delete = $kohaobj->delete;
180 is( $delete, $name, 'Delete successful' );
181 isnt( -e $path, 1, 'File no longer found after delete' );
184 sub test06 { #search_term with[out] private flag
185 my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
186 is( @recs, 1, 'Returns only one public result' );
187 is( $recs[0]->filename, 'file3', 'Should be file3' );
189 is( Koha::UploadedFiles->search_term({
190 term => 'file', include_private => 1,
191 })->count, 4, 'Returns now four results' );
194 sub test07 { #simple test for httpheaders and getCategories
195 my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next;
196 my @hdrs = $rec->httpheaders;
197 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
198 my $builder = t::lib::TestBuilder->new;
199 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
200 my $cat = Koha::UploadedFile->getCategories;
201 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
204 sub test08 { # allows_add_by
205 my $builder = t::lib::TestBuilder->new;
206 my $patron = $builder->build({
207 source => 'Borrower',
208 value => { flags => 0 }, #no permissions
210 my $patronid = $patron->{borrowernumber};
211 is( Koha::Upload->allows_add_by( $patron->{userid} ),
212 undef, 'Patron is not allowed to do anything' );
214 # add some permissions: edit_catalogue
215 my $fl = 2**9; # edit_catalogue
216 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
217 is( Koha::Upload->allows_add_by( $patron->{userid} ),
218 undef, 'Patron is still not allowed to add uploaded files' );
220 # replace flags by all tools
222 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
223 is( Koha::Upload->allows_add_by( $patron->{userid} ),
224 1, 'Patron should be allowed now to add uploaded files' );
226 # remove all tools and add upload_general_files only
227 $fl = 0; # no modules
228 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
230 source => 'UserPermission',
232 borrowernumber => $patronid,
233 module_bit => { module_bit => { flag => 'tools' } },
234 code => 'upload_general_files',
237 is( Koha::Upload->allows_add_by( $patron->{userid} ),
238 1, 'Patron is still allowed to add uploaded files' );
241 # Additional tests for Koha::UploadedFiles
242 # TODO Rearrange the tests after this migration
243 subtest 'Some basic CRUD testing' => sub {
246 # Test find and attribute id, delete and search
247 my $builder = t::lib::TestBuilder->new;
248 my $upload01 = $builder->build({ source => 'UploadedFile' });
249 my $found = Koha::UploadedFiles->find( $upload01->{id} );
250 is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
251 $found->delete({ keep_file => 1 }); #note that it does not exist
252 $found = Koha::UploadedFiles->search(
253 { id => $upload01->{id} },
255 is( $found->count, 0, 'Delete seems successful' );
259 my ( $class, $hook ) = @_;
261 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
262 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
264 &$hook( $uh->{name}, 'a'x1000, $read );