Bugzilla – Attachment 61510 Details for
Bug 17502
Add type check to output_pref and use exceptions
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 17502: Throw some exceptions in output_pref
Bug-17502-Throw-some-exceptions-in-outputpref.patch (text/plain), 5.97 KB, created by
Marcel de Rooy
on 2017-03-23 08:40:56 UTC
(
hide
)
Description:
Bug 17502: Throw some exceptions in output_pref
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2017-03-23 08:40:56 UTC
Size:
5.97 KB
patch
obsolete
>From 0e6aabee04ca7bb0425036f1b8618fef1e4bc7f2 Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >Date: Thu, 23 Mar 2017 09:35:57 +0100 >Subject: [PATCH] Bug 17502: Throw some exceptions in output_pref >Content-Type: text/plain; charset=utf-8 > >This patch adds Koha/Exceptions/DateUtils.pm. >It contains now 4 exceptions for output_pref: >[1] default general exception, >[2] conflicting parameters (dt and str), >[3] invalid date string, >[4] no date time object. > >Test plan: >Run the adjusted t/DateUtils.t >--- > Koha/DateUtils.pm | 18 ++++++++++------ > Koha/Exceptions/DateUtils.pm | 21 +++++++++++++++++++ > t/DateUtils.t | 49 +++++++++++++++++++++++++++++++++++--------- > 3 files changed, 72 insertions(+), 16 deletions(-) > create mode 100644 Koha/Exceptions/DateUtils.pm > >diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm >index 1bc4ec7..4a22325 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::DateUtils; > > use base 'Exporter'; > >@@ -209,17 +209,23 @@ 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::DateUtils::output_pref::ConflictingParameters->throw >+ 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::DateUtils::output_pref::InvalidDateString->throw( >+ "Invalid date '$str' passed to output_pref" >+ ) if $@; > } > >- return unless defined $dt && ref($dt) eq 'DateTime'; >+ if( !defined $dt || ref($dt) ne 'DateTime' ) { >+ Koha::Exceptions::DateUtils::output_pref::NoDateTime->throw( >+ "Object should have been a DateTime; check the call from ". >+ join ',', (caller), >+ ); >+ } > > # FIXME: see bug 13242 => no TZ for dates 'infinite' > if ( $dt->ymd !~ /^9999/ ) { >diff --git a/Koha/Exceptions/DateUtils.pm b/Koha/Exceptions/DateUtils.pm >new file mode 100644 >index 0000000..541c5b3 >--- /dev/null >+++ b/Koha/Exceptions/DateUtils.pm >@@ -0,0 +1,21 @@ >+package Koha::Exceptions::DateUtils; >+ >+use Modern::Perl; >+ >+use Exception::Class ( >+ >+ 'Koha::Exceptions::DateUtils' => { >+ description => 'Something went wrong in DateUtils', >+ }, >+ 'Koha::Exceptions::DateUtils::output_pref::ConflictingParameters' => { >+ description => 'output_pref should not be called with both dt and str parameters', >+ }, >+ 'Koha::Exceptions::DateUtils::output_pref::InvalidDateString' => { >+ description => 'Invalid date string passed to output_pref', >+ }, >+ 'Koha::Exceptions::DateUtils::output_pref::NoDateTime' => { >+ description => 'Object should have been a DateTime', >+ }, >+); >+ >+1; >diff --git a/t/DateUtils.t b/t/DateUtils.t >index 6550167..91472fa 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,46 @@ $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' ); >+try { >+ output_pref(); >+ ok( 0, 'output_pref without parameters' ); >+} catch { >+ is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime', >+ 'output_pref without parameters' ); >+}; >+try { >+ output_pref( 'no_dt' ); >+ ok( 0, 'Passed single invalid dt to output_pref' ); >+} catch { >+ is( ref($_), 'Koha::Exceptions::DateUtils::output_pref::NoDateTime', >+ '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::DateUtils::output_pref::NoDateTime', >+ '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::DateUtils::output_pref::InvalidDateString', >+ '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::DateUtils::output_pref::ConflictingParameters', 'output_pref should throw an exception if str and dt parameters are passed together' ); >+}; >+ >+# End of tests >-- >2.1.4
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 17502
:
56916
|
56917
|
57597
|
61458
|
61510
|
61518
|
61519
|
62168
|
62169
|
62408
|
62409
|
62410