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

(-)a/Koha/Patrons.pm (-5 / +15 lines)
Lines 218-239 sub anonymise_issue_history { Link Here
218
    Includes a move to deletedborrowers if move flag set.
218
    Includes a move to deletedborrowers if move flag set.
219
219
220
    Just like DBIx, the delete will only succeed when all entries could be
220
    Just like DBIx, the delete will only succeed when all entries could be
221
    deleted. Returns true or throws an exception.
221
    deleted. Returns number of deletes or throws an exception. A number of
222
    zero deletes is represented as the true value '0E0'.
222
223
223
=cut
224
=cut
224
225
225
sub delete {
226
sub delete {
226
    my ( $self, $params ) = @_;
227
    my ( $self, $params ) = @_;
227
    my $patrons_deleted;
228
    my $patrons_deleted = 0;
228
    $self->_resultset->result_source->schema->txn_do( sub {
229
    $self->_resultset->result_source->schema->txn_do( sub {
229
        my ( $set, $params ) = @_;
230
        my ( $set, $params ) = @_;
230
        while( my $patron = $set->next ) {
231
        while( my $patron = $set->next ) {
231
            $patron->move_to_deleted if $params->{move};
232
            $patron->move_to_deleted if $params->{move};
232
            $patron->delete == 1 || Koha::Exceptions::Patron::FailedDelete->throw;
233
            my $delete = eval { $patron->delete };
233
            $patrons_deleted++;
234
            if( defined($delete) && $delete == 1 ) {
235
                $patrons_deleted++;
236
            } elsif( defined($delete) && $delete == -1 ) {
237
                next; # not in storage: no reason to stop
238
            } elsif( defined($delete) && $delete == 0 ) {
239
                Koha::Exceptions::Patron::FailedDelete->throw;
240
            } else { # an undef is a bit unclear, add exception to be safe
241
                Koha::Exceptions::Patron::FailedDelete->throw;
242
            }
234
        }
243
        }
235
    }, $self, $params );
244
    }, $self, $params );
236
    return $patrons_deleted;
245
    return if $@;
246
    return $patrons_deleted || '0E0';
237
}
247
}
238
248
239
=head3 _type
249
=head3 _type
(-)a/t/db_dependent/Koha/Patrons.t (-5 / +22 lines)
Lines 424-430 subtest "delete" => sub { Link Here
424
};
424
};
425
425
426
subtest 'Koha::Patrons->delete' => sub {
426
subtest 'Koha::Patrons->delete' => sub {
427
    plan tests => 4;
427
    plan tests => 6;
428
428
429
    my $mod_patron = Test::MockModule->new( 'Koha::Patron' );
429
    my $mod_patron = Test::MockModule->new( 'Koha::Patron' );
430
    my $moved_to_deleted = 0;
430
    my $moved_to_deleted = 0;
Lines 438-450 subtest 'Koha::Patrons->delete' => sub { Link Here
438
    is( $set->delete({ move => 1 }), 2, 'Two patrons deleted' );
438
    is( $set->delete({ move => 1 }), 2, 'Two patrons deleted' );
439
    is( $moved_to_deleted, 2, 'Patrons moved to deletedborrowers' );
439
    is( $moved_to_deleted, 2, 'Patrons moved to deletedborrowers' );
440
440
441
    # Add again, test if we can raise an exception
441
    # Check return value / exception, rollback
442
    $mod_patron->mock( 'delete', sub { return -1; } );
442
    my $mod_object = Test::MockModule->new( 'Koha::Object' );
443
    my $mock_ret = -1; # no rollback expected
444
    $mod_object->mock( 'delete', sub { return $mock_ret; } );
443
    $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
445
    $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
444
    $id1 = $patron1->borrowernumber;
446
    $id1 = $patron1->borrowernumber;
445
    $set = Koha::Patrons->search({ borrowernumber => { '>=' => $id1 }});
447
    $set = Koha::Patrons->search({ borrowernumber => { '>=' => $id1 }});
448
    is( $set->delete, '0E0', 'No exception for return value -1' );
449
450
    $mock_ret = 0; # this should trigger rollback/exception
451
    $set->_resultset->reset;
452
    throws_ok { $set->delete } 'Koha::Exceptions::Patron::FailedDelete',
453
        'Exception raised for deleting patron on return value 0';
454
455
    # We now trigger a FK error by deleting patron with issue
456
    $mod_object->unmock_all;
457
    my $issue = $builder->build_object({ class => 'Koha::Checkouts' });
458
    my $id2 = $issue->borrowernumber;
459
    $set->_resultset->reset;
446
    throws_ok { $set->delete } 'Koha::Exceptions::Patron::FailedDelete',
460
    throws_ok { $set->delete } 'Koha::Exceptions::Patron::FailedDelete',
447
        'Exception raised for deleting patron';
461
        'Exception raised for deleting patron with issue';
462
    # FIXME And here we should love to check the number of patrons,
463
    # BUT unfortunately we cannot. Why? Since Koha::Patrons' txn_do creates
464
    # a nested transaction, the rollback does nothing in a test.
465
    # So we should *TRUST* txn_do here..
448
};
466
};
449
467
450
subtest 'add_enrolment_fee_if_needed' => sub {
468
subtest 'add_enrolment_fee_if_needed' => sub {
451
- 

Return to bug 21337