From de699909c29bf877b6bf37f15127804c1a2abc62 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 23 Mar 2017 09:35:57 +0100 Subject: [PATCH] Bug 17502: Throw some exceptions in output_pref Test plan: Run the adjusted t/DateUtils.t Signed-off-by: Josef Moravec NOTE: This patch is amended in QA. The exceptions are moved from a separate module to the general section of Exceptions.pm. Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- Koha/DateUtils.pm | 11 +++++------ Koha/Exceptions.pm | 4 ++++ t/DateUtils.t | 43 +++++++++++++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index 1bc4ec77da..66ad2642fe 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -19,7 +19,7 @@ package Koha::DateUtils; use Modern::Perl; use DateTime; use C4::Context; -use Carp; +use Koha::Exceptions; use base 'Exporter'; @@ -209,17 +209,16 @@ sub output_pref { $dt = $params; } - carp "output_pref should not be called with both dt and str parameters" - and return - if $dt and $str; + Koha::Exceptions::WrongParameter->throw( 'output_pref should not be called with both dt and str parameter' ) if $dt and $str; if ( $str ) { local $@; $dt = eval { dt_from_string( $str ) }; - carp "Invalid date '$str' passed to output_pref\n" if $@; + Koha::Exceptions::WrongParameter->throw("Invalid date '$str' passed to output_pref" ) if $@; } - return unless defined $dt && ref($dt) eq 'DateTime'; + return if !defined $dt; # NULL date + Koha::Exceptions::WrongParameter->throw( 'dt is not a datetime' ) if ref($dt) ne 'DateTime'; # FIXME: see bug 13242 => no TZ for dates 'infinite' if ( $dt->ymd !~ /^9999/ ) { diff --git a/Koha/Exceptions.pm b/Koha/Exceptions.pm index 12540c9aaf..60b9e6adfc 100644 --- a/Koha/Exceptions.pm +++ b/Koha/Exceptions.pm @@ -24,6 +24,10 @@ use Exception::Class ( isa => 'Koha::Exceptions::Exception', description => 'A required parameter is missing' }, + 'Koha::Exceptions::WrongParameter' => { + isa => 'Koha::Exceptions::Exception', + description => 'One or more parameters are wrong', + }, 'Koha::Exceptions::CannotAddLibraryLimit' => { isa => 'Koha::Exceptions::Exception', description => 'General problem adding a library limit' diff --git a/t/DateUtils.t b/t/DateUtils.t index 6550167199..884990b274 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -9,6 +9,8 @@ use Test::More tests => 63; use Test::MockModule; use Test::Warn; use Time::HiRes qw/ gettimeofday /; +use Try::Tiny; + use t::lib::Mocks; BEGIN { use_ok('Koha::DateUtils'); } @@ -223,19 +225,40 @@ $dt = dt_from_string('2015-01-31 01:02:03'); is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' ); # output_pref with no parameters, single parameter (no hash) -is( output_pref(), undef, 'output_pref without parameters' ); -is( output_pref( 'no_dt' ), undef, 'Passed single invalid dt to output_pref' ); +is( output_pref(), undef, 'Call output_pref without parameters' ); +try { + output_pref( 'no_dt' ); + ok( 0, 'Passed single invalid dt to output_pref' ); +} catch { + is( ref($_), 'Koha::Exceptions::WrongParameter', + 'Passed single invalid dt to output_pref' ); +}; # pass invalid dt via hash -is( output_pref({ dt => 'no_dt' }), undef, 'Passed invalid dt in hash to output_pref' ); +try { + output_pref({ dt => 'no_dt' }); + ok( 0, 'Passed invalid dt in hash to output_pref' ); +} catch { + is( ref($_), 'Koha::Exceptions::WrongParameter', + 'Passed invalid dt in hash to output_pref' ); +}; # output_pref with str parameter is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' ); my $output_for_invalid_date; -warning_like { $output_for_invalid_date = output_pref( { str => 'invalid_date' } ) } - { carped => qr[^Invalid date 'invalid_date' passed to output_pref] }, - 'output_pref should carp if an invalid date is passed for the str parameter'; -is( $output_for_invalid_date, undef, 'output_pref should return undef if an invalid date is passed' ); -warning_is { output_pref( { 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 } ) } - { carped => 'output_pref should not be called with both dt and str parameters' }, - 'output_pref should carp if str and dt parameters are passed together'; +try { + $output_for_invalid_date = output_pref({ str => 'invalid_date' }); + ok( 0, 'Invalid date string passed to output_pref' ); +} catch { + is( ref($_), 'Koha::Exceptions::WrongParameter', + 'Invalid date string passed to output_pref' ); +}; +is( $output_for_invalid_date, undef, 'No return value from output_pref' ); +try { + output_pref({ 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 }); + ok( 0, 'output_pref should carp if str and dt parameters are passed together' ); +} catch { + is( ref($_), 'Koha::Exceptions::WrongParameter', 'output_pref should throw an exception if str and dt parameters are passed together' ); +}; + +# End of tests -- 2.39.5