Bugzilla – Attachment 62409 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.33 KB, created by
Jonathan Druart
on 2017-04-19 18:35:05 UTC
(
hide
)
Description:
Bug 17502: Throw some exceptions in output_pref
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2017-04-19 18:35:05 UTC
Size:
5.33 KB
patch
obsolete
>From 023320017b9d8fa1d8510838e8098d8969854975 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 > >Test plan: >Run the adjusted t/DateUtils.t > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> > >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 <m.de.rooy@rijksmuseum.nl> > >Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >--- > 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 1bc4ec7..66ad264 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 12540c9..60b9e6a 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 6550167..884990b 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.9.3
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