Bug 17501: Remove Koha::Upload::get from Koha::Upload
[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::UploadedFiles;
15
16 my $schema  = Koha::Database->new->schema;
17 $schema->storage->txn_begin;
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         { name => 'file5', cat => undef, size => 7000 },
40     ],
41 ];
42
43 # Redirect upload dir structure and mock File::Spec and CGI
44 my $tempdir = tempdir( CLEANUP => 1 );
45 t::lib::Mocks::mock_config('upload_path', $tempdir);
46 my $specmod = Test::MockModule->new( 'File::Spec' );
47 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
48 my $cgimod = Test::MockModule->new( 'CGI' );
49 $cgimod->mock( 'new' => \&newCGI );
50
51 # Start testing
52 subtest 'Test01' => sub {
53     plan tests => 11;
54     test01();
55 };
56 subtest 'Test02' => sub {
57     plan tests => 5;
58     test02();
59 };
60 subtest 'Test03' => sub {
61     plan tests => 2;
62     test03();
63 };
64 subtest 'Test04' => sub {
65     plan tests => 3;
66     test04();
67 };
68 subtest 'Test05' => sub {
69     plan tests => 6;
70     test05();
71 };
72 subtest 'Test06' => sub {
73     plan tests => 3;
74     test06();
75 };
76 subtest 'Test07' => sub {
77     plan tests => 2;
78     test07();
79 };
80 subtest 'Test08: allows_add_by' => sub {
81     plan tests => 4;
82     test08();
83 };
84 $schema->storage->txn_rollback;
85
86 sub test01 {
87     # Delete existing records (for later tests)
88     # Passing keep_file suppresses warnings
89     Koha::UploadedFiles->new->delete({ keep_file => 1 });
90
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' );
96
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     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' );
117 }
118
119 sub test02 {
120     my $upl = Koha::Upload->new({
121         category => $uploads->[$current_upload]->[0]->{cat},
122         public => 1,
123     });
124     my $cgi= $upl->cgi;
125     is( $upl->count, 1, 'Upload 2 includes one file' );
126     my $res= $upl->result;
127     my $rec = Koha::UploadedFiles->find( $res );
128     is( $rec->uploadcategorycode, 'B', 'Check category B' );
129     is( $rec->public, 1, 'Check public == 1' );
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;
137 }
138
139 sub test03 {
140     my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
141     my $cgi= $upl->cgi;
142     is( $upl->count, 1, 'Upload 3 includes one temporary file' );
143     my $rec = Koha::UploadedFiles->find( $upl->result );
144     is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
145 }
146
147 sub test04 { # Fail on a file already there
148     my $upl = Koha::Upload->new({
149         category => $uploads->[$current_upload]->[0]->{cat},
150     });
151     my $cgi= $upl->cgi;
152     is( $upl->count, 0, 'Upload 4 failed as expected' );
153     is( $upl->result, undef, 'Result is undefined' );
154     my $e = $upl->err;
155     is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
156 }
157
158 sub test05 { # add temporary file with same name and contents, delete it
159     my $upl = Koha::Upload->new({ tmp => 1 });
160     my $cgi= $upl->cgi;
161     is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
162     my $id = $upl->result;
163     my $path = Koha::UploadedFiles->find( $id )->full_path;
164
165     # testing delete via UploadedFiles (plural)
166     my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
167     is( $delete, 1, 'Delete successful' );
168     isnt( -e $path, 1, 'File no longer found after delete' );
169     is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
170
171     # testing delete via UploadedFile (singular)
172     # Note that find returns a Koha::Object
173     $upl = Koha::Upload->new({ tmp => 1 });
174     $upl->cgi;
175     my $kohaobj = Koha::UploadedFiles->find( $upl->result );
176     my $name = $kohaobj->filename;
177     $path = $kohaobj->full_path;
178     $delete = $kohaobj->delete;
179     is( $delete, $name, 'Delete successful' );
180     isnt( -e $path, 1, 'File no longer found after delete' );
181 }
182
183 sub test06 { #search_term with[out] private flag
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 sub test07 { #simple test for httpheaders and getCategories
194     my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
195     is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
196     my $builder = t::lib::TestBuilder->new;
197     $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
198     my $cat = Koha::Upload->getCategories;
199     is( @$cat >= 1, 1, 'getCategories returned at least one category' );
200 }
201
202 sub test08 { # allows_add_by
203     my $builder = t::lib::TestBuilder->new;
204     my $patron = $builder->build({
205         source => 'Borrower',
206         value  => { flags => 0 }, #no permissions
207     });
208     my $patronid = $patron->{borrowernumber};
209     is( Koha::Upload->allows_add_by( $patron->{userid} ),
210         undef, 'Patron is not allowed to do anything' );
211
212     # add some permissions: edit_catalogue
213     my $fl = 2**9; # edit_catalogue
214     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
215     is( Koha::Upload->allows_add_by( $patron->{userid} ),
216         undef, 'Patron is still not allowed to add uploaded files' );
217
218     # replace flags by all tools
219     $fl = 2**13; # tools
220     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
221     is( Koha::Upload->allows_add_by( $patron->{userid} ),
222         1, 'Patron should be allowed now to add uploaded files' );
223
224     # remove all tools and add upload_general_files only
225     $fl = 0; # no modules
226     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
227     $builder->build({
228         source => 'UserPermission',
229         value  => {
230             borrowernumber => $patronid,
231             module_bit     => { module_bit => { flag => 'tools' } },
232             code           => 'upload_general_files',
233         },
234     });
235     is( Koha::Upload->allows_add_by( $patron->{userid} ),
236         1, 'Patron is still allowed to add uploaded files' );
237 }
238
239 # Additional tests for Koha::UploadedFiles
240 # TODO Rearrange the tests after this migration
241 subtest 'Some basic CRUD testing' => sub {
242     plan tests => 2;
243
244     # Test find and attribute id, delete and search
245     my $builder = t::lib::TestBuilder->new;
246     my $upload01 = $builder->build({ source => 'UploadedFile' });
247     my $found = Koha::UploadedFiles->find( $upload01->{id} );
248     is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
249     $found->delete({ keep_file => 1 }); #note that it does not exist
250     $found = Koha::UploadedFiles->search(
251         { id => $upload01->{id} },
252     );
253     is( $found->count, 0, 'Delete seems successful' );
254 };
255
256 sub newCGI {
257     my ( $class, $hook ) = @_;
258     my $read = 0;
259     foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
260         for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
261             $read+= 1000;
262             &$hook( $uh->{name}, 'a'x1000, $read );
263         }
264     }
265     $current_upload++;
266     return $class;
267 }