From 6603f129ee237ec3eb70f2feb604d416258646cc Mon Sep 17 00:00:00 2001 From: Shi Yao Wang Date: Mon, 29 Apr 2024 10:10:46 -0400 Subject: [PATCH] Bug 32610: Patron attribute is_date error handling and tests Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Exceptions/Patron/Attribute.pm | 12 +++++++ Koha/Patron/Attribute.pm | 25 +++++++++++++ t/db_dependent/Koha/Patron/Attribute.t | 50 +++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/Koha/Exceptions/Patron/Attribute.pm b/Koha/Exceptions/Patron/Attribute.pm index 90e7b0e555..145f0008c7 100644 --- a/Koha/Exceptions/Patron/Attribute.pm +++ b/Koha/Exceptions/Patron/Attribute.pm @@ -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; diff --git a/Koha/Patron/Attribute.pm b/Koha/Patron/Attribute.pm index aaaefbcbb8..00b55362ce 100644 --- a/Koha/Patron/Attribute.pm +++ b/Koha/Patron/Attribute.pm @@ -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 diff --git a/t/db_dependent/Koha/Patron/Attribute.t b/t/db_dependent/Koha/Patron/Attribute.t index a24de0fb2d..3caccd0b96 100755 --- a/t/db_dependent/Koha/Patron/Attribute.t +++ b/t/db_dependent/Koha/Patron/Attribute.t @@ -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; -- 2.39.5