Bugzilla – Attachment 165747 Details for
Bug 32610
Add ability to specify patron attribute as a date
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 32610: Patron attribute is_date error handling and tests
Bug-32610-Patron-attribute-isdate-error-handling-a.patch (text/plain), 4.91 KB, created by
Shi Yao Wang
on 2024-04-29 14:17:35 UTC
(
hide
)
Description:
Bug 32610: Patron attribute is_date error handling and tests
Filename:
MIME Type:
Creator:
Shi Yao Wang
Created:
2024-04-29 14:17:35 UTC
Size:
4.91 KB
patch
obsolete
>From 869b72c29dcf4751f8c9f1d2014b6d78732a6004 Mon Sep 17 00:00:00 2001 >From: Shi Yao Wang <shi-yao.wang@inlibro.com> >Date: Mon, 29 Apr 2024 10:10:46 -0400 >Subject: [PATCH] Bug 32610: Patron attribute is_date error handling and tests > >--- > 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.34.1
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 32610
:
148400
|
148401
|
148402
|
148403
|
148404
|
148405
|
148825
|
148826
|
148827
|
148828
|
148829
|
148830
|
157862
|
157863
|
157864
|
157865
|
157866
|
157867
|
157868
|
158994
|
158995
|
158996
|
158997
|
158998
|
158999
|
159000
|
165747
|
165750
|
165854
|
165855
|
165856
|
165857
|
165858
|
165859
|
165860
|
165861
|
165862
|
165863
|
165864
|
165865
|
165925
|
165926