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
->new->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 my $name = $kohaobj->filename;
168 $path = $kohaobj->full_path;
169 $delete = $kohaobj->delete;
170 is
( $delete, $name, 'Delete successful' );
171 isnt
( -e
$path, 1, 'File no longer found after delete' );
173 # add another record with TestBuilder, so file does not exist
175 my $upload01 = $builder->build({ source
=> 'UploadedFile' });
176 warning_like
{ Koha
::UploadedFiles
->find( $upload01->{id
} )->delete; }
177 qr/file was missing/,
178 'delete warns when file is missing';
179 is
( Koha
::UploadedFiles
->count, 4, 'Back to four uploads now' );
182 subtest
'Call search_term with[out] private flag' => sub {
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 subtest
'Simple tests for httpheaders and getCategories' => sub {
197 my $rec = Koha
::UploadedFiles
->search_term({ term
=> 'file' })->next;
198 my @hdrs = $rec->httpheaders;
199 is
( @hdrs == 4 && $hdrs[1] =~ /application\/octet
-stream
/, 1, 'Simple test for httpheaders');
200 $builder->build({ source
=> 'AuthorisedValue', value
=> { category
=> 'UPLOAD', authorised_value
=> 'HAVE_AT_LEAST_ONE', lib
=> 'Hi there' } });
201 my $cat = Koha
::UploadedFile
->getCategories;
202 is
( @
$cat >= 1, 1, 'getCategories returned at least one category' );
205 subtest
'Testing allows_add_by' => sub {
208 my $patron = $builder->build({
209 source
=> 'Borrower',
210 value
=> { flags
=> 0 }, #no permissions
212 my $patronid = $patron->{borrowernumber
};
213 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
214 undef, 'Patron is not allowed to do anything' );
216 # add some permissions: edit_catalogue
217 my $fl = 2**9; # edit_catalogue
218 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
219 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
220 undef, 'Patron is still not allowed to add uploaded files' );
222 # replace flags by all tools
224 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
225 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
226 1, 'Patron should be allowed now to add uploaded files' );
228 # remove all tools and add upload_general_files only
229 $fl = 0; # no modules
230 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
232 source
=> 'UserPermission',
234 borrowernumber
=> $patronid,
235 module_bit
=> { module_bit
=> { flag
=> 'tools' } },
236 code
=> 'upload_general_files',
239 is
( Koha
::Uploader
->allows_add_by( $patron->{userid
} ),
240 1, 'Patron is still allowed to add uploaded files' );
244 $schema->storage->txn_rollback;
248 my ( $class, $hook ) = @_;
250 foreach my $uh ( @
{$uploads->[ $current_upload ]} ) {
251 for( my $i=0; $i< $uh->{size
}; $i+=1000 ) {
253 &$hook( $uh->{name
}, 'a'x1000
, $read );