From 0591a0f1f2a5ded9ff447b875a9dc8e30d26d9eb Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 12 Jan 2022 16:24:35 -0300 Subject: [PATCH] Bug 29868: Add Koha::Old::Hold->anonymize This patch introduces a new method in Koha::Old::Hold. The method is fully covered by tests. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Old/Hold.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Andrew Fuerste-Henry --- Koha/Old/Hold.pm | 21 ++++++++- t/db_dependent/Koha/Hold.t | 2 +- t/db_dependent/Koha/Old/Hold.t | 80 ++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100755 t/db_dependent/Koha/Old/Hold.t diff --git a/Koha/Old/Hold.pm b/Koha/Old/Hold.pm index 0701e64fd4..33642b33f6 100644 --- a/Koha/Old/Hold.pm +++ b/Koha/Old/Hold.pm @@ -21,6 +21,8 @@ use Modern::Perl; use base qw(Koha::Hold); +use C4::Context; + =head1 NAME Koha::Old::Hold - Koha Old Hold object class @@ -29,12 +31,27 @@ This object represents a hold that has been filled or canceled =head1 API -=head2 Class Methods +=head2 Class methods + +=head3 anonymize + + $old_hold->anonymize(); + +Anonymize the given I object. =cut +sub anonymize { + my ($self) = @_; + + my $anonymous_id = C4::Context->preference('AnonymousPatron') || undef; + + return $self->update( { borrowernumber => $anonymous_id } ); +} + +=head2 Internal methods -=head3 type +=head3 _type =cut diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t index 01901f6cb9..c592d0e73e 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; use Test::Exception; use Test::MockModule; diff --git a/t/db_dependent/Koha/Old/Hold.t b/t/db_dependent/Koha/Old/Hold.t new file mode 100755 index 0000000000..b660ec4914 --- /dev/null +++ b/t/db_dependent/Koha/Old/Hold.t @@ -0,0 +1,80 @@ +#!/usr/bin/perl + +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; + +use Koha::Database; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'anonymize() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + is( $patron->old_holds->count, 0, 'Patron has no old holds' ); + + my $hold_1 = $builder->build_object( + { + class => 'Koha::Old::Holds', + value => { borrowernumber => $patron->id } + } + ); + my $hold_2 = $builder->build_object( + { + class => 'Koha::Old::Holds', + value => { borrowernumber => $patron->id } + } + ); + + is( $patron->old_holds->count, 2, 'Patron has 2 completed holds' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', undef ); + + $hold_1->anonymize; + + is( $patron->old_holds->count, 1, 'Patron has 1 linked completed holds' ); + + # Reload + $hold_1->discard_changes; + $hold_2->discard_changes; + is( $hold_1->borrowernumber, undef, + 'Anonymized hold not linked to patron' ); + is( $hold_2->borrowernumber, $patron->id, + 'Not anonymized hold still linked to patron' ); + + my $anonymous_patron = + $builder->build_object( { class => 'Koha::Patrons' } ); + t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id ); + + # anonymize second hold + $hold_2->anonymize; + $hold_2->discard_changes; + is( $hold_2->borrowernumber, $anonymous_patron->id, + 'Anonymized hold linked to anonymouspatron' ); + + $schema->storage->txn_rollback; +}; -- 2.30.2