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

(-)a/Koha/Patrons.pm (-5 / +12 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 = $patron->delete;
233
            $patrons_deleted++;
234
            # throw exception on zero only: if delete threw an exception, we do not come here and -1 is no reason to stop
235
            if( defined($delete) && $delete == 0 ) {
236
                Koha::Exceptions::Patron::FailedDelete->throw;
237
            } elsif( defined($delete) && $delete == 1 ) {
238
                $patrons_deleted++;
239
            }
234
        }
240
        }
235
    }, $self, $params );
241
    }, $self, $params );
236
    return $patrons_deleted;
242
    return if $@;
243
    return $patrons_deleted || '0E0';
237
}
244
}
238
245
239
=head3 _type
246
=head3 _type
(-)a/t/db_dependent/Koha/Patrons.t (-4 / +13 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-448 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 / 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', 'Try -1: no rollback' );
449
450
    undef $mock_ret; # still no rollback expected
451
    $set->_resultset->reset; # needed for reiterate
452
    is( $set->delete, '0E0', 'Try undef: no rollback' );
453
454
    $mock_ret = 0; # this should trigger rollback/exception
455
    $set->_resultset->reset;
446
    throws_ok { $set->delete } 'Koha::Exceptions::Patron::FailedDelete',
456
    throws_ok { $set->delete } 'Koha::Exceptions::Patron::FailedDelete',
447
        'Exception raised for deleting patron';
457
        'Exception raised for deleting patron';
448
};
458
};
449
- 

Return to bug 21337