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

(-)a/Koha/Exceptions/Patron.pm (+14 lines)
Line 0 Link Here
1
package Koha::Exceptions::Patron;
2
3
use Modern::Perl;
4
5
use Exception::Class (
6
    'Koha::Exceptions::Patron' => {
7
        description => "Something went wrong!"
8
    },
9
    'Koha::Exceptions::Patron::Delete' => {
10
        description => "Deleting patron failed"
11
    },
12
);
13
14
1;
(-)a/Koha/Patrons.pm (-14 / +11 lines)
Lines 28-33 use Koha::DateUtils; Link Here
28
use Koha::ArticleRequests;
28
use Koha::ArticleRequests;
29
use Koha::ArticleRequest::Status;
29
use Koha::ArticleRequest::Status;
30
use Koha::Patron;
30
use Koha::Patron;
31
use Koha::Exceptions::Patron;
31
32
32
use base qw(Koha::Objects);
33
use base qw(Koha::Objects);
33
34
Lines 216-240 sub anonymise_issue_history { Link Here
216
    and let DBIx do the job without further housekeeping.)
217
    and let DBIx do the job without further housekeeping.)
217
    Includes a move to deletedborrowers if move flag set.
218
    Includes a move to deletedborrowers if move flag set.
218
219
219
    Return value (if relevant) is based on the individual return values.
220
    Just like DBIx, the delete will only succeed when all entries could be
221
    deleted. Returns true or throws an exception.
220
222
221
=cut
223
=cut
222
224
223
sub delete {
225
sub delete {
224
    my ( $self, $params ) = @_;
226
    my ( $self, $params ) = @_;
225
    my (@res, $rv);
227
    $self->_resultset->result_source->schema->txn_do( sub {
226
    $rv = 1;
228
        my ( $set, $params ) = @_;
227
    while( my $patron = $self->next ) {
229
        while( my $patron = $set->next ) {
228
        $patron->move_to_deleted if $params->{move};
230
            $patron->move_to_deleted if $params->{move};
229
        push @res, $patron->delete;
231
            $patron->delete == 1 || Koha::Exceptions::Patron::Delete->throw;
230
        $rv=-1 if $res[-1]==-1;
232
        }
231
        $rv=0 if $rv==1 && $res[-1]==0;
233
    }, $self, $params );
232
    }
234
    return 1;
233
234
    # Return -1 if we encountered a single -1
235
    # Return 0 if we encountered a single 0 (but not a -1)
236
    # Return 1 if all individual deletes passed
237
    return $rv;
238
}
235
}
239
236
240
=head3 _type
237
=head3 _type
(-)a/t/db_dependent/Koha/Patrons.t (-5 / +16 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Test::More tests => 33;
22
use Test::More tests => 33;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Time::Fake;
26
use Time::Fake;
26
use DateTime;
27
use DateTime;
27
use JSON;
28
use JSON;
Lines 422-437 subtest "delete" => sub { Link Here
422
};
423
};
423
424
424
subtest 'Koha::Patrons->delete' => sub {
425
subtest 'Koha::Patrons->delete' => sub {
425
    plan tests => 3;
426
    plan tests => 4;
427
428
    my $mod_patron = Test::MockModule->new( 'Koha::Patron' );
429
    my $moved_to_deleted = 0;
430
    $mod_patron->mock( 'move_to_deleted', sub { $moved_to_deleted++; } );
431
426
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
432
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
427
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
433
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
428
    my $id1 = $patron1->borrowernumber;
434
    my $id1 = $patron1->borrowernumber;
429
    my $set = Koha::Patrons->search({ borrowernumber => { '>=' => $id1 }});
435
    my $set = Koha::Patrons->search({ borrowernumber => { '>=' => $id1 }});
430
    is( $set->count, 2, 'Two patrons found as expected' );
436
    is( $set->count, 2, 'Two patrons found as expected' );
431
    my $count1 = $schema->resultset('Deletedborrower')->count;
432
    is( $set->delete({ move => 1 }), 1, 'Two patrons deleted' );
437
    is( $set->delete({ move => 1 }), 1, 'Two patrons deleted' );
433
    my $count2 = $schema->resultset('Deletedborrower')->count;
438
    is( $moved_to_deleted, 2, 'Patrons moved to deletedborrowers' );
434
    is( $count2, $count1 + 2, 'Patrons moved to deletedborrowers' );
439
440
    # Add again, test if we can raise an exception
441
    $mod_patron->mock( 'delete', sub { return -1; } );
442
    $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
443
    $id1 = $patron1->borrowernumber;
444
    $set = Koha::Patrons->search({ borrowernumber => { '>=' => $id1 }});
445
    throws_ok { $set->delete } 'Koha::Exceptions::Patron::Delete',
446
        'Exception raised for deleting patron';
435
};
447
};
436
448
437
subtest 'add_enrolment_fee_if_needed' => sub {
449
subtest 'add_enrolment_fee_if_needed' => sub {
438
- 

Return to bug 21337