Bug 16453: Make Elasticsearch tests be skipped if configuration entry missing
[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 => 8;
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
15 my $schema  = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $dbh = C4::Context->dbh;
18
19 our $current_upload = 0;
20 our $uploads = [
21     [
22         { name => 'file1', cat => 'A', size => 6000 },
23         { name => 'file2', cat => 'A', size => 8000 },
24     ],
25     [
26         { name => 'file3', cat => 'B', size => 1000 },
27     ],
28     [
29         { name => 'file4', cat => undef, size => 5000 }, # temporary
30     ],
31     [
32         { name => 'file2', cat => 'A', size => 8000 },
33         # uploading a duplicate in cat A should fail
34     ],
35     [
36         { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
37     ],
38 ];
39
40 # Before we mock upload_path, we are checking the real folder
41 # This may help identifying upload problems
42 my $realdir = C4::Context->config('upload_path');
43 if( !$realdir ) {
44     warn "WARNING: You do not have upload_path in koha-conf.xml";
45 } elsif( !-w $realdir ) {
46     warn "WARNING: You do not have write permissions in $realdir";
47 }
48
49 # Redirect upload dir structure and mock File::Spec and CGI
50 my $tempdir = tempdir( CLEANUP => 1 );
51 t::lib::Mocks::mock_config('upload_path', $tempdir);
52 my $specmod = Test::MockModule->new( 'File::Spec' );
53 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
54 my $cgimod = Test::MockModule->new( 'CGI' );
55 $cgimod->mock( 'new' => \&newCGI );
56
57 # Start testing
58 subtest 'Test01' => sub {
59     plan tests => 7;
60     test01();
61 };
62 subtest 'Test02' => sub {
63     plan tests => 4;
64     test02();
65 };
66 subtest 'Test03' => sub {
67     plan tests => 2;
68     test03();
69 };
70 subtest 'Test04' => sub {
71     plan tests => 3;
72     test04();
73 };
74 subtest 'Test05' => sub {
75     plan tests => 5;
76     test05();
77 };
78 subtest 'Test06' => sub {
79     plan tests => 2;
80     test06();
81 };
82 subtest 'Test07' => sub {
83     plan tests => 2;
84     test07();
85 };
86 subtest 'Test08: allows_add_by' => sub {
87     plan tests => 4;
88     test08();
89 };
90 $schema->storage->txn_rollback;
91
92 sub test01 {
93     # Delete existing records (for later tests)
94     $dbh->do( "DELETE FROM uploaded_files" );
95
96     my $upl = Koha::Upload->new({
97         category => $uploads->[$current_upload]->[0]->{cat},
98     });
99     my $cgi= $upl->cgi;
100     my $res= $upl->result;
101     is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
102     is( $upl->count, 2, 'Count returns 2 also' );
103     foreach my $r ( $upl->get({ id => $res }) ) {
104         if( $r->{name} eq 'file1' ) {
105             is( $r->{uploadcategorycode}, 'A', 'Check category A' );
106             is( $r->{filesize}, 6000, 'Check size of file1' );
107         } elsif( $r->{name} eq 'file2' ) {
108             is( $r->{filesize}, 8000, 'Check size of file2' );
109             is( $r->{public}, undef, 'Check public undefined' );
110         }
111     }
112     is( $upl->err, undef, 'No errors reported' );
113 }
114
115 sub test02 {
116     my $upl = Koha::Upload->new({
117         category => $uploads->[$current_upload]->[0]->{cat},
118         public => 1,
119     });
120     my $cgi= $upl->cgi;
121     is( $upl->count, 1, 'Upload 2 includes one file' );
122     my $res= $upl->result;
123     my $r = $upl->get({ id => $res, filehandle => 1 });
124     is( $r->{uploadcategorycode}, 'B', 'Check category B' );
125     is( $r->{public}, 1, 'Check public == 1' );
126     is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
127 }
128
129 sub test03 {
130     my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
131     my $cgi= $upl->cgi;
132     is( $upl->count, 1, 'Upload 3 includes one temporary file' );
133     my $r = $upl->get({ id => $upl->result });
134     is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
135 }
136
137 sub test04 { # Fail on a file already there
138     my $upl = Koha::Upload->new({
139         category => $uploads->[$current_upload]->[0]->{cat},
140     });
141     my $cgi= $upl->cgi;
142     is( $upl->count, 0, 'Upload 4 failed as expected' );
143     is( $upl->result, undef, 'Result is undefined' );
144     my $e = $upl->err;
145     is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
146 }
147
148 sub test05 { # add temporary file with same name and contents, delete it
149     my $upl = Koha::Upload->new({ tmp => 1 });
150     my $cgi= $upl->cgi;
151     is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
152     my $id = $upl->result;
153     my $r = $upl->get({ id => $id });
154     my @d = $upl->delete({ id => $id });
155     is( $d[0], $r->{name}, 'Delete successful' );
156     is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' );
157     is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
158     is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' );
159 }
160
161 sub test06 { #some extra tests for get
162     my $upl = Koha::Upload->new({ public => 1 });
163     my @rec = $upl->get({ term => 'file' });
164     is( @rec, 1, 'Get returns only one public result (file3)' );
165     $upl = Koha::Upload->new; # public == 0
166     @rec = $upl->get({ term => 'file' });
167     is( @rec, 4, 'Get returns now four results' );
168 }
169
170 sub test07 { #simple test for httpheaders and getCategories
171     my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
172     is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
173     $dbh->do("INSERT INTO authorised_values (category, authorised_value, lib) VALUES (?,?,?) ", undef, ( 'UPLOAD', 'HAVE_AT_LEAST_ONE', 'Hi there' ));
174     my $cat = Koha::Upload->getCategories;
175     is( @$cat >= 1, 1, 'getCategories returned at least one category' );
176 }
177
178 sub test08 { # allows_add_by
179     my $builder = t::lib::TestBuilder->new;
180     my $patron = $builder->build({
181         source => 'Borrower',
182         value  => { flags => 0 }, #no permissions
183     });
184     my $patronid = $patron->{borrowernumber};
185     is( Koha::Upload->allows_add_by( $patron->{userid} ),
186         undef, 'Patron is not allowed to do anything' );
187
188     # add some permissions: edit_catalogue
189     my $fl = 2**9; # edit_catalogue
190     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
191     is( Koha::Upload->allows_add_by( $patron->{userid} ),
192         undef, 'Patron is still not allowed to add uploaded files' );
193
194     # replace flags by all tools
195     $fl = 2**13; # tools
196     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
197     is( Koha::Upload->allows_add_by( $patron->{userid} ),
198         1, 'Patron should be allowed now to add uploaded files' );
199
200     # remove all tools and add upload_general_files only
201     $fl = 0; # no modules
202     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
203     $builder->build({
204         source => 'UserPermission',
205         value  => {
206             borrowernumber => $patronid,
207             module_bit     => { module_bit => { flag => 'tools' } },
208             code           => 'upload_general_files',
209         },
210     });
211     is( Koha::Upload->allows_add_by( $patron->{userid} ),
212         1, 'Patron is still allowed to add uploaded files' );
213 }
214
215 sub newCGI {
216     my ( $class, $hook ) = @_;
217     my $read = 0;
218     foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
219         for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
220             $read+= 1000;
221             &$hook( $uh->{name}, 'a'x1000, $read );
222         }
223     }
224     $current_upload++;
225     return $class;
226 }