From d24747b92cb34555e057d543f546be5873017568 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Tue, 26 Nov 2019 11:11:35 +0100
Subject: [PATCH] Bug 24114: Remove warn statements from Koha::Patrons
Content-Type: text/plain; charset=utf-8

The warn must be done in the cronjob.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Followed this test plan (with two follow-ups applied):
[1] Prefs: UnsubscribeReflectionDelay=1, PatronAnonymizeDelay=2, PatronRemovalDelay=3, FailedLoginAttempts was undef
[2] Pick borrower and set expiry to NOW-2, and lock him (login_attempts=-1) Could be achieved too by settings FailedLoginAttempts and trying wrong passwords. Run cleanup job:
    Locked 0 patrons
    Anonymized 1 patrons
    Deleted 0 patrons
[3] Pick borrower, set expiry to NOW-3. Run cleanup job:
    Locked 0 patrons
    Anonymized 0 patrons
    Deleted 1 patrons
---
 Koha/Patrons.pm                   | 17 ++++-------------
 misc/cronjobs/cleanup_database.pl | 20 ++++++++++++++++----
 2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm
index fbe696ec7a..e67bb9f165 100644
--- a/Koha/Patrons.pm
+++ b/Koha/Patrons.pm
@@ -202,7 +202,7 @@ sub anonymise_issue_history {
 
 =head3 delete
 
-    Koha::Patrons->search({ some filters here })->delete({ move => 1, verbose => 1 });
+    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
 
     Delete passed set of patron objects.
     Wrapper for Koha::Patron->delete. (We do not want to bypass Koha::Patron
@@ -225,7 +225,6 @@ sub delete {
             $patron->delete == 1 || Koha::Exceptions::Patron::FailedDelete->throw;
             $patrons_deleted++;
         }
-        warn "Deleted $count patrons\n" if $params->{verbose};
     }, $self, $params );
     return $patrons_deleted;
 }
@@ -322,10 +321,9 @@ sub search_anonymized {
 
 =head3 lock
 
-    Koha::Patrons->search({ some filters })->lock({ expire => 1, remove => 1, verbose => 1 })
+    Koha::Patrons->search({ some filters })->lock({ expire => 1, remove => 1 })
 
     Lock the passed set of patron objects. Optionally expire and remove holds.
-    Optional verbose flag is used in cron job.
     Wrapper around Koha::Patron->lock.
 
 =cut
@@ -336,30 +334,23 @@ sub lock {
     while( my $patron = $self->next ) {
         $patron->lock($params);
     }
-    if( $params->{verbose} ) {
-        warn "Locked $count patrons\n";
-    }
 }
 
 =head3 anonymize
 
-    Koha::Patrons->search({ some filters })->anonymize({ verbose => 1 });
+    Koha::Patrons->search({ some filters })->anonymize();
 
     Anonymize passed set of patron objects.
-    Optional verbose flag is used in cron job.
     Wrapper around Koha::Patron->anonymize.
 
 =cut
 
 sub anonymize {
-    my ( $self, $params ) = @_;
+    my ( $self ) = @_;
     my $count = $self->count;
     while( my $patron = $self->next ) {
         $patron->anonymize;
     }
-    if( $params->{verbose} ) {
-        warn "Anonymized $count patrons\n";
-    }
 }
 
 =head3 search_patrons_to_update_category
diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl
index 975aaabd2a..964214122b 100755
--- a/misc/cronjobs/cleanup_database.pl
+++ b/misc/cronjobs/cleanup_database.pl
@@ -306,12 +306,24 @@ if($allDebarments) {
 }
 
 # Handle unsubscribe requests from GDPR consent form, depends on UnsubscribeReflectionDelay preference
-Koha::Patrons->search_unsubscribed->lock({ expire => 1, remove => 1, verbose => $verbose });
+my $unsubscribed_patrons = Koha::Patrons->search_unsubscribed;
+$unsubscribed_patrons->lock( { expire => 1, remove => 1 } );
+say sprintf "Locked %d patrons", $unsubscribed_patrons->count if $verbose;
+
 # Anonymize patron data, depending on PatronAnonymizeDelay
-Koha::Patrons->search_anonymize_candidates({ locked => 1 })->anonymize({ verbose => $verbose });
+my $anonymize_candidates = Koha::Patrons->search_anonymize_candidates( { locked => 1 } );
+$anonymize_candidates->anonymize;
+say sprintf "Anonymized %s patrons", $anonymize_candidates->count if $verbose;
+
 # Remove patron data, depending on PatronRemovalDelay (will raise an exception if problem encountered
-eval { Koha::Patrons->search_anonymized->delete({ move => 1, verbose => $verbose }) };
-warn $@ if $@;
+my $anonymized_patrons = Koha::Patrons->search_anonymized;
+$anonymized_patrons->delete( { move => 1 } );
+if ($@) {
+    warn $@;
+}
+elsif ($verbose) {
+    say sprintf "Deleted %d patrons", $anonymized_patrons->count;
+}
 
 if( $pExpSelfReg ) {
     DeleteExpiredSelfRegs();
-- 
2.11.0