Bug 28031: Avoid wrong exception on saving updated attribute

In the case a non-repeatable attribute for a patron is being updated
(its value) the routines that check the type is repeatable should
exclude its own id from the search for things to work.

This patch solves that.

To test:
1. Apply the regression tests patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Patron/Attribute.t
=> FAIL: Tests explode! An unexpected exception is thrown!
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass!
5. Sign off :-D

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Tomás Cohen Arazi 2021-03-25 09:53:13 -03:00 committed by Jonathan Druart
parent f382fafae0
commit 9489054527

View file

@ -146,13 +146,16 @@ sub _check_repeatable {
my $self = shift;
if ( !$self->type->repeatable ) {
my $attr_count = Koha::Patron::Attributes->search(
{ borrowernumber => $self->borrowernumber,
code => $self->code
}
)->count;
my $params = {
borrowernumber => $self->borrowernumber,
code => $self->code
};
$params->{id} = { '!=' => $self->id }
if $self->in_storage;
Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( attribute => $self )
if $attr_count > 0;
if Koha::Patron::Attributes->search($params)->count > 0;
}
return $self;