Marcel de Rooy
e429db4746
This patch makes the following changes, as requested by QA: [1] UploadedFile->delete always calls SUPER::delete. The return value normally comes from SUPER::delete; if removing the file failed, we return false. Two warns are kept. Since delete does no longer return the filename, a few changes were needed in tools/upload.pl. [2] Method getCategories is moved to UploadedFiles. Script tools/upload.pl now only contains one call. Added a use C4::Koha. [3] Calls UploadedFiles->delete as class method. As a result I removed method delete_errors for now; may be reconsidered on a new report. [4] Adjusted three ->search calls for id and public to ->find calls. [5] If you pass no id to upload.pl when deleting, you don't get an alert. All by all, we got rid of 15 lines ! Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
257 lines
8.6 KiB
Perl
257 lines
8.6 KiB
Perl
#!/usr/bin/perl
|
|
|
|
use Modern::Perl;
|
|
use File::Temp qw/ tempdir /;
|
|
use Test::More tests => 10;
|
|
use Test::Warn;
|
|
|
|
use Test::MockModule;
|
|
use t::lib::Mocks;
|
|
use t::lib::TestBuilder;
|
|
|
|
use C4::Context;
|
|
use Koha::Database;
|
|
use Koha::UploadedFile;
|
|
use Koha::UploadedFiles;
|
|
use Koha::Uploader;
|
|
|
|
my $schema = Koha::Database->new->schema;
|
|
$schema->storage->txn_begin;
|
|
my $builder = t::lib::TestBuilder->new;
|
|
|
|
our $current_upload = 0;
|
|
our $uploads = [
|
|
[
|
|
{ name => 'file1', cat => 'A', size => 6000 },
|
|
{ name => 'file2', cat => 'A', size => 8000 },
|
|
],
|
|
[
|
|
{ name => 'file3', cat => 'B', size => 1000 },
|
|
],
|
|
[
|
|
{ name => 'file4', cat => undef, size => 5000 }, # temporary
|
|
],
|
|
[
|
|
{ name => 'file2', cat => 'A', size => 8000 },
|
|
# uploading a duplicate in cat A should fail
|
|
],
|
|
[
|
|
{ name => 'file4', cat => undef, size => 5000 }, # temp duplicate
|
|
],
|
|
[
|
|
{ name => 'file5', cat => undef, size => 7000 },
|
|
],
|
|
];
|
|
|
|
# Redirect upload dir structure and mock File::Spec and CGI
|
|
my $tempdir = tempdir( CLEANUP => 1 );
|
|
t::lib::Mocks::mock_config('upload_path', $tempdir);
|
|
my $specmod = Test::MockModule->new( 'File::Spec' );
|
|
$specmod->mock( 'tmpdir' => sub { return $tempdir; } );
|
|
my $cgimod = Test::MockModule->new( 'CGI' );
|
|
$cgimod->mock( 'new' => \&newCGI );
|
|
|
|
# Start testing
|
|
subtest 'Make a fresh start' => sub {
|
|
plan tests => 1;
|
|
|
|
# Delete existing records (for later tests)
|
|
# Passing keep_file suppresses warnings (and does not delete files)
|
|
# Note that your files are not in danger, since we redirected
|
|
# all files to a new empty temp folder
|
|
Koha::UploadedFiles->delete({ keep_file => 1 });
|
|
is( Koha::UploadedFiles->count, 0, 'No records left' );
|
|
};
|
|
|
|
subtest 'permanent_directory and temporary_directory' => sub {
|
|
plan tests => 2;
|
|
|
|
# Check mocked directories
|
|
is( Koha::UploadedFile->permanent_directory, $tempdir,
|
|
'Check permanent directory' );
|
|
is( Koha::UploadedFile->temporary_directory, $tempdir,
|
|
'Check temporary directory' );
|
|
};
|
|
|
|
subtest 'Add two uploads in category A' => sub {
|
|
plan tests => 9;
|
|
|
|
my $upl = Koha::Uploader->new({
|
|
category => $uploads->[$current_upload]->[0]->{cat},
|
|
});
|
|
my $cgi= $upl->cgi;
|
|
my $res= $upl->result;
|
|
is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
|
|
is( $upl->count, 2, 'Count returns 2 also' );
|
|
is( $upl->err, undef, 'No errors reported' );
|
|
|
|
my $rs = Koha::UploadedFiles->search({
|
|
id => [ split ',', $res ]
|
|
}, { order_by => { -asc => 'filename' }});
|
|
my $rec = $rs->next;
|
|
is( $rec->filename, 'file1', 'Check file name' );
|
|
is( $rec->uploadcategorycode, 'A', 'Check category A' );
|
|
is( $rec->filesize, 6000, 'Check size of file1' );
|
|
$rec = $rs->next;
|
|
is( $rec->filename, 'file2', 'Check file name 2' );
|
|
is( $rec->filesize, 8000, 'Check size of file2' );
|
|
is( $rec->public, undef, 'Check public undefined' );
|
|
};
|
|
|
|
subtest 'Add another upload, check file_handle' => sub {
|
|
plan tests => 5;
|
|
|
|
my $upl = Koha::Uploader->new({
|
|
category => $uploads->[$current_upload]->[0]->{cat},
|
|
public => 1,
|
|
});
|
|
my $cgi= $upl->cgi;
|
|
is( $upl->count, 1, 'Upload 2 includes one file' );
|
|
my $res= $upl->result;
|
|
my $rec = Koha::UploadedFiles->find( $res );
|
|
is( $rec->uploadcategorycode, 'B', 'Check category B' );
|
|
is( $rec->public, 1, 'Check public == 1' );
|
|
my $fh = $rec->file_handle;
|
|
is( ref($fh) eq 'IO::File' && $fh->opened, 1, 'Get returns a file handle' );
|
|
|
|
my $orgname = $rec->filename;
|
|
$rec->filename( 'doesprobablynotexist' )->store;
|
|
is( $rec->file_handle, undef, 'Sabotage with file handle' );
|
|
$rec->filename( $orgname )->store;
|
|
};
|
|
|
|
subtest 'Add temporary upload' => sub {
|
|
plan tests => 2;
|
|
|
|
my $upl = Koha::Uploader->new({ tmp => 1 }); #temporary
|
|
my $cgi= $upl->cgi;
|
|
is( $upl->count, 1, 'Upload 3 includes one temporary file' );
|
|
my $rec = Koha::UploadedFiles->find( $upl->result );
|
|
is( $rec->uploadcategorycode =~ /_upload$/, 1, 'Check category temp file' );
|
|
};
|
|
|
|
subtest 'Add same file in same category' => sub {
|
|
plan tests => 3;
|
|
|
|
my $upl = Koha::Uploader->new({
|
|
category => $uploads->[$current_upload]->[0]->{cat},
|
|
});
|
|
my $cgi= $upl->cgi;
|
|
is( $upl->count, 0, 'Upload 4 failed as expected' );
|
|
is( $upl->result, undef, 'Result is undefined' );
|
|
my $e = $upl->err;
|
|
is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
|
|
};
|
|
|
|
subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
|
|
plan tests => 8;
|
|
|
|
# add temporary file with same name and contents (file4)
|
|
my $upl = Koha::Uploader->new({ tmp => 1 });
|
|
my $cgi= $upl->cgi;
|
|
is( $upl->count, 1, 'Add duplicate temporary file (file4)' );
|
|
my $id = $upl->result;
|
|
my $path = Koha::UploadedFiles->find( $id )->full_path;
|
|
|
|
# testing delete via UploadedFiles (plural)
|
|
my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
|
|
is( $delete, 1, 'Delete successful' );
|
|
isnt( -e $path, 1, 'File no longer found after delete' );
|
|
is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
|
|
|
|
# testing delete via UploadedFile (singular)
|
|
# Note that find returns a Koha::Object
|
|
$upl = Koha::Uploader->new({ tmp => 1 });
|
|
$upl->cgi;
|
|
my $kohaobj = Koha::UploadedFiles->find( $upl->result );
|
|
$path = $kohaobj->full_path;
|
|
$delete = $kohaobj->delete;
|
|
is( $delete, 1, 'Delete successful' );
|
|
isnt( -e $path, 1, 'File no longer found after delete' );
|
|
|
|
# add another record with TestBuilder, so file does not exist
|
|
# catch warning
|
|
my $upload01 = $builder->build({ source => 'UploadedFile' });
|
|
warning_like { Koha::UploadedFiles->find( $upload01->{id} )->delete; }
|
|
qr/file was missing/,
|
|
'delete warns when file is missing';
|
|
is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
|
|
};
|
|
|
|
subtest 'Call search_term with[out] private flag' => sub {
|
|
plan tests => 3;
|
|
|
|
my @recs = Koha::UploadedFiles->search_term({ term => 'file' });
|
|
is( @recs, 1, 'Returns only one public result' );
|
|
is( $recs[0]->filename, 'file3', 'Should be file3' );
|
|
|
|
is( Koha::UploadedFiles->search_term({
|
|
term => 'file', include_private => 1,
|
|
})->count, 4, 'Returns now four results' );
|
|
};
|
|
|
|
subtest 'Simple tests for httpheaders and getCategories' => sub {
|
|
plan tests => 2;
|
|
|
|
my $rec = Koha::UploadedFiles->search_term({ term => 'file' })->next;
|
|
my @hdrs = $rec->httpheaders;
|
|
is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
|
|
$builder->build({ source => 'AuthorisedValue', value => { category => 'UPLOAD', authorised_value => 'HAVE_AT_LEAST_ONE', lib => 'Hi there' } });
|
|
my $cat = Koha::UploadedFiles->getCategories;
|
|
is( @$cat >= 1, 1, 'getCategories returned at least one category' );
|
|
};
|
|
|
|
subtest 'Testing allows_add_by' => sub {
|
|
plan tests => 4;
|
|
|
|
my $patron = $builder->build({
|
|
source => 'Borrower',
|
|
value => { flags => 0 }, #no permissions
|
|
});
|
|
my $patronid = $patron->{borrowernumber};
|
|
is( Koha::Uploader->allows_add_by( $patron->{userid} ),
|
|
undef, 'Patron is not allowed to do anything' );
|
|
|
|
# add some permissions: edit_catalogue
|
|
my $fl = 2**9; # edit_catalogue
|
|
$schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
|
|
is( Koha::Uploader->allows_add_by( $patron->{userid} ),
|
|
undef, 'Patron is still not allowed to add uploaded files' );
|
|
|
|
# replace flags by all tools
|
|
$fl = 2**13; # tools
|
|
$schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
|
|
is( Koha::Uploader->allows_add_by( $patron->{userid} ),
|
|
1, 'Patron should be allowed now to add uploaded files' );
|
|
|
|
# remove all tools and add upload_general_files only
|
|
$fl = 0; # no modules
|
|
$schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
|
|
$builder->build({
|
|
source => 'UserPermission',
|
|
value => {
|
|
borrowernumber => $patronid,
|
|
module_bit => { module_bit => { flag => 'tools' } },
|
|
code => 'upload_general_files',
|
|
},
|
|
});
|
|
is( Koha::Uploader->allows_add_by( $patron->{userid} ),
|
|
1, 'Patron is still allowed to add uploaded files' );
|
|
};
|
|
|
|
# The end
|
|
$schema->storage->txn_rollback;
|
|
|
|
# Helper routine
|
|
sub newCGI {
|
|
my ( $class, $hook ) = @_;
|
|
my $read = 0;
|
|
foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
|
|
for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
|
|
$read+= 1000;
|
|
&$hook( $uh->{name}, 'a'x1000, $read );
|
|
}
|
|
}
|
|
$current_upload++;
|
|
return $class;
|
|
}
|