From 53f5d55ab4938cf827108bef98609ca165ef6cb7 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 13 Dec 2016 14:04:29 +0100 Subject: [PATCH] Bug 17669: Add delete_temporary method with unit tests Test plan: Run t/db_dependent/Upload.t Signed-off-by: Marcel de Rooy Signed-off-by: Mirko Tietgen Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- Koha/UploadedFiles.pm | 26 +++++++++++++++++++++ t/db_dependent/Upload.t | 50 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Koha/UploadedFiles.pm b/Koha/UploadedFiles.pm index 74211faf99..06a99655b8 100644 --- a/Koha/UploadedFiles.pm +++ b/Koha/UploadedFiles.pm @@ -20,6 +20,8 @@ package Koha::UploadedFiles; use Modern::Perl; use C4::Koha; +use Koha::Database; +use Koha::DateUtils; use Koha::UploadedFile; use parent qw(Koha::Objects); @@ -72,6 +74,30 @@ sub delete { return $err==0; } +=head3 delete_temporary + +Delete_temporary is called by cleanup_database and only removes temporary +uploads older than [pref Upload_PurgeTemporaryFiles_Days] days. +It is possible to override the pref with the override_pref parameter. + +Returns true if no errors occur. (Even when no files had to be deleted.) + +=cut + +sub delete_temporary { + my ( $self, $params ) = @_; + my $days = $params->{override_pref} || + C4::Context->preference('Upload_PurgeTemporaryFiles_Days'); + return 1 if !$days; + my $dt = dt_from_string(); + $dt->subtract( days => $days ); + my $parser = Koha::Database->new->schema->storage->datetime_parser; + return $self->search({ + permanent => [ undef, 0 ], + dtcreated => { '<' => $parser->format_datetime($dt) }, + })->delete; +} + =head3 search_term Search_term allows you to pass a term to search in filename and hashvalue. diff --git a/t/db_dependent/Upload.t b/t/db_dependent/Upload.t index 5aeb7a41bc..6d7864d901 100644 --- a/t/db_dependent/Upload.t +++ b/t/db_dependent/Upload.t @@ -2,7 +2,7 @@ use Modern::Perl; use File::Temp qw/ tempdir /; -use Test::More tests => 10; +use Test::More tests => 11; use Test::Warn; use Test::MockModule; @@ -11,6 +11,7 @@ use t::lib::TestBuilder; use C4::Context; use Koha::Database; +use Koha::DateUtils; use Koha::UploadedFile; use Koha::UploadedFiles; use Koha::Uploader; @@ -41,6 +42,10 @@ our $uploads = [ [ { name => 'file5', cat => undef, size => 7000 }, ], + [ + { name => 'file6', cat => undef, size => 6500 }, + { name => 'file7', cat => undef, size => 6501 }, + ], ]; # Redirect upload dir structure and mock File::Spec and CGI @@ -239,6 +244,49 @@ subtest 'Testing allows_add_by' => sub { 1, 'Patron is still allowed to add uploaded files' ); }; +subtest 'Testing delete_temporary' => sub { + plan tests => 7; + + # Add two temporary files: result should be 3 + 3 + Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7 + is( Koha::UploadedFiles->search->count, 6, 'Test starting count' ); + is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3, + 'Includes 3 permanent' ); + + # Move all permanents to today - 1 + # Move temp 1 to today - 3, and temp 2,3 to today - 5 + my $today = dt_from_string; + $today->subtract( minutes => 2 ); # should be enough :) + my $dt = $today->clone->subtract( days => 1 ); + foreach my $rec ( Koha::UploadedFiles->search({ permanent => 1 }) ) { + $rec->dtcreated($dt)->store; + } + my @recs = Koha::UploadedFiles->search({ permanent => 0 }); + $dt = $today->clone->subtract( days => 3 ); + $recs[0]->dtcreated($dt)->store; + $dt = $today->clone->subtract( days => 5 ); + $recs[1]->dtcreated($dt)->store; + $recs[2]->dtcreated($dt)->store; + + # Now call delete_temporary with 0, 6, 5 and 1 (via override) + t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 0 ); + Koha::UploadedFiles->delete_temporary; + is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==0' ); + + t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 6 ); + Koha::UploadedFiles->delete_temporary; + is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' ); + + t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 5 ); + Koha::UploadedFiles->delete_temporary; + is( Koha::UploadedFiles->search->count, 4, 'Delete with pref==5 makes 4' ); + + Koha::UploadedFiles->delete_temporary({ override_pref => 1 }); + is( Koha::UploadedFiles->search->count, 3, 'Delete override==1 makes 3' ); + is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3, + 'Still 3 permanent uploads' ); +}; + # The end $schema->storage->txn_rollback; -- 2.39.5