From d78656f17eec9c6ee1ffd2f3b55ede18b9b06a50 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 13 Jan 2022 14:40:04 -0300
Subject: [PATCH] Bug 29525: Make Koha::Hold->fill anonymize if required

This patch makes filling a hold anonymize it on the same call, if
settings require it (i.e. if borrowers.privacy is set to 2).

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Hold.t
=> SUCCESS: The code actually does what it is meant to
3. Try on the UI, notice it gets anonymized if the patron has privacy == always/2.
4. Sign off :-D

Note: AnonymousPatron should be set. Otherwise it would set NULL. But
that's fine, that's what Koha does already.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 Koha/Hold.pm               |  6 +++-
 t/db_dependent/Koha/Hold.t | 63 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/Koha/Hold.pm b/Koha/Hold.pm
index 405dd0978f..e0fbc7af36 100644
--- a/Koha/Hold.pm
+++ b/Koha/Hold.pm
@@ -607,7 +607,11 @@ sub fill {
                 }
             );
 
-            $self->_move_to_old;
+            my $old_me = $self->_move_to_old;
+            # anonymize if required
+            $old_me->anonymize
+                if $patron->privacy == 2;
+
             $self->SUPER::delete(); # Do not add a DELETE log
 
             # now fix the priority on the others....
diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t
index 51626272b6..33290ac205 100755
--- a/t/db_dependent/Koha/Hold.t
+++ b/t/db_dependent/Koha/Hold.t
@@ -38,7 +38,7 @@ my $builder = t::lib::TestBuilder->new;
 
 subtest 'fill() tests' => sub {
 
-    plan tests => 11;
+    plan tests => 12;
 
     $schema->storage->txn_begin;
 
@@ -164,6 +164,67 @@ subtest 'fill() tests' => sub {
 
     is( $logs->count, 0, 'HoldsLog disabled, no logs added' );
 
+    subtest 'anonymization behavior tests' => sub {
+
+        plan tests => 4;
+
+        # reduce the tests noise
+        t::lib::Mocks::mock_preference( 'HoldsLog',    0 );
+        t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
+
+        # 0 == keep forever
+        $patron->privacy(0)->store;
+        my $hold = $builder->build_object(
+            {
+                class => 'Koha::Holds',
+                value => { borrowernumber => $patron->id, status => undef }
+            }
+        );
+        $hold->fill();
+        is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
+            $patron->borrowernumber, 'Patron link is kept' );
+
+        # 1 == "default", meaning it is not protected from removal
+        $patron->privacy(1)->store;
+        $hold = $builder->build_object(
+            {
+                class => 'Koha::Holds',
+                value => { borrowernumber => $patron->id, status => undef }
+            }
+        );
+        $hold->fill();
+        is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
+            $patron->borrowernumber, 'Patron link is kept' );
+
+        # 2 == delete immediately
+        $patron->privacy(2)->store;
+        $hold = $builder->build_object(
+            {
+                class => 'Koha::Holds',
+                value => { borrowernumber => $patron->id, status => undef }
+            }
+        );
+        $hold->fill();
+        is( Koha::Old::Holds->find( $hold->id )->borrowernumber,
+            undef, 'Patron link is deleted immediately' );
+
+        my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
+        t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
+
+        $hold = $builder->build_object(
+            {
+                class => 'Koha::Holds',
+                value => { borrowernumber => $patron->id, status => undef }
+            }
+        );
+        $hold->cancel();
+        is(
+            Koha::Old::Holds->find( $hold->id )->borrowernumber,
+            $anonymous_patron->id,
+            'Patron link is set to the configured anonymous patron immediately'
+        );
+    };
+
     $schema->storage->txn_rollback;
 };
 
-- 
2.20.1