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

(-)a/Koha/UploadedFiles.pm (-9 / +15 lines)
Lines 115-138 Deletes all records where the actual file is not found. Link Here
115
115
116
Supports a keep_record hash parameter to do a check only.
116
Supports a keep_record hash parameter to do a check only.
117
117
118
Returns the number of missing files (and/or deleted records).
118
Return value: If you pass keep_record, it returns the number of records where
119
the file is not found, or 0E0. Otherwise it returns a number, 0E0 or -1 just
120
as delete does.
119
121
120
=cut
122
=cut
121
123
122
sub delete_missing {
124
sub delete_missing {
123
    my ( $self, $params ) = @_;
125
    my ( $self, $params ) = @_;
124
    $self = Koha::UploadedFiles->new if !ref($self); # handle class call
126
    $self = Koha::UploadedFiles->new if !ref($self); # handle class call
125
    my $cnt = 0;
127
    my $rv = 0;
126
    while( my $row = $self->next ) {
128
    while( my $row = $self->next ) {
127
        if( my $file = $row->full_path ) {
129
        my $file = $row->full_path;
128
            next if -e $file;
130
        next if -e $file;
129
            # We are passing keep_file since we already know that the file
131
        if( $params->{keep_record} ) {
130
            # is missing and we do not want to see the warning
132
            $rv++;
131
            $row->delete({ keep_file => 1 }) if !$params->{keep_record};
133
            next;
132
            $cnt++;
133
        }
134
        }
135
        # We are passing keep_file since we already know that the file
136
        # is missing and we do not want to see the warning
137
        # Apply the same logic as in delete for the return value
138
        my $delete = $row->delete({ keep_file => 1 }); # 1, 0E0 or -1
139
        $rv = ( $delete < 0 || $rv < 0 ) ? -1 : ( $rv + $delete );
134
    }
140
    }
135
    return $cnt;
141
    return $rv==0 ? "0E0" : $rv;
136
}
142
}
137
143
138
=head3 search_term
144
=head3 search_term
(-)a/t/db_dependent/Upload.t (-4 / +6 lines)
Lines 193-212 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub { Link Here
193
};
193
};
194
194
195
subtest 'Test delete_missing' => sub {
195
subtest 'Test delete_missing' => sub {
196
    plan tests => 4;
196
    plan tests => 5;
197
197
198
    # If we add files via TestBuilder, they do not exist
198
    # If we add files via TestBuilder, they do not exist
199
    my $upload01 = $builder->build({ source => 'UploadedFile' });
199
    my $upload01 = $builder->build({ source => 'UploadedFile' });
200
    my $upload02 = $builder->build({ source => 'UploadedFile' });
200
    my $upload02 = $builder->build({ source => 'UploadedFile' });
201
    # dry run first
201
    # dry run first
202
    my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
202
    my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
203
    is( $deleted, 2, 'Expect two missing files' );
203
    is( $deleted, 2, 'Expect two records with missing files' );
204
    isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
204
    isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
205
    $deleted = Koha::UploadedFiles->delete_missing;
205
    $deleted = Koha::UploadedFiles->delete_missing;
206
    is( $deleted, 2, 'Deleted two missing files' );
206
    ok( $deleted =~ /^(2|-1)$/, 'Deleted two records with missing files' );
207
    is( Koha::UploadedFiles->search({
207
    is( Koha::UploadedFiles->search({
208
        id => [ $upload01->{id}, $upload02->{id} ],
208
        id => [ $upload01->{id}, $upload02->{id} ],
209
    })->count, 0, 'Records are gone' );
209
    })->count, 0, 'Records are gone' );
210
    # Repeat it
211
    $deleted = Koha::UploadedFiles->delete_missing;
212
    is( $deleted, "0E0", "Return value of 0E0 expected" );
210
};
213
};
211
214
212
subtest 'Call search_term with[out] private flag' => sub {
215
subtest 'Call search_term with[out] private flag' => sub {
213
- 

Return to bug 18300