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 <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Tomas Cohen Arazi 2022-02-04 08:40:13 -03:00 committed by Fridolin Somers
parent c4e5e04f57
commit bafd1c25e9
2 changed files with 102 additions and 0 deletions

View File

@ -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<Koha::Old::Checkout> 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

View File

@ -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 <http://www.gnu.org/licenses>.
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;
};