From af5c7fa501be6117ca47ab295db2462d19711806 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 26 Nov 2019 21:14:52 +0100 Subject: [PATCH] Bug 24151: Handle case where the setting is turned on later Sponsored-by: Association KohaLa - https://koha-fr.org/ --- Koha/Anonymized/Transaction.pm | 6 ++++++ Koha/Patron.pm | 14 ++++++++++---- t/db_dependent/Koha/Pseudonymization.t | 27 ++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Koha/Anonymized/Transaction.pm b/Koha/Anonymized/Transaction.pm index e57712c936..fcc5f06149 100644 --- a/Koha/Anonymized/Transaction.pm +++ b/Koha/Anonymized/Transaction.pm @@ -42,6 +42,12 @@ sub new_from_statistic { hashed_borrowernumber => Koha::Anonymized->get_hash($statistic->borrowernumber), }; + # In case the patron does not exist yet in the anonymized_borrowers table + unless ( Koha::Anonymized::Patrons->find($values->{hashed_borrowernumber}) ) { + my $patron = Koha::Patrons->find($statistic->borrowernumber); + Koha::Anonymized::Patron->new_from_patron($patron)->store; + } + my @fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || ''; if ( grep { $_ eq 'branchcode' } @fields_to_copy ) { diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 5894dedaac..834b6e120d 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -343,11 +343,17 @@ sub store { } if ( C4::Context->preference('Pseudonymization') ) { - unless ( $in_storage ) { - Koha::Anonymized::Patron->new_from_patron($self)->store; - } else { - Koha::Anonymized::Patrons->find_from_borrowernumber($self->borrowernumber)->update_from_patron($self)->store; + my $anonymized; + if ( $in_storage ) { + $anonymized = Koha::Anonymized::Patrons->find_from_borrowernumber($self->borrowernumber); + $anonymized->update_from_patron($self)->store if $anonymized; } + + # If not set at this point, either it's a new patron + # or the setting has been turned on after the patron has been created + $anonymized = Koha::Anonymized::Patron->new_from_patron($self) unless $anonymized; + + $anonymized->store; } } diff --git a/t/db_dependent/Koha/Pseudonymization.t b/t/db_dependent/Koha/Pseudonymization.t index b5297f3dc5..37e6de8f2d 100644 --- a/t/db_dependent/Koha/Pseudonymization.t +++ b/t/db_dependent/Koha/Pseudonymization.t @@ -63,7 +63,7 @@ subtest 'Config does not exist' => sub { subtest 'Koha::Anonymized::Patrons tests' => sub { - plan tests => 9; + plan tests => 10; $schema->storage->txn_begin; @@ -98,6 +98,31 @@ subtest 'Koha::Anonymized::Patrons tests' => sub { is( $patron->sort1, "another sort1", "correctly updated" ); $patron->delete; + subtest 'Patron was created before the Pseudonymization was actived' => sub { + + plan tests => 2; + + t::lib::Mocks::mock_preference( 'Pseudonymization', 0 ); + my $patron = Koha::Patron->new($patron_info)->store; + $patron->discard_changes; + + t::lib::Mocks::mock_preference( 'Pseudonymization', 1 ); + $patron->sort1("another sort1")->store; + $anonymized = Koha::Anonymized::Patrons->find_from_borrowernumber( $patron->borrowernumber ); + is( $anonymized->sort1, "another sort1", "correctly updated" ); + $anonymized->delete; + + my $item = $builder->build_sample_item; + t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch }); + AddIssue( $patron->unblessed, $item->barcode, dt_from_string ); + AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string ); + $anonymized = Koha::Anonymized::Transactions->search( { itemnumber => $item->itemnumber } )->next; + like( $anonymized->hashed_borrowernumber, + qr{^\$2a\$08\$}, "The hashed_borrowernumber has been created (and so the corresponding anonymized_borrowers entry)" ); + + $patron->delete; + }; + $schema->storage->txn_rollback; }; -- 2.11.0