View | Details | Raw Unified | Return to bug 24151
Collapse All | Expand All

(-)a/Koha/Anonymized/Patron.pm (+42 lines)
Lines 21-26 use Carp; Link Here
21
21
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::Anonymized;
23
use Koha::Anonymized;
24
use Koha::Anonymized::Patron::Attributes;
24
25
25
use base qw(Koha::Object);
26
use base qw(Koha::Object);
26
27
Lines 63-68 sub update_from_patron { Link Here
63
    return $self;
64
    return $self;
64
}
65
}
65
66
67
sub add_extended_attribute {
68
    my ( $self, $attribute ) = @_;
69
    delete $attribute->{borrowernumber};
70
71
    return unless Koha::Patron::Attribute::Types->find(
72
        $attribute->{code} )->keep_for_anonymized;
73
74
    $self->_result->create_related('anonymized_borrower_attributes', $attribute);
75
}
76
77
78
sub extended_attributes {
79
    my ( $self, $attributes ) = @_;
80
    if ($attributes) {    # setter
81
        my $schema = $self->_result->result_source->schema;
82
        $schema->txn_do(
83
            sub {
84
                # Remove the existing one
85
                # FIXME We could use the filtering method, but do not want to copy/paste Koha::Patron::Attributes->filter_by_branch_limitations.
86
                # Will do when Koha::Patron::Attributes will use Koha::Objects::Limit::Library
87
                $self->extended_attributes->delete;
88
89
                # Insert the new ones
90
                for my $attribute (@$attributes) {
91
                    delete $attribute->{borrowernumber};
92
                    next unless Koha::Patron::Attribute::Types->find(
93
                        $attribute->{code} )->keep_for_anonymized;
94
95
                    $self->_result->create_related('anonymized_borrower_attributes', $attribute);
96
                }
97
            }
98
        );
99
    }
100
    else {
101
        # Behavior differs from Koha::Patron->extended_attributes
102
        # No need to return them, so moving in else
103
        my $rs = $self->_result->anonymized_borrower_attributes;
104
        return Koha::Anonymized::Patron::Attributes->_new_from_dbic($rs)->search;
105
    }
106
}
107
66
=head2 Internal methods
108
=head2 Internal methods
67
109
68
=head3 _type
110
=head3 _type
(-)a/Koha/Patron.pm (-2 / +20 lines)
Lines 1451-1457 sub generate_userid { Link Here
1451
sub add_extended_attribute {
1451
sub add_extended_attribute {
1452
    my ($self, $attribute) = @_;
1452
    my ($self, $attribute) = @_;
1453
    $attribute->{borrowernumber} = $self->borrowernumber;
1453
    $attribute->{borrowernumber} = $self->borrowernumber;
1454
    return Koha::Patron::Attribute->new($attribute)->store;
1454
1455
    my $stored = Koha::Patron::Attribute->new($attribute)->store;
1456
1457
    if ( C4::Context->preference('Pseudonymization') ) {
1458
        my $anonymized =
1459
          Koha::Anonymized::Patrons->find_from_borrowernumber(
1460
            $self->borrowernumber );
1461
        $anonymized->add_extended_attribute($attribute);
1462
    }
1463
1464
    return $stored;
1465
1455
}
1466
}
1456
1467
1457
=head3 extended_attributes
1468
=head3 extended_attributes
Lines 1475-1484 sub extended_attributes { Link Here
1475
                for my $attribute (@$attributes) {
1486
                for my $attribute (@$attributes) {
1476
                    $self->_result->create_related('borrower_attributes', $attribute);
1487
                    $self->_result->create_related('borrower_attributes', $attribute);
1477
                }
1488
                }
1489
1490
                if ( C4::Context->preference('Pseudonymization') ) {
1491
                    my $anonymized =
1492
                      Koha::Anonymized::Patrons->find_from_borrowernumber(
1493
                        $self->borrowernumber );
1494
                    $anonymized->extended_attributes(
1495
                        $self->extended_attributes->unblessed );
1496
                }
1478
            }
1497
            }
1479
        );
1498
        );
1480
    }
1499
    }
1481
1482
    my $rs = $self->_result->borrower_attributes;
1500
    my $rs = $self->_result->borrower_attributes;
1483
    # We call search to use the filters in Koha::Patron::Attributes->search
1501
    # We call search to use the filters in Koha::Patron::Attributes->search
1484
    return Koha::Patron::Attributes->_new_from_dbic($rs)->search;
1502
    return Koha::Patron::Attributes->_new_from_dbic($rs)->search;
(-)a/t/db_dependent/Koha/Pseudonymization.t (-2 / +124 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 3;
22
use Test::More tests => 4;
23
use Try::Tiny;
23
use Try::Tiny;
24
24
25
use C4::Circulation;
25
use C4::Circulation;
Lines 169-171 subtest 'Koha::Anonymized::Transactions tests' => sub { Link Here
169
169
170
    $schema->storage->txn_rollback;
170
    $schema->storage->txn_rollback;
171
};
171
};
172
- 
172
173
subtest 'Koha::Anonymized::Patrons tests' => sub {
174
175
    plan tests => 6;
176
177
    $schema->storage->txn_begin;
178
179
    t::lib::Mocks::mock_config( 'key', '$2a$08$9lmorEKnwQloheaCLFIfje' );
180
    t::lib::Mocks::mock_preference( 'Pseudonymization', 1 );
181
    t::lib::Mocks::mock_preference( 'PseudonymizationPatronFields',
182
        'branchcode,categorycode,sort1' );
183
184
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
185
    my $patron_info = $patron->unblessed;
186
    delete $patron_info->{borrowernumber};
187
    $patron->delete;
188
189
    my $attribute_type1 = Koha::Patron::Attribute::Type->new(
190
        {
191
            code                => 'my code1',
192
            description         => 'my description1',
193
            repeatable          => 1,
194
            keep_for_anonymized => 1,
195
        }
196
    )->store;
197
    my $attribute_type2 = Koha::Patron::Attribute::Type->new(
198
        {
199
            code                => 'my code2',
200
            description         => 'my description2',
201
            keep_for_anonymized => 0,
202
        }
203
    )->store;
204
    my $attribute_type3 = Koha::Patron::Attribute::Type->new(
205
        {
206
            code                => 'my code3',
207
            description         => 'my description3',
208
            keep_for_anonymized => 1,
209
        }
210
    )->store;
211
    my $attribute_type4 = Koha::Patron::Attribute::Type->new(
212
        {
213
            code                => 'my code4',
214
            description         => 'my description4',
215
            keep_for_anonymized => 0,
216
        }
217
    )->store;
218
    my $attribute_type5 = Koha::Patron::Attribute::Type->new(
219
        {
220
            code                => 'my code5',
221
            description         => 'my description5',
222
            keep_for_anonymized => 1,
223
        }
224
    )->store;
225
226
    $patron = Koha::Patron->new($patron_info)->store->get_from_storage;
227
    my $attribute_values = [
228
        {
229
            attribute => 'attribute for code1',
230
            code      => $attribute_type1->code,
231
        },
232
        {
233
            attribute => 'attribute for code2',
234
            code      => $attribute_type2->code
235
        },
236
        {
237
            attribute => 'attribute for code3',
238
            code      => $attribute_type3->code
239
        },
240
    ];
241
242
    $patron->extended_attributes($attribute_values);
243
    my $anonymized = Koha::Anonymized::Patrons->find_from_borrowernumber(
244
        $patron->borrowernumber );
245
    my $attributes = $anonymized->extended_attributes;
246
    is( $attributes->count, 2,
247
        'Only the 2 attributes that have a type with keep_for_anonymized set should be kept'
248
    );
249
    my $attribute_1 = $attributes->next;
250
    is_deeply(
251
        { attribute => $attribute_1->attribute, code => $attribute_1->code },
252
        $attribute_values->[0],
253
        'Attribute 1 should be retrieved correctly'
254
    );
255
    my $attribute_2 = $attributes->next;
256
    is_deeply(
257
        { attribute => $attribute_2->attribute, code => $attribute_2->code },
258
        $attribute_values->[2],
259
        'Attribute 2 should be retrieved correctly'
260
    );
261
262
    # Adding another attribute for code 1, that is repeatable
263
    $patron->add_extended_attribute(
264
        {
265
            attribute => 'attribute 2 for code 1',
266
            code      => $attribute_type1->code
267
        }
268
    );
269
    $attributes = $anonymized->extended_attributes;
270
    is( $attributes->count, 3,
271
        'New repeatable attribute should have been added' );
272
273
    # Adding an attribute for code 4, that should not be kept
274
    $patron->add_extended_attribute(
275
        {
276
            attribute => 'attribute 1 for code 4',
277
            code      => $attribute_type4->code
278
        }
279
    );
280
    $attributes = $anonymized->extended_attributes;
281
    is( $attributes->count, 3, 'No new attribute kept' );
282
283
    # Adding a new attribute for code 5, no repeatable but kept
284
    $patron->add_extended_attribute(
285
        {
286
            attribute => 'attribute 1 for code 5',
287
            code      => $attribute_type5->code
288
        }
289
    );
290
    $attributes = $anonymized->extended_attributes;
291
    is( $attributes->count, 4, 'New attribute kept' );
292
293
    $schema->storage->txn_rollback;
294
};

Return to bug 24151