From e652f47cedfdf069b054b4f623ec5674640555ec Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 12 Sep 2018 15:49:45 +0200 Subject: [PATCH] Bug 21337: Add Koha::Patrons->delete (trivial wrapper) Adds Koha::Patrons->delete as wrapper around Koha::Patron->delete. We do not want to bypass Koha::Patron and let DBIx do the job without further housekeeping. A call to move_to_deleted is included now, but could be made optional with a parameter if needed. Test plan: Run t/db_dependent/Koha/Patrons.t Signed-off-by: Marcel de Rooy Signed-off-by: Michal Denar Signed-off-by: Michal Denar Signed-off-by: Josef Moravec Signed-off-by: Nick Clemens --- Koha/Patrons.pm | 30 ++++++++++++++++++++++++++++++ t/db_dependent/Koha/Patrons.t | 13 +++++++++++++ 2 files changed, 43 insertions(+) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 0d2d9ec6b4..713ad6c312 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -207,6 +207,36 @@ sub anonymise_issue_history { return $nb_rows; } +=head3 delete + + Koha::Patrons->search({ some filters here })->delete; + + Delete passed set of patron objects. + Wrapper for Koha::Patron->delete. (We do not want to bypass Koha::Patron + and let DBIx do the job without further housekeeping.) + NOTE: By default includes a move to deletedborrowers. + + Return value (if relevant) is based on the individual return values. + +=cut + +sub delete { + my ( $self ) = @_; + my (@res, $rv); + $rv = 1; + while( my $patron = $self->next ) { + $patron->move_to_deleted; # Needed here, since it is no default action.. + push @res, $patron->delete; + $rv=-1 if $res[-1]==-1; + $rv=0 if $rv==1 && $res[-1]==0; + } + + # Return -1 if we encountered a single -1 + # Return 0 if we encountered a single 0 (but not a -1) + # Return 1 if all individual deletes passed + return $rv; +} + =head3 _type =cut diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 61e552c31c..3f51e2321e 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -422,6 +422,19 @@ subtest "delete" => sub { is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' ); }; +subtest 'Koha::Patrons->delete' => sub { + plan tests => 3; + my $patron1 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron2 = $builder->build_object({ class => 'Koha::Patrons' }); + my $id1 = $patron1->borrowernumber; + my $set = Koha::Patrons->search({ borrowernumber => { '>=' => $id1 }}); + is( $set->count, 2, 'Two patrons found as expected' ); + my $count1 = $schema->resultset('Deletedborrower')->count; + is( $set->delete, 1, 'Two patrons deleted' ); + my $count2 = $schema->resultset('Deletedborrower')->count; + is( $count2, $count1 + 2, 'Patrons moved to deletedborrowers' ); +}; + subtest 'add_enrolment_fee_if_needed' => sub { plan tests => 4; -- 2.39.5