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

(-)a/Koha/UploadedFile.pm (-2 / +11 lines)
Lines 61-67 Delete uploaded file. Link Here
61
It deletes not only the record, but also the actual file (unless you pass
61
It deletes not only the record, but also the actual file (unless you pass
62
the keep_file parameter).
62
the keep_file parameter).
63
63
64
Returns filename on successful delete or undef.
64
Returns number of deleted records (1 or 0E0), or -1 for unknown.
65
Please keep in mind that a deleted record does not automatically imply a
66
deleted file; a warning may have been raised.
67
(TODO: Use exceptions.)
65
68
66
=cut
69
=cut
67
70
Lines 72-84 sub delete { Link Here
72
    my $file = $self->full_path;
75
    my $file = $self->full_path;
73
76
74
    my $retval = $self->SUPER::delete;
77
    my $retval = $self->SUPER::delete;
78
    if( !defined($retval) ) { # undef is Unknown (-1)
79
        $retval = -1;
80
    } elsif( $retval eq '0' ) { # 0 => 0E0
81
        $retval = "0E0";
82
    } elsif( $retval !~ /^(0E0|1)$/ ) { # Unknown too
83
        $retval = -1;
84
    }
75
    return $retval if $params->{keep_file};
85
    return $retval if $params->{keep_file};
76
86
77
    if( ! -e $file ) {
87
    if( ! -e $file ) {
78
        warn "Removing record for $name within category ".
88
        warn "Removing record for $name within category ".
79
            $self->uploadcategorycode. ", but file was missing.";
89
            $self->uploadcategorycode. ", but file was missing.";
80
    } elsif( ! unlink($file) ) {
90
    } elsif( ! unlink($file) ) {
81
        $retval = 0;
82
        warn "Problem while deleting: $file";
91
        warn "Problem while deleting: $file";
83
    }
92
    }
84
    return $retval;
93
    return $retval;
(-)a/Koha/UploadedFiles.pm (-6 / +10 lines)
Lines 57-77 provides a wrapper around search to look for a term in multiple columns. Link Here
57
=head3 delete
57
=head3 delete
58
58
59
Delete uploaded files.
59
Delete uploaded files.
60
Returns true if no errors occur. (So, false may mean partial success.)
61
60
62
Parameter keep_file may be used to delete records, but keep files.
61
Parameter keep_file may be used to delete records, but keep files.
63
62
63
Returns the number of deleted records, 0E0 or -1 (Unknown).
64
Please note that the number of deleted records is not automatically the same
65
as the number of files.
66
64
=cut
67
=cut
65
68
66
sub delete {
69
sub delete {
67
    my ( $self, $params ) = @_;
70
    my ( $self, $params ) = @_;
68
    $self = Koha::UploadedFiles->new if !ref($self); # handle class call
71
    $self = Koha::UploadedFiles->new if !ref($self); # handle class call
69
    # We use the individual delete on each resultset record
72
    # We use the individual delete on each resultset record
70
    my $err = 0;
73
    my $rv = 0;
71
    while( my $row = $self->next ) {
74
    while( my $row = $self->next ) {
72
        $err++ if !$row->delete( $params );
75
        my $delete= $row->delete( $params ); # 1, 0E0 or -1
76
        $rv = ( $delete < 0 || $rv < 0 ) ? -1 : ( $rv + $delete );
73
    }
77
    }
74
    return $err==0;
78
    return $rv==0 ? "0E0" : $rv;
75
}
79
}
76
80
77
=head3 delete_temporary
81
=head3 delete_temporary
Lines 80-86 Delete_temporary is called by cleanup_database and only removes temporary Link Here
80
uploads older than [pref UploadPurgeTemporaryFilesDays] days.
84
uploads older than [pref UploadPurgeTemporaryFilesDays] days.
81
It is possible to override the pref with the override_pref parameter.
85
It is possible to override the pref with the override_pref parameter.
82
86
83
Returns true if no errors occur. (Even when no files had to be deleted.)
87
Return value: see delete.
84
88
85
=cut
89
=cut
86
90
Lines 90-96 sub delete_temporary { Link Here
90
    if( exists $params->{override_pref} ) {
94
    if( exists $params->{override_pref} ) {
91
        $days = $params->{override_pref};
95
        $days = $params->{override_pref};
92
    } elsif( !defined($days) || $days eq '' ) { # allow 0, not NULL or ""
96
    } elsif( !defined($days) || $days eq '' ) { # allow 0, not NULL or ""
93
        return 1;
97
        return "0E0";
94
    }
98
    }
95
    my $dt = dt_from_string();
99
    my $dt = dt_from_string();
96
    $dt->subtract( days => $days );
100
    $dt->subtract( days => $days );
(-)a/t/db_dependent/Upload.t (-9 / +20 lines)
Lines 149-155 subtest 'Add same file in same category' => sub { Link Here
149
};
149
};
150
150
151
subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
151
subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
152
    plan tests => 8;
152
    plan tests => 10;
153
153
154
    # add temporary file with same name and contents (file4)
154
    # add temporary file with same name and contents (file4)
155
    my $upl = Koha::Uploader->new({ tmp => 1 });
155
    my $upl = Koha::Uploader->new({ tmp => 1 });
Lines 160-166 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { Link Here
160
160
161
    # testing delete via UploadedFiles (plural)
161
    # testing delete via UploadedFiles (plural)
162
    my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
162
    my $delete = Koha::UploadedFiles->search({ id => $id })->delete;
163
    is( $delete, 1, 'Delete successful' );
163
    isnt( $delete, "0E0", 'Delete successful' );
164
    isnt( -e $path, 1, 'File no longer found after delete' );
164
    isnt( -e $path, 1, 'File no longer found after delete' );
165
    is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
165
    is( Koha::UploadedFiles->find( $id ), undef, 'Record also gone' );
166
166
Lines 171-186 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { Link Here
171
    my $kohaobj = Koha::UploadedFiles->find( $upl->result );
171
    my $kohaobj = Koha::UploadedFiles->find( $upl->result );
172
    $path = $kohaobj->full_path;
172
    $path = $kohaobj->full_path;
173
    $delete = $kohaobj->delete;
173
    $delete = $kohaobj->delete;
174
    is( $delete, 1, 'Delete successful' );
174
    ok( $delete=~/^-?1$/, 'Delete successful' );
175
    isnt( -e $path, 1, 'File no longer found after delete' );
175
    isnt( -e $path, 1, 'File no longer found after delete' );
176
176
177
    # add another record with TestBuilder, so file does not exist
177
    # add another record with TestBuilder, so file does not exist
178
    # catch warning
178
    # catch warning
179
    my $upload01 = $builder->build({ source => 'UploadedFile' });
179
    my $upload01 = $builder->build({ source => 'UploadedFile' });
180
    warning_like { Koha::UploadedFiles->find( $upload01->{id} )->delete; }
180
    warning_like { $delete = Koha::UploadedFiles->find( $upload01->{id} )->delete; }
181
        qr/file was missing/,
181
        qr/file was missing/,
182
        'delete warns when file is missing';
182
        'delete warns when file is missing';
183
    ok( $delete=~/^-?1$/, 'Deleting record was successful' );
183
    is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
184
    is( Koha::UploadedFiles->count, 4, 'Back to four uploads now' );
185
186
    # add another one with TestBuilder and delete twice (file does not exist)
187
    $upload01 = $builder->build({ source => 'UploadedFile' });
188
    my $kohaobj = Koha::UploadedFiles->find( $upload01->{id} );
189
    $delete = $kohaobj->delete({ keep_file => 1 });
190
    $delete = $kohaobj->delete({ keep_file => 1 });
191
    ok( $delete =~ /^(0E0|-1)$/, 'Repeated delete unsuccessful' );
192
    # NOTE: Koha::Object->delete does not return 0E0 (yet?)
184
};
193
};
185
194
186
subtest 'Call search_term with[out] private flag' => sub {
195
subtest 'Call search_term with[out] private flag' => sub {
Lines 245-251 subtest 'Testing allows_add_by' => sub { Link Here
245
};
254
};
246
255
247
subtest 'Testing delete_temporary' => sub {
256
subtest 'Testing delete_temporary' => sub {
248
    plan tests => 6;
257
    plan tests => 9;
249
258
250
    # Add two temporary files: result should be 3 + 3
259
    # Add two temporary files: result should be 3 + 3
251
    Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7
260
    Koha::Uploader->new({ tmp => 1 })->cgi; # add file6 and file7
Lines 270-284 subtest 'Testing delete_temporary' => sub { Link Here
270
279
271
    # Now call delete_temporary with 6, 5 and 0
280
    # Now call delete_temporary with 6, 5 and 0
272
    t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 6 );
281
    t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 6 );
273
    Koha::UploadedFiles->delete_temporary;
282
    my $delete = Koha::UploadedFiles->delete_temporary;
283
    ok( $delete =~ /^(-1|0E0)$/, 'Check return value with 6' );
274
    is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
284
    is( Koha::UploadedFiles->search->count, 6, 'Delete with pref==6' );
275
285
276
    # use override parameter
286
    # use override parameter
277
    Koha::UploadedFiles->delete_temporary({ override_pref => 5 });
287
    $delete = Koha::UploadedFiles->delete_temporary({ override_pref => 5 });
288
    ok( $delete =~ /^(2|-1)$/, 'Check return value with 5' );
278
    is( Koha::UploadedFiles->search->count, 4, 'Delete with override==5' );
289
    is( Koha::UploadedFiles->search->count, 4, 'Delete with override==5' );
279
290
280
    t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 0 );
291
    t::lib::Mocks::mock_preference('UploadPurgeTemporaryFilesDays', 0 );
281
    Koha::UploadedFiles->delete_temporary;
292
    $delete = Koha::UploadedFiles->delete_temporary;
293
    ok( $delete =~ /^(-1|1)$/, 'Check return value with 0' );
282
    is( Koha::UploadedFiles->search->count, 3, 'Delete with pref==0 makes 3' );
294
    is( Koha::UploadedFiles->search->count, 3, 'Delete with pref==0 makes 3' );
283
    is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
295
    is( Koha::UploadedFiles->search({ permanent => 1 })->count, 3,
284
        'Still 3 permanent uploads' );
296
        'Still 3 permanent uploads' );
285
- 

Return to bug 17669