Bug 15909 - Remove the use of makepartialpayment
[koha.git] / t / db_dependent / Upload.t
blob20791f396487eb5e5c2fef949168023bcbfaf71d
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 8;
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;
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $dbh = C4::Context->dbh;
19 our $current_upload = 0;
20 our $uploads = [
22 { name => 'file1', cat => 'A', size => 6000 },
23 { name => 'file2', cat => 'A', size => 8000 },
26 { name => 'file3', cat => 'B', size => 1000 },
29 { name => 'file4', cat => undef, size => 5000 }, # temporary
32 { name => 'file2', cat => 'A', size => 8000 },
33 # uploading a duplicate in cat A should fail
36 { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
40 # Redirect upload dir structure and mock File::Spec and CGI
41 my $tempdir = tempdir( CLEANUP => 1 );
42 t::lib::Mocks::mock_config('upload_path', $tempdir);
43 my $specmod = Test::MockModule->new( 'File::Spec' );
44 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
45 my $cgimod = Test::MockModule->new( 'CGI' );
46 $cgimod->mock( 'new' => \&newCGI );
48 # Start testing
49 subtest 'Test01' => sub {
50 plan tests => 7;
51 test01();
53 subtest 'Test02' => sub {
54 plan tests => 4;
55 test02();
57 subtest 'Test03' => sub {
58 plan tests => 2;
59 test03();
61 subtest 'Test04' => sub {
62 plan tests => 3;
63 test04();
65 subtest 'Test05' => sub {
66 plan tests => 5;
67 test05();
69 subtest 'Test06' => sub {
70 plan tests => 2;
71 test06();
73 subtest 'Test07' => sub {
74 plan tests => 2;
75 test07();
77 subtest 'Test08: allows_add_by' => sub {
78 plan tests => 4;
79 test08();
81 $schema->storage->txn_rollback;
83 sub test01 {
84 # Delete existing records (for later tests)
85 $dbh->do( "DELETE FROM uploaded_files" );
87 my $upl = Koha::Upload->new({
88 category => $uploads->[$current_upload]->[0]->{cat},
89 });
90 my $cgi= $upl->cgi;
91 my $res= $upl->result;
92 is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
93 is( $upl->count, 2, 'Count returns 2 also' );
94 foreach my $r ( $upl->get({ id => $res }) ) {
95 if( $r->{name} eq 'file1' ) {
96 is( $r->{uploadcategorycode}, 'A', 'Check category A' );
97 is( $r->{filesize}, 6000, 'Check size of file1' );
98 } elsif( $r->{name} eq 'file2' ) {
99 is( $r->{filesize}, 8000, 'Check size of file2' );
100 is( $r->{public}, undef, 'Check public undefined' );
103 is( $upl->err, undef, 'No errors reported' );
106 sub test02 {
107 my $upl = Koha::Upload->new({
108 category => $uploads->[$current_upload]->[0]->{cat},
109 public => 1,
111 my $cgi= $upl->cgi;
112 is( $upl->count, 1, 'Upload 2 includes one file' );
113 my $res= $upl->result;
114 my $r = $upl->get({ id => $res, filehandle => 1 });
115 is( $r->{uploadcategorycode}, 'B', 'Check category B' );
116 is( $r->{public}, 1, 'Check public == 1' );
117 is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
120 sub test03 {
121 my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
122 my $cgi= $upl->cgi;
123 is( $upl->count, 1, 'Upload 3 includes one temporary file' );
124 my $r = $upl->get({ id => $upl->result });
125 is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
128 sub test04 { # Fail on a file already there
129 my $upl = Koha::Upload->new({
130 category => $uploads->[$current_upload]->[0]->{cat},
132 my $cgi= $upl->cgi;
133 is( $upl->count, 0, 'Upload 4 failed as expected' );
134 is( $upl->result, undef, 'Result is undefined' );
135 my $e = $upl->err;
136 is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
139 sub test05 { # add temporary file with same name and contents, delete it
140 my $upl = Koha::Upload->new({ tmp => 1 });
141 my $cgi= $upl->cgi;
142 is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
143 my $id = $upl->result;
144 my $r = $upl->get({ id => $id });
145 my @d = $upl->delete({ id => $id });
146 is( $d[0], $r->{name}, 'Delete successful' );
147 is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' );
148 is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
149 is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' );
152 sub test06 { #some extra tests for get
153 my $upl = Koha::Upload->new({ public => 1 });
154 my @rec = $upl->get({ term => 'file' });
155 is( @rec, 1, 'Get returns only one public result (file3)' );
156 $upl = Koha::Upload->new; # public == 0
157 @rec = $upl->get({ term => 'file' });
158 is( @rec, 4, 'Get returns now four results' );
161 sub test07 { #simple test for httpheaders and getCategories
162 my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
163 is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
164 my $builder = t::lib::TestBuilder->new;
165 $builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
166 my $cat = Koha::Upload->getCategories;
167 is( @$cat >= 1, 1, 'getCategories returned at least one category' );
170 sub test08 { # allows_add_by
171 my $builder = t::lib::TestBuilder->new;
172 my $patron = $builder->build({
173 source => 'Borrower',
174 value => { flags => 0 }, #no permissions
176 my $patronid = $patron->{borrowernumber};
177 is( Koha::Upload->allows_add_by( $patron->{userid} ),
178 undef, 'Patron is not allowed to do anything' );
180 # add some permissions: edit_catalogue
181 my $fl = 2**9; # edit_catalogue
182 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
183 is( Koha::Upload->allows_add_by( $patron->{userid} ),
184 undef, 'Patron is still not allowed to add uploaded files' );
186 # replace flags by all tools
187 $fl = 2**13; # tools
188 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
189 is( Koha::Upload->allows_add_by( $patron->{userid} ),
190 1, 'Patron should be allowed now to add uploaded files' );
192 # remove all tools and add upload_general_files only
193 $fl = 0; # no modules
194 $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
195 $builder->build({
196 source => 'UserPermission',
197 value => {
198 borrowernumber => $patronid,
199 module_bit => { module_bit => { flag => 'tools' } },
200 code => 'upload_general_files',
203 is( Koha::Upload->allows_add_by( $patron->{userid} ),
204 1, 'Patron is still allowed to add uploaded files' );
207 sub newCGI {
208 my ( $class, $hook ) = @_;
209 my $read = 0;
210 foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
211 for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
212 $read+= 1000;
213 &$hook( $uh->{name}, 'a'x1000, $read );
216 $current_upload++;
217 return $class;