View | Details | Raw Unified | Return to bug 17669
Collapse All | Expand All

(-)a/Koha/UploadedFiles.pm (+26 lines)
Lines 20-25 package Koha::UploadedFiles; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use C4::Koha;
22
use C4::Koha;
23
use Koha::Database;
24
use Koha::DateUtils;
23
use Koha::UploadedFile;
25
use Koha::UploadedFile;
24
26
25
use parent qw(Koha::Objects);
27
use parent qw(Koha::Objects);
Lines 72-77 sub delete { Link Here
72
    return $err==0;
74
    return $err==0;
73
}
75
}
74
76
77
=head3 delete_temporary
78
79
Delete_temporary is called by cleanup_database and only removes temporary
80
uploads older than [pref Upload_PurgeTemporaryFiles_Days] days.
81
It is possible to override the pref with the override_pref parameter.
82
83
Returns true if no errors occur. (Even when no files had to be deleted.)
84
85
=cut
86
87
sub delete_temporary {
88
    my ( $self, $params ) = @_;
89
    my $days = $params->{override_pref} ||
90
        C4::Context->preference('Upload_PurgeTemporaryFiles_Days');
91
    return 1 if !$days;
92
    my $dt = dt_from_string();
93
    $dt->subtract( days => $days );
94
    my $parser = Koha::Database->new->schema->storage->datetime_parser;
95
    return $self->search({
96
        permanent => [ undef, 0 ],
97
        dtcreated => { '<' => $parser->format_datetime($dt) },
98
    })->delete;
99
}
100
75
=head3 search_term
101
=head3 search_term
76
102
77
Search_term allows you to pass a term to search in filename and hashvalue.
103
Search_term allows you to pass a term to search in filename and hashvalue.
(-)a/t/db_dependent/Upload.t (-2 / +49 lines)
Lines 2-8 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use File::Temp qw/ tempdir /;
4
use File::Temp qw/ tempdir /;
5
use Test::More tests => 10;
5
use Test::More tests => 11;
6
use Test::Warn;
6
use Test::Warn;
7
7
8
use Test::MockModule;
8
use Test::MockModule;
Lines 11-16 use t::lib::TestBuilder; Link Here
11
11
12
use C4::Context;
12
use C4::Context;
13
use Koha::Database;
13
use Koha::Database;
14
use Koha::DateUtils;
14
use Koha::UploadedFile;
15
use Koha::UploadedFile;
15
use Koha::UploadedFiles;
16
use Koha::UploadedFiles;
16
use Koha::Uploader;
17
use Koha::Uploader;
Lines 41-46 our $uploads = [ Link Here
41
    [
42
    [
42
        { name => 'file5', cat => undef, size => 7000 },
43
        { name => 'file5', cat => undef, size => 7000 },
43
    ],
44
    ],
45
    [
46
        { name => 'file6', cat => undef, size => 6500 },
47
        { name => 'file7', cat => undef, size => 6501 },
48
    ],
44
];
49
];
45
50
46
# Redirect upload dir structure and mock File::Spec and CGI
51
# Redirect upload dir structure and mock File::Spec and CGI
Lines 239-244 subtest 'Testing allows_add_by' => sub { Link Here
239
        1, 'Patron is still allowed to add uploaded files' );
244
        1, 'Patron is still allowed to add uploaded files' );
240
};
245
};
241
246
247
subtest 'Testing delete_temporary' => sub {
248
    plan tests => 7;
249
250
    # Add two temporary files: result should be 3 + 3
251
    Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7
252
    is( Koha::UploadedFiles->search->count, 6, 'Test starting count' );
253
    is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
254
        'Includes 3 permanent' );
255
256
    # Move all permanents to today - 1
257
    # Move temp 1 to today - 3, and temp 2,3 to today - 5
258
    my $today = dt_from_string;
259
    $today->subtract( minutes => 2 ); # should be enough :)
260
    my $dt = $today->clone->subtract( days => 1 );
261
    foreach my $rec ( Koha::UploadedFiles->search({ permanent => 1 }) ) {
262
        $rec->dtcreated($dt)->store;
263
    }
264
    my @recs = Koha::UploadedFiles->search({ permanent => 0 });
265
    $dt = $today->clone->subtract( days => 3 );
266
    $recs[0]->dtcreated($dt)->store;
267
    $dt = $today->clone->subtract( days => 5 );
268
    $recs[1]->dtcreated($dt)->store;
269
    $recs[2]->dtcreated($dt)->store;
270
271
    # Now call delete_temporary with 0, 6, 5 and 1 (via override)
272
    t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 0 );
273
    Koha::UploadedFiles->delete_temporary;
274
    is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==0' );
275
276
    t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 6 );
277
    Koha::UploadedFiles->delete_temporary;
278
    is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
279
280
    t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 5 );
281
    Koha::UploadedFiles->delete_temporary;
282
    is( Koha::UploadedFiles->search->count, 4, 'Delete with pref==5 makes 4' );
283
284
    Koha::UploadedFiles->delete_temporary({ override_pref => 1 });
285
    is( Koha::UploadedFiles->search->count, 3, 'Delete override==1 makes 3' );
286
    is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
287
        'Still 3 permanent uploads' );
288
};
289
242
# The end
290
# The end
243
$schema->storage->txn_rollback;
291
$schema->storage->txn_rollback;
244
292
245
- 

Return to bug 17669