Bugzilla – Attachment 61241 Details for
Bug 17669
Add purging temporary uploads to cleanup_database
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 17669: Add delete_temporary method with unit tests
Bug-17669-Add-deletetemporary-method-with-unit-tes.patch (text/plain), 4.67 KB, created by
Marcel de Rooy
on 2017-03-20 07:53:39 UTC
(
hide
)
Description:
Bug 17669: Add delete_temporary method with unit tests
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2017-03-20 07:53:39 UTC
Size:
4.67 KB
patch
obsolete
>From a38120e63ac41e41ac9aed9878de260aad8458f8 Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >Date: Tue, 13 Dec 2016 14:04:29 +0100 >Subject: [PATCH] Bug 17669: Add delete_temporary method with unit tests >Content-Type: text/plain; charset=utf-8 > >Test plan: >Run t/db_dependent/Upload.t > >Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >--- > 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 74211fa..06a9965 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 5aeb7a4..6d7864d 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.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 17669
:
58176
|
58177
|
58178
|
58179
|
58810
|
58811
|
58812
|
61239
|
61240
|
61241
|
61242
|
61243
|
61465
|
61466
|
61467
|
61468
|
61469
|
61727
|
61729
|
61730
|
61742
|
61743
|
61805
|
61809
|
61818
|
61819
|
62486
|
62487
|
62488
|
62489
|
62490
|
62491
|
62492
|
62493
|
62494
|
62495