From 310a3ed2c60d72699410daf09452472a4e37a918 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Sat, 11 Jan 2020 16:43:40 +0100 Subject: [PATCH] Bug 24151: Sync patron's attributes Signed-off-by: Signed-off-by: Sonia Bouis Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- Koha/PseudonymizedTransaction.pm | 15 +++- t/db_dependent/Koha/Pseudonymization.t | 99 +++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/Koha/PseudonymizedTransaction.pm b/Koha/PseudonymizedTransaction.pm index 04aab176df..c5b0e8836f 100644 --- a/Koha/PseudonymizedTransaction.pm +++ b/Koha/PseudonymizedTransaction.pm @@ -79,7 +79,20 @@ sub new_from_statistic { $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0; - return $class->SUPER::new($values); + my $self = $class->SUPER::new($values); + + my $extended_attributes = $patron->extended_attributes->unblessed; + for my $attribute (@$extended_attributes) { + next unless Koha::Patron::Attribute::Types->find( + $attribute->{code} )->keep_for_pseudonymization; + + delete $attribute->{id}; + delete $attribute->{borrowernumber}; + + $self->_result->create_related('pseudonymized_borrower_attributes', $attribute); + } + + return $self; } sub get_hash { diff --git a/t/db_dependent/Koha/Pseudonymization.t b/t/db_dependent/Koha/Pseudonymization.t index 6c8031224b..4e29dcbd47 100644 --- a/t/db_dependent/Koha/Pseudonymization.t +++ b/t/db_dependent/Koha/Pseudonymization.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Try::Tiny; use C4::Circulation; @@ -54,11 +54,12 @@ subtest 'Config does not exist' => sub { C4::Stats::UpdateStats( { type => 'issue', - branch => 'BBB', + branch => $library->branchcode, itemnumber => $item->itemnumber, borrowernumber => $patron->borrowernumber, itemtype => $item->effective_itemtype, location => $item->location, + ccode => $item->ccode, } ); @@ -113,3 +114,97 @@ subtest 'Koha::Anonymized::Transactions tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'PseudonymizedBorrowerAttributes tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' ); + t::lib::Mocks::mock_preference( 'Pseudonymization', 1 ); + t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields', + 'branchcode,categorycode,sort1' ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $patron_info = $patron->unblessed; + delete $patron_info->{borrowernumber}; + $patron->delete; + + my $attribute_type1 = Koha::Patron::Attribute::Type->new( + { + code => 'my code1', + description => 'my description1', + repeatable => 1, + keep_for_pseudonymization => 1, + } + )->store; + my $attribute_type2 = Koha::Patron::Attribute::Type->new( + { + code => 'my code2', + description => 'my description2', + keep_for_pseudonymization => 0, + } + )->store; + my $attribute_type3 = Koha::Patron::Attribute::Type->new( + { + code => 'my code3', + description => 'my description3', + keep_for_pseudonymization => 1, + } + )->store; + + $patron = Koha::Patron->new($patron_info)->store->get_from_storage; + my $attribute_values = [ + { + attribute => 'attribute for code1', + code => $attribute_type1->code, + }, + { + attribute => 'attribute for code2', + code => $attribute_type2->code + }, + { + attribute => 'attribute for code3', + code => $attribute_type3->code + }, + ]; + + $patron->extended_attributes($attribute_values); + + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $item = $builder->build_sample_item; + + C4::Stats::UpdateStats( + { + type => 'issue', + branch => $library->branchcode, + itemnumber => $item->itemnumber, + borrowernumber => $patron->borrowernumber, + itemtype => $item->effective_itemtype, + location => $item->location, + ccode => $item->ccode, + } + ); + + my $p = Koha::PseudonymizedTransactions->search({itemnumber => $item->itemnumber})->next; + my $attributes = Koha::Database->new->schema->resultset('PseudonymizedBorrowerAttribute')->search({transaction_id => $p->id }); + is( $attributes->count, 2, + 'Only the 2 attributes that have a type with keep_for_pseudonymization set should be kept' + ); + my $attribute_1 = $attributes->next; + is_deeply( + { attribute => $attribute_1->attribute, code => $attribute_1->code->code }, + $attribute_values->[0], + 'Attribute 1 should be retrieved correctly' + ); + my $attribute_2 = $attributes->next; + is_deeply( + { attribute => $attribute_2->attribute, code => $attribute_2->code->code }, + $attribute_values->[2], + 'Attribute 2 should be retrieved correctly' + ); + + $schema->storage->txn_rollback; +}; -- 2.39.5