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;
18 my $dbh = C4
::Context
->dbh;
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 }, # temp duplicate
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 $dbh->do( "DELETE FROM uploaded_files" );
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 foreach my $r ( $upl->get({ id
=> $res }) ) {
105 if( $r->{name
} eq 'file1' ) {
106 is
( $r->{uploadcategorycode
}, 'A', 'Check category A' );
107 is
( $r->{filesize
}, 6000, 'Check size of file1' );
108 } elsif( $r->{name
} eq 'file2' ) {
109 is
( $r->{filesize
}, 8000, 'Check size of file2' );
110 is
( $r->{public
}, undef, 'Check public undefined' );
113 is
( $upl->err, undef, 'No errors reported' );
117 my $upl = Koha
::Upload
->new({
118 category
=> $uploads->[$current_upload]->[0]->{cat
},
122 is
( $upl->count, 1, 'Upload 2 includes one file' );
123 my $res= $upl->result;
124 my $r = $upl->get({ id
=> $res, filehandle
=> 1 });
125 is
( $r->{uploadcategorycode
}, 'B', 'Check category B' );
126 is
( $r->{public
}, 1, 'Check public == 1' );
127 is
( ref($r->{fh
}) eq 'IO::File' && $r->{fh
}->opened, 1, 'Get returns a file handle' );
131 my $upl = Koha
::Upload
->new({ tmp
=> 1 }); #temporary
133 is
( $upl->count, 1, 'Upload 3 includes one temporary file' );
134 my $r = $upl->get({ id
=> $upl->result });
135 is
( $r->{uploadcategorycode
} =~ /_upload$/, 1, 'Check category temp file' );
138 sub test04
{ # Fail on a file already there
139 my $upl = Koha
::Upload
->new({
140 category
=> $uploads->[$current_upload]->[0]->{cat
},
143 is
( $upl->count, 0, 'Upload 4 failed as expected' );
144 is
( $upl->result, undef, 'Result is undefined' );
146 is
( $e->{file2
}, 1, "Errcode 1 [already exists] reported" );
149 sub test05
{ # add temporary file with same name and contents, delete it
150 my $upl = Koha
::Upload
->new({ tmp
=> 1 });
152 is
( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
153 my $id = $upl->result;
154 my $r = $upl->get({ id
=> $id });
156 # testing delete via UploadedFiles (plural)
157 my $delete = Koha
::UploadedFiles
->search({ id
=> $id })->delete;
158 is
( $delete, 1, 'Delete successful' );
159 isnt
( -e
$r->{path
}, 1, 'File no longer found after delete' );
160 is
( scalar $upl->get({ id
=> $id }), undef, 'Record also gone' );
162 # testing delete via UploadedFile (singular)
163 # Note that find returns a Koha::Object
164 $upl = Koha
::Upload
->new({ tmp
=> 1 });
167 my $kohaobj = Koha
::UploadedFiles
->find( $id );
168 my $name = $kohaobj->filename;
169 my $path = $kohaobj->full_path;
170 $delete = $kohaobj->delete;
171 is
( $delete, $name, 'Delete successful' );
172 isnt
( -e
$path, 1, 'File no longer found after delete' );
175 sub test06
{ #some extra tests for get
176 my $upl = Koha
::Upload
->new({ public
=> 1 });
177 my @rec = $upl->get({ term
=> 'file' });
178 is
( @rec, 1, 'Get returns only one public result (file3)' );
179 $upl = Koha
::Upload
->new; # public == 0
180 @rec = $upl->get({ term
=> 'file' });
181 is
( @rec, 4, 'Get returns now four results' );
184 sub test07
{ #simple test for httpheaders and getCategories
185 my @hdrs = Koha
::Upload
->httpheaders('does_not_matter_yet');
186 is
( @hdrs == 4 && $hdrs[1] =~ /application\/octet
-stream
/, 1, 'Simple test for httpheaders');
187 my $builder = t
::lib
::TestBuilder
->new;
188 $builder->build({ source
=> 'AuthorisedValue', value
=> { category
=> 'UPLOAD', authorised_value
=> 'HAVE_AT_LEAST_ONE', lib
=> 'Hi there' } });
189 my $cat = Koha
::Upload
->getCategories;
190 is
( @
$cat >= 1, 1, 'getCategories returned at least one category' );
193 sub test08
{ # allows_add_by
194 my $builder = t
::lib
::TestBuilder
->new;
195 my $patron = $builder->build({
196 source
=> 'Borrower',
197 value
=> { flags
=> 0 }, #no permissions
199 my $patronid = $patron->{borrowernumber
};
200 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
201 undef, 'Patron is not allowed to do anything' );
203 # add some permissions: edit_catalogue
204 my $fl = 2**9; # edit_catalogue
205 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
206 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
207 undef, 'Patron is still not allowed to add uploaded files' );
209 # replace flags by all tools
211 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
212 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
213 1, 'Patron should be allowed now to add uploaded files' );
215 # remove all tools and add upload_general_files only
216 $fl = 0; # no modules
217 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
219 source
=> 'UserPermission',
221 borrowernumber
=> $patronid,
222 module_bit
=> { module_bit
=> { flag
=> 'tools' } },
223 code
=> 'upload_general_files',
226 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
227 1, 'Patron is still allowed to add uploaded files' );
230 # Additional tests for Koha::UploadedFiles
231 # TODO Rearrange the tests after this migration
232 subtest
'Some basic CRUD testing' => sub {
235 # Test find and attribute id, delete and search
236 my $builder = t
::lib
::TestBuilder
->new;
237 my $upload01 = $builder->build({ source
=> 'UploadedFile' });
238 my $found = Koha
::UploadedFiles
->find( $upload01->{id
} );
239 is
( $found->id, $upload01->{id
}, 'Koha::Object returns id' );
241 $found = Koha
::UploadedFiles
->search(
242 { id
=> $upload01->{id
} },
244 is
( $found->count, 0, 'Delete seems successful' );
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 );