Bug 17501: Move getCategories and httpheaders from Upload.pm
[koha.git] / t / db_dependent / Upload.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 9;
6
7 use Test::MockModule;
8 use t::lib::Mocks;
9 use t::lib::TestBuilder;
10
11 use C4::Context;
12 use Koha::Database;
13 use Koha::Upload;
14 use Koha::UploadedFile;
15 use Koha::UploadedFiles;
16
17 my $schema  = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
19
20 our $current_upload = 0;
21 our $uploads = [
22     [
23         { name => 'file1', cat => 'A', size => 6000 },
24         { name => 'file2', cat => 'A', size => 8000 },
25     ],
26     [
27         { name => 'file3', cat => 'B', size => 1000 },
28     ],
29     [
30         { name => 'file4', cat => undef, size => 5000 }, # temporary
31     ],
32     [
33         { name => 'file2', cat => 'A', size => 8000 },
34         # uploading a duplicate in cat A should fail
35     ],
36     [
37         { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
38     ],
39     [
40         { name => 'file5', cat => undef, size => 7000 },
41     ],
42 ];
43
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 );
51
52 # Start testing
53 subtest 'Test01' => sub {
54     plan tests => 11;
55     test01();
56 };
57 subtest 'Test02' => sub {
58     plan tests => 5;
59     test02();
60 };
61 subtest 'Test03' => sub {
62     plan tests => 2;
63     test03();
64 };
65 subtest 'Test04' => sub {
66     plan tests => 3;
67     test04();
68 };
69 subtest 'Test05' => sub {
70     plan tests => 6;
71     test05();
72 };
73 subtest 'Test06' => sub {
74     plan tests => 3;
75     test06();
76 };
77 subtest 'Test07' => sub {
78     plan tests => 2;
79     test07();
80 };
81 subtest 'Test08: allows_add_by' => sub {
82     plan tests => 4;
83     test08();
84 };
85 $schema->storage->txn_rollback;
86
87 sub test01 {
88     # Delete existing records (for later tests)
89     # Passing keep_file suppresses warnings
90     Koha::UploadedFiles->new->delete({ keep_file => 1 });
91
92     # Check mocked directories
93     is( Koha::UploadedFile->permanent_directory, $tempdir,
94         'Check permanent directory' );
95     is( Koha::UploadedFile->temporary_directory, $tempdir,
96         'Check temporary directory' );
97
98     my $upl = Koha::Upload->new({
99         category => $uploads->[$current_upload]->[0]->{cat},
100     });
101     my $cgi= $upl->cgi;
102     my $res= $upl->result;
103     is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
104     is( $upl->count, 2, 'Count returns 2 also' );
105     is( $upl->err, undef, 'No errors reported' );
106
107     my $rs = Koha::UploadedFiles->search({
108         id => [ split ',', $res ]
109     }, { order_by => { -asc => 'filename' }});
110     my $rec = $rs->next;
111     is( $rec->filename, 'file1', 'Check file name' );
112     is( $rec->uploadcategorycode, 'A', 'Check category A' );
113     is( $rec->filesize, 6000, 'Check size of file1' );
114     $rec = $rs->next;
115     is( $rec->filename, 'file2', 'Check file name 2' );
116     is( $rec->filesize, 8000, 'Check size of file2' );
117     is( $rec->public, undef, 'Check public undefined' );
118 }
119
120 sub test02 {
121     my $upl = Koha::Upload->new({
122         category => $uploads->[$current_upload]->[0]->{cat},
123         public => 1,
124     });
125     my $cgi= $upl->cgi;
126     is( $upl->count, 1, 'Upload 2 includes one file' );
127     my $res= $upl->result;
128     my $rec = Koha::UploadedFiles->find( $res );
129     is( $rec->uploadcategorycode, 'B', 'Check category B' );
130     is( $rec->public, 1, 'Check public == 1' );
131     my $fh = $rec->file_handle;
132     is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
133
134     my $orgname = $rec->filename;
135     $rec->filename( 'doesprobablynotexist' )->store;
136     is( $rec->file_handle, undef, 'Sabotage with file handle' );
137     $rec->filename( $orgname )->store;
138 }
139
140 sub test03 {
141     my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
142     my $cgi= $upl->cgi;
143     is( $upl->count, 1, 'Upload 3 includes one temporary file' );
144     my $rec = Koha::UploadedFiles->find( $upl->result );
145     is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
146 }
147
148 sub test04 { # Fail on a file already there
149     my $upl = Koha::Upload->new({
150         category => $uploads->[$current_upload]->[0]->{cat},
151     });
152     my $cgi= $upl->cgi;
153     is( $upl->count, 0, 'Upload 4 failed as expected' );
154     is( $upl->result, undef, 'Result is undefined' );
155     my $e = $upl->err;
156     is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
157 }
158
159 sub test05 { # add temporary file with same name and contents, delete it
160     my $upl = Koha::Upload->new({ tmp => 1 });
161     my $cgi= $upl->cgi;
162     is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
163     my $id = $upl->result;
164     my $path = Koha::UploadedFiles->find( $id )->full_path;
165
166     # testing delete via UploadedFiles (plural)
167     my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
168     is( $delete, 1, 'Delete successful' );
169     isnt( -e $path, 1, 'File no longer found after delete' );
170     is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
171
172     # testing delete via UploadedFile (singular)
173     # Note that find returns a Koha::Object
174     $upl = Koha::Upload->new({ tmp => 1 });
175     $upl->cgi;
176     my $kohaobj = Koha::UploadedFiles->find( $upl->result );
177     my $name = $kohaobj->filename;
178     $path = $kohaobj->full_path;
179     $delete = $kohaobj->delete;
180     is( $delete, $name, 'Delete successful' );
181     isnt( -e $path, 1, 'File no longer found after delete' );
182 }
183
184 sub test06 { #search_term with[out] private flag
185     my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
186     is( @recs, 1, 'Returns only one public result' );
187     is( $recs[0]->filename, 'file3', 'Should be file3' );
188
189     is( Koha::UploadedFiles->search_term({
190         term => 'file', include_private => 1,
191     })->count, 4, 'Returns now four results' );
192 }
193
194 sub test07 { #simple test for httpheaders and getCategories
195     my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next;
196     my @hdrs = $rec->httpheaders;
197     is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
198     my $builder = t::lib::TestBuilder->new;
199     $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
200     my $cat = Koha::UploadedFile->getCategories;
201     is( @$cat >= 1, 1, 'getCategories returned at least one category' );
202 }
203
204 sub test08 { # allows_add_by
205     my $builder = t::lib::TestBuilder->new;
206     my $patron = $builder->build({
207         source => 'Borrower',
208         value  => { flags => 0 }, #no permissions
209     });
210     my $patronid = $patron->{borrowernumber};
211     is( Koha::Upload->allows_add_by( $patron->{userid} ),
212         undef, 'Patron is not allowed to do anything' );
213
214     # add some permissions: edit_catalogue
215     my $fl = 2**9; # edit_catalogue
216     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
217     is( Koha::Upload->allows_add_by( $patron->{userid} ),
218         undef, 'Patron is still not allowed to add uploaded files' );
219
220     # replace flags by all tools
221     $fl = 2**13; # tools
222     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
223     is( Koha::Upload->allows_add_by( $patron->{userid} ),
224         1, 'Patron should be allowed now to add uploaded files' );
225
226     # remove all tools and add upload_general_files only
227     $fl = 0; # no modules
228     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
229     $builder->build({
230         source => 'UserPermission',
231         value  => {
232             borrowernumber => $patronid,
233             module_bit     => { module_bit => { flag => 'tools' } },
234             code           => 'upload_general_files',
235         },
236     });
237     is( Koha::Upload->allows_add_by( $patron->{userid} ),
238         1, 'Patron is still allowed to add uploaded files' );
239 }
240
241 # Additional tests for Koha::UploadedFiles
242 # TODO Rearrange the tests after this migration
243 subtest 'Some basic CRUD testing' => sub {
244     plan tests => 2;
245
246     # Test find and attribute id, delete and search
247     my $builder = t::lib::TestBuilder->new;
248     my $upload01 = $builder->build({ source => 'UploadedFile' });
249     my $found = Koha::UploadedFiles->find( $upload01->{id} );
250     is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
251     $found->delete({ keep_file => 1 }); #note that it does not exist
252     $found = Koha::UploadedFiles->search(
253         { id => $upload01->{id} },
254     );
255     is( $found->count, 0, 'Delete seems successful' );
256 };
257
258 sub newCGI {
259     my ( $class, $hook ) = @_;
260     my $read = 0;
261     foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
262         for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
263             $read+= 1000;
264             &$hook( $uh->{name}, 'a'x1000, $read );
265         }
266     }
267     $current_upload++;
268     return $class;
269 }