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