From 69b617fc58eed52dd3759fbfe76ba01bce1dd46d Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 8 Mar 2021 17:24:28 +0000 Subject: [PATCH] Bug 27857: Add handling for globally mandatory attributes This patch adds handling for globally mandatory extended attributes and the corresponding unit tests. Signed-off-by: David Nind --- Koha/Patron.pm | 19 +++++++++++++++++++ t/db_dependent/Koha/Patron.t | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 2d484beae7..1160084ca6 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1561,8 +1561,27 @@ sub extended_attributes { $self->extended_attributes->filter_by_branch_limitations->delete; # Insert the new ones + my $new_types = {}; for my $attribute (@$attributes) { $self->add_extended_attribute($attribute); + $new_types->{$attribute->{code}} = 1; + } + + # Check globally mandatory types + my @required_attribute_types = + Koha::Patron::Attribute::Types->search( + { + mandatory => 1, + 'borrower_attribute_types_branches.b_branchcode' => + undef + }, + { join => 'borrower_attribute_types_branches' } + )->get_column('class'); + for my $type ( @required_attribute_types ) { + Koha::Exceptions::Object::FKConstraint->throw( + broken_fk => "$type", + value => "$type", + ) if !$new_types->{$type}; } } ); diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index f5f33e0d8a..98b079f4c3 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -369,7 +369,7 @@ subtest 'is_superlibrarian() tests' => sub { subtest 'extended_attributes' => sub { - plan tests => 14; + plan tests => 15; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -614,5 +614,41 @@ subtest 'extended_attributes' => sub { is( $patron->extended_attributes->count, 0, 'Extended attributes storing rolled back' ); }; + subtest 'globally mandatory attributes tests' => sub { + + plan tests => 3; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + + my $attribute_type_1 = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types', + value => { mandatory => 1 } + } + ); + + my $attribute_type_2 = $builder->build_object( + { + class => 'Koha::Patron::Attribute::Types', + value => { mandatory => 0 } + } + ); + + is( $patron->extended_attributes->count, 0, 'Patron has no extended attributes' ); + + throws_ok + { + $patron->extended_attributes( + [ + { code => $attribute_type_2->code, attribute => 'b' } + ] + ); + } + 'Koha::Exceptions::Object::FKConstraint', + 'Exception thrown on missing mandatory attribute type'; + + is( $patron->extended_attributes->count, 0, 'Extended attributes storing rolled back' ); + }; + $schema->storage->txn_rollback; }; -- 2.11.0