From 47f553c7d76cbebc98286875ab1933a5599ac059 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 4 Feb 2022 08:40:13 -0300 Subject: [PATCH] Bug 30023: Add Koha::Old::Checkout->anonymize This patch adds the mentioned method. It replicates the Koha::Old::Hold behavior, including the exception thrown on bad configuration. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Old/Checkout.t => SUCCESS: Test pass 3. Sign off :-D Signed-off-by: David Nind --- Koha/Old/Checkout.pm | 20 ++++++++ t/db_dependent/Koha/Old/Checkout.t | 82 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 t/db_dependent/Koha/Old/Checkout.t diff --git a/Koha/Old/Checkout.pm b/Koha/Old/Checkout.pm index 441cc7d27d..e4cd9989f1 100644 --- a/Koha/Old/Checkout.pm +++ b/Koha/Old/Checkout.pm @@ -18,6 +18,7 @@ package Koha::Old::Checkout; use Modern::Perl; use Koha::Database; +use Koha::Exceptions::SysPref; use Koha::Libraries; use base qw(Koha::Object); @@ -89,6 +90,25 @@ sub issuer { return Koha::Patron->_new_from_dbic( $issuer_rs ); } +=head3 anonymize + + $checkout->anonymize(); + +Anonymize the given I object. + +=cut + +sub anonymize { + my ($self) = @_; + + my $anonymous_id = C4::Context->preference('AnonymousPatron'); + + Koha::Exceptions::SysPref::NotSet->throw( syspref => 'AnonymousPatron' ) + unless $anonymous_id; + + return $self->update( { borrowernumber => $anonymous_id } ); +} + =head3 to_api_mapping This method returns the mapping for representing a Koha::Old::Checkout object diff --git a/t/db_dependent/Koha/Old/Checkout.t b/t/db_dependent/Koha/Old/Checkout.t new file mode 100755 index 0000000000..a5c8f0c38d --- /dev/null +++ b/t/db_dependent/Koha/Old/Checkout.t @@ -0,0 +1,82 @@ +#!/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 Test::Exception; + +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 => 8; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + is( $patron->old_checkouts->count, 0, 'Patron has no old checkouts' ); + + my $checkout_1 = $builder->build_object( + { + class => 'Koha::Old::Checkouts', + value => { borrowernumber => $patron->id } + } + ); + my $checkout_2 = $builder->build_object( + { + class => 'Koha::Old::Checkouts', + value => { borrowernumber => $patron->id } + } + ); + + is( $patron->old_checkouts->count, 2, 'Patron has 2 completed checkouts' ); + + t::lib::Mocks::mock_preference( 'AnonymousPatron', undef ); + + throws_ok + { $checkout_1->anonymize; } + 'Koha::Exceptions::SysPref::NotSet', + 'Exception thrown because AnonymousPatron not set'; + + is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' ); + is( $patron->old_checkouts->count, 2, 'No changes, patron has 2 linked completed checkouts' ); + + is( $checkout_1->borrowernumber, $patron->id, + 'Anonymized hold not linked to patron' ); + is( $checkout_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 + $checkout_2->anonymize; + $checkout_2->discard_changes; + is( $checkout_2->borrowernumber, $anonymous_patron->id, + 'Anonymized hold linked to anonymouspatron' ); + + $schema->storage->txn_rollback; +}; -- 2.30.2