|
Lines 15-21
use Koha::UploadedFiles;
Link Here
|
| 15 |
|
15 |
|
| 16 |
my $schema = Koha::Database->new->schema; |
16 |
my $schema = Koha::Database->new->schema; |
| 17 |
$schema->storage->txn_begin; |
17 |
$schema->storage->txn_begin; |
| 18 |
my $dbh = C4::Context->dbh; |
|
|
| 19 |
|
18 |
|
| 20 |
our $current_upload = 0; |
19 |
our $current_upload = 0; |
| 21 |
our $uploads = [ |
20 |
our $uploads = [ |
|
Lines 37-43
our $uploads = [
Link Here
|
| 37 |
{ name => 'file4', cat => undef, size => 5000 }, # temp duplicate |
36 |
{ name => 'file4', cat => undef, size => 5000 }, # temp duplicate |
| 38 |
], |
37 |
], |
| 39 |
[ |
38 |
[ |
| 40 |
{ name => 'file5', cat => undef, size => 7000 }, # temp duplicate |
39 |
{ name => 'file5', cat => undef, size => 7000 }, |
| 41 |
], |
40 |
], |
| 42 |
]; |
41 |
]; |
| 43 |
|
42 |
|
|
Lines 51-61
$cgimod->mock( 'new' => \&newCGI );
Link Here
|
| 51 |
|
50 |
|
| 52 |
# Start testing |
51 |
# Start testing |
| 53 |
subtest 'Test01' => sub { |
52 |
subtest 'Test01' => sub { |
| 54 |
plan tests => 9; |
53 |
plan tests => 11; |
| 55 |
test01(); |
54 |
test01(); |
| 56 |
}; |
55 |
}; |
| 57 |
subtest 'Test02' => sub { |
56 |
subtest 'Test02' => sub { |
| 58 |
plan tests => 4; |
57 |
plan tests => 5; |
| 59 |
test02(); |
58 |
test02(); |
| 60 |
}; |
59 |
}; |
| 61 |
subtest 'Test03' => sub { |
60 |
subtest 'Test03' => sub { |
|
Lines 71-77
subtest 'Test05' => sub {
Link Here
|
| 71 |
test05(); |
70 |
test05(); |
| 72 |
}; |
71 |
}; |
| 73 |
subtest 'Test06' => sub { |
72 |
subtest 'Test06' => sub { |
| 74 |
plan tests => 2; |
73 |
plan tests => 3; |
| 75 |
test06(); |
74 |
test06(); |
| 76 |
}; |
75 |
}; |
| 77 |
subtest 'Test07' => sub { |
76 |
subtest 'Test07' => sub { |
|
Lines 86-92
$schema->storage->txn_rollback;
Link Here
|
| 86 |
|
85 |
|
| 87 |
sub test01 { |
86 |
sub test01 { |
| 88 |
# Delete existing records (for later tests) |
87 |
# Delete existing records (for later tests) |
| 89 |
$dbh->do( "DELETE FROM uploaded_files" ); |
88 |
# Passing keep_file suppresses warnings |
|
|
89 |
Koha::UploadedFiles->new->delete({ keep_file => 1 }); |
| 90 |
|
90 |
|
| 91 |
# Check mocked directories |
91 |
# Check mocked directories |
| 92 |
is( Koha::UploadedFile->permanent_directory, $tempdir, |
92 |
is( Koha::UploadedFile->permanent_directory, $tempdir, |
|
Lines 101-116
sub test01 {
Link Here
|
| 101 |
my $res= $upl->result; |
101 |
my $res= $upl->result; |
| 102 |
is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' ); |
102 |
is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' ); |
| 103 |
is( $upl->count, 2, 'Count returns 2 also' ); |
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' ); |
| 111 |
} |
| 112 |
} |
| 113 |
is( $upl->err, undef, 'No errors reported' ); |
104 |
is( $upl->err, undef, 'No errors reported' ); |
|
|
105 |
|
| 106 |
my $rs = Koha::UploadedFiles->search({ |
| 107 |
id => [ split ',', $res ] |
| 108 |
}, { order_by => { -asc => 'filename' }}); |
| 109 |
my $rec = $rs->next; |
| 110 |
is( $rec->filename, 'file1', 'Check file name' ); |
| 111 |
is( $rec->uploadcategorycode, 'A', 'Check category A' ); |
| 112 |
is( $rec->filesize, 6000, 'Check size of file1' ); |
| 113 |
$rec = $rs->next; |
| 114 |
is( $rec->filename, 'file2', 'Check file name 2' ); |
| 115 |
is( $rec->filesize, 8000, 'Check size of file2' ); |
| 116 |
is( $rec->public, undef, 'Check public undefined' ); |
| 114 |
} |
117 |
} |
| 115 |
|
118 |
|
| 116 |
sub test02 { |
119 |
sub test02 { |
|
Lines 121-138
sub test02 {
Link Here
|
| 121 |
my $cgi= $upl->cgi; |
124 |
my $cgi= $upl->cgi; |
| 122 |
is( $upl->count, 1, 'Upload 2 includes one file' ); |
125 |
is( $upl->count, 1, 'Upload 2 includes one file' ); |
| 123 |
my $res= $upl->result; |
126 |
my $res= $upl->result; |
| 124 |
my $r = $upl->get({ id => $res, filehandle => 1 }); |
127 |
my $rec = Koha::UploadedFiles->find( $res ); |
| 125 |
is( $r->{uploadcategorycode}, 'B', 'Check category B' ); |
128 |
is( $rec->uploadcategorycode, 'B', 'Check category B' ); |
| 126 |
is( $r->{public}, 1, 'Check public == 1' ); |
129 |
is( $rec->public, 1, 'Check public == 1' ); |
| 127 |
is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' ); |
130 |
my $fh = $rec->file_handle; |
|
|
131 |
is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' ); |
| 132 |
|
| 133 |
my $orgname = $rec->filename; |
| 134 |
$rec->filename( 'doesprobablynotexist' )->store; |
| 135 |
is( $rec->file_handle, undef, 'Sabotage with file handle' ); |
| 136 |
$rec->filename( $orgname )->store; |
| 128 |
} |
137 |
} |
| 129 |
|
138 |
|
| 130 |
sub test03 { |
139 |
sub test03 { |
| 131 |
my $upl = Koha::Upload->new({ tmp => 1 }); #temporary |
140 |
my $upl = Koha::Upload->new({ tmp => 1 }); #temporary |
| 132 |
my $cgi= $upl->cgi; |
141 |
my $cgi= $upl->cgi; |
| 133 |
is( $upl->count, 1, 'Upload 3 includes one temporary file' ); |
142 |
is( $upl->count, 1, 'Upload 3 includes one temporary file' ); |
| 134 |
my $r = $upl->get({ id => $upl->result }); |
143 |
my $rec = Koha::UploadedFiles->find( $upl->result ); |
| 135 |
is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' ); |
144 |
is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' ); |
| 136 |
} |
145 |
} |
| 137 |
|
146 |
|
| 138 |
sub test04 { # Fail on a file already there |
147 |
sub test04 { # Fail on a file already there |
|
Lines 151-184
sub test05 { # add temporary file with same name and contents, delete it
Link Here
|
| 151 |
my $cgi= $upl->cgi; |
160 |
my $cgi= $upl->cgi; |
| 152 |
is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' ); |
161 |
is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' ); |
| 153 |
my $id = $upl->result; |
162 |
my $id = $upl->result; |
| 154 |
my $r = $upl->get({ id => $id }); |
163 |
my $path = Koha::UploadedFiles->find( $id )->full_path; |
| 155 |
|
164 |
|
| 156 |
# testing delete via UploadedFiles (plural) |
165 |
# testing delete via UploadedFiles (plural) |
| 157 |
my $delete = Koha::UploadedFiles->search({ id => $id })->delete; |
166 |
my $delete = Koha::UploadedFiles->search({ id => $id })->delete; |
| 158 |
is( $delete, 1, 'Delete successful' ); |
167 |
is( $delete, 1, 'Delete successful' ); |
| 159 |
isnt( -e $r->{path}, 1, 'File no longer found after delete' ); |
168 |
isnt( -e $path, 1, 'File no longer found after delete' ); |
| 160 |
is( scalar $upl->get({ id => $id }), undef, 'Record also gone' ); |
169 |
is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' ); |
| 161 |
|
170 |
|
| 162 |
# testing delete via UploadedFile (singular) |
171 |
# testing delete via UploadedFile (singular) |
| 163 |
# Note that find returns a Koha::Object |
172 |
# Note that find returns a Koha::Object |
| 164 |
$upl = Koha::Upload->new({ tmp => 1 }); |
173 |
$upl = Koha::Upload->new({ tmp => 1 }); |
| 165 |
$upl->cgi; |
174 |
$upl->cgi; |
| 166 |
$id = $upl->result; |
175 |
my $kohaobj = Koha::UploadedFiles->find( $upl->result ); |
| 167 |
my $kohaobj = Koha::UploadedFiles->find( $id ); |
|
|
| 168 |
my $name = $kohaobj->filename; |
176 |
my $name = $kohaobj->filename; |
| 169 |
my $path = $kohaobj->full_path; |
177 |
$path = $kohaobj->full_path; |
| 170 |
$delete = $kohaobj->delete; |
178 |
$delete = $kohaobj->delete; |
| 171 |
is( $delete, $name, 'Delete successful' ); |
179 |
is( $delete, $name, 'Delete successful' ); |
| 172 |
isnt( -e $path, 1, 'File no longer found after delete' ); |
180 |
isnt( -e $path, 1, 'File no longer found after delete' ); |
| 173 |
} |
181 |
} |
| 174 |
|
182 |
|
| 175 |
sub test06 { #some extra tests for get |
183 |
sub test06 { #search_term with[out] private flag |
| 176 |
my $upl = Koha::Upload->new({ public => 1 }); |
184 |
my @recs = Koha::UploadedFiles->search_term({ term => 'file' }); |
| 177 |
my @rec = $upl->get({ term => 'file' }); |
185 |
is( @recs, 1, 'Returns only one public result' ); |
| 178 |
is( @rec, 1, 'Get returns only one public result (file3)' ); |
186 |
is( $recs[0]->filename, 'file3', 'Should be file3' ); |
| 179 |
$upl = Koha::Upload->new; # public == 0 |
187 |
|
| 180 |
@rec = $upl->get({ term => 'file' }); |
188 |
is( Koha::UploadedFiles->search_term({ |
| 181 |
is( @rec, 4, 'Get returns now four results' ); |
189 |
term => 'file', include_private => 1, |
|
|
190 |
})->count, 4, 'Returns now four results' ); |
| 182 |
} |
191 |
} |
| 183 |
|
192 |
|
| 184 |
sub test07 { #simple test for httpheaders and getCategories |
193 |
sub test07 { #simple test for httpheaders and getCategories |
|
Lines 237-243
subtest 'Some basic CRUD testing' => sub {
Link Here
|
| 237 |
my $upload01 = $builder->build({ source => 'UploadedFile' }); |
246 |
my $upload01 = $builder->build({ source => 'UploadedFile' }); |
| 238 |
my $found = Koha::UploadedFiles->find( $upload01->{id} ); |
247 |
my $found = Koha::UploadedFiles->find( $upload01->{id} ); |
| 239 |
is( $found->id, $upload01->{id}, 'Koha::Object returns id' ); |
248 |
is( $found->id, $upload01->{id}, 'Koha::Object returns id' ); |
| 240 |
$found->delete; |
249 |
$found->delete({ keep_file => 1 }); #note that it does not exist |
| 241 |
$found = Koha::UploadedFiles->search( |
250 |
$found = Koha::UploadedFiles->search( |
| 242 |
{ id => $upload01->{id} }, |
251 |
{ id => $upload01->{id} }, |
| 243 |
); |
252 |
); |