From 01095454a2e9175410c0320fbbd13dcd2feb8512 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 4 Apr 2019 11:53:22 -0300 Subject: [PATCH] Bug 22508: Delete the public file on deleting the upload --- Koha/UploadedFile.pm | 6 ++++++ t/db_dependent/Upload.t | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Koha/UploadedFile.pm b/Koha/UploadedFile.pm index 982cdc6ae3..990c7ebfc8 100644 --- a/Koha/UploadedFile.pm +++ b/Koha/UploadedFile.pm @@ -73,6 +73,7 @@ sub delete { my $name = $self->filename; my $file = $self->full_path; + my $public_filepath = $self->local_public_path; my $retval = $self->SUPER::delete; if( !defined($retval) ) { # undef is Unknown (-1) @@ -90,6 +91,11 @@ sub delete { } elsif( ! unlink($file) ) { warn "Problem while deleting: $file"; } + + if( -e $public_filepath and ! unlink($public_filepath) ) { + warn "Problem while deleting: $public_filepath"; + } + return $retval; } diff --git a/t/db_dependent/Upload.t b/t/db_dependent/Upload.t index 802dc58da0..16903e4675 100644 --- a/t/db_dependent/Upload.t +++ b/t/db_dependent/Upload.t @@ -51,6 +51,10 @@ our $uploads = [ # Redirect upload dir structure and mock C4::Context and CGI my $tempdir = tempdir( CLEANUP => 1 ); t::lib::Mocks::mock_config('upload_path', $tempdir); + my $publictempdir = tempdir( CLEANUP => 1 ); + my $public_base_url = 'http://my_opac/public_dir'; + t::lib::Mocks::mock_config('upload_public_path', $publictempdir); + t::lib::Mocks::mock_config('upload_public_url', $public_base_url); my $specmod = Test::MockModule->new( 'C4::Context' ); $specmod->mock( 'temporary_directory' => sub { return $tempdir; } ); my $cgimod = Test::MockModule->new( 'CGI' ); @@ -149,7 +153,7 @@ subtest 'Add same file in same category' => sub { }; subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { - plan tests => 10; + plan tests => 12; # add temporary file with same name and contents (file4) my $upl = Koha::Uploader->new({ tmp => 1 }); @@ -190,6 +194,16 @@ subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { $delete = $kohaobj->delete({ keep_file => 1 }); ok( $delete =~ /^(0E0|-1)$/, 'Repeated delete unsuccessful' ); # NOTE: Koha::Object->delete does not return 0E0 (yet?) + + # testing delete direct public path + $upl = Koha::Uploader->new({ tmp => 1 }); + $upl->cgi; + $kohaobj = Koha::UploadedFiles->find( $upl->result ); + my $local_public_path = $kohaobj->local_public_path; + is( -e $local_public_path, 1, 'Local public file exist after create' ); + $delete = $kohaobj->delete; + ok( $delete=~/^-?1$/, 'Delete successful' ); + isnt( -e $local_public_path, 1, 'Local public file no longer found after delete' ); }; subtest 'Test delete_missing' => sub { -- 2.11.0