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
41 # Redirect upload dir structure and mock File::Spec and CGI
42 my $tempdir = tempdir
( CLEANUP
=> 1 );
43 t
::lib
::Mocks
::mock_config
('upload_path', $tempdir);
44 my $specmod = Test
::MockModule
->new( 'File::Spec' );
45 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
46 my $cgimod = Test
::MockModule
->new( 'CGI' );
47 $cgimod->mock( 'new' => \
&newCGI
);
50 subtest
'Test01' => sub {
54 subtest
'Test02' => sub {
58 subtest
'Test03' => sub {
62 subtest
'Test04' => sub {
66 subtest
'Test05' => sub {
70 subtest
'Test06' => sub {
74 subtest
'Test07' => sub {
78 subtest
'Test08: allows_add_by' => sub {
82 $schema->storage->txn_rollback;
85 # Delete existing records (for later tests)
86 $dbh->do( "DELETE FROM uploaded_files" );
88 my $upl = Koha
::Upload
->new({
89 category
=> $uploads->[$current_upload]->[0]->{cat
},
92 my $res= $upl->result;
93 is
( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
94 is
( $upl->count, 2, 'Count returns 2 also' );
95 foreach my $r ( $upl->get({ id
=> $res }) ) {
96 if( $r->{name
} eq 'file1' ) {
97 is
( $r->{uploadcategorycode
}, 'A', 'Check category A' );
98 is
( $r->{filesize
}, 6000, 'Check size of file1' );
99 } elsif( $r->{name
} eq 'file2' ) {
100 is
( $r->{filesize
}, 8000, 'Check size of file2' );
101 is
( $r->{public
}, undef, 'Check public undefined' );
104 is
( $upl->err, undef, 'No errors reported' );
108 my $upl = Koha
::Upload
->new({
109 category
=> $uploads->[$current_upload]->[0]->{cat
},
113 is
( $upl->count, 1, 'Upload 2 includes one file' );
114 my $res= $upl->result;
115 my $r = $upl->get({ id
=> $res, filehandle
=> 1 });
116 is
( $r->{uploadcategorycode
}, 'B', 'Check category B' );
117 is
( $r->{public
}, 1, 'Check public == 1' );
118 is
( ref($r->{fh
}) eq 'IO::File' && $r->{fh
}->opened, 1, 'Get returns a file handle' );
122 my $upl = Koha
::Upload
->new({ tmp
=> 1 }); #temporary
124 is
( $upl->count, 1, 'Upload 3 includes one temporary file' );
125 my $r = $upl->get({ id
=> $upl->result });
126 is
( $r->{uploadcategorycode
} =~ /_upload$/, 1, 'Check category temp file' );
129 sub test04
{ # Fail on a file already there
130 my $upl = Koha
::Upload
->new({
131 category
=> $uploads->[$current_upload]->[0]->{cat
},
134 is
( $upl->count, 0, 'Upload 4 failed as expected' );
135 is
( $upl->result, undef, 'Result is undefined' );
137 is
( $e->{file2
}, 1, "Errcode 1 [already exists] reported" );
140 sub test05
{ # add temporary file with same name and contents, delete it
141 my $upl = Koha
::Upload
->new({ tmp
=> 1 });
143 is
( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
144 my $id = $upl->result;
145 my $r = $upl->get({ id
=> $id });
146 my @d = $upl->delete({ id
=> $id });
147 is
( $d[0], $r->{name
}, 'Delete successful' );
148 is
( -e
$r->{path
}?
1: 0, 0, 'File no longer found after delete' );
149 is
( scalar $upl->get({ id
=> $id }), undef, 'Record also gone' );
150 is
( $upl->delete({ id
=> $id }), undef, 'Repeated delete failed' );
153 sub test06
{ #some extra tests for get
154 my $upl = Koha
::Upload
->new({ public
=> 1 });
155 my @rec = $upl->get({ term
=> 'file' });
156 is
( @rec, 1, 'Get returns only one public result (file3)' );
157 $upl = Koha
::Upload
->new; # public == 0
158 @rec = $upl->get({ term
=> 'file' });
159 is
( @rec, 4, 'Get returns now four results' );
162 sub test07
{ #simple test for httpheaders and getCategories
163 my @hdrs = Koha
::Upload
->httpheaders('does_not_matter_yet');
164 is
( @hdrs == 4 && $hdrs[1] =~ /application\/octet
-stream
/, 1, 'Simple test for httpheaders');
165 my $builder = t
::lib
::TestBuilder
->new;
166 $builder->build({ source
=> 'AuthorisedValue', value
=> { category
=> 'UPLOAD', authorised_value
=> 'HAVE_AT_LEAST_ONE', lib
=> 'Hi there' } });
167 my $cat = Koha
::Upload
->getCategories;
168 is
( @
$cat >= 1, 1, 'getCategories returned at least one category' );
171 sub test08
{ # allows_add_by
172 my $builder = t
::lib
::TestBuilder
->new;
173 my $patron = $builder->build({
174 source
=> 'Borrower',
175 value
=> { flags
=> 0 }, #no permissions
177 my $patronid = $patron->{borrowernumber
};
178 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
179 undef, 'Patron is not allowed to do anything' );
181 # add some permissions: edit_catalogue
182 my $fl = 2**9; # edit_catalogue
183 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
184 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
185 undef, 'Patron is still not allowed to add uploaded files' );
187 # replace flags by all tools
189 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
190 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
191 1, 'Patron should be allowed now to add uploaded files' );
193 # remove all tools and add upload_general_files only
194 $fl = 0; # no modules
195 $schema->resultset('Borrower')->find( $patronid )->update({ flags
=> $fl });
197 source
=> 'UserPermission',
199 borrowernumber
=> $patronid,
200 module_bit
=> { module_bit
=> { flag
=> 'tools' } },
201 code
=> 'upload_general_files',
204 is
( Koha
::Upload
->allows_add_by( $patron->{userid
} ),
205 1, 'Patron is still allowed to add uploaded files' );
208 # Additional tests for Koha::UploadedFiles
209 # TODO Rearrange the tests after this migration
210 subtest
'Some basic CRUD testing' => sub {
213 # Test find and attribute id, delete and search
214 my $builder = t
::lib
::TestBuilder
->new;
215 my $upload01 = $builder->build({ source
=> 'UploadedFile' });
216 my $found = Koha
::UploadedFiles
->find( $upload01->{id
} );
217 is
( $found->id, $upload01->{id
}, 'Koha::Object returns id' );
219 $found = Koha
::UploadedFiles
->search(
220 { id
=> $upload01->{id
} },
222 is
( $found->count, 0, 'Delete seems successful' );
226 my ( $class, $hook ) = @_;
228 foreach my $uh ( @
{$uploads->[ $current_upload ]} ) {
229 for( my $i=0; $i< $uh->{size
}; $i+=1000 ) {
231 &$hook( $uh->{name
}, 'a'x1000
, $read );