Bug 17669: Add delete_temporary method with unit tests
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Tue, 13 Dec 2016 13:04:29 +0000 (14:04 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 21 Apr 2017 17:55:24 +0000 (13:55 -0400)
Test plan:
Run t/db_dependent/Upload.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Mirko Tietgen <mirko@abunchofthings.net>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/UploadedFiles.pm
t/db_dependent/Upload.t

index 74211fa..06a9965 100644 (file)
@@ -20,6 +20,8 @@ package Koha::UploadedFiles;
 use Modern::Perl;
 
 use C4::Koha;
 use Modern::Perl;
 
 use C4::Koha;
+use Koha::Database;
+use Koha::DateUtils;
 use Koha::UploadedFile;
 
 use parent qw(Koha::Objects);
 use Koha::UploadedFile;
 
 use parent qw(Koha::Objects);
@@ -72,6 +74,30 @@ sub delete {
     return $err==0;
 }
 
     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.
 =head3 search_term
 
 Search_term allows you to pass a term to search in filename and hashvalue.
index 5aeb7a4..6d7864d 100644 (file)
@@ -2,7 +2,7 @@
 
 use Modern::Perl;
 use File::Temp qw/ tempdir /;
 
 use Modern::Perl;
 use File::Temp qw/ tempdir /;
-use Test::More tests => 10;
+use Test::More tests => 11;
 use Test::Warn;
 
 use Test::MockModule;
 use Test::Warn;
 
 use Test::MockModule;
@@ -11,6 +11,7 @@ use t::lib::TestBuilder;
 
 use C4::Context;
 use Koha::Database;
 
 use C4::Context;
 use Koha::Database;
+use Koha::DateUtils;
 use Koha::UploadedFile;
 use Koha::UploadedFiles;
 use Koha::Uploader;
 use Koha::UploadedFile;
 use Koha::UploadedFiles;
 use Koha::Uploader;
@@ -41,6 +42,10 @@ our $uploads = [
     [
         { name => 'file5', cat => undef, size => 7000 },
     ],
     [
         { 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
 ];
 
 # 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' );
 };
 
         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;
 
 # The end
 $schema->storage->txn_rollback;