4 use File
::Temp qw
/ tempdir /;
5 use Test
::More tests
=> 10;
10 use t
::lib
::TestBuilder
;
14 use Koha
::UploadedFile
;
15 use Koha
::UploadedFiles
;
18 my $schema = Koha
::Database
->new->schema;
19 $schema->storage->txn_begin;
20 my $builder = t
::lib
::TestBuilder
->new;
22 our $current_upload = 0;
25 { name
=> 'file1', cat
=> 'A', size
=> 6000 },
26 { name
=> 'file2', cat
=> 'A', size
=> 8000 },
29 { name
=> 'file3', cat
=> 'B', size
=> 1000 },
32 { name
=> 'file4', cat
=> undef, size
=> 5000 }, # temporary
35 { name
=> 'file2', cat
=> 'A', size
=> 8000 },
36 # uploading a duplicate in cat A should fail
39 { name
=> 'file4', cat
=> undef, size
=> 5000 }, # temp duplicate
42 { name
=> 'file5', cat
=> undef, size
=> 7000 },
46 # Redirect upload dir structure and mock File::Spec and CGI
47 my $tempdir = tempdir
( CLEANUP
=> 1 );
48 t
::lib
::Mocks
::mock_config
('upload_path', $tempdir);
49 my $specmod = Test
::MockModule
->new( 'File::Spec' );
50 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
51 my $cgimod = Test
::MockModule
->new( 'CGI' );
52 $cgimod->mock( 'new' => \
&newCGI
);
55 subtest
'Make a fresh start' => sub {
58 # Delete existing records (for later tests)
59 # Passing keep_file suppresses warnings (and does not delete files)
60 # Note that your files are not in danger, since we redirected
61 # all files to a new empty temp folder
62 Koha
::UploadedFiles
->delete({ keep_file
=> 1 });
63 is
( Koha
::UploadedFiles
->count, 0, 'No records left' );
66 subtest
'permanent_directory and temporary_directory' => sub {
69 # Check mocked directories
70 is
( Koha
::UploadedFile
->permanent_directory, $tempdir,
71 'Check permanent directory' );
72 is
( Koha
::UploadedFile
->temporary_directory, $tempdir,
73 'Check temporary directory' );
76 subtest
'Add two uploads in category A' => sub {
79 my $upl = Koha
::Uploader
->new({
80 category
=> $uploads->[$current_upload]->[0]->{cat
},
83 my $res= $upl->result;
84 is
( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
85 is
( $upl->count, 2, 'Count returns 2 also' );
86 is
( $upl->err, undef, 'No errors reported' );
88 my $rs = Koha
::UploadedFiles
->search({
89 id
=> [ split ',', $res ]
90 }, { order_by
=> { -asc
=> 'filename' }});
92 is
( $rec->filename, 'file1', 'Check file name' );
93 is
( $rec->uploadcategorycode, 'A', 'Check category A' );
94 is
( $rec->filesize, 6000, 'Check size of file1' );
96 is
( $rec->filename, 'file2', 'Check file name 2' );
97 is
( $rec->filesize, 8000, 'Check size of file2' );
98 is
( $rec->public, undef, 'Check public undefined' );
101 subtest
'Add another upload, check file_handle' => sub {
104 my $upl = Koha
::Uploader
->new({
105 category
=> $uploads->[$current_upload]->[0]->{cat
},
109 is
( $upl->count, 1, 'Upload 2 includes one file' );
110 my $res= $upl->result;
111 my $rec = Koha
::UploadedFiles
->find( $res );
112 is
( $rec->uploadcategorycode, 'B', 'Check category B' );
113 is
( $rec->public, 1, 'Check public == 1' );
114 my $fh = $rec->file_handle;
115 is
( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
117 my $orgname = $rec->filename;
118 $rec->filename( 'doesprobablynotexist' )->store;
119 is
( $rec->file_handle, undef, 'Sabotage with file handle' );
120 $rec->filename( $orgname )->store;
123 subtest
'Add temporary upload' => sub {
126 my $upl = Koha
::Uploader
->new({ tmp
=> 1 }); #temporary
128 is
( $upl->count, 1, 'Upload 3 includes one temporary file' );
129 my $rec = Koha
::UploadedFiles
->find( $upl->result );
130 is
( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
133 subtest
'Add same file in same category' => sub {
136 my $upl = Koha
::Uploader
->new({
137 category
=> $uploads->[$current_upload]->[0]->{cat
},
140 is
( $upl->count, 0, 'Upload 4 failed as expected' );
141 is
( $upl->result, undef, 'Result is undefined' );
143 is
( $e->{file2
}, 1, "Errcode 1 [already exists] reported" );
146 subtest
'Test delete via UploadedFile as well as UploadedFiles' => sub {
149 # add temporary file with same name and contents (file4)
150 my $upl = Koha
::Uploader
->new({ tmp
=> 1 });
152 is
( $upl->count, 1, 'Add duplicate temporary file (file4)' );
153 my $id = $upl->result;
154 my $path = Koha
::UploadedFiles
->find( $id )->full_path;
156 # testing delete via UploadedFiles (plural)
157 my $delete = Koha
::UploadedFiles
->search({ id
=> $id })->delete;
158 is
( $delete, 1, 'Delete successful' );
159 isnt
( -e
$path, 1, 'File no longer found after delete' );
160 is
( Koha
::UploadedFiles
->find( $id ), undef, 'Record also gone' );
162 # testing delete via UploadedFile (singular)
163 # Note that find returns a Koha::Object
164 $upl = Koha
::Uploader
->new({ tmp
=> 1 });
166 my $kohaobj = Koha
::UploadedFiles
->find( $upl->result );
167 $path = $kohaobj->full_path;
168 $delete = $kohaobj->delete;
169 is
( $delete, 1, 'Delete successful' );
170 isnt
( -e
$path, 1, 'File no longer found after delete' );
172 # add another record with TestBuilder, so file does not exist
174 my $upload01 = $builder->build({ source
=> 'UploadedFile' });
175 warning_like
{ Koha
::UploadedFiles
->find( $upload01->{id
} )->delete; }
176 qr/file was missing/,
177 'delete warns when file is missing';
178 is
( Koha
::UploadedFiles
->count, 4, 'Back to four uploads now' );
181 subtest
'Call search_term with[out] private flag' => sub {
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 subtest
'Simple tests for httpheaders and getCategories' => sub {
196 my $rec = Koha
::UploadedFiles
->search_term({ term
=> 'file' })->next;
197 my @hdrs = $rec->httpheaders;
198 is
( @hdrs == 4 && $hdrs[1] =~ /application\/octet
-stream
/, 1, 'Simple test for httpheaders');
199 $builder->build({ source
=> 'AuthorisedValue', value
=> { category
=> 'UPLOAD', authorised_value
=> 'HAVE_AT_LEAST_ONE', lib
=> 'Hi there' } });
200 my $cat = Koha
::UploadedFiles
->getCategories;
201 is
( @
$cat >= 1, 1, 'getCategories returned at least one category' );
204 subtest
'Testing allows_add_by' => sub {
207 my $patron = $builder->build({
208 source
=> 'Borrower',
209 value
=> { flags
=> 0 }, #no permissions
211 my $patronid = $patron->{borrowernumber
};
212 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
213 undef, 'Patron is not allowed to do anything' );
215 # add some permissions: edit_catalogue
216 my $fl = 2**9; # edit_catalogue
217 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
218 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
219 undef, 'Patron is still not allowed to add uploaded files' );
221 # replace flags by all tools
223 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
224 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
225 1, 'Patron should be allowed now to add uploaded files' );
227 # remove all tools and add upload_general_files only
228 $fl = 0; # no modules
229 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
231 source
=> 'UserPermission',
233 borrowernumber
=> $patronid,
234 module_bit
=> { module_bit
=> { flag
=> 'tools' } },
235 code
=> 'upload_general_files',
238 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
239 1, 'Patron is still allowed to add uploaded files' );
243 $schema->storage->txn_rollback;
247 my ( $class, $hook ) = @_;
249 foreach my $uh ( @
{$uploads->[ $current_upload ]} ) {
250 for( my $i=0; $i< $uh->{size
}; $i+=1000 ) {
252 &$hook( $uh->{name
}, 'a'x1000
, $read );