Bug 32610: Patron attribute is_date error handling and tests

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Shi Yao Wang 2024-04-29 10:10:46 -04:00 committed by Katrin Fischer
parent 21d47773cb
commit 6603f129ee
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
3 changed files with 86 additions and 1 deletions

View File

@ -23,6 +23,11 @@ use Exception::Class (
isa => 'Koha::Exceptions::Patron::Attribute',
description => "unique_id set for attribute type and tried to add a new with the same code and value",
fields => [ "attribute" ]
},
'Koha::Exceptions::Patron::Attribute::InvalidAttributeValue' => {
isa => 'Koha::Exceptions::Patron::Attribute',
description => "the passed value is invalid for attribute type",
fields => [ "attribute" ]
}
);
@ -52,6 +57,13 @@ sub full_message {
$self->type
);
}
elsif ( $self->isa('Koha::Exceptions::Patron::Attribute::InvalidAttributeValue') ) {
$msg = sprintf(
"Tried to use an invalid value for attribute type. type=%s value=%s",
$self->attribute->code,
$self->attribute->attribute
);
}
}
return $msg;

View File

@ -21,6 +21,7 @@ use Koha::Database;
use Koha::Exceptions::Patron::Attribute;
use Koha::Patron::Attribute::Types;
use Koha::AuthorisedValues;
use Koha::DateUtils qw( dt_from_string );
use base qw(Koha::Object);
@ -55,6 +56,9 @@ sub store {
Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
unless $self->unique_ok();
Koha::Exceptions::Patron::Attribute::InvalidAttributeValue->throw( attribute => $self )
unless $self->value_ok();
return $self->SUPER::store();
}
@ -189,6 +193,27 @@ sub unique_ok {
return $ok;
}
=head3 value_ok
Checks if the value of the attribute is valid for the type
=cut
sub value_ok {
my ( $self ) = @_;
my $ok = 1;
if ( $self->type->is_date ) {
eval { dt_from_string($self->attribute); };
if ($@) {
$ok = 0;
}
}
return $ok;
}
=head2 Internal methods
=head3 _type

View File

@ -33,7 +33,7 @@ my $builder = t::lib::TestBuilder->new;
subtest 'store() tests' => sub {
plan tests => 4;
plan tests => 5;
subtest 'repeatable attributes tests' => sub {
@ -106,6 +106,54 @@ subtest 'store() tests' => sub {
$schema->storage->txn_rollback;
};
subtest 'is_date attributes tests' => sub {
plan tests => 2;
$schema->storage->txn_begin;
my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber};
my $attribute_type_1 = $builder->build(
{ source => 'BorrowerAttributeType',
value => { is_date => 1 }
}
);
throws_ok {
Koha::Patron::Attribute->new(
{ borrowernumber => $patron,
code => $attribute_type_1->{code},
attribute => 'not_a_date'
}
)->store;
}
'Koha::Exceptions::Patron::Attribute::InvalidAttributeValue',
'Exception thrown trying to store a date attribute with non-date value';
is(
"$@",
"Tried to use an invalid value for attribute type. type="
. $attribute_type_1->{code}
. " value=not_a_date",
'Exception stringified correctly, attribute passed correctly'
);
Koha::Patron::Attribute->new(
{ borrowernumber => $patron,
code => $attribute_type_1->{code},
attribute => '2024-03-04'
}
)->store;
my $attr_count
= Koha::Patron::Attributes->search(
{ borrowernumber => $patron, code => $attribute_type_1->{code} } )
->count;
is( $attr_count, 1,
'1 date attribute stored and retrieved correctly' );
$schema->storage->txn_rollback;
};
subtest 'unique_id attributes tests' => sub {
plan tests => 5;