From 47268dda626d166c40d8dcaf779fe6e47564a34f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 12 Jan 2022 16:32:24 -0300 Subject: [PATCH] Bug 29525: Make Koha::Hold->cancel anonymize if required --- Koha/Hold.pm | 8 +++++- t/db_dependent/Koha/Hold.t | 56 +++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Koha/Hold.pm b/Koha/Hold.pm index 9736dc527f..51bba2c297 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -511,6 +511,8 @@ sub cancel { my ( $self, $params ) = @_; $self->_result->result_source->schema->txn_do( sub { + my $patron = $self->patron; + $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) ); $self->priority(0); $self->cancellation_reason( $params->{cancellation_reason} ); @@ -544,7 +546,11 @@ sub cancel { } } - $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 5f6a9016c8..66bfcc3c39 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -19,14 +19,16 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Exception; use Test::MockModule; +use t::lib::Mocks; use t::lib::TestBuilder; use Koha::Libraries; +use Koha::Old::Holds; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -202,3 +204,55 @@ subtest 'is_pickup_location_valid() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'cancel() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + # reduce the tests noise + t::lib::Mocks::mock_preference( 'HoldsLog', 0 ); + t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge', + undef ); + + # 0 == keep forever + $patron->privacy(0)->store; + my $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { borrowernumber => $patron->id, status => undef } + } + ); + $hold->cancel(); + 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->cancel(); + 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->cancel(); + is( Koha::Old::Holds->find( $hold->id )->borrowernumber, + undef, 'Patron link is deleted immediately' ); + + $schema->storage->txn_rollback; +}; -- 2.32.0