|
Lines 2-8
Link Here
|
| 2 |
|
2 |
|
| 3 |
use Modern::Perl; |
3 |
use Modern::Perl; |
| 4 |
use File::Temp qw/ tempdir /; |
4 |
use File::Temp qw/ tempdir /; |
| 5 |
use Test::More tests => 13; |
5 |
use Test::More tests => 12; |
| 6 |
use Test::Warn; |
6 |
use Test::Warn; |
| 7 |
use Try::Tiny; |
7 |
use Try::Tiny; |
| 8 |
|
8 |
|
|
Lines 23-56
our $builder = t::lib::TestBuilder->new;
Link Here
|
| 23 |
our $current_upload = 0; |
23 |
our $current_upload = 0; |
| 24 |
our $uploads = [ |
24 |
our $uploads = [ |
| 25 |
[ |
25 |
[ |
| 26 |
{ name => 'file1', cat => 'A', size => 6000 }, |
26 |
{ name => 'file1', storage => 'DEFAULT', dir => 'A', size => 6000 }, |
| 27 |
{ name => 'file2', cat => 'A', size => 8000 }, |
27 |
{ name => 'file2', storage => 'DEFAULT', dir => 'A', size => 8000 }, |
| 28 |
], |
28 |
], |
| 29 |
[ |
29 |
[ |
| 30 |
{ name => 'file3', cat => 'B', size => 1000 }, |
30 |
{ name => 'file3', storage => 'DEFAULT', dir => 'B', size => 1000 }, |
| 31 |
], |
31 |
], |
| 32 |
[ |
32 |
[ |
| 33 |
{ name => 'file4', cat => undef, size => 5000 }, # temporary |
33 |
{ name => 'file4', storage => 'TMP', dir => undef, size => 5000 }, |
| 34 |
], |
34 |
], |
| 35 |
[ |
35 |
[ |
| 36 |
{ name => 'file2', cat => 'A', size => 8000 }, |
36 |
{ name => 'file2', storage => 'DEFAULT', dir => 'A', size => 8000 }, |
| 37 |
# uploading a duplicate in cat A should fail |
37 |
# uploading a duplicate in dir A should fail |
| 38 |
], |
38 |
], |
| 39 |
[ |
39 |
[ |
| 40 |
{ name => 'file4', cat => undef, size => 5000 }, # temp duplicate |
40 |
{ name => 'file4', storage => 'TMP', dir => undef, size => 5000 }, # temp duplicate |
| 41 |
], |
41 |
], |
| 42 |
[ |
42 |
[ |
| 43 |
{ name => 'file5', cat => undef, size => 7000 }, |
43 |
{ name => 'file5', storage => 'DEFAULT', dir => undef, size => 7000 }, |
| 44 |
], |
44 |
], |
| 45 |
[ |
45 |
[ |
| 46 |
{ name => 'file6', cat => undef, size => 6500 }, |
46 |
{ name => 'file6', storage => 'TMP', dir => undef, size => 6500 }, |
| 47 |
{ name => 'file7', cat => undef, size => 6501 }, |
47 |
{ name => 'file7', storage => 'TMP', dir => undef, size => 6501 }, |
| 48 |
], |
48 |
], |
| 49 |
]; |
49 |
]; |
| 50 |
|
50 |
|
| 51 |
# Redirect upload dir structure and mock C4::Context and CGI |
51 |
# Redirect upload dir structure and mock C4::Context and CGI |
| 52 |
my $tempdir = tempdir( CLEANUP => 1 ); |
52 |
my $tempdir = tempdir( CLEANUP => 1 ); |
| 53 |
t::lib::Mocks::mock_config('upload_path', $tempdir); |
53 |
my $storage_config = [ |
|
|
54 |
{ |
| 55 |
name => 'DEFAULT', |
| 56 |
adapter => 'directory', |
| 57 |
adapter_params => { |
| 58 |
path => $tempdir, |
| 59 |
}, |
| 60 |
hash_filename => 1, |
| 61 |
}, |
| 62 |
]; |
| 63 |
t::lib::Mocks::mock_config('storage', $storage_config); |
| 54 |
my $specmod = Test::MockModule->new( 'C4::Context' ); |
64 |
my $specmod = Test::MockModule->new( 'C4::Context' ); |
| 55 |
$specmod->mock( 'temporary_directory' => sub { return $tempdir; } ); |
65 |
$specmod->mock( 'temporary_directory' => sub { return $tempdir; } ); |
| 56 |
my $cgimod = Test::MockModule->new( 'CGI' ); |
66 |
my $cgimod = Test::MockModule->new( 'CGI' ); |
|
Lines 68-88
subtest 'Make a fresh start' => sub {
Link Here
|
| 68 |
is( Koha::UploadedFiles->count, 0, 'No records left' ); |
78 |
is( Koha::UploadedFiles->count, 0, 'No records left' ); |
| 69 |
}; |
79 |
}; |
| 70 |
|
80 |
|
| 71 |
subtest 'permanent_directory and temporary_directory' => sub { |
|
|
| 72 |
plan tests => 2; |
| 73 |
|
| 74 |
# Check mocked directories |
| 75 |
is( Koha::UploadedFile->permanent_directory, $tempdir, |
| 76 |
'Check permanent directory' ); |
| 77 |
is( C4::Context::temporary_directory, $tempdir, |
| 78 |
'Check temporary directory' ); |
| 79 |
}; |
| 80 |
|
| 81 |
subtest 'Add two uploads in category A' => sub { |
81 |
subtest 'Add two uploads in category A' => sub { |
| 82 |
plan tests => 9; |
82 |
plan tests => 9; |
| 83 |
|
83 |
|
| 84 |
my $upl = Koha::Uploader->new({ |
84 |
my $upl = Koha::Uploader->new({ |
| 85 |
category => $uploads->[$current_upload]->[0]->{cat}, |
85 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
|
|
86 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 86 |
}); |
87 |
}); |
| 87 |
my $cgi= $upl->cgi; |
88 |
my $cgi= $upl->cgi; |
| 88 |
my $res= $upl->result; |
89 |
my $res= $upl->result; |
|
Lines 95-120
subtest 'Add two uploads in category A' => sub {
Link Here
|
| 95 |
}, { order_by => { -asc => 'filename' }}); |
96 |
}, { order_by => { -asc => 'filename' }}); |
| 96 |
my $rec = $rs->next; |
97 |
my $rec = $rs->next; |
| 97 |
is( $rec->filename, 'file1', 'Check file name' ); |
98 |
is( $rec->filename, 'file1', 'Check file name' ); |
| 98 |
is( $rec->uploadcategorycode, 'A', 'Check category A' ); |
99 |
is( $rec->dir, 'A', 'Check dir A' ); |
| 99 |
is( $rec->filesize, 6000, 'Check size of file1' ); |
100 |
is( $rec->filesize, 6000, 'Check size of file1' ); |
| 100 |
$rec = $rs->next; |
101 |
$rec = $rs->next; |
| 101 |
is( $rec->filename, 'file2', 'Check file name 2' ); |
102 |
is( $rec->filename, 'file2', 'Check file name 2' ); |
| 102 |
is( $rec->filesize, 8000, 'Check size of file2' ); |
103 |
is( $rec->filesize, 8000, 'Check size of file2' ); |
| 103 |
is( $rec->public, undef, 'Check public undefined' ); |
104 |
is( $rec->public, 0, 'Check public 0' ); |
| 104 |
}; |
105 |
}; |
| 105 |
|
106 |
|
| 106 |
subtest 'Add another upload, check file_handle' => sub { |
107 |
subtest 'Add another upload, check file_handle' => sub { |
| 107 |
plan tests => 5; |
108 |
plan tests => 5; |
| 108 |
|
109 |
|
| 109 |
my $upl = Koha::Uploader->new({ |
110 |
my $upl = Koha::Uploader->new({ |
| 110 |
category => $uploads->[$current_upload]->[0]->{cat}, |
111 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
|
|
112 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 111 |
public => 1, |
113 |
public => 1, |
| 112 |
}); |
114 |
}); |
| 113 |
my $cgi= $upl->cgi; |
115 |
my $cgi= $upl->cgi; |
| 114 |
is( $upl->count, 1, 'Upload 2 includes one file' ); |
116 |
is( $upl->count, 1, 'Upload 2 includes one file' ); |
| 115 |
my $res= $upl->result; |
117 |
my $res= $upl->result; |
| 116 |
my $rec = Koha::UploadedFiles->find( $res ); |
118 |
my $rec = Koha::UploadedFiles->find( $res ); |
| 117 |
is( $rec->uploadcategorycode, 'B', 'Check category B' ); |
119 |
is( $rec->dir, 'B', 'Check dir B' ); |
| 118 |
is( $rec->public, 1, 'Check public == 1' ); |
120 |
is( $rec->public, 1, 'Check public == 1' ); |
| 119 |
my $fh = $rec->file_handle; |
121 |
my $fh = $rec->file_handle; |
| 120 |
is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' ); |
122 |
is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' ); |
|
Lines 128-145
subtest 'Add another upload, check file_handle' => sub {
Link Here
|
| 128 |
subtest 'Add temporary upload' => sub { |
130 |
subtest 'Add temporary upload' => sub { |
| 129 |
plan tests => 2; |
131 |
plan tests => 2; |
| 130 |
|
132 |
|
| 131 |
my $upl = Koha::Uploader->new({ tmp => 1 }); #temporary |
133 |
my $upl = Koha::Uploader->new({ |
|
|
134 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 135 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 136 |
}); |
| 132 |
my $cgi= $upl->cgi; |
137 |
my $cgi= $upl->cgi; |
| 133 |
is( $upl->count, 1, 'Upload 3 includes one temporary file' ); |
138 |
is( $upl->count, 1, 'Upload 3 includes one temporary file' ); |
| 134 |
my $rec = Koha::UploadedFiles->find( $upl->result ); |
139 |
my $rec = Koha::UploadedFiles->find( $upl->result ); |
| 135 |
is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' ); |
140 |
is( $rec->dir, '', 'Check dir is empty' ); |
| 136 |
}; |
141 |
}; |
| 137 |
|
142 |
|
| 138 |
subtest 'Add same file in same category' => sub { |
143 |
subtest 'Add same file in same category' => sub { |
| 139 |
plan tests => 3; |
144 |
plan tests => 3; |
| 140 |
|
145 |
|
| 141 |
my $upl = Koha::Uploader->new({ |
146 |
my $upl = Koha::Uploader->new({ |
| 142 |
category => $uploads->[$current_upload]->[0]->{cat}, |
147 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
|
|
148 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 143 |
}); |
149 |
}); |
| 144 |
my $cgi= $upl->cgi; |
150 |
my $cgi= $upl->cgi; |
| 145 |
is( $upl->count, 0, 'Upload 4 failed as expected' ); |
151 |
is( $upl->count, 0, 'Upload 4 failed as expected' ); |
|
Lines 152-182
subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
Link Here
|
| 152 |
plan tests => 10; |
158 |
plan tests => 10; |
| 153 |
|
159 |
|
| 154 |
# add temporary file with same name and contents (file4) |
160 |
# add temporary file with same name and contents (file4) |
| 155 |
my $upl = Koha::Uploader->new({ tmp => 1 }); |
161 |
my $upl = Koha::Uploader->new({ |
|
|
162 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 163 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 164 |
}); |
| 156 |
my $cgi= $upl->cgi; |
165 |
my $cgi= $upl->cgi; |
| 157 |
is( $upl->count, 1, 'Add duplicate temporary file (file4)' ); |
166 |
is( $upl->count, 1, 'Add duplicate temporary file (file4)' ); |
| 158 |
my $id = $upl->result; |
167 |
my $id = $upl->result; |
| 159 |
my $path = Koha::UploadedFiles->find( $id )->full_path; |
168 |
my $uploaded_file = Koha::UploadedFiles->find( $id ); |
|
|
169 |
my $storage = Koha::Storage->get_instance($uploaded_file->storage); |
| 160 |
|
170 |
|
| 161 |
# testing delete via UploadedFiles (plural) |
171 |
# testing delete via UploadedFiles (plural) |
| 162 |
my $delete = Koha::UploadedFiles->search({ id => $id })->delete; |
172 |
my $delete = Koha::UploadedFiles->search({ id => $id })->delete; |
| 163 |
isnt( $delete, "0E0", 'Delete successful' ); |
173 |
isnt( $delete, "0E0", 'Delete successful' ); |
| 164 |
isnt( -e $path, 1, 'File no longer found after delete' ); |
174 |
isnt( $storage->exists($uploaded_file->filepath), 1, 'File no longer found after delete' ); |
| 165 |
is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' ); |
175 |
is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' ); |
| 166 |
|
176 |
|
| 167 |
# testing delete via UploadedFile (singular) |
177 |
# testing delete via UploadedFile (singular) |
| 168 |
# Note that find returns a Koha::Object |
178 |
# Note that find returns a Koha::Object |
| 169 |
$upl = Koha::Uploader->new({ tmp => 1 }); |
179 |
$upl = Koha::Uploader->new({ |
|
|
180 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 181 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 182 |
}); |
| 170 |
$upl->cgi; |
183 |
$upl->cgi; |
| 171 |
my $kohaobj = Koha::UploadedFiles->find( $upl->result ); |
184 |
my $kohaobj = Koha::UploadedFiles->find( $upl->result ); |
| 172 |
$path = $kohaobj->full_path; |
185 |
$storage = Koha::Storage->get_instance($kohaobj->storage); |
| 173 |
$delete = $kohaobj->delete; |
186 |
$delete = $kohaobj->delete; |
| 174 |
ok( $delete, 'Delete successful' ); |
187 |
ok( $delete, 'Delete successful' ); |
| 175 |
isnt( -e $path, 1, 'File no longer found after delete' ); |
188 |
isnt($storage->exists($kohaobj->filepath), 1, 'File no longer found after delete' ); |
| 176 |
|
189 |
|
| 177 |
# add another record with TestBuilder, so file does not exist |
190 |
# add another record with TestBuilder, so file does not exist |
| 178 |
# catch warning |
191 |
# catch warning |
| 179 |
my $upload01 = $builder->build({ source => 'UploadedFile' }); |
192 |
my $upload01 = $builder->build({ source => 'UploadedFile', value => {storage => 'TMP'} }); |
| 180 |
warning_like { $delete = Koha::UploadedFiles->find( $upload01->{id} )->delete; } |
193 |
warning_like { $delete = Koha::UploadedFiles->find( $upload01->{id} )->delete; } |
| 181 |
qr/file was missing/, |
194 |
qr/file was missing/, |
| 182 |
'delete warns when file is missing'; |
195 |
'delete warns when file is missing'; |
|
Lines 198-205
subtest 'Test delete_missing' => sub {
Link Here
|
| 198 |
plan tests => 5; |
211 |
plan tests => 5; |
| 199 |
|
212 |
|
| 200 |
# If we add files via TestBuilder, they do not exist |
213 |
# If we add files via TestBuilder, they do not exist |
| 201 |
my $upload01 = $builder->build({ source => 'UploadedFile' }); |
214 |
my $upload01 = $builder->build({ source => 'UploadedFile', value => {storage => 'TMP'} }); |
| 202 |
my $upload02 = $builder->build({ source => 'UploadedFile' }); |
215 |
my $upload02 = $builder->build({ source => 'UploadedFile', value => {storage => 'TMP'} }); |
| 203 |
# dry run first |
216 |
# dry run first |
| 204 |
my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 }); |
217 |
my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 }); |
| 205 |
is( $deleted, 2, 'Expect two records with missing files' ); |
218 |
is( $deleted, 2, 'Expect two records with missing files' ); |
|
Lines 226-240
subtest 'Call search_term with[out] private flag' => sub {
Link Here
|
| 226 |
})->count, 4, 'Returns now four results' ); |
239 |
})->count, 4, 'Returns now four results' ); |
| 227 |
}; |
240 |
}; |
| 228 |
|
241 |
|
| 229 |
subtest 'Simple tests for httpheaders and getCategories' => sub { |
242 |
subtest 'Simple tests for httpheaders' => sub { |
| 230 |
plan tests => 2; |
243 |
plan tests => 1; |
| 231 |
|
244 |
|
| 232 |
my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next; |
245 |
my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next; |
| 233 |
my @hdrs = $rec->httpheaders; |
246 |
my @hdrs = $rec->httpheaders; |
| 234 |
is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders'); |
247 |
is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders'); |
| 235 |
$builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } }); |
248 |
$builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } }); |
| 236 |
my $cat = Koha::UploadedFiles->getCategories; |
|
|
| 237 |
is( @$cat >= 1, 1, 'getCategories returned at least one category' ); |
| 238 |
}; |
249 |
}; |
| 239 |
|
250 |
|
| 240 |
subtest 'Testing allows_add_by' => sub { |
251 |
subtest 'Testing allows_add_by' => sub { |
|
Lines 279-285
subtest 'Testing delete_temporary' => sub {
Link Here
|
| 279 |
plan tests => 9; |
290 |
plan tests => 9; |
| 280 |
|
291 |
|
| 281 |
# Add two temporary files: result should be 3 + 3 |
292 |
# Add two temporary files: result should be 3 + 3 |
| 282 |
Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7 |
293 |
Koha::Uploader->new({ |
|
|
294 |
storage => $uploads->[$current_upload]->[0]->{storage}, |
| 295 |
dir => $uploads->[$current_upload]->[0]->{dir}, |
| 296 |
})->cgi; # add file6 and file7 |
| 283 |
is( Koha::UploadedFiles->search->count, 6, 'Test starting count' ); |
297 |
is( Koha::UploadedFiles->search->count, 6, 'Test starting count' ); |
| 284 |
is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3, |
298 |
is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3, |
| 285 |
'Includes 3 permanent' ); |
299 |
'Includes 3 permanent' ); |