From 87c11f5da2955be97145e68a0c9d8cbe1193b8e2 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi 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 Signed-off-by: John Doe --- 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 64e6b33e74..e258606554 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -500,7 +500,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.21.0 (Apple Git-122.2)