Bugzilla – Attachment 120783 Details for
Bug 28220
Exception not caught when importing patrons
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 28220: Deal with merge
Bug-28220-Deal-with-merge.patch (text/plain), 6.19 KB, created by
Nick Clemens (kidclamp)
on 2021-05-10 13:27:57 UTC
(
hide
)
Description:
Bug 28220: Deal with merge
Filename:
MIME Type:
Creator:
Nick Clemens (kidclamp)
Created:
2021-05-10 13:27:57 UTC
Size:
6.19 KB
patch
obsolete
>From cf6da70369d34c9ee430df60ebcd0199c4dc5c8f Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Wed, 5 May 2021 17:37:31 +0200 >Subject: [PATCH] Bug 28220: Deal with merge > >Signed-off-by: Nick Clemens <nick@bywatersolutions.com> >--- > Koha/Patron/Attributes.pm | 19 +++--- > t/db_dependent/Koha/Patron/Attribute.t | 114 ++++++++++++++++++++++++++++++++- > 2 files changed, 121 insertions(+), 12 deletions(-) > >diff --git a/Koha/Patron/Attributes.pm b/Koha/Patron/Attributes.pm >index 8acef02fac..7b7c42b7f6 100644 >--- a/Koha/Patron/Attributes.pm >+++ b/Koha/Patron/Attributes.pm >@@ -87,29 +87,24 @@ sub filter_by_branch_limitations { > return $self->search( $or, $join ); > } > >-=head3 merge_with >+=head3 merge_and_replace_with > > $new_attributes is an arrayref of hashrefs > > =cut > >-sub merge_with { >+sub merge_and_replace_with { > my ( $self, $new_attributes ) = @_; > > my @merged = @{$self->unblessed}; > my $attribute_types = { map { $_->code => $_->unblessed } Koha::Patron::Attribute::Types->search }; > for my $attr ( @$new_attributes ) { >- unless ( $attr->{code} ) { >- warn "Cannot merge element: no 'code' defined"; >- next; >- } > > my $attribute_type = $attribute_types->{$attr->{code}}; >- # FIXME Do we need that here or let ->store do the job? >- unless ( $attribute_type ) { >- warn "Cannot merge element: unrecognized code = '$attr->{code}'"; >- next; >- } >+ >+ Koha::Exceptions::Patron::Attribute::InvalidType->throw( type => $attr->{code} ) >+ unless $attribute_types->{$attr->{code}}; >+ > unless ( $attribute_type->{repeatable} ) { > # filter out any existing attributes of the same code > @merged = grep {$attr->{code} ne $_->{code}} @merged; >@@ -118,6 +113,8 @@ sub merge_with { > push @merged, $attr; > } > >+ @merged = map { { code => $_->{code}, attribute => $_->{attribute} } } @merged; >+ > # WARNING - we would like to return a set, but $new_attributes is not in storage yet > # Maybe there is something obvious I (JD) am missing > return [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @merged ]; >diff --git a/t/db_dependent/Koha/Patron/Attribute.t b/t/db_dependent/Koha/Patron/Attribute.t >index 17f54f6a8e..a24de0fb2d 100755 >--- a/t/db_dependent/Koha/Patron/Attribute.t >+++ b/t/db_dependent/Koha/Patron/Attribute.t >@@ -19,7 +19,7 @@ > > use Modern::Perl; > >-use Test::More tests => 2; >+use Test::More tests => 3; > > use t::lib::TestBuilder; > use Test::Exception; >@@ -292,3 +292,115 @@ subtest 'type() tests' => sub { > > $schema->storage->txn_rollback; > }; >+ >+subtest 'merge_and_replace_with' => sub { >+ plan tests => 2; >+ >+ my $schema = Koha::Database->new->schema; >+ $schema->storage->txn_begin; >+ >+ my $patron = $builder->build_object({class=> 'Koha::Patrons'}); >+ >+ my $unique_attribute_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { unique_id=> 1, repeatable => 0 } >+ } >+ ); >+ my $repeatable_attribute_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { unique_id => 0, repeatable => 1 } >+ } >+ ); >+ my $normal_attribute_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ value => { unique_id => 0, repeatable => 0 } >+ } >+ ); >+ my $non_existent_attribute_type = $builder->build_object( >+ { >+ class => 'Koha::Patron::Attribute::Types', >+ } >+ ); >+ my $non_existent_attribute_type_code = $non_existent_attribute_type->code; >+ $non_existent_attribute_type->delete; >+ >+ my $attributes = [ >+ { >+ attribute => 'my unique attribute 1', >+ code => $unique_attribute_type->code(), >+ }, >+ { >+ attribute => 'my repeatable attribute 1', >+ code => $repeatable_attribute_type->code(), >+ }, >+ { >+ attribute => 'my normal attribute 1', >+ code => $normal_attribute_type->code(), >+ } >+ ]; >+ $patron->extended_attributes($attributes); >+ >+ my $new_attributes = [ >+ { >+ attribute => 'my repeatable attribute 2', >+ code => $repeatable_attribute_type->code(), >+ }, >+ { >+ attribute => 'my repeatable attribute 3', >+ code => $repeatable_attribute_type->code(), >+ }, >+ { >+ attribute => 'my normal attribute 2', >+ code => $normal_attribute_type->code(), >+ }, >+ { >+ attribute => 'my unique attribute 2', >+ code => $unique_attribute_type->code(), >+ } >+ ]; >+ >+ my $new_extended_attributes = $patron->extended_attributes->merge_and_replace_with($new_attributes); >+ >+ my $expected = [ >+ { >+ attribute => 'my normal attribute 2', # Attribute 1 has been replaced by attribute 2 >+ code => $normal_attribute_type->code(), >+ }, >+ { >+ attribute => 'my unique attribute 2', # Attribute 1 has been replaced by attribute 2 >+ code => $unique_attribute_type->code(), >+ }, >+ { >+ attribute => 'my repeatable attribute 1', >+ code => $repeatable_attribute_type->code(), >+ }, >+ { >+ attribute => 'my repeatable attribute 2', >+ code => $repeatable_attribute_type->code(), >+ }, >+ { >+ attribute => 'my repeatable attribute 3', >+ code => $repeatable_attribute_type->code(), >+ }, >+ ]; >+ $expected = [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @$expected ]; >+ is_deeply($new_extended_attributes, $expected); >+ >+ >+ throws_ok >+ { >+ $patron->extended_attributes->merge_and_replace_with( >+ [ >+ { code => $non_existent_attribute_type_code, attribute => 'foobar' }, >+ ] >+ ); >+ } >+ 'Koha::Exceptions::Patron::Attribute::InvalidType', >+ 'Exception thrown on invalid attribute type'; >+ >+ $schema->storage->txn_rollback; >+ >+}; >-- >2.11.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 28220
:
120549
|
120607
|
120608
|
120609
|
120610
|
120611
|
120612
|
120613
|
120614
|
120783
|
120784
|
120785
|
120786
|
120787
|
120788
|
120789
|
120790
|
120885
|
120886
|
120887
|
120888
|
120889
|
120890
|
120891
|
120892