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 # Redirect upload dir structure and mock File::Spec and CGI
41 my $tempdir = tempdir( CLEANUP => 1 );
42 t::lib::Mocks::mock_config('upload_path', $tempdir);
43 my $specmod = Test::MockModule->new( 'File::Spec' );
44 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
45 my $cgimod = Test::MockModule->new( 'CGI' );
46 $cgimod->mock( 'new' => \&newCGI );
49 subtest 'Test01' => sub {
53 subtest 'Test02' => sub {
57 subtest 'Test03' => sub {
61 subtest 'Test04' => sub {
65 subtest 'Test05' => sub {
69 subtest 'Test06' => sub {
73 subtest 'Test07' => sub {
77 subtest 'Test08: allows_add_by' => sub {
81 $schema->storage->txn_rollback;
84 # Delete existing records (for later tests)
85 $dbh->do( "DELETE FROM uploaded_files" );
87 my $upl = Koha::Upload->new({
88 category => $uploads->[$current_upload]->[0]->{cat},
91 my $res= $upl->result;
92 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
93 is( $upl->count, 2, 'Count returns 2 also' );
94 foreach my $r ( $upl->get({ id => $res }) ) {
95 if( $r->{name} eq 'file1' ) {
96 is( $r->{uploadcategorycode}, 'A', 'Check category A' );
97 is( $r->{filesize}, 6000, 'Check size of file1' );
98 } elsif( $r->{name} eq 'file2' ) {
99 is( $r->{filesize}, 8000, 'Check size of file2' );
100 is( $r->{public}, undef, 'Check public undefined' );
103 is( $upl->err, undef, 'No errors reported' );
107 my $upl = Koha::Upload->new({
108 category => $uploads->[$current_upload]->[0]->{cat},
112 is( $upl->count, 1, 'Upload 2 includes one file' );
113 my $res= $upl->result;
114 my $r = $upl->get({ id => $res, filehandle => 1 });
115 is( $r->{uploadcategorycode}, 'B', 'Check category B' );
116 is( $r->{public}, 1, 'Check public == 1' );
117 is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
121 my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
123 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
124 my $r = $upl->get({ id => $upl->result });
125 is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
128 sub test04 { # Fail on a file already there
129 my $upl = Koha::Upload->new({
130 category => $uploads->[$current_upload]->[0]->{cat},
133 is( $upl->count, 0, 'Upload 4 failed as expected' );
134 is( $upl->result, undef, 'Result is undefined' );
136 is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
139 sub test05 { # add temporary file with same name and contents, delete it
140 my $upl = Koha::Upload->new({ tmp => 1 });
142 is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
143 my $id = $upl->result;
144 my $r = $upl->get({ id => $id });
145 my @d = $upl->delete({ id => $id });
146 is( $d[0], $r->{name}, 'Delete successful' );
147 is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' );
148 is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
149 is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' );
152 sub test06 { #some extra tests for get
153 my $upl = Koha::Upload->new({ public => 1 });
154 my @rec = $upl->get({ term => 'file' });
155 is( @rec, 1, 'Get returns only one public result (file3)' );
156 $upl = Koha::Upload->new; # public == 0
157 @rec = $upl->get({ term => 'file' });
158 is( @rec, 4, 'Get returns now four results' );
161 sub test07 { #simple test for httpheaders and getCategories
162 my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
163 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
164 my $builder = t::lib::TestBuilder->new;
165 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
166 my $cat = Koha::Upload->getCategories;
167 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
170 sub test08 { # allows_add_by
171 my $builder = t::lib::TestBuilder->new;
172 my $patron = $builder->build({
173 source => 'Borrower',
174 value => { flags => 0 }, #no permissions
176 my $patronid = $patron->{borrowernumber};
177 is( Koha::Upload->allows_add_by( $patron->{userid} ),
178 undef, 'Patron is not allowed to do anything' );
180 # add some permissions: edit_catalogue
181 my $fl = 2**9; # edit_catalogue
182 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
183 is( Koha::Upload->allows_add_by( $patron->{userid} ),
184 undef, 'Patron is still not allowed to add uploaded files' );
186 # replace flags by all tools
188 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
189 is( Koha::Upload->allows_add_by( $patron->{userid} ),
190 1, 'Patron should be allowed now to add uploaded files' );
192 # remove all tools and add upload_general_files only
193 $fl = 0; # no modules
194 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
196 source => 'UserPermission',
198 borrowernumber => $patronid,
199 module_bit => { module_bit => { flag => 'tools' } },
200 code => 'upload_general_files',
203 is( Koha::Upload->allows_add_by( $patron->{userid} ),
204 1, 'Patron is still allowed to add uploaded files' );
208 my ( $class, $hook ) = @_;
210 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
211 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
213 &$hook( $uh->{name}, 'a'x1000, $read );