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

(-)a/Koha/Patron/Attributes.pm (-11 / +8 lines)
Lines 87-115 sub filter_by_branch_limitations { Link Here
87
    return $self->search( $or, $join );
87
    return $self->search( $or, $join );
88
}
88
}
89
89
90
=head3 merge_with
90
=head3 merge_and_replace_with
91
91
92
$new_attributes is an arrayref of hashrefs
92
$new_attributes is an arrayref of hashrefs
93
93
94
=cut
94
=cut
95
95
96
sub merge_with {
96
sub merge_and_replace_with {
97
    my ( $self, $new_attributes ) = @_;
97
    my ( $self, $new_attributes ) = @_;
98
98
99
    my @merged = @{$self->unblessed};
99
    my @merged = @{$self->unblessed};
100
    my $attribute_types = { map { $_->code => $_->unblessed } Koha::Patron::Attribute::Types->search };
100
    my $attribute_types = { map { $_->code => $_->unblessed } Koha::Patron::Attribute::Types->search };
101
    for my $attr ( @$new_attributes ) {
101
    for my $attr ( @$new_attributes ) {
102
        unless ( $attr->{code} ) {
103
            warn "Cannot merge element: no 'code' defined";
104
            next;
105
        }
106
102
107
        my $attribute_type = $attribute_types->{$attr->{code}};
103
        my $attribute_type = $attribute_types->{$attr->{code}};
108
        # FIXME Do we need that here or let ->store do the job?
104
109
        unless ( $attribute_type ) {
105
        Koha::Exceptions::Patron::Attribute::InvalidType->throw( type => $attr->{code} )
110
            warn "Cannot merge element: unrecognized code = '$attr->{code}'";
106
            unless $attribute_types->{$attr->{code}};
111
            next;
107
112
        }
113
        unless ( $attribute_type->{repeatable} ) {
108
        unless ( $attribute_type->{repeatable} ) {
114
            # filter out any existing attributes of the same code
109
            # filter out any existing attributes of the same code
115
            @merged = grep {$attr->{code} ne $_->{code}} @merged;
110
            @merged = grep {$attr->{code} ne $_->{code}} @merged;
Lines 118-123 sub merge_with { Link Here
118
        push @merged, $attr;
113
        push @merged, $attr;
119
    }
114
    }
120
115
116
    @merged = map { { code => $_->{code}, attribute => $_->{attribute} } } @merged;
117
121
    # WARNING - we would like to return a set, but $new_attributes is not in storage yet
118
    # WARNING - we would like to return a set, but $new_attributes is not in storage yet
122
    # Maybe there is something obvious I (JD) am missing
119
    # Maybe there is something obvious I (JD) am missing
123
    return [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @merged ];
120
    return [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @merged ];
(-)a/t/db_dependent/Koha/Patron/Attribute.t (-2 / +113 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
23
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use Test::Exception;
25
use Test::Exception;
Lines 292-294 subtest 'type() tests' => sub { Link Here
292
292
293
    $schema->storage->txn_rollback;
293
    $schema->storage->txn_rollback;
294
};
294
};
295
- 
295
296
subtest 'merge_and_replace_with' => sub {
297
    plan tests => 2;
298
299
    my $schema = Koha::Database->new->schema;
300
    $schema->storage->txn_begin;
301
302
    my $patron = $builder->build_object({class=> 'Koha::Patrons'});
303
304
    my $unique_attribute_type = $builder->build_object(
305
        {
306
            class => 'Koha::Patron::Attribute::Types',
307
            value => { unique_id=> 1, repeatable => 0 }
308
        }
309
    );
310
    my $repeatable_attribute_type = $builder->build_object(
311
        {
312
            class => 'Koha::Patron::Attribute::Types',
313
            value => { unique_id => 0, repeatable => 1 }
314
        }
315
    );
316
    my $normal_attribute_type = $builder->build_object(
317
        {
318
            class => 'Koha::Patron::Attribute::Types',
319
            value => { unique_id => 0, repeatable => 0 }
320
        }
321
    );
322
    my $non_existent_attribute_type = $builder->build_object(
323
        {
324
            class => 'Koha::Patron::Attribute::Types',
325
        }
326
    );
327
    my $non_existent_attribute_type_code = $non_existent_attribute_type->code;
328
    $non_existent_attribute_type->delete;
329
330
    my $attributes = [
331
        {
332
            attribute => 'my unique attribute 1',
333
            code => $unique_attribute_type->code(),
334
        },
335
        {
336
            attribute => 'my repeatable attribute 1',
337
            code => $repeatable_attribute_type->code(),
338
        },
339
        {
340
            attribute => 'my normal attribute 1',
341
            code => $normal_attribute_type->code(),
342
        }
343
    ];
344
    $patron->extended_attributes($attributes);
345
346
    my $new_attributes = [
347
        {
348
            attribute => 'my repeatable attribute 2',
349
            code => $repeatable_attribute_type->code(),
350
        },
351
        {
352
            attribute => 'my repeatable attribute 3',
353
            code => $repeatable_attribute_type->code(),
354
        },
355
        {
356
            attribute => 'my normal attribute 2',
357
            code => $normal_attribute_type->code(),
358
        },
359
        {
360
            attribute => 'my unique attribute 2',
361
            code => $unique_attribute_type->code(),
362
        }
363
    ];
364
365
    my $new_extended_attributes = $patron->extended_attributes->merge_and_replace_with($new_attributes);
366
367
    my $expected = [
368
        {
369
            attribute => 'my normal attribute 2', # Attribute 1 has been replaced by attribute 2
370
            code => $normal_attribute_type->code(),
371
        },
372
        {
373
            attribute => 'my unique attribute 2', # Attribute 1 has been replaced by attribute 2
374
            code => $unique_attribute_type->code(),
375
        },
376
        {
377
            attribute => 'my repeatable attribute 1',
378
            code => $repeatable_attribute_type->code(),
379
        },
380
        {
381
            attribute => 'my repeatable attribute 2',
382
            code => $repeatable_attribute_type->code(),
383
        },
384
        {
385
            attribute => 'my repeatable attribute 3',
386
            code => $repeatable_attribute_type->code(),
387
        },
388
    ];
389
    $expected = [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @$expected ];
390
    is_deeply($new_extended_attributes, $expected);
391
392
393
    throws_ok
394
        {
395
            $patron->extended_attributes->merge_and_replace_with(
396
                [
397
                    { code => $non_existent_attribute_type_code, attribute => 'foobar' },
398
                ]
399
            );
400
        }
401
        'Koha::Exceptions::Patron::Attribute::InvalidType',
402
        'Exception thrown on invalid attribute type';
403
404
    $schema->storage->txn_rollback;
405
406
};

Return to bug 28220