Bugzilla – Attachment 96666 Details for
Bug 23893
Add ->new_from_api and ->set_from_api methods to Koha::Object
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 23893: Catch dt_from_string exceptions
Bug-23893-Catch-dtfromstring-exceptions.patch (text/plain), 3.47 KB, created by
Tomás Cohen Arazi (tcohen)
on 2019-12-27 18:29:09 UTC
(
hide
)
Description:
Bug 23893: Catch dt_from_string exceptions
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2019-12-27 18:29:09 UTC
Size:
3.47 KB
patch
obsolete
>From 58fac9d190e59db0a9034b6588c323b7f7e4120f Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Fri, 27 Dec 2019 15:21:30 -0300 >Subject: [PATCH] Bug 23893: Catch dt_from_string exceptions > >This patch adds exception handling to the attributes_from_api() method. >This can happen with invalid date/datetimes, for example. > >Tests are added: > >1. Apply this patch >2. Run: > $ kshell > k$ prove t/db_dependent/Koha/Object.t >=> SUCCESS: Tests pass! >3. Sign off :-D > >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> >--- > Koha/Object.pm | 7 +++- > t/db_dependent/Koha/Object.t | 70 +++++++++++++++++++++++++++++++++++- > 2 files changed, 75 insertions(+), 2 deletions(-) > >diff --git a/Koha/Object.pm b/Koha/Object.pm >index d31ae16c81..fffe211d1a 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -499,7 +499,12 @@ sub attributes_from_api { > : $key; > > if ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) { >- $value = dt_from_string($value, 'rfc3339'); >+ try { >+ $value = dt_from_string($value, 'rfc3339'); >+ } >+ catch { >+ Koha::Exceptions::BadParameter->throw( parameter => $key ); >+ }; > } > > $params->{$koha_field_name} = $value; >diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t >index 4474b3ac21..b3c57bdac9 100755 >--- a/t/db_dependent/Koha/Object.t >+++ b/t/db_dependent/Koha/Object.t >@@ -17,7 +17,7 @@ > > use Modern::Perl; > >-use Test::More tests => 16; >+use Test::More tests => 17; > use Test::Exception; > use Test::Warn; > use DateTime; >@@ -385,6 +385,74 @@ subtest 'new_from_api() tests' => sub { > $schema->storage->txn_rollback; > }; > >+subtest 'attributes_from_api() tests' => sub { >+ >+ plan tests => 8; >+ >+ my $patron = Koha::Patron->new(); >+ >+ use Data::Printer colored => 1; >+ >+ my $attrs = $patron->attributes_from_api( >+ { >+ updated_on => '2019-12-27T14:53:00' >+ } >+ ); >+ >+ ok( exists $attrs->{updated_on}, >+ 'No translation takes place if no mapping' ); >+ is( >+ ref( $attrs->{updated_on} ), >+ 'DateTime', >+ 'Given a string, a timestamp field is converted into a DateTime object' >+ ); >+ >+ $attrs = $patron->attributes_from_api( >+ { >+ last_seen => '2019-12-27T14:53:00' >+ } >+ ); >+ >+ ok( exists $attrs->{lastseen}, >+ 'Translation takes place because of the defined mapping' ); >+ is( >+ ref( $attrs->{lastseen} ), >+ 'DateTime', >+ 'Given a string, a datetime field is converted into a DateTime object' >+ ); >+ >+ $attrs = $patron->attributes_from_api( >+ { >+ date_of_birth => '2019-12-27' >+ } >+ ); >+ >+ ok( exists $attrs->{dateofbirth}, >+ 'Translation takes place because of the defined mapping' ); >+ is( >+ ref( $attrs->{dateofbirth} ), >+ 'DateTime', >+ 'Given a string, a date field is converted into a DateTime object' >+ ); >+ >+ throws_ok >+ { >+ $attrs = $patron->attributes_from_api( >+ { >+ date_of_birth => '20141205', >+ } >+ ); >+ } >+ 'Koha::Exceptions::BadParameter', >+ 'Bad date throws an exception'; >+ >+ is( >+ $@->parameter, >+ 'date_of_birth', >+ 'Exception parameter is the API field name, not the DB one' >+ ); >+}; >+ > subtest "Test update method" => sub { > plan tests => 6; > >-- >2.24.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 23893
:
95276
|
95277
|
95278
|
95311
|
95312
|
95313
|
95314
|
95315
|
96126
|
96127
|
96128
|
96129
|
96130
|
96652
|
96655
|
96656
|
96657
|
96658
|
96659
|
96660
|
96661
|
96662
|
96665
|
96666
|
96920
|
96921
|
96922
|
96923
|
96924
|
96925
|
96926
|
96927
|
96928
|
96929
|
96930
|
96931
|
96932
|
96933
|
96934
|
96935
|
96936
|
96937
|
97391