Bug 17501: Introduce Koha::Object[s] classes for UploadedFile(s)
[koha.git] / t / db_dependent / Upload.t
blob6c69521b8cb2d6958733e0effa8150e7faae9c2a
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
41 # Redirect upload dir structure and mock File::Spec and CGI
42 my $tempdir = tempdir( CLEANUP => 1 );
43 t::lib::Mocks::mock_config('upload_path', $tempdir);
44 my $specmod = Test::MockModule->new( 'File::Spec' );
45 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
46 my $cgimod = Test::MockModule->new( 'CGI' );
47 $cgimod->mock( 'new' => \&newCGI );
49 # Start testing
50 subtest 'Test01' => sub {
51 plan tests => 7;
52 test01();
54 subtest 'Test02' => sub {
55 plan tests => 4;
56 test02();
58 subtest 'Test03' => sub {
59 plan tests => 2;
60 test03();
62 subtest 'Test04' => sub {
63 plan tests => 3;
64 test04();
66 subtest 'Test05' => sub {
67 plan tests => 5;
68 test05();
70 subtest 'Test06' => sub {
71 plan tests => 2;
72 test06();
74 subtest 'Test07' => sub {
75 plan tests => 2;
76 test07();
78 subtest 'Test08: allows_add_by' => sub {
79 plan tests => 4;
80 test08();
82 $schema->storage->txn_rollback;
84 sub test01 {
85 # Delete existing records (for later tests)
86 $dbh->do( "DELETE FROM uploaded_files" );
88 my $upl = Koha::Upload->new({
89 category => $uploads->[$current_upload]->[0]->{cat},
90 });
91 my $cgi= $upl->cgi;
92 my $res= $upl->result;
93 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
94 is( $upl->count, 2, 'Count returns 2 also' );
95 foreach my $r ( $upl->get({ id => $res }) ) {
96 if( $r->{name} eq 'file1' ) {
97 is( $r->{uploadcategorycode}, 'A', 'Check category A' );
98 is( $r->{filesize}, 6000, 'Check size of file1' );
99 } elsif( $r->{name} eq 'file2' ) {
100 is( $r->{filesize}, 8000, 'Check size of file2' );
101 is( $r->{public}, undef, 'Check public undefined' );
104 is( $upl->err, undef, 'No errors reported' );
107 sub test02 {
108 my $upl = Koha::Upload->new({
109 category => $uploads->[$current_upload]->[0]->{cat},
110 public => 1,
112 my $cgi= $upl->cgi;
113 is( $upl->count, 1, 'Upload 2 includes one file' );
114 my $res= $upl->result;
115 my $r = $upl->get({ id => $res, filehandle => 1 });
116 is( $r->{uploadcategorycode}, 'B', 'Check category B' );
117 is( $r->{public}, 1, 'Check public == 1' );
118 is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
121 sub test03 {
122 my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
123 my $cgi= $upl->cgi;
124 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
125 my $r = $upl->get({ id => $upl->result });
126 is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
129 sub test04 { # Fail on a file already there
130 my $upl = Koha::Upload->new({
131 category => $uploads->[$current_upload]->[0]->{cat},
133 my $cgi= $upl->cgi;
134 is( $upl->count, 0, 'Upload 4 failed as expected' );
135 is( $upl->result, undef, 'Result is undefined' );
136 my $e = $upl->err;
137 is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
140 sub test05 { # add temporary file with same name and contents, delete it
141 my $upl = Koha::Upload->new({ tmp => 1 });
142 my $cgi= $upl->cgi;
143 is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
144 my $id = $upl->result;
145 my $r = $upl->get({ id => $id });
146 my @d = $upl->delete({ id => $id });
147 is( $d[0], $r->{name}, 'Delete successful' );
148 is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' );
149 is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
150 is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' );
153 sub test06 { #some extra tests for get
154 my $upl = Koha::Upload->new({ public => 1 });
155 my @rec = $upl->get({ term => 'file' });
156 is( @rec, 1, 'Get returns only one public result (file3)' );
157 $upl = Koha::Upload->new; # public == 0
158 @rec = $upl->get({ term => 'file' });
159 is( @rec, 4, 'Get returns now four results' );
162 sub test07 { #simple test for httpheaders and getCategories
163 my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
164 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
165 my $builder = t::lib::TestBuilder->new;
166 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
167 my $cat = Koha::Upload->getCategories;
168 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
171 sub test08 { # allows_add_by
172 my $builder = t::lib::TestBuilder->new;
173 my $patron = $builder->build({
174 source => 'Borrower',
175 value => { flags => 0 }, #no permissions
177 my $patronid = $patron->{borrowernumber};
178 is( Koha::Upload->allows_add_by( $patron->{userid} ),
179 undef, 'Patron is not allowed to do anything' );
181 # add some permissions: edit_catalogue
182 my $fl = 2**9; # edit_catalogue
183 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
184 is( Koha::Upload->allows_add_by( $patron->{userid} ),
185 undef, 'Patron is still not allowed to add uploaded files' );
187 # replace flags by all tools
188 $fl = 2**13; # tools
189 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
190 is( Koha::Upload->allows_add_by( $patron->{userid} ),
191 1, 'Patron should be allowed now to add uploaded files' );
193 # remove all tools and add upload_general_files only
194 $fl = 0; # no modules
195 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
196 $builder->build({
197 source => 'UserPermission',
198 value => {
199 borrowernumber => $patronid,
200 module_bit => { module_bit => { flag => 'tools' } },
201 code => 'upload_general_files',
204 is( Koha::Upload->allows_add_by( $patron->{userid} ),
205 1, 'Patron is still allowed to add uploaded files' );
208 # Additional tests for Koha::UploadedFiles
209 # TODO Rearrange the tests after this migration
210 subtest 'Some basic CRUD testing' => sub {
211 plan tests => 2;
213 # Test find and attribute id, delete and search
214 my $builder = t::lib::TestBuilder->new;
215 my $upload01 = $builder->build({ source => 'UploadedFile' });
216 my $found = Koha::UploadedFiles->find( $upload01->{id} );
217 is( $found->id, $upload01->{id}, 'Koha::Object returns id' );
218 $found->delete;
219 $found = Koha::UploadedFiles->search(
220 { id => $upload01->{id} },
222 is( $found->count, 0, 'Delete seems successful' );
225 sub newCGI {
226 my ( $class, $hook ) = @_;
227 my $read = 0;
228 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
229 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
230 $read+= 1000;
231 &$hook( $uh->{name}, 'a'x1000, $read );
234 $current_upload++;
235 return $class;