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

(-)a/Koha/UploadedFiles.pm (+26 lines)
Lines 19-24 package Koha::UploadedFiles; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Koha::Database;
23
use Koha::DateUtils;
22
use Koha::UploadedFile;
24
use Koha::UploadedFile;
23
25
24
use parent qw(Koha::Objects);
26
use parent qw(Koha::Objects);
Lines 78-83 sub delete_errors { Link Here
78
    return $self->{delete_errors};
80
    return $self->{delete_errors};
79
}
81
}
80
82
83
=head3 delete_temporary
84
85
Delete_temporary is called by cleanup_database and only removes temporary
86
uploads older than [pref Upload_PurgeTemporaryFiles_Days] days.
87
It is possible to override the pref with the override_pref parameter.
88
89
Returns true if no errors occur. (Even when no files had to be deleted.)
90
91
=cut
92
93
sub delete_temporary {
94
    my ( $self, $params ) = @_;
95
    my $days = $params->{override_pref} ||
96
        C4::Context->preference('Upload_PurgeTemporaryFiles_Days');
97
    return 1 if !$days;
98
    my $dt = dt_from_string();
99
    $dt->subtract( days => $days );
100
    my $parser = Koha::Database->new->schema->storage->datetime_parser;
101
    return $self->search({
102
        permanent => [ undef, 0 ],
103
        dtcreated => { '<' => $parser->format_datetime($dt) },
104
    })->delete;
105
}
106
81
=head3 search_term
107
=head3 search_term
82
108
83
Search_term allows you to pass a term to search in filename and hashvalue.
109
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 240-245 subtest 'Testing allows_add_by' => sub { Link Here
240
        1, 'Patron is still allowed to add uploaded files' );
245
        1, 'Patron is still allowed to add uploaded files' );
241
};
246
};
242
247
248
subtest 'Testing delete_temporary' => sub {
249
    plan tests => 7;
250
251
    # Add two temporary files: result should be 3 + 3
252
    Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7
253
    is( Koha::UploadedFiles->search->count, 6, 'Test starting count' );
254
    is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
255
        'Includes 3 permanent' );
256
257
    # Move all permanents to today - 1
258
    # Move temp 1 to today - 3, and temp 2,3 to today - 5
259
    my $today = dt_from_string;
260
    $today->subtract( minutes => 2 ); # should be enough :)
261
    my $dt = $today->clone->subtract( days => 1 );
262
    foreach my $rec ( Koha::UploadedFiles->search({ permanent => 1 }) ) {
263
        $rec->dtcreated($dt)->store;
264
    }
265
    my @recs = Koha::UploadedFiles->search({ permanent => 0 });
266
    $dt = $today->clone->subtract( days => 3 );
267
    $recs[0]->dtcreated($dt)->store;
268
    $dt = $today->clone->subtract( days => 5 );
269
    $recs[1]->dtcreated($dt)->store;
270
    $recs[2]->dtcreated($dt)->store;
271
272
    # Now call delete_temporary with 0, 6, 5 and 1 (via override)
273
    t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 0 );
274
    Koha::UploadedFiles->delete_temporary;
275
    is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==0' );
276
277
    t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 6 );
278
    Koha::UploadedFiles->delete_temporary;
279
    is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
280
281
    t::lib::Mocks::mock_preference('Upload_PurgeTemporaryFiles_Days', 5 );
282
    Koha::UploadedFiles->delete_temporary;
283
    is( Koha::UploadedFiles->search->count, 4, 'Delete with pref==5 makes 4' );
284
285
    Koha::UploadedFiles->delete_temporary({ override_pref => 1 });
286
    is( Koha::UploadedFiles->search->count, 3, 'Delete override==1 makes 3' );
287
    is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
288
        'Still 3 permanent uploads' );
289
};
290
243
# The end
291
# The end
244
$schema->storage->txn_rollback;
292
$schema->storage->txn_rollback;
245
293
246
- 

Return to bug 17669