4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 8;
9 use t::lib::TestBuilder;
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $dbh = C4::Context->dbh;
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
40 # Before we mock upload_path, we are checking the real folder
41 # This may help identifying upload problems
42 my $realdir = C4::Context->config('upload_path');
44 warn "WARNING: You do not have upload_path in koha-conf.xml";
45 } elsif( !-w $realdir ) {
46 warn "WARNING: You do not have write permissions in $realdir";
49 # Redirect upload dir structure and mock File::Spec and CGI
50 my $tempdir = tempdir( CLEANUP => 1 );
51 t::lib::Mocks::mock_config('upload_path', $tempdir);
52 my $specmod = Test::MockModule->new( 'File::Spec' );
53 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
54 my $cgimod = Test::MockModule->new( 'CGI' );
55 $cgimod->mock( 'new' => \&newCGI );
58 subtest 'Test01' => sub {
62 subtest 'Test02' => sub {
66 subtest 'Test03' => sub {
70 subtest 'Test04' => sub {
74 subtest 'Test05' => sub {
78 subtest 'Test06' => sub {
82 subtest 'Test07' => sub {
86 subtest 'Test08: allows_add_by' => sub {
90 $schema->storage->txn_rollback;
93 # Delete existing records (for later tests)
94 $dbh->do( "DELETE FROM uploaded_files" );
96 my $upl = Koha::Upload->new({
97 category => $uploads->[$current_upload]->[0]->{cat},
100 my $res= $upl->result;
101 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
102 is( $upl->count, 2, 'Count returns 2 also' );
103 foreach my $r ( $upl->get({ id => $res }) ) {
104 if( $r->{name} eq 'file1' ) {
105 is( $r->{uploadcategorycode}, 'A', 'Check category A' );
106 is( $r->{filesize}, 6000, 'Check size of file1' );
107 } elsif( $r->{name} eq 'file2' ) {
108 is( $r->{filesize}, 8000, 'Check size of file2' );
109 is( $r->{public}, undef, 'Check public undefined' );
112 is( $upl->err, undef, 'No errors reported' );
116 my $upl = Koha::Upload->new({
117 category => $uploads->[$current_upload]->[0]->{cat},
121 is( $upl->count, 1, 'Upload 2 includes one file' );
122 my $res= $upl->result;
123 my $r = $upl->get({ id => $res, filehandle => 1 });
124 is( $r->{uploadcategorycode}, 'B', 'Check category B' );
125 is( $r->{public}, 1, 'Check public == 1' );
126 is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
130 my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
132 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
133 my $r = $upl->get({ id => $upl->result });
134 is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
137 sub test04 { # Fail on a file already there
138 my $upl = Koha::Upload->new({
139 category => $uploads->[$current_upload]->[0]->{cat},
142 is( $upl->count, 0, 'Upload 4 failed as expected' );
143 is( $upl->result, undef, 'Result is undefined' );
145 is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
148 sub test05 { # add temporary file with same name and contents, delete it
149 my $upl = Koha::Upload->new({ tmp => 1 });
151 is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
152 my $id = $upl->result;
153 my $r = $upl->get({ id => $id });
154 my @d = $upl->delete({ id => $id });
155 is( $d[0], $r->{name}, 'Delete successful' );
156 is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' );
157 is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
158 is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' );
161 sub test06 { #some extra tests for get
162 my $upl = Koha::Upload->new({ public => 1 });
163 my @rec = $upl->get({ term => 'file' });
164 is( @rec, 1, 'Get returns only one public result (file3)' );
165 $upl = Koha::Upload->new; # public == 0
166 @rec = $upl->get({ term => 'file' });
167 is( @rec, 4, 'Get returns now four results' );
170 sub test07 { #simple test for httpheaders and getCategories
171 my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
172 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
173 $dbh->do("INSERT INTO authorised_values (category, authorised_value, lib) VALUES (?,?,?) ", undef, ( 'UPLOAD', 'HAVE_AT_LEAST_ONE', 'Hi there' ));
174 my $cat = Koha::Upload->getCategories;
175 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
178 sub test08 { # allows_add_by
179 my $builder = t::lib::TestBuilder->new;
180 my $patron = $builder->build({
181 source => 'Borrower',
182 value => { flags => 0 }, #no permissions
184 my $patronid = $patron->{borrowernumber};
185 is( Koha::Upload->allows_add_by( $patron->{userid} ),
186 undef, 'Patron is not allowed to do anything' );
188 # add some permissions: edit_catalogue
189 my $fl = 2**9; # edit_catalogue
190 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
191 is( Koha::Upload->allows_add_by( $patron->{userid} ),
192 undef, 'Patron is still not allowed to add uploaded files' );
194 # replace flags by all tools
196 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
197 is( Koha::Upload->allows_add_by( $patron->{userid} ),
198 1, 'Patron should be allowed now to add uploaded files' );
200 # remove all tools and add upload_general_files only
201 $fl = 0; # no modules
202 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
204 source => 'UserPermission',
206 borrowernumber => $patronid,
207 module_bit => { module_bit => { flag => 'tools' } },
208 code => 'upload_general_files',
211 is( Koha::Upload->allows_add_by( $patron->{userid} ),
212 1, 'Patron is still allowed to add uploaded files' );
216 my ( $class, $hook ) = @_;
218 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
219 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
221 &$hook( $uh->{name}, 'a'x1000, $read );