Bug 17501: Move Koha::Upload::delete to Koha::UploadedFile[s]
[koha.git] / t / db_dependent / Upload.t
blob239874d6488a8896d499bb2b0990c32724142a6f
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 9;
7 use Test::MockModule;
8 use t::lib::Mocks;
9 use t::lib::TestBuilder;
11 use C4::Context;
12 use Koha::Database;
13 use Koha::Upload;
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;
21 our $uploads = [
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
40 { name => 'file5', cat => undef, size => 7000 }, # temp duplicate
44 # Redirect upload dir structure and mock File::Spec and CGI
45 my $tempdir = tempdir( CLEANUP => 1 );
46 t::lib::Mocks::mock_config('upload_path', $tempdir);
47 my $specmod = Test::MockModule->new( 'File::Spec' );
48 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
49 my $cgimod = Test::MockModule->new( 'CGI' );
50 $cgimod->mock( 'new' => \&newCGI );
52 # Start testing
53 subtest 'Test01' => sub {
54 plan tests => 9;
55 test01();
57 subtest 'Test02' => sub {
58 plan tests => 4;
59 test02();
61 subtest 'Test03' => sub {
62 plan tests => 2;
63 test03();
65 subtest 'Test04' => sub {
66 plan tests => 3;
67 test04();
69 subtest 'Test05' => sub {
70 plan tests => 6;
71 test05();
73 subtest 'Test06' => sub {
74 plan tests => 2;
75 test06();
77 subtest 'Test07' => sub {
78 plan tests => 2;
79 test07();
81 subtest 'Test08: allows_add_by' => sub {
82 plan tests => 4;
83 test08();
85 $schema->storage->txn_rollback;
87 sub test01 {
88 # Delete existing records (for later tests)
89 $dbh->do( "DELETE FROM uploaded_files" );
91 # Check mocked directories
92 is( Koha::UploadedFile->permanent_directory, $tempdir,
93 'Check permanent directory' );
94 is( Koha::UploadedFile->temporary_directory, $tempdir,
95 'Check temporary directory' );
97 my $upl = Koha::Upload->new({
98 category => $uploads->[$current_upload]->[0]->{cat},
99 });
100 my $cgi= $upl->cgi;
101 my $res= $upl->result;
102 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
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' );
113 is( $upl->err, undef, 'No errors reported' );
116 sub test02 {
117 my $upl = Koha::Upload->new({
118 category => $uploads->[$current_upload]->[0]->{cat},
119 public => 1,
121 my $cgi= $upl->cgi;
122 is( $upl->count, 1, 'Upload 2 includes one file' );
123 my $res= $upl->result;
124 my $r = $upl->get({ id => $res, filehandle => 1 });
125 is( $r->{uploadcategorycode}, 'B', 'Check category B' );
126 is( $r->{public}, 1, 'Check public == 1' );
127 is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
130 sub test03 {
131 my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
132 my $cgi= $upl->cgi;
133 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
134 my $r = $upl->get({ id => $upl->result });
135 is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
138 sub test04 { # Fail on a file already there
139 my $upl = Koha::Upload->new({
140 category => $uploads->[$current_upload]->[0]->{cat},
142 my $cgi= $upl->cgi;
143 is( $upl->count, 0, 'Upload 4 failed as expected' );
144 is( $upl->result, undef, 'Result is undefined' );
145 my $e = $upl->err;
146 is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
149 sub test05 { # add temporary file with same name and contents, delete it
150 my $upl = Koha::Upload->new({ tmp => 1 });
151 my $cgi= $upl->cgi;
152 is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
153 my $id = $upl->result;
154 my $r = $upl->get({ id => $id });
156 # testing delete via UploadedFiles (plural)
157 my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
158 is( $delete, 1, 'Delete successful' );
159 isnt( -e $r->{path}, 1, 'File no longer found after delete' );
160 is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
162 # testing delete via UploadedFile (singular)
163 # Note that find returns a Koha::Object
164 $upl = Koha::Upload->new({ tmp => 1 });
165 $upl->cgi;
166 $id = $upl->result;
167 my $kohaobj = Koha::UploadedFiles->find( $id );
168 my $name = $kohaobj->filename;
169 my $path = $kohaobj->full_path;
170 $delete = $kohaobj->delete;
171 is( $delete, $name, 'Delete successful' );
172 isnt( -e $path, 1, 'File no longer found after delete' );
175 sub test06 { #some extra tests for get
176 my $upl = Koha::Upload->new({ public => 1 });
177 my @rec = $upl->get({ term => 'file' });
178 is( @rec, 1, 'Get returns only one public result (file3)' );
179 $upl = Koha::Upload->new; # public == 0
180 @rec = $upl->get({ term => 'file' });
181 is( @rec, 4, 'Get returns now four results' );
184 sub test07 { #simple test for httpheaders and getCategories
185 my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
186 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
187 my $builder = t::lib::TestBuilder->new;
188 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
189 my $cat = Koha::Upload->getCategories;
190 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
193 sub test08 { # allows_add_by
194 my $builder = t::lib::TestBuilder->new;
195 my $patron = $builder->build({
196 source => 'Borrower',
197 value => { flags => 0 }, #no permissions
199 my $patronid = $patron->{borrowernumber};
200 is( Koha::Upload->allows_add_by( $patron->{userid} ),
201 undef, 'Patron is not allowed to do anything' );
203 # add some permissions: edit_catalogue
204 my $fl = 2**9; # edit_catalogue
205 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
206 is( Koha::Upload->allows_add_by( $patron->{userid} ),
207 undef, 'Patron is still not allowed to add uploaded files' );
209 # replace flags by all tools
210 $fl = 2**13; # tools
211 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
212 is( Koha::Upload->allows_add_by( $patron->{userid} ),
213 1, 'Patron should be allowed now to add uploaded files' );
215 # remove all tools and add upload_general_files only
216 $fl = 0; # no modules
217 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
218 $builder->build({
219 source => 'UserPermission',
220 value => {
221 borrowernumber => $patronid,
222 module_bit => { module_bit => { flag => 'tools' } },
223 code => 'upload_general_files',
226 is( Koha::Upload->allows_add_by( $patron->{userid} ),
227 1, 'Patron is still allowed to add uploaded files' );
230 # Additional tests for Koha::UploadedFiles
231 # TODO Rearrange the tests after this migration
232 subtest 'Some basic CRUD testing' => sub {
233 plan tests => 2;
235 # Test find and attribute id, delete and search
236 my $builder = t::lib::TestBuilder->new;
237 my $upload01 = $builder->build({ source => 'UploadedFile' });
238 my $found = Koha::UploadedFiles->find( $upload01->{id} );
239 is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
240 $found->delete;
241 $found = Koha::UploadedFiles->search(
242 { id => $upload01->{id} },
244 is( $found->count, 0, 'Delete seems successful' );
247 sub newCGI {
248 my ( $class, $hook ) = @_;
249 my $read = 0;
250 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
251 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
252 $read+= 1000;
253 &$hook( $uh->{name}, 'a'x1000, $read );
256 $current_upload++;
257 return $class;