From c3128ec8a5d2a048bf5599200649741af3cf4333 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 --- Koha/Patrons.pm | 30 ++++++++++++++++++++++++++++++ t/db_dependent/Koha/Patrons.t | 15 ++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 0d2d9ec..713ad6c 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 6521db1..81b0da2 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 32; +use Test::More tests => 33; use Test::Warn; use Test::Exception; use Time::Fake; @@ -421,6 +421,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.1.4