4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 9;
9 use t::lib::TestBuilder;
14 use Koha::UploadedFiles;
16 my $schema = Koha::Database->new->schema;
17 $schema->storage->txn_begin;
19 our $current_upload = 0;
22 { name => 'file1', cat => 'A', size => 6000 },
23 { name => 'file2', cat => 'A', size => 8000 },
26 { name => 'file3', cat => 'B', size => 1000 },
29 { name => 'file4', cat => undef, size => 5000 }, # temporary
32 { name => 'file2', cat => 'A', size => 8000 },
33 # uploading a duplicate in cat A should fail
36 { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
39 { name => 'file5', cat => undef, size => 7000 },
43 # Redirect upload dir structure and mock File::Spec and CGI
44 my $tempdir = tempdir( CLEANUP => 1 );
45 t::lib::Mocks::mock_config('upload_path', $tempdir);
46 my $specmod = Test::MockModule->new( 'File::Spec' );
47 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
48 my $cgimod = Test::MockModule->new( 'CGI' );
49 $cgimod->mock( 'new' => \&newCGI );
52 subtest 'Test01' => sub {
56 subtest 'Test02' => sub {
60 subtest 'Test03' => sub {
64 subtest 'Test04' => sub {
68 subtest 'Test05' => sub {
72 subtest 'Test06' => sub {
76 subtest 'Test07' => sub {
80 subtest 'Test08: allows_add_by' => sub {
84 $schema->storage->txn_rollback;
87 # Delete existing records (for later tests)
88 # Passing keep_file suppresses warnings
89 Koha::UploadedFiles->new->delete({ keep_file => 1 });
91 # Check mocked directories
92 is( Koha::UploadedFile->permanent_directory, $tempdir,
93 'Check permanent directory' );
94 is( Koha::UploadedFile->temporary_directory, $tempdir,
95 'Check temporary directory' );
97 my $upl = Koha::Upload->new({
98 category => $uploads->[$current_upload]->[0]->{cat},
101 my $res= $upl->result;
102 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
103 is( $upl->count, 2, 'Count returns 2 also' );
104 is( $upl->err, undef, 'No errors reported' );
106 my $rs = Koha::UploadedFiles->search({
107 id => [ split ',', $res ]
108 }, { order_by => { -asc => 'filename' }});
110 is( $rec->filename, 'file1', 'Check file name' );
111 is( $rec->uploadcategorycode, 'A', 'Check category A' );
112 is( $rec->filesize, 6000, 'Check size of file1' );
114 is( $rec->filename, 'file2', 'Check file name 2' );
115 is( $rec->filesize, 8000, 'Check size of file2' );
116 is( $rec->public, undef, 'Check public undefined' );
120 my $upl = Koha::Upload->new({
121 category => $uploads->[$current_upload]->[0]->{cat},
125 is( $upl->count, 1, 'Upload 2 includes one file' );
126 my $res= $upl->result;
127 my $rec = Koha::UploadedFiles->find( $res );
128 is( $rec->uploadcategorycode, 'B', 'Check category B' );
129 is( $rec->public, 1, 'Check public == 1' );
130 my $fh = $rec->file_handle;
131 is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
133 my $orgname = $rec->filename;
134 $rec->filename( 'doesprobablynotexist' )->store;
135 is( $rec->file_handle, undef, 'Sabotage with file handle' );
136 $rec->filename( $orgname )->store;
140 my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
142 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
143 my $rec = Koha::UploadedFiles->find( $upl->result );
144 is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
147 sub test04 { # Fail on a file already there
148 my $upl = Koha::Upload->new({
149 category => $uploads->[$current_upload]->[0]->{cat},
152 is( $upl->count, 0, 'Upload 4 failed as expected' );
153 is( $upl->result, undef, 'Result is undefined' );
155 is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
158 sub test05 { # add temporary file with same name and contents, delete it
159 my $upl = Koha::Upload->new({ tmp => 1 });
161 is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
162 my $id = $upl->result;
163 my $path = Koha::UploadedFiles->find( $id )->full_path;
165 # testing delete via UploadedFiles (plural)
166 my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
167 is( $delete, 1, 'Delete successful' );
168 isnt( -e $path, 1, 'File no longer found after delete' );
169 is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
171 # testing delete via UploadedFile (singular)
172 # Note that find returns a Koha::Object
173 $upl = Koha::Upload->new({ tmp => 1 });
175 my $kohaobj = Koha::UploadedFiles->find( $upl->result );
176 my $name = $kohaobj->filename;
177 $path = $kohaobj->full_path;
178 $delete = $kohaobj->delete;
179 is( $delete, $name, 'Delete successful' );
180 isnt( -e $path, 1, 'File no longer found after delete' );
183 sub test06 { #search_term with[out] private flag
184 my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
185 is( @recs, 1, 'Returns only one public result' );
186 is( $recs[0]->filename, 'file3', 'Should be file3' );
188 is( Koha::UploadedFiles->search_term({
189 term => 'file', include_private => 1,
190 })->count, 4, 'Returns now four results' );
193 sub test07 { #simple test for httpheaders and getCategories
194 my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
195 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
196 my $builder = t::lib::TestBuilder->new;
197 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
198 my $cat = Koha::Upload->getCategories;
199 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
202 sub test08 { # allows_add_by
203 my $builder = t::lib::TestBuilder->new;
204 my $patron = $builder->build({
205 source => 'Borrower',
206 value => { flags => 0 }, #no permissions
208 my $patronid = $patron->{borrowernumber};
209 is( Koha::Upload->allows_add_by( $patron->{userid} ),
210 undef, 'Patron is not allowed to do anything' );
212 # add some permissions: edit_catalogue
213 my $fl = 2**9; # edit_catalogue
214 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
215 is( Koha::Upload->allows_add_by( $patron->{userid} ),
216 undef, 'Patron is still not allowed to add uploaded files' );
218 # replace flags by all tools
220 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
221 is( Koha::Upload->allows_add_by( $patron->{userid} ),
222 1, 'Patron should be allowed now to add uploaded files' );
224 # remove all tools and add upload_general_files only
225 $fl = 0; # no modules
226 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
228 source => 'UserPermission',
230 borrowernumber => $patronid,
231 module_bit => { module_bit => { flag => 'tools' } },
232 code => 'upload_general_files',
235 is( Koha::Upload->allows_add_by( $patron->{userid} ),
236 1, 'Patron is still allowed to add uploaded files' );
239 # Additional tests for Koha::UploadedFiles
240 # TODO Rearrange the tests after this migration
241 subtest 'Some basic CRUD testing' => sub {
244 # Test find and attribute id, delete and search
245 my $builder = t::lib::TestBuilder->new;
246 my $upload01 = $builder->build({ source => 'UploadedFile' });
247 my $found = Koha::UploadedFiles->find( $upload01->{id} );
248 is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
249 $found->delete({ keep_file => 1 }); #note that it does not exist
250 $found = Koha::UploadedFiles->search(
251 { id => $upload01->{id} },
253 is( $found->count, 0, 'Delete seems successful' );
257 my ( $class, $hook ) = @_;
259 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
260 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
262 &$hook( $uh->{name}, 'a'x1000, $read );