@@ -, +, @@ $ kshell k$ prove t/db_dependent/Koha/Old/Hold.t --- 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 --- a/Koha/Old/Hold.pm +++ a/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 --- a/t/db_dependent/Koha/Hold.t +++ a/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; --- a/t/db_dependent/Koha/Old/Hold.t +++ a/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; +}; --